From aa8431385ff05c5d824ae9b4ad225ba6e2eedf41 Mon Sep 17 00:00:00 2001 From: Fahim-zzz Date: Mon, 21 Oct 2024 20:10:03 -0400 Subject: [PATCH] Fixed build error due to renaming DisplayConfig.cs function. Cleaned up monitor sleep implementation. --- DAIRemote/DAIRemoteApplicationUI.cs | 2 +- DAIRemote/SystemTray.cs | 28 +++++++++------------- DisplayProfileManager/DisplayConfig.cs | 33 +++++++++++++------------- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/DAIRemote/DAIRemoteApplicationUI.cs b/DAIRemote/DAIRemoteApplicationUI.cs index b04aa0d..6f1ff48 100644 --- a/DAIRemote/DAIRemoteApplicationUI.cs +++ b/DAIRemote/DAIRemoteApplicationUI.cs @@ -14,7 +14,7 @@ public partial class DAIRemoteApplicationUI : Form public DAIRemoteApplicationUI() { UDPServerHost udpServer = new UDPServerHost(); - Thread udpThread = new Thread(() => udpServer.hostUDPServer()); + Thread udpThread = new Thread(() => udpServer.HostUDPServer()); udpThread.IsBackground = true; udpThread.Start(); diff --git a/DAIRemote/SystemTray.cs b/DAIRemote/SystemTray.cs index e87f4ab..1452816 100644 --- a/DAIRemote/SystemTray.cs +++ b/DAIRemote/SystemTray.cs @@ -1,8 +1,4 @@ using DisplayProfileManager; -using System; -using System.Drawing; -using System.Runtime.InteropServices; -using System.Windows.Forms; namespace DAIRemote { @@ -13,7 +9,6 @@ public class TrayIconManager private Form form; private FileSystemWatcher profileDirWatcher; private string profilesFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DAIRemote/DisplayProfiles"); - private DisplayConfig displayConfig; private Image aboutIcon; private Image DAIRemoteLogo; @@ -27,7 +22,6 @@ public class TrayIconManager public TrayIconManager(Form form) { this.form = form; - displayConfig = new DisplayConfig(); aboutIcon = Image.FromFile("Resources/About.ico"); DAIRemoteLogo = Image.FromFile("Resources/DAIRemoteLogo.ico"); @@ -40,7 +34,7 @@ public TrayIconManager(Form form) if (!Directory.Exists(profilesFolderPath)) { - Directory.CreateDirectory(profilesFolderPath); + Directory.CreateDirectory(profilesFolderPath); } InitializeTrayIcon(); @@ -71,7 +65,7 @@ private ContextMenuStrip CreateTrayMenu() { ContextMenuStrip menu = new ContextMenuStrip { - ForeColor = System.Drawing.Color.Black, + ForeColor = Color.Black, ShowImageMargin = true, Font = new Font("Segoe UI Variable", 9, FontStyle.Regular), AutoSize = true, @@ -84,7 +78,6 @@ private ContextMenuStrip CreateTrayMenu() private void OnProfilesChanged(object sender, FileSystemEventArgs e) { - if (form.InvokeRequired) { form.BeginInvoke((MethodInvoker)delegate @@ -109,14 +102,14 @@ private void PopulateTrayMenu(ContextMenuStrip menu) Enabled = false, }; - ToolStripMenuItem deleteProfilesLabel = new ToolStripMenuItem("Select Profile To Delete") + ToolStripMenuItem deleteProfilesLabel = new ToolStripMenuItem("Select Profile to Delete") { Font = new Font("Segoe UI Variable", 9, FontStyle.Regular), ForeColor = Color.Gray, Enabled = false, }; - ToolStripMenuItem saveProfilesLabel = new ToolStripMenuItem("Select Profile To Save") + ToolStripMenuItem saveProfilesLabel = new ToolStripMenuItem("Overwrite profile") { Font = new Font("Segoe UI Variable", 9, FontStyle.Regular), ForeColor = Color.Gray, @@ -141,7 +134,7 @@ private void PopulateTrayMenu(ContextMenuStrip menu) string fileName = Path.GetFileNameWithoutExtension(jsonProfile); var jsonMenuItem = new ToolStripMenuItem(fileName, monitorIcon, (sender, e) => { - DisplayProfileManager.DisplayConfig.SetDisplaySettings(jsonProfile); + DisplayConfig.SetDisplaySettings(jsonProfile); }); menu.Items.Insert(2, jsonMenuItem); @@ -163,9 +156,10 @@ private void PopulateTrayMenu(ContextMenuStrip menu) menu.Items.Add(new ToolStripSeparator()); deleteProfileMenuItem.DropDownItems.Insert(0, deleteProfilesLabel); deleteProfileMenuItem.DropDownItems.Insert(1, new ToolStripSeparator()); - saveProfileMenuItem.DropDownItems.Insert(0, saveProfilesLabel); + saveProfileMenuItem.DropDownItems.Insert(0, addNewProfile); saveProfileMenuItem.DropDownItems.Insert(1, new ToolStripSeparator()); - saveProfileMenuItem.DropDownItems.Insert(2, addNewProfile); + saveProfileMenuItem.DropDownItems.Insert(2, saveProfilesLabel); + saveProfileMenuItem.DropDownItems.Insert(3, new ToolStripSeparator()); menu.Items.Add(saveProfileMenuItem); menu.Items.Add(deleteProfileMenuItem); @@ -253,17 +247,17 @@ private void SaveNewProfile(string profilesFolderPath) private void SaveProfile(string profilePath) { - DisplayConfig.SaveDisplaySettings(profilePath); + DisplayConfig.SaveDisplaySettings(profilePath); } private void DeleteProfile(string profilePath) { - File.Delete(profilePath); + File.Delete(profilePath); } private void TurnOffMonitors(object? sender, EventArgs e) { - displayConfig.TurnOffMonitors(); + DisplayConfig.DisplayToggleSleep(true); } private void OnAboutClick(object? sender, EventArgs e) diff --git a/DisplayProfileManager/DisplayConfig.cs b/DisplayProfileManager/DisplayConfig.cs index b87aa79..3b05375 100644 --- a/DisplayProfileManager/DisplayConfig.cs +++ b/DisplayProfileManager/DisplayConfig.cs @@ -1297,24 +1297,25 @@ public static bool SaveDisplaySettings(string fileName) [DllImport("user32.dll")] private static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam); - private const int WM_SYSCOMMAND = 0x0112; - private const int SC_MONITORPOWER = 0xF170; - private const int MONITOR_OFF = 2; - private const int MONITOR_ON = -1; - private const int HWND_BROADCAST = 0xFFFF; - - public void TurnOffMonitors() + public static void DisplayToggleSleep(bool sleep = true) { - PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF); - } - - public void TurnOnMonitors() - { - Task.Run(() => + const int WM_SYSCOMMAND = 0x0112; + const int SC_MONITORPOWER = 0xF170; + const int HWND_BROADCAST = 0xFFFF; + if (sleep) { - PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON); - System.Threading.Thread.Sleep(100); - }); + // Go into sleep mode (2) + PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2); + } + else + { + // Wakeup from sleep mode (-1) + Task.Run(() => + { + PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1); + Thread.Sleep(100); + }); + } } static void Main(string[] args)