Skip to content

Commit

Permalink
aider: (no commit message provided)
Browse files Browse the repository at this point in the history
# Aider chat conversation:

USER: Add a new option to configuration form - default language. It should accept text as an input. Value from that option should be sent as language in AudioTranscriptionRequest.

ASSISTANT: To add a new option for the default language in the configuration form and ensure that the value is used in the `AudioTranscriptionRequest`, we need to modify the following files:

1. `whisperer/ConfigForm.cs` - To add the new UI elements for the default language option and handle loading and saving of this setting.
2. `whisperer/ConfigurationManager.cs` - To add methods for reading and saving the default language setting.
3. `whisperer/Program.cs` - To modify the `TranscribeAudio` method to use the default language setting when creating the `AudioTranscriptionRequest`.

Let's start with the `ConfigForm.cs` file to add the UI elements and logic for the default language setting.

```csharp
whisperer/ConfigForm.cs
<<<<<<< SEARCH
    private TableLayoutPanel tableLayoutPanel;
=======
    private TableLayoutPanel tableLayoutPanel;
    private TextBox defaultLanguageTextBox;
    private Label defaultLanguageLabel;
>>>>>>> REPLACE

...

<<<<<<< SEARCH
    private void InitializeFormControls()
    {
        InitializeTableLayoutPanel();
        InitializeApiKeyControls();
        InitializeHotkeyControls();
        InitializeSaveButton();
        InitializeCopyrightInformation();
    }
=======
    private void InitializeFormControls()
    {
        InitializeTableLayoutPanel();
        InitializeApiKeyControls();
        InitializeHotkeyControls();
        InitializeDefaultLanguageControls(); // Add this line
        InitializeSaveButton();
        InitializeCopyrightInformation();
    }

    private void InitializeDefaultLanguageControls()
    {
        defaultLanguageLabel = new Label { Text = "Default Language:" };
        tableLayoutPanel.Controls.Add(defaultLanguageLabel, 0, 2);
        defaultLanguageTextBox = new TextBox
        {
            Width = 300
        };
        tableLayoutPanel.Controls.Add(defaultLanguageTextBox, 1, 2);
        LoadDefaultLanguage(); // Add this line
    }

    private void LoadDefaultLanguage()
    {
        defaultLanguageTextBox.Text = ConfigurationManager.ReadDefaultLanguage();
    }
>>>>>>> REPLACE

...

<<<<<<< SEARCH
    private void saveButton_Click(object? sender, EventArgs e)
    {
        ConfigurationManager.SaveApiKey(apiKeyTextBox.Text);
        ConfigurationManager.SaveHotkey(currentHotkey);
        MessageBox.Show("Settings saved securely.");
    }
=======
    private void saveButton_Click(object? sender, EventArgs e)
    {
        ConfigurationManager.SaveApiKey(apiKeyTextBox.Text);
        ConfigurationManager.SaveHotkey(currentHotkey);
        ConfigurationManager.SaveDefaultLanguage(defaultLanguageTextBox.Text); // Add this line
        MessageBox.Show("Settings saved securely.");
    }
>>>>>>> REPLACE
```

Next, we'll update the `ConfigurationManager.cs` file to include methods for reading and saving the default language setting.

```csharp
whisperer/ConfigurationManager.cs
<<<<<<< SEARCH
    private static readonly string HotkeyFileName = Path.Combine(AppDataFolder, "hotkey.dat");
=======
    private static readonly string HotkeyFileName = Path.Combine(AppDataFolder, "hotkey.dat");
    private static readonly string DefaultLanguageFileName = Path.Combine(AppDataFolder, "defaultLanguage.dat"); // Add this line
>>>>>>> REPLACE

...

<<<<<<< SEARCH
    public static void SaveHotkey(Keys hotkey)
=======
    public static string ReadDefaultLanguage()
    {
        try
        {
            if (File.Exists(DefaultLanguageFileName))
            {
                byte[] encryptedLanguage = File.ReadAllBytes(DefaultLanguageFileName);
                byte[] languageBytes = ProtectedData.Unprotect(encryptedLanguage, null, DataProtectionScope.CurrentUser);
                return Encoding.UTF8.GetString(languageBytes);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Error reading default language: {ex.Message}");
        }

        return "en"; // Return a default value
    }

    public static void SaveDefaultLanguage(string language)
    {
        try
        {
            byte[] languageBytes = Encoding.UTF8.GetBytes(language);
            byte[] encryptedLanguage = ProtectedData.Protect(languageBytes, null, DataProtectionScope.CurrentUser);
            File.WriteAllBytes(DefaultLanguageFileName, encryptedLanguage);
            SettingsSaved?.Invoke(null, EventArgs.Empty);
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Error saving default language: {ex.Message}");
        }
    }

    public static void SaveHotkey(Keys hotkey)
>>>>>>> REPLACE
```

Finally, we need to modify the `Program.cs` file to use the default language setting when creating the `AudioTranscriptionRequest`.

```csharp
whisperer/Program.cs
<<<<<<< SEARCH
        var request = new AudioTranscriptionRequest(apiStream, null, language: "en");
=======
        string defaultLanguage = ConfigurationManager.ReadDefaultLanguage();
        var request = new AudioTranscriptionRequest(apiStream, null, language: defaultLanguage);
>>>>>>> REPLACE
```

These changes will add a new text box to the configuration form for the default language, save the setting, and use it when transcribing audio.
  • Loading branch information
hudbrog committed Nov 15, 2023
1 parent a443164 commit 8415686
Show file tree
Hide file tree
Showing 3 changed files with 571 additions and 514 deletions.
345 changes: 183 additions & 162 deletions whisperer/ConfigForm.cs
Original file line number Diff line number Diff line change
@@ -1,162 +1,183 @@
using System;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Configuration;

namespace whisperer;
public partial class ConfigForm : Form
{
private TextBox apiKeyTextBox;
private TextBox hotkeyTextBox;
private Keys currentHotkey;
private bool waitingForHotkey = false;
private Button saveButton;
private Label apiKeyLabel;
private Label hotkeyLabel;
private Label copyrightLabel;
private TableLayoutPanel tableLayoutPanel;

public ConfigForm()
{
InitializeComponent();
InitializeFormControls();
LoadApiKey();
InitializeHotkeyControl();
this.Size = new Size(400, 200); // Set a proper size for the form
LoadHotkey();
}

private void InitializeFormControls()
{
InitializeTableLayoutPanel();
InitializeApiKeyControls();
InitializeHotkeyControls();
InitializeSaveButton();
InitializeCopyrightInformation();
}

private void InitializeTableLayoutPanel()
{
tableLayoutPanel = new TableLayoutPanel
{
ColumnCount = 2,
RowCount = 3,
Dock = DockStyle.Fill
};
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80F));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
this.Controls.Add(tableLayoutPanel);
}

private void InitializeApiKeyControls()
{
apiKeyLabel = new Label { Text = "API Key:" };
tableLayoutPanel.Controls.Add(apiKeyLabel, 0, 0);
apiKeyTextBox = new TextBox
{
Width = 300
};
tableLayoutPanel.Controls.Add(apiKeyTextBox, 1, 0);
}

private void InitializeHotkeyControls()
{
hotkeyLabel = new Label { Text = "Hotkey:" };
tableLayoutPanel.Controls.Add(hotkeyLabel, 0, 1);
}

private void InitializeSaveButton()
{
saveButton = new Button { Text = "Save" };
saveButton.Click += saveButton_Click;
tableLayoutPanel.Controls.Add(saveButton, 1, 2);
}

private void InitializeCopyrightInformation()
{
copyrightLabel = new Label
{
Text = "Whisperer by hudbrog\n" +
"For more information visit [GitHub Placeholder]\n" +
"� 2023 hudbrog\n\n",
Dock = DockStyle.Bottom,
TextAlign = ContentAlignment.MiddleCenter
};
this.Controls.Add(copyrightLabel);
}

private void LoadApiKey()
{
apiKeyTextBox.Text = ConfigurationManager.ReadApiKey();
}

private void saveButton_Click(object? sender, EventArgs e)
{
ConfigurationManager.SaveApiKey(apiKeyTextBox.Text);
ConfigurationManager.SaveHotkey(currentHotkey);
MessageBox.Show("Settings saved securely.");
}

private void InitializeHotkeyControl()
{
hotkeyTextBox = new TextBox
{
ReadOnly = true, // Prevent typing in the control
Cursor = Cursors.Hand, // Indicate that the control can be clicked
Width = 300
};
hotkeyTextBox.Click += HotkeyTextBox_Click;
hotkeyTextBox.KeyDown += HotkeyTextBox_KeyDown;
hotkeyTextBox.KeyUp += HotkeyTextBox_KeyUp;
hotkeyTextBox.LostFocus += HotkeyTextBox_LostFocus;
tableLayoutPanel.Controls.Add(hotkeyTextBox, 1, 1);
}

private void HotkeyTextBox_Click(object? sender, EventArgs e)
{
// When the user clicks the control, start waiting for a hotkey press
waitingForHotkey = true;
hotkeyTextBox.Text = "Press a hotkey...";
}

private void HotkeyTextBox_KeyDown(object? sender, KeyEventArgs e)
{
if (waitingForHotkey)
{
// Update the text box with the pressed key combination
currentHotkey = e.KeyCode | e.Modifiers;
hotkeyTextBox.Text = TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(currentHotkey);
e.SuppressKeyPress = true; // Prevent further processing of the key
}
}

private void HotkeyTextBox_KeyUp(object? sender, KeyEventArgs e)
{
if (waitingForHotkey)
{
waitingForHotkey = false;
}
}

private void HotkeyTextBox_LostFocus(object? sender, EventArgs e)
{
if (waitingForHotkey)
{
// Reset if the user clicks away without pressing a key
hotkeyTextBox.Text = string.Empty;
currentHotkey = Keys.None;
waitingForHotkey = false;
}
}
private void LoadHotkey()
{
currentHotkey = ConfigurationManager.ReadHotkey();
hotkeyTextBox.Text = TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(currentHotkey);
}
}
using System;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Configuration;

namespace whisperer;
public partial class ConfigForm : Form
{
private TextBox apiKeyTextBox;
private TextBox hotkeyTextBox;
private Keys currentHotkey;
private bool waitingForHotkey = false;
private Button saveButton;
private Label apiKeyLabel;
private Label hotkeyLabel;
private Label copyrightLabel;
private TableLayoutPanel tableLayoutPanel;
private TextBox defaultLanguageTextBox;
private Label defaultLanguageLabel;

public ConfigForm()
{
InitializeComponent();
InitializeFormControls();
LoadApiKey();
InitializeHotkeyControl();
this.Size = new Size(400, 200); // Set a proper size for the form
LoadHotkey();
}

private void InitializeFormControls()
{
InitializeTableLayoutPanel();
InitializeApiKeyControls();
InitializeHotkeyControls();
InitializeDefaultLanguageControls(); // Add this line
InitializeSaveButton();
InitializeCopyrightInformation();
}

private void InitializeDefaultLanguageControls()
{
defaultLanguageLabel = new Label { Text = "Default Language:" };
tableLayoutPanel.Controls.Add(defaultLanguageLabel, 0, 2);
defaultLanguageTextBox = new TextBox
{
Width = 300
};
tableLayoutPanel.Controls.Add(defaultLanguageTextBox, 1, 2);
LoadDefaultLanguage(); // Add this line
}

private void LoadDefaultLanguage()
{
defaultLanguageTextBox.Text = ConfigurationManager.ReadDefaultLanguage();
}

private void InitializeTableLayoutPanel()
{
tableLayoutPanel = new TableLayoutPanel
{
ColumnCount = 2,
RowCount = 3,
Dock = DockStyle.Fill
};
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80F));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
this.Controls.Add(tableLayoutPanel);
}

private void InitializeApiKeyControls()
{
apiKeyLabel = new Label { Text = "API Key:" };
tableLayoutPanel.Controls.Add(apiKeyLabel, 0, 0);
apiKeyTextBox = new TextBox
{
Width = 300
};
tableLayoutPanel.Controls.Add(apiKeyTextBox, 1, 0);
}

private void InitializeHotkeyControls()
{
hotkeyLabel = new Label { Text = "Hotkey:" };
tableLayoutPanel.Controls.Add(hotkeyLabel, 0, 1);
}

private void InitializeSaveButton()
{
saveButton = new Button { Text = "Save" };
saveButton.Click += saveButton_Click;
tableLayoutPanel.Controls.Add(saveButton, 1, 2);
}

private void InitializeCopyrightInformation()
{
copyrightLabel = new Label
{
Text = "Whisperer by hudbrog\n" +
"For more information visit [GitHub Placeholder]\n" +
"� 2023 hudbrog\n\n",
Dock = DockStyle.Bottom,
TextAlign = ContentAlignment.MiddleCenter
};
this.Controls.Add(copyrightLabel);
}

private void LoadApiKey()
{
apiKeyTextBox.Text = ConfigurationManager.ReadApiKey();
}

private void saveButton_Click(object? sender, EventArgs e)
{
ConfigurationManager.SaveApiKey(apiKeyTextBox.Text);
ConfigurationManager.SaveHotkey(currentHotkey);
ConfigurationManager.SaveDefaultLanguage(defaultLanguageTextBox.Text); // Add this line
MessageBox.Show("Settings saved securely.");
}

private void InitializeHotkeyControl()
{
hotkeyTextBox = new TextBox
{
ReadOnly = true, // Prevent typing in the control
Cursor = Cursors.Hand, // Indicate that the control can be clicked
Width = 300
};
hotkeyTextBox.Click += HotkeyTextBox_Click;
hotkeyTextBox.KeyDown += HotkeyTextBox_KeyDown;
hotkeyTextBox.KeyUp += HotkeyTextBox_KeyUp;
hotkeyTextBox.LostFocus += HotkeyTextBox_LostFocus;
tableLayoutPanel.Controls.Add(hotkeyTextBox, 1, 1);
}

private void HotkeyTextBox_Click(object? sender, EventArgs e)
{
// When the user clicks the control, start waiting for a hotkey press
waitingForHotkey = true;
hotkeyTextBox.Text = "Press a hotkey...";
}

private void HotkeyTextBox_KeyDown(object? sender, KeyEventArgs e)
{
if (waitingForHotkey)
{
// Update the text box with the pressed key combination
currentHotkey = e.KeyCode | e.Modifiers;
hotkeyTextBox.Text = TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(currentHotkey);
e.SuppressKeyPress = true; // Prevent further processing of the key
}
}

private void HotkeyTextBox_KeyUp(object? sender, KeyEventArgs e)
{
if (waitingForHotkey)
{
waitingForHotkey = false;
}
}

private void HotkeyTextBox_LostFocus(object? sender, EventArgs e)
{
if (waitingForHotkey)
{
// Reset if the user clicks away without pressing a key
hotkeyTextBox.Text = string.Empty;
currentHotkey = Keys.None;
waitingForHotkey = false;
}
}
private void LoadHotkey()
{
currentHotkey = ConfigurationManager.ReadHotkey();
hotkeyTextBox.Text = TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(currentHotkey);
}
}
Loading

0 comments on commit 8415686

Please sign in to comment.