refactor: cleaup and implemented mvvm

This commit is contained in:
tux
2025-07-05 06:18:00 +05:30
parent 896b2a9f40
commit 7ffdfea18f
13 changed files with 249 additions and 210 deletions

View File

@@ -27,14 +27,15 @@
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets> <IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets> <PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="IconPacks.Avalonia" Version="1.0.0" /> <PackageReference Include="IconPacks.Avalonia" Version="1.0.0" />
<PackageReference Include="IconPacks.Avalonia.Lucide" Version="1.0.0" /> <PackageReference Include="IconPacks.Avalonia.Lucide" Version="1.0.0" />
<PackageReference Include="Markdig" Version="0.41.3" /> <PackageReference Include="Markdig" Version="0.41.3" />
<PackageReference Include="Markdown.ColorCode" Version="3.0.0" /> <PackageReference Include="Markdown.ColorCode" Version="3.0.0" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="OpenAI" Version="2.2.0-beta.4" /> <PackageReference Include="OpenAI" Version="2.2.0-beta.4" />
<PackageReference Include="SharpHook" Version="6.1.2" /> <PackageReference Include="SharpHook" Version="6.1.2" />
<PackageReference Include="SharpHook.Reactive" Version="6.1.2" /> <PackageReference Include="SharpHook.Reactive" Version="6.1.2" />
<PackageReference Include="SoundFlow" Version="1.1.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

9
models/ChatViewModel.cs Normal file
View File

@@ -0,0 +1,9 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace highminded.models;
public partial class ChatViewModel : ObservableObject
{
[ObservableProperty] private string _content = "";
[ObservableProperty] private string _prompt = "";
}

13
models/MainViewModel.cs Normal file
View File

@@ -0,0 +1,13 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace highminded.models;
public partial class MainViewModel : ObservableObject
{
[ObservableProperty] private string _appName = "High Minded";
[ObservableProperty] private string _hideShortcutKey = "SHIFT + \\";
[ObservableProperty] private string _screenshotShortcutKey = "SHIFT + S";
[ObservableProperty] private string _audioShortcutKey = "SHIFT + A";
[ObservableProperty] private bool _isRecording = false;
[ObservableProperty] private bool _isHidden = true;
}

View File

@@ -0,0 +1,12 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace highminded.models;
public partial class SettingsViewModel : ObservableObject
{
[ObservableProperty] private string _model = "";
[ObservableProperty] private string _apiUrl = "";
[ObservableProperty] private string _apiKey = "";
[ObservableProperty] private string _screenshotPrompt = "";
[ObservableProperty] private string _audioPrompt = "";
}

View File

@@ -2,14 +2,16 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:highminded.models"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="highminded.ui.controls.ChatUserControl"> x:Class="highminded.ui.controls.ChatUserControl"
x:DataType="vm:ChatViewModel">
<Grid RowDefinitions="*,Auto" Margin="15" RowSpacing="10"> <Grid RowDefinitions="*,Auto" Margin="15" RowSpacing="10">
<HtmlPanel Grid.Row="0" Name="ResultBlock" BaseStylesheet="* { font: Inter; color: white; }" <HtmlPanel Grid.Row="0" BaseStylesheet="* { font: Inter; color: white; }"
IsContextMenuEnabled="False" IsSelectionEnabled="False" /> IsContextMenuEnabled="False" IsSelectionEnabled="False" Text="{Binding Content}" />
<TextBox Grid.Row="1" Name="PromptBox" CornerRadius="5" TextWrapping="Wrap" <TextBox Grid.Row="1" CornerRadius="5" TextWrapping="Wrap"
KeyDown="PromptBox_OnKeyDown" /> KeyDown="PromptBox_OnKeyDown" Text="{Binding Prompt}" />
</Grid> </Grid>
</UserControl> </UserControl>

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Input; using Avalonia.Input;
using highminded.models;
using highminded.utils; using highminded.utils;
using OpenAI.Chat; using OpenAI.Chat;
using Markdig; using Markdig;
@@ -16,14 +17,18 @@ namespace highminded.ui.controls;
public partial class ChatUserControl : UserControl public partial class ChatUserControl : UserControl
{ {
private readonly MarkdownPipeline _pipeline = null!; private readonly ChatViewModel _model;
private AudioCapture _audioCapture = null!; private readonly MarkdownPipeline _pipeline;
private readonly AudioCapture _audioCapture;
public ChatUserControl() public ChatUserControl()
{ {
InitializeComponent(); InitializeComponent();
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build(); DataContext = InMemoryDb.Obj.ChatViewModel;
_audioCapture = new AudioCapture(); _audioCapture = new AudioCapture();
_model = InMemoryDb.Obj.ChatViewModel;
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
} }
public void StartRecord() public void StartRecord()
@@ -33,43 +38,16 @@ public partial class ChatUserControl : UserControl
var dirPath = Path.Combine(Environment.CurrentDirectory, "audio"); var dirPath = Path.Combine(Environment.CurrentDirectory, "audio");
var filePath = Path.Combine(dirPath, fileName); var filePath = Path.Combine(dirPath, fileName);
Directory.CreateDirectory(dirPath); Directory.CreateDirectory(dirPath);
InMemoryDb.Obj.MainViewModel.IsRecording = true;
_audioCapture.StartRecording(filePath); _audioCapture.StartRecording(filePath);
} }
public void StopRecord() public void StopRecord()
{
if (_audioCapture != null)
{ {
_audioCapture.StopRecording(); _audioCapture.StopRecording();
InMemoryDb.Obj.MainViewModel.IsRecording = false;
SendAudio(); SendAudio();
} }
}
public async void SendAudio()
{
try
{
var dirPath = Path.Combine(Environment.CurrentDirectory, "audio");
var latestAudio = new DirectoryInfo(dirPath).GetFiles().OrderByDescending(f => f.LastWriteTime).First();
var filePath = Path.Combine(dirPath, latestAudio.Name);
var audioFileBytes = await File.ReadAllBytesAsync(filePath);
var audioBytes = BinaryData.FromBytes(audioFileBytes);
List<ChatMessage> messages =
[
new UserChatMessage(
ChatMessageContentPart.CreateTextPart(InMemoryDb.Obj.SettingsManager.Settings.AudioPrompt),
ChatMessageContentPart.CreateInputAudioPart(audioBytes, ChatInputAudioFormat.Wav)
)
];
await ProcessChatStreamAsync(messages);
}
catch (Exception err)
{
ResultBlock.Text = err.Message;
}
}
public async void SendScreenshot() public async void SendScreenshot()
{ {
@@ -99,7 +77,33 @@ public partial class ChatUserControl : UserControl
} }
catch (Exception err) catch (Exception err)
{ {
ResultBlock.Text = err.Message; _model.Content = err.Message;
}
}
private async void SendAudio()
{
try
{
var dirPath = Path.Combine(Environment.CurrentDirectory, "audio");
var latestAudio = new DirectoryInfo(dirPath).GetFiles().OrderByDescending(f => f.LastWriteTime).First();
var filePath = Path.Combine(dirPath, latestAudio.Name);
var audioFileBytes = await File.ReadAllBytesAsync(filePath);
var audioBytes = BinaryData.FromBytes(audioFileBytes);
List<ChatMessage> messages =
[
new UserChatMessage(
ChatMessageContentPart.CreateTextPart(InMemoryDb.Obj.SettingsManager.Settings.AudioPrompt),
ChatMessageContentPart.CreateInputAudioPart(audioBytes, ChatInputAudioFormat.Wav)
)
];
await ProcessChatStreamAsync(messages);
}
catch (Exception err)
{
_model.Content = err.Message;
} }
} }
@@ -109,15 +113,14 @@ public partial class ChatUserControl : UserControl
{ {
if (e.Key != Key.Enter) return; if (e.Key != Key.Enter) return;
var prompt = PromptBox.Text; var prompt = _model.Prompt;
if (prompt is null) return; if (prompt == string.Empty) return;
PromptBox.Clear(); _model.Prompt = string.Empty;
await ProcessChatStreamAsync(prompt); await ProcessChatStreamAsync(prompt);
} }
catch (Exception err) catch (Exception err)
{ {
ResultBlock.Text = err.Message; _model.Content = err.Message;
} }
} }
@@ -140,7 +143,7 @@ public partial class ChatUserControl : UserControl
responseBuilder.Append(token); responseBuilder.Append(token);
var html = Markdig.Markdown.ToHtml(responseBuilder.ToString(), _pipeline); var html = Markdig.Markdown.ToHtml(responseBuilder.ToString(), _pipeline);
ResultBlock.Text = html; _model.Content = html;
} }
} }
} }

View File

@@ -2,38 +2,41 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:highminded.models"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="highminded.ui.controls.SettingsUserControl"> x:Class="highminded.ui.controls.SettingsUserControl"
x:DataType="vm:SettingsViewModel">
<Grid Margin="15" RowSpacing="10" ColumnSpacing="10" RowDefinitions="Auto,Auto,Auto,Auto,Auto" ColumnDefinitions="*,*"> <Grid Margin="15" RowSpacing="10" ColumnSpacing="10" RowDefinitions="Auto,Auto,Auto,Auto,Auto"
ColumnDefinitions="*,*">
<StackPanel Grid.Row="0" Grid.Column="0"> <StackPanel Grid.Row="0" Grid.Column="0">
<TextBlock Text="Model" /> <TextBlock Text="Model" />
<TextBox Name="ModelTextBox" /> <TextBox Text="{Binding Model}" />
</StackPanel> </StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1"> <StackPanel Grid.Row="0" Grid.Column="1">
<TextBlock Text="API URL" /> <TextBlock Text="API URL" />
<TextBox Name="ApiUrlTextBox" /> <TextBox Text="{Binding ApiUrl}" />
</StackPanel> </StackPanel>
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"> <StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
<TextBlock Text="API Key" /> <TextBlock Text="API Key" />
<TextBox Name="ApiKeyTextBox" PasswordChar="*" /> <TextBox PasswordChar="*" Text="{Binding ApiKey}" />
</StackPanel> </StackPanel>
<StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"> <StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
<TextBlock Text="Screenshot Prompt" /> <TextBlock Text="Screenshot Prompt" />
<TextBox Name="ScreenshotPromptTextBox" TextWrapping="Wrap" MinLines="3" MaxLines="3"/> <TextBox TextWrapping="Wrap" MinLines="3" MaxLines="3" Text="{Binding ScreenshotPrompt}" />
</StackPanel> </StackPanel>
<StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"> <StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2">
<TextBlock Text="Audio Prompt" /> <TextBlock Text="Audio Prompt" />
<TextBox Name="AudioPromptTextbox" TextWrapping="Wrap" MinLines="3" MaxLines="3"/> <TextBox TextWrapping="Wrap" MinLines="3" MaxLines="3" Text="{Binding AudioPrompt}" />
</StackPanel> </StackPanel>
<StackPanel Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"> <StackPanel Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2">
<Button Name="SaveSettingsBtn" Content="Save" Click="SaveSettingsBtn_OnClick"/> <Button Content="Save" Click="SaveSettingsBtn_OnClick" />
</StackPanel> </StackPanel>
</Grid> </Grid>

View File

@@ -9,20 +9,11 @@ public partial class SettingsUserControl : UserControl
public SettingsUserControl() public SettingsUserControl()
{ {
InitializeComponent(); InitializeComponent();
DataContext = InMemoryDb.Obj.SettingsViewModel;
ModelTextBox.Text = InMemoryDb.Obj.SettingsManager.Settings.Model;
ApiUrlTextBox.Text = InMemoryDb.Obj.SettingsManager.Settings.ApiURL;
ApiKeyTextBox.Text = InMemoryDb.Obj.SettingsManager.Settings.ApiKey;
ScreenshotPromptTextBox.Text = InMemoryDb.Obj.SettingsManager.Settings.ScreenshotPrompt;
AudioPromptTextbox.Text = InMemoryDb.Obj.SettingsManager.Settings.AudioPrompt;
} }
private void SaveSettingsBtn_OnClick(object? sender, RoutedEventArgs e) private void SaveSettingsBtn_OnClick(object? sender, RoutedEventArgs e)
{ {
InMemoryDb.Obj.SaveSettings(new AppSettings() InMemoryDb.Obj.SaveSettings();
{
ApiKey = ApiKeyTextBox.Text, ApiURL = ApiUrlTextBox.Text, Model = ModelTextBox.Text,
ScreenshotPrompt = ScreenshotPromptTextBox.Text, AudioPrompt = AudioPromptTextbox.Text
});
} }
} }

View File

@@ -3,8 +3,10 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:iconPacks="https://github.com/MahApps/IconPacks.Avalonia" xmlns:iconPacks="https://github.com/MahApps/IconPacks.Avalonia"
xmlns:vm="using:highminded.models"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="highminded.ui.windows.MainWindow" x:Class="highminded.ui.windows.MainWindow"
x:DataType="vm:MainViewModel"
Title="highminded" Title="highminded"
Height="600" Height="600"
Width="800" Width="800"
@@ -18,10 +20,20 @@
Foreground="#fff" Foreground="#fff"
TransparencyBackgroundFallback="Transparent" TransparencyBackgroundFallback="Transparent"
FontSize="16"> FontSize="16">
<Window.Styles>
<Style Selector="Border.recording">
<Setter Property="BorderBrush" Value="#19ffffff" />
</Style>
<Style Selector="Border.recording[IsVisible=True]">
<Setter Property="BorderBrush" Value="#997ce87c" />
</Style>
</Window.Styles>
<Grid RowDefinitions="Auto,*" Margin="5" RowSpacing="10"> <Grid RowDefinitions="Auto,*" Margin="5" RowSpacing="10">
<Border Grid.Row="0" CornerRadius="5" Background="Black" Opacity="0.8" PointerPressed="OnPointerPressed"> <Border Grid.Row="0" CornerRadius="5" Background="Black" Opacity="0.8" PointerPressed="OnPointerPressed">
<Grid ColumnDefinitions="*,Auto" Margin="15 10"> <Grid ColumnDefinitions="*,Auto" Margin="15 10">
<TextBlock Grid.Column="0" VerticalAlignment="Center" Margin="5">High Minded</TextBlock> <TextBlock Grid.Column="0" VerticalAlignment="Center" Margin="5" Text="{Binding AppName}" />
<StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center" <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center"
HorizontalAlignment="Center"> HorizontalAlignment="Center">
<Border Background="Black" BorderBrush="#19ffffff" BorderThickness="2" CornerRadius="5" <Border Background="Black" BorderBrush="#19ffffff" BorderThickness="2" CornerRadius="5"
@@ -29,7 +41,7 @@
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"
Spacing="5" Margin="10 0"> Spacing="5" Margin="10 0">
<TextBlock FontSize="12" Text="Hide:" /> <TextBlock FontSize="12" Text="Hide:" />
<TextBlock FontSize="12" Text="SHIFT + \" /> <TextBlock FontSize="12" Text="{Binding HideShortcutKey}" />
</StackPanel> </StackPanel>
</Border> </Border>
<Border Background="Black" BorderBrush="#19ffffff" BorderThickness="2" CornerRadius="5" <Border Background="Black" BorderBrush="#19ffffff" BorderThickness="2" CornerRadius="5"
@@ -37,15 +49,31 @@
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"
Spacing="5" Margin="10 0"> Spacing="5" Margin="10 0">
<TextBlock FontSize="12" Text="Screen:" /> <TextBlock FontSize="12" Text="Screen:" />
<TextBlock FontSize="12" Text="SHIFT + S" /> <TextBlock FontSize="12" Text="{Binding ScreenshotShortcutKey}" />
</StackPanel> </StackPanel>
</Border> </Border>
<Border Background="Black" BorderBrush="#19ffffff" BorderThickness="2" CornerRadius="5" <Border
IsVisible="{Binding !IsRecording}"
BorderBrush="#19ffffff"
Background="Black"
BorderThickness="2" CornerRadius="5"
Margin="0 0 10 0"> Margin="0 0 10 0">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"
Spacing="5" Margin="10 0"> Spacing="5" Margin="10 0">
<TextBlock FontSize="12" Text="Audio:" /> <TextBlock FontSize="12" Text="Audio:" />
<TextBlock FontSize="12" Text="SHIFT + A" /> <TextBlock FontSize="12" Text="{Binding AudioShortcutKey}" />
</StackPanel>
</Border>
<Border
IsVisible="{Binding IsRecording}"
BorderBrush="#997ce87c"
Background="Black"
BorderThickness="2" CornerRadius="5"
Margin="0 0 10 0">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"
Spacing="5" Margin="10 0">
<TextBlock FontSize="12" Text="Audio:" />
<TextBlock FontSize="12" Text="{Binding AudioShortcutKey}" />
</StackPanel> </StackPanel>
</Border> </Border>
<Button Name="ChatBtn" Background="Transparent" Click="ChatBtnClick"> <Button Name="ChatBtn" Background="Transparent" Click="ChatBtnClick">
@@ -55,7 +83,7 @@
<iconPacks:PackIconLucide Grid.Column="1" Kind="Settings" Height="16" Width="16" /> <iconPacks:PackIconLucide Grid.Column="1" Kind="Settings" Height="16" Width="16" />
</Button> </Button>
<Button Name="HideBtn" Background="Transparent" Click="HideBtnClick"> <Button Name="HideBtn" Background="Transparent" Click="HideBtnClick">
<iconPacks:PackIconLucide Grid.Column="1" Kind="X" Height="13" Width="13"/> <iconPacks:PackIconLucide Grid.Column="1" Kind="X" Height="13" Width="13" />
</Button> </Button>
</StackPanel> </StackPanel>

View File

@@ -6,6 +6,7 @@ using Avalonia.Interactivity;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Threading; using Avalonia.Threading;
using highminded.ui.controls; using highminded.ui.controls;
using highminded.utils;
using SharpHook; using SharpHook;
using SharpHook.Data; using SharpHook.Data;
@@ -36,13 +37,14 @@ public partial class MainWindow : Window
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
DataContext = InMemoryDb.Obj.MainViewModel;
UControl.Content = _chatUserControl; UControl.Content = _chatUserControl;
ChatBtnActive(); ChatBtnActive();
HideOverlay(); HideOverlay();
// Global Hotkey // Global Hotkey
_hook.KeyPressed += OnKeyPressed; _hook.KeyPressed += OnKeyPressed;
_hook.RunAsync(); _hook.RunAsync();
_chatUserControl.SendAudio();
} }
private void OnPointerPressed(object? sender, PointerPressedEventArgs e) private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
@@ -74,56 +76,6 @@ public partial class MainWindow : Window
SettingsBtn.Background = new SolidColorBrush(Color.FromArgb(25, 255, 255, 255)); SettingsBtn.Background = new SolidColorBrush(Color.FromArgb(25, 255, 255, 255));
} }
private void OnKeyPressed(object? sender, KeyboardHookEventArgs e)
{
bool hasCtrl = (e.RawEvent.Mask & EventMask.Ctrl) != EventMask.None;
bool hasAlt = (e.RawEvent.Mask & EventMask.Alt) != EventMask.None;
bool hasShift = (e.RawEvent.Mask & EventMask.Shift) != EventMask.None;
bool hasH = e.Data.KeyCode == KeyCode.VcH;
bool hasBackslash = e.Data.KeyCode == KeyCode.VcBackslash;
bool hasA = e.Data.KeyCode == KeyCode.VcA;
bool hasS = e.Data.KeyCode == KeyCode.VcS;
bool hasQ = e.Data.KeyCode == KeyCode.VcQ;
if (hasCtrl && hasShift && hasAlt && hasH)
{
ShowOverlay();
}
if (hasShift && hasS)
{
Dispatcher.UIThread.Post(() => { _chatUserControl.SendScreenshot(); });
}
if (hasShift && hasA)
{
Dispatcher.UIThread.Post(() => { _chatUserControl.StartRecord(); });
}
if (hasAlt && hasShift && hasQ)
{
Dispatcher.UIThread.Post(() => { Environment.Exit(0); });
}
if (hasShift && hasBackslash)
{
Dispatcher.UIThread.Post(() =>
{
if (WindowState == WindowState.Minimized || !IsVisible)
{
Show();
WindowState = WindowState.Normal;
Activate();
Topmost = true;
}
else
{
Hide();
}
});
}
}
private void ShowOverlay() private void ShowOverlay()
{ {
var handle = TryGetPlatformHandle(); var handle = TryGetPlatformHandle();
@@ -188,4 +140,54 @@ public partial class MainWindow : Window
{ {
Hide(); Hide();
} }
private void OnKeyPressed(object? sender, KeyboardHookEventArgs e)
{
bool hasCtrl = (e.RawEvent.Mask & EventMask.Ctrl) != EventMask.None;
bool hasAlt = (e.RawEvent.Mask & EventMask.Alt) != EventMask.None;
bool hasShift = (e.RawEvent.Mask & EventMask.Shift) != EventMask.None;
bool hasH = e.Data.KeyCode == KeyCode.VcH;
bool hasBackslash = e.Data.KeyCode == KeyCode.VcBackslash;
bool hasA = e.Data.KeyCode == KeyCode.VcA;
bool hasS = e.Data.KeyCode == KeyCode.VcS;
bool hasQ = e.Data.KeyCode == KeyCode.VcQ;
if (hasCtrl && hasShift && hasAlt && hasH)
{
ShowOverlay();
}
if (hasShift && hasS)
{
Dispatcher.UIThread.Post(() => { _chatUserControl.SendScreenshot(); });
}
if (hasShift && hasA)
{
Dispatcher.UIThread.Post(() => { _chatUserControl.StartRecord(); });
}
if (hasAlt && hasShift && hasQ)
{
Dispatcher.UIThread.Post(() => { Environment.Exit(0); });
}
if (hasShift && hasBackslash)
{
Dispatcher.UIThread.Post(() =>
{
if (WindowState == WindowState.Minimized || !IsVisible)
{
Show();
WindowState = WindowState.Normal;
Activate();
Topmost = true;
}
else
{
Hide();
}
});
}
}
} }

View File

@@ -1,51 +1,27 @@
using NAudio.CoreAudioApi; using System.IO;
using NAudio.Wave; using SoundFlow.Backends.MiniAudio;
using SoundFlow.Components;
using SoundFlow.Enums;
namespace highminded.utils; namespace highminded.utils;
public class AudioCapture public class AudioCapture
{ {
private WasapiLoopbackCapture capture; private readonly MiniAudioEngine _audioEngine = new(48000, Capability.Loopback);
private WaveFileWriter writer; private Stream? _fileStream;
private string outputFilePath; private Recorder? _recorder;
public AudioCapture() public void StartRecording(string filePath)
{ {
capture = new WasapiLoopbackCapture(); _fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
} _recorder = new Recorder(_fileStream, sampleRate: 48000);
_recorder.StartRecording();
public void StartRecording(string outPath)
{
writer = new WaveFileWriter(outPath, capture.WaveFormat);
capture.DataAvailable += Capture_DataAvailable;
capture.RecordingStopped += Capture_RecordingStopped;
capture.StartRecording();
} }
public void StopRecording() public void StopRecording()
{ {
capture.StopRecording(); _recorder?.StopRecording();
} _recorder?.Dispose();
_fileStream?.Dispose();
public bool IsRecording()
{
if (capture.CaptureState == CaptureState.Capturing || capture.CaptureState == CaptureState.Starting)
{
return true;
}
return false;
}
private void Capture_DataAvailable(object sender, WaveInEventArgs e)
{
writer.Write(e.Buffer, 0, e.BytesRecorded);
}
private void Capture_RecordingStopped(object sender, StoppedEventArgs e)
{
writer.Flush();
writer.Dispose();
capture.Dispose();
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.ClientModel; using System.ClientModel;
using highminded.models;
using OpenAI; using OpenAI;
using OpenAI.Chat; using OpenAI.Chat;
@@ -7,37 +8,45 @@ namespace highminded.utils;
public class InMemoryDb public class InMemoryDb
{ {
internal ChatClient ChatClient; public static readonly InMemoryDb Obj = new InMemoryDb();
internal SettingsManager<AppSettings> SettingsManager;
internal readonly SettingsManager<SettingsViewModel> SettingsManager;
internal readonly MainViewModel MainViewModel;
internal readonly ChatViewModel ChatViewModel;
internal readonly SettingsViewModel SettingsViewModel;
internal ChatClient? ChatClient;
// Initialize Singleton Class // Initialize Singleton Class
private InMemoryDb() private InMemoryDb()
{ {
SettingsManager = new SettingsManager<AppSettings>(); SettingsManager = new SettingsManager<SettingsViewModel>();
if (SettingsManager.Settings.ApiKey != null)
MainViewModel = new MainViewModel();
ChatViewModel = new ChatViewModel();
SettingsViewModel = new SettingsViewModel();
SettingsViewModel = SettingsManager.Settings;
if (SettingsManager.Settings.ApiKey != string.Empty)
{ {
InitOpenAIClient(); InitOpenAiClient();
} }
} }
public void InitOpenAIClient() public void SaveSettings()
{
SettingsManager.Save();
InitOpenAiClient();
}
private void InitOpenAiClient()
{ {
ChatClient = new ChatClient( ChatClient = new ChatClient(
model: SettingsManager.Settings.Model, model: SettingsManager.Settings.Model,
credential: new ApiKeyCredential(SettingsManager.Settings.ApiKey), credential: new ApiKeyCredential(SettingsManager.Settings.ApiKey),
options: new OpenAIClientOptions options: new OpenAIClientOptions
{ {
Endpoint = new Uri(SettingsManager.Settings.ApiURL) Endpoint = new Uri(SettingsManager.Settings.ApiUrl)
} }
); );
} }
public void SaveSettings(AppSettings settings)
{
SettingsManager.Settings = settings;
SettingsManager.Save();
InitOpenAIClient();
}
public static readonly InMemoryDb Obj = new InMemoryDb();
} }

View File

@@ -4,22 +4,12 @@ using System.Text.Json;
namespace highminded.utils; namespace highminded.utils;
public class AppSettings
{
public string Model { get; set; }
public string ApiURL { get; set; }
public string ApiKey { get; set; }
public string ScreenshotPrompt { get; set; }
public string AudioPrompt { get; set; }
}
public class SettingsManager<T> where T : class, new() public class SettingsManager<T> where T : class, new()
{ {
private readonly string _settingsPath; private readonly string _settingsPath;
public T Settings { get; internal set; } public T Settings { get; internal set; }
public SettingsManager(string appName = "highminded") public SettingsManager()
{ {
_settingsPath = Path.Combine(Environment.CurrentDirectory, "settings.json"); _settingsPath = Path.Combine(Environment.CurrentDirectory, "settings.json");
Settings = Load(); Settings = Load();