diff --git a/DAIRemote/DAIRemoteApplicationUI.cs b/DAIRemote/DAIRemoteApplicationUI.cs index e318fef..3e32901 100644 --- a/DAIRemote/DAIRemoteApplicationUI.cs +++ b/DAIRemote/DAIRemoteApplicationUI.cs @@ -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; } } @@ -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() @@ -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(); + } + } } diff --git a/DAIRemote/DAIRemoteApplicationUI.designer.cs b/DAIRemote/DAIRemoteApplicationUI.designer.cs index 329cead..5db36ac 100644 --- a/DAIRemote/DAIRemoteApplicationUI.designer.cs +++ b/DAIRemote/DAIRemoteApplicationUI.designer.cs @@ -214,6 +214,7 @@ private void InitializeComponent() this.Controls.Add(this.BtnSaveDisplayConfig); this.Name = "DAIRemoteApplicationUI"; this.Text = "DAIRemote"; + Resize += this.DAIRemoteApplicationUI_Resize; ((System.ComponentModel.ISupportInitialize)this.Logo).EndInit(); this.ResumeLayout(false); this.PerformLayout(); diff --git a/DAIRemote/HotkeyManager.Designer.cs b/DAIRemote/HotkeyManager.Designer.cs index d31a2c6..96c13e9 100644 --- a/DAIRemote/HotkeyManager.Designer.cs +++ b/DAIRemote/HotkeyManager.Designer.cs @@ -28,11 +28,105 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + this.HotkeyInputBox = new TextBox(); + this.HotkeyFormBtnPanel = new TableLayoutPanel(); + this.HotkeyFormClearBtn = new Button(); + this.HotkeyFormOkBtn = new Button(); + this.HotkeyFormCancelBtn = new Button(); + this.HotkeyFormBtnPanel.SuspendLayout(); + this.SuspendLayout(); + // + // HotkeyInputBox + // + this.HotkeyInputBox.AcceptsReturn = true; + this.HotkeyInputBox.Dock = DockStyle.Top; + this.HotkeyInputBox.Location = new Point(0, 0); + this.HotkeyInputBox.Margin = new Padding(2); + this.HotkeyInputBox.Name = "HotkeyInputBox"; + this.HotkeyInputBox.PlaceholderText = "Press a key/key combination"; + this.HotkeyInputBox.ReadOnly = true; + this.HotkeyInputBox.Size = new Size(254, 23); + this.HotkeyInputBox.TabIndex = 0; + // + // HotkeyFormBtnPanel + // + this.HotkeyFormBtnPanel.AutoSize = true; + this.HotkeyFormBtnPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink; + this.HotkeyFormBtnPanel.ColumnCount = 3; + this.HotkeyFormBtnPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.3333321F)); + this.HotkeyFormBtnPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.3333321F)); + this.HotkeyFormBtnPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.3333321F)); + this.HotkeyFormBtnPanel.Controls.Add(this.HotkeyFormClearBtn, 0, 0); + this.HotkeyFormBtnPanel.Controls.Add(this.HotkeyFormOkBtn, 1, 0); + this.HotkeyFormBtnPanel.Controls.Add(this.HotkeyFormCancelBtn, 2, 0); + this.HotkeyFormBtnPanel.Dock = DockStyle.Fill; + this.HotkeyFormBtnPanel.GrowStyle = TableLayoutPanelGrowStyle.FixedSize; + this.HotkeyFormBtnPanel.Location = new Point(0, 23); + this.HotkeyFormBtnPanel.Name = "HotkeyFormBtnPanel"; + this.HotkeyFormBtnPanel.RowCount = 1; + this.HotkeyFormBtnPanel.RowStyles.Add(new RowStyle()); + this.HotkeyFormBtnPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F)); + this.HotkeyFormBtnPanel.Size = new Size(254, 28); + this.HotkeyFormBtnPanel.TabIndex = 1; + // + // HotkeyFormClearBtn + // + this.HotkeyFormClearBtn.AutoSizeMode = AutoSizeMode.GrowAndShrink; + this.HotkeyFormClearBtn.Location = new Point(3, 3); + this.HotkeyFormClearBtn.Name = "HotkeyFormClearBtn"; + this.HotkeyFormClearBtn.Size = new Size(75, 23); + this.HotkeyFormClearBtn.TabIndex = 0; + this.HotkeyFormClearBtn.Text = "Clear"; + this.HotkeyFormClearBtn.UseVisualStyleBackColor = true; + // + // HotkeyFormOkBtn + // + this.HotkeyFormOkBtn.DialogResult = DialogResult.OK; + this.HotkeyFormOkBtn.Location = new Point(87, 3); + this.HotkeyFormOkBtn.Name = "HotkeyFormOkBtn"; + this.HotkeyFormOkBtn.Size = new Size(75, 23); + this.HotkeyFormOkBtn.TabIndex = 1; + this.HotkeyFormOkBtn.Text = "Ok"; + this.HotkeyFormOkBtn.UseVisualStyleBackColor = true; + // + // HotkeyFormCancelBtn + // + this.HotkeyFormCancelBtn.DialogResult = DialogResult.Cancel; + this.HotkeyFormCancelBtn.Location = new Point(171, 3); + this.HotkeyFormCancelBtn.Name = "HotkeyFormCancelBtn"; + this.HotkeyFormCancelBtn.Size = new Size(75, 23); + this.HotkeyFormCancelBtn.TabIndex = 2; + this.HotkeyFormCancelBtn.Text = "Cancel"; + this.HotkeyFormCancelBtn.UseVisualStyleBackColor = true; + // + // HotkeyManager + // + this.AutoScaleDimensions = new SizeF(96F, 96F); + this.AutoScaleMode = AutoScaleMode.Dpi; + this.AutoSizeMode = AutoSizeMode.GrowAndShrink; + this.ClientSize = new Size(254, 51); + this.Controls.Add(this.HotkeyFormBtnPanel); + this.Controls.Add(this.HotkeyInputBox); + this.FormBorderStyle = FormBorderStyle.FixedSingle; + this.MaximizeBox = false; + this.MinimumSize = new Size(265, 74); + this.Name = "HotkeyManager"; + this.ShowIcon = false; + this.ShowInTaskbar = false; + this.SizeGripStyle = SizeGripStyle.Hide; + this.StartPosition = FormStartPosition.CenterScreen; + this.Text = "Hotkey Input"; + this.TopMost = true; + this.HotkeyFormBtnPanel.ResumeLayout(false); + this.ResumeLayout(false); + this.PerformLayout(); } #endregion + + private TextBox HotkeyInputBox; + private TableLayoutPanel HotkeyFormBtnPanel; + private Button HotkeyFormClearBtn; + private Button HotkeyFormOkBtn; + private Button HotkeyFormCancelBtn; } \ No newline at end of file diff --git a/DAIRemote/HotkeyManager.cs b/DAIRemote/HotkeyManager.cs index 158ff94..a8ce971 100644 --- a/DAIRemote/HotkeyManager.cs +++ b/DAIRemote/HotkeyManager.cs @@ -5,13 +5,16 @@ namespace DAIRemote; public partial class HotkeyManager : Form { - private AudioManager.AudioDeviceManager audioManager; + private AudioManager.AudioDeviceManager audioManager = AudioManager.AudioDeviceManager.GetInstance(); public Dictionary 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(); } @@ -51,7 +54,6 @@ public void LoadHotkeyConfigs() if (File.Exists(ConfigFilePath)) { hotkeyConfigs = JsonConvert.DeserializeObject>(File.ReadAllText(ConfigFilePath)); - InitializeHotkeys(); } else { @@ -74,7 +76,7 @@ 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); } } @@ -82,7 +84,7 @@ public void UnregisterHotkeys() { foreach (var config in hotkeyConfigs.Values) { - UnregisterHotKey(this.Handle, config.Action.GetHashCode()); + _ = UnregisterHotKey(this.Handle, config.Action.GetHashCode()); } } @@ -92,124 +94,84 @@ 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) @@ -217,7 +179,7 @@ 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) @@ -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); - } } diff --git a/DAIRemote/HotkeyManager.resx b/DAIRemote/HotkeyManager.resx index 1af7de1..8b2ff64 100644 --- a/DAIRemote/HotkeyManager.resx +++ b/DAIRemote/HotkeyManager.resx @@ -1,17 +1,17 @@  - diff --git a/DAIRemote/SystemTray.cs b/DAIRemote/SystemTray.cs index dbc1c7d..890c8d5 100644 --- a/DAIRemote/SystemTray.cs +++ b/DAIRemote/SystemTray.cs @@ -40,6 +40,7 @@ public TrayIconManager(Form form) // Registers any prexisting hotkeys, otherwise initializes // an empty dictionary in preparation for hotkeys. hotkeyManager = new HotkeyManager(); + hotkeyManager.InitializeHotkeys(); InitializeTrayIcon(); DisplayConfig.NotificationRequested += OnNotificationRequested; @@ -421,6 +422,7 @@ private void ShowForm() private void OnExit(object sender, EventArgs e) { HideIcon(); + GetHotkeyManager().UnregisterHotkeys(); Application.Exit(); } diff --git a/DAIRemoteApp/app/src/main/res/layout/main_menu_header.xml b/DAIRemoteApp/app/src/main/res/layout/main_menu_header.xml index e77a4f4..c965ed5 100644 --- a/DAIRemoteApp/app/src/main/res/layout/main_menu_header.xml +++ b/DAIRemoteApp/app/src/main/res/layout/main_menu_header.xml @@ -16,20 +16,20 @@ android:id="@+id/imageView" android:layout_width="41dp" android:layout_height="40dp" - android:layout_marginStart="10dp" + android:layout_marginStart="3dp" android:layout_marginTop="44dp" android:src="@drawable/menu_header_phone" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />