Skip to content

Commit

Permalink
Fixed scaling issue with hotkey input form (#132)
Browse files Browse the repository at this point in the history
* Fixed issue with buttons displaying improperly on hotkey input form due to display scaling being set >100%. Closes [BUG] Hotkey Input Form Scaling Issue #131
* Modified desktop application to minimize to system tray on minimize or exit press on the application window.
* Reworded temporary text in the app's side bar.
  • Loading branch information
Fahim-zzz authored Dec 7, 2024
1 parent 3f17b94 commit 98e26ad
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 133 deletions.
13 changes: 11 additions & 2 deletions DAIRemote/DAIRemoteApplicationUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ private void DAIRemoteApplicationUI_FormClosing(object sender, FormClosingEventA
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = false;
trayIconManager.HideIcon();
this.Hide();
e.Cancel = true;
}
}

Expand Down Expand Up @@ -231,6 +231,7 @@ private void BtnCycleAudioOutputs_Click(object sender, EventArgs e)
private void BtnSetAudioHotkey_Click(object sender, EventArgs e)
{
trayIconManager.GetHotkeyManager().ShowHotkeyInput("Audio Cycling", audioManager.CycleAudioDevice);
trayIconManager.RefreshSystemTray();
}

private void InitializeDisplayProfilesList()
Expand Down Expand Up @@ -291,4 +292,12 @@ private void BtnSetDisplayProfileHotkey_click(object sender, EventArgs e)
trayIconManager.RefreshSystemTray();
}
}

private void DAIRemoteApplicationUI_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
this.Hide();
}
}
}
1 change: 1 addition & 0 deletions DAIRemote/DAIRemoteApplicationUI.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 98 additions & 4 deletions DAIRemote/HotkeyManager.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 48 additions & 92 deletions DAIRemote/HotkeyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace DAIRemote;
public partial class HotkeyManager : Form
{
private AudioManager.AudioDeviceManager audioManager;
private AudioManager.AudioDeviceManager audioManager = AudioManager.AudioDeviceManager.GetInstance();
public Dictionary<string, HotkeyConfig> hotkeyConfigs;
private readonly string ConfigFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DAIRemote/hotkeys.json");
private uint modifiers = 0;
private uint keyCode = 0;
private string action;

public HotkeyManager()
{
audioManager = AudioManager.AudioDeviceManager.GetInstance();
InitializeComponent();
LoadHotkeyConfigs();
}

Expand Down Expand Up @@ -51,7 +54,6 @@ public void LoadHotkeyConfigs()
if (File.Exists(ConfigFilePath))
{
hotkeyConfigs = JsonConvert.DeserializeObject<Dictionary<string, HotkeyConfig>>(File.ReadAllText(ConfigFilePath));
InitializeHotkeys();
}
else
{
Expand All @@ -74,15 +76,15 @@ public void RegisterHotkeys()
{
foreach (var config in hotkeyConfigs.Values)
{
RegisterHotKey(this.Handle, config.Action.GetHashCode(), config.Modifiers, config.Key);
_ = RegisterHotKey(this.Handle, config.Action.GetHashCode(), config.Modifiers, config.Key);
}
}

public void UnregisterHotkeys()
{
foreach (var config in hotkeyConfigs.Values)
{
UnregisterHotKey(this.Handle, config.Action.GetHashCode());
_ = UnregisterHotKey(this.Handle, config.Action.GetHashCode());
}
}

Expand All @@ -92,132 +94,92 @@ public void RegisterCallbacks()
foreach (string profile in DisplayConfig.GetDisplayProfiles())
{
string fileName = Path.GetFileNameWithoutExtension(profile);
if (hotkeyConfigs.ContainsKey(fileName))
if (hotkeyConfigs.TryGetValue(fileName, out HotkeyConfig? display))
{
hotkeyConfigs[fileName].Callback = () => DisplayConfig.SetDisplaySettings(profile);
display.Callback = () => DisplayConfig.SetDisplaySettings(profile);
}
}

// Register Audio Cycling Callback
if (hotkeyConfigs.ContainsKey("Audio Cycling"))
if (hotkeyConfigs.TryGetValue("Audio Cycling", out HotkeyConfig? audioCycling))
{
hotkeyConfigs["Audio Cycling"].Callback = audioManager.CycleAudioDevice;
audioCycling.Callback = audioManager.CycleAudioDevice;
}

// Register Audio Callbacks
foreach (string audioDeviceName in audioManager.ActiveDeviceNames)
{
if (hotkeyConfigs.ContainsKey(audioDeviceName))
if (hotkeyConfigs.TryGetValue(audioDeviceName, out HotkeyConfig? audioDevices))
{
hotkeyConfigs[audioDeviceName].Callback = () => audioManager.SetDefaultAudioDevice(audioDeviceName);
audioDevices.Callback = () => audioManager.SetDefaultAudioDevice(audioDeviceName);
}
}
}

public void ShowHotkeyInput(string action, Action functionAction)
{
Form inputForm = new()
HotkeyManager HotkeyInputForm = new()
{
Text = $"Set Hotkey for {action}",
Size = new Size(300, 150),
StartPosition = FormStartPosition.CenterScreen
action = action,
Text = $"Set Hotkey for {action}"
};

TextBox inputBox = new()
{
Dock = DockStyle.Top,
ReadOnly = true // Prevents Manual input
};

Button okButton = new()
if (hotkeyConfigs.ContainsKey(action))
{
Text = "OK",
Dock = DockStyle.Bottom,
DialogResult = DialogResult.OK
};
HotkeyInputForm.HotkeyInputBox.Text = GetHotkeyText(hotkeyConfigs[action]);
}

Button clearButton = new()
HotkeyInputForm.HotkeyInputBox.KeyDown += (s, e) =>
{
Text = "Clear",
Dock = DockStyle.Bottom
};
HotkeyInputForm.modifiers = 0;
if (e.Control) HotkeyInputForm.modifiers |= MOD_CONTROL;
if (e.Alt) HotkeyInputForm.modifiers |= MOD_ALT;
if (e.Shift) HotkeyInputForm.modifiers |= MOD_SHIFT;
HotkeyInputForm.keyCode = (uint)e.KeyCode;

Button cancelButton = new()
{
Text = "Cancel",
Dock = DockStyle.Bottom,
DialogResult = DialogResult.Cancel
// Display the combination in the input box
HotkeyInputForm.HotkeyInputBox.Text = $"{(e.Control ? "Ctrl+" : "")}{(e.Alt ? "Alt+" : "")}{(e.Shift ? "Shift+" : "")}{e.KeyCode}";
};

Panel buttonPanel = new()
HotkeyInputForm.HotkeyFormOkBtn.Click += (sender, e) =>
{
Dock = DockStyle.Bottom
if (HotkeyInputForm.keyCode != 0)
{
HotkeyConfig config = new()
{
Action = action,
Modifiers = HotkeyInputForm.modifiers,
Key = HotkeyInputForm.keyCode,
Callback = functionAction
};

RegisterNewHotkey(action, config);
}
};
buttonPanel.Controls.Add(clearButton);
buttonPanel.Controls.Add(okButton);
buttonPanel.Controls.Add(cancelButton);

// Set the OK button as the action for Enter key
inputForm.AcceptButton = okButton;

if (hotkeyConfigs.ContainsKey(action))
{
inputBox.Text = GetHotkeyText(hotkeyConfigs[action]);
}

uint modifiers = 0;
uint keyCode = 0;

clearButton.Click += (s, e) =>
HotkeyInputForm.HotkeyFormClearBtn.Click += (sender, e) =>
{
inputBox.Text = "Cleared";
modifiers = 0;
keyCode = 0;
HotkeyInputForm.HotkeyInputBox.Text = "Cleared";
HotkeyInputForm.modifiers = 0;
HotkeyInputForm.keyCode = 0;

UnregisterHotkey(action);
};

inputBox.KeyDown += (s, args) =>
HotkeyInputForm.HotkeyFormCancelBtn.Click += (sender, e) =>
{
modifiers = 0;
if (args.Control) modifiers |= MOD_CONTROL;
if (args.Alt) modifiers |= MOD_ALT;
if (args.Shift) modifiers |= MOD_SHIFT;
keyCode = (uint)args.KeyCode;

// Display the combination in the input box
inputBox.Text = $"{(args.Control ? "Ctrl+" : "")}{(args.Alt ? "Alt+" : "")}{(args.Shift ? "Shift+" : "")}{args.KeyCode}";
HotkeyInputForm.Close();
};

inputForm.Controls.Add(inputBox);
inputForm.Controls.Add(buttonPanel);

DialogResult result = inputForm.ShowDialog();

if (result == DialogResult.OK && keyCode != 0)
{
HotkeyConfig config = new()
{
Action = action,
Modifiers = modifiers,
Key = keyCode,
Callback = functionAction
};

RegisterNewHotkey(action, config);
}
else if (result == DialogResult.Cancel)
{
inputForm.Close();
}
HotkeyInputForm.ShowDialog();
}

private void RegisterNewHotkey(string action, HotkeyConfig newConfig)
{
UnregisterHotkey(action);
hotkeyConfigs[action] = newConfig;
SaveHotkeyConfigs();
RegisterHotKey(this.Handle, action.GetHashCode(), newConfig.Modifiers, newConfig.Key);
_ = RegisterHotKey(this.Handle, action.GetHashCode(), newConfig.Modifiers, newConfig.Key);
}

protected override void WndProc(ref Message m)
Expand All @@ -235,10 +197,4 @@ protected override void WndProc(ref Message m)
}
base.WndProc(ref m);
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
UnregisterHotkeys(); // Unregister hotkeys on application close
base.OnFormClosing(e);
}
}
Loading

0 comments on commit 98e26ad

Please sign in to comment.