From d39dc5db9a5e791d71b9438e67b28af743368bb5 Mon Sep 17 00:00:00 2001 From: RubenSilveira Date: Sun, 16 Jul 2023 18:04:25 +0100 Subject: [PATCH] Configurable startup state; Solved crash on startup --- README.md | 14 ++++++-- WinAwake/App.cs | 38 ++++++++++++++-------- WinAwake/Settings.cs | 23 ++++++++++++++ WinAwake/SysTray.cs | 69 ++++++++++++++++++++++++++++------------ WinAwake/WinAwake.csproj | 1 + 5 files changed, 108 insertions(+), 37 deletions(-) create mode 100644 WinAwake/Settings.cs diff --git a/README.md b/README.md index 52be2ee..f873fa3 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ install in mind: - Contributions are always welcome! - To do: - - Configurable startup state, battery power detection, lock status detection, -timers, hotkeys, configurable action time interval, application configuration per -settings & per command line, different action modes and a simple about box! :) + - Battery power detection, lock status detection, timers, hotkeys, +configurable action time interval, application configuration per settings vs. per +command line, different action modes and a simple about box! :grinning: ## License @@ -39,6 +39,14 @@ MIT License](https://github.com/RubenSilveira/WinAwake/blob/main/LICENSE). ## Changelog +### Version 2 (2023-07-16) + +Features: +* Configurable startup state. + +Bugs: +* Registry reading on startup was causing a crash. + ### Version 1 (2023-07-16) Features: diff --git a/WinAwake/App.cs b/WinAwake/App.cs index 3cd29e0..89ebc0b 100644 --- a/WinAwake/App.cs +++ b/WinAwake/App.cs @@ -15,15 +15,15 @@ [assembly: AssemblyProduct("WinAwake")] [assembly: Guid("FE3D24DB-5C73-4AC0-AB1F-C4E76A45D5C3")] -[assembly: AssemblyVersion("1.23.7.16")] +[assembly: AssemblyVersion("2.23.7.16")] class App : ApplicationContext { private static readonly Icon MainIcon; private static readonly Icon IdleIcon; - private readonly SysTray sysTray; private readonly Worker worker; + private readonly SysTray sysTray; static App() { @@ -64,34 +64,46 @@ static int Main() public App() { - sysTray = new SysTray("Idle", IdleIcon); worker = new Worker(); - sysTray.DoubleClicked += SysTray_DoubleClicked; + sysTray = new SysTray("Idle", false, IdleIcon) + { + ActiveOnStartChecked = Settings.ActiveOnStart + }; + + sysTray.ActiveActivated += SysTray_ActiveActivated; + sysTray.ActiveOnStartActivated += SysTray_ActiveOnStartActivated; sysTray.ExitActivated += SysTray_ExitActivated; - if (0 != (int)Registry.GetValue("HKEY_CURRENT_USER\\Software\\WinAwake", "ActiveOnStart", 0)) + if (sysTray.ActiveOnStartChecked) { - SysTray_DoubleClicked(null, null); + SysTray_ActiveActivated(null, null); } } - private void SysTray_DoubleClicked(object sender, EventArgs e) + private void SysTray_ActiveActivated(object sender, EventArgs e) { - if (worker.Enabled) + worker.Enabled = false; + + if (sysTray.ActiveChecked) { - worker.Enabled = false; - sysTray.AppendedTitleText = "Idle"; - sysTray.Icon = IdleIcon; + sysTray.SetStatus("Idle", false, IdleIcon); } else { + sysTray.SetStatus("Active", true, MainIcon); + worker.Enabled = true; - sysTray.AppendedTitleText = "Active"; - sysTray.Icon = MainIcon; } } + private void SysTray_ActiveOnStartActivated(object sender, EventArgs e) + { + sysTray.ActiveOnStartChecked = !sysTray.ActiveOnStartChecked; + + Settings.ActiveOnStart = sysTray.ActiveOnStartChecked; + } + private void SysTray_ExitActivated(object sender, EventArgs e) { worker.Dispose(); diff --git a/WinAwake/Settings.cs b/WinAwake/Settings.cs new file mode 100644 index 0000000..fe14e09 --- /dev/null +++ b/WinAwake/Settings.cs @@ -0,0 +1,23 @@ +using Microsoft.Win32; + +static class Settings +{ + internal static bool ActiveOnStart + { + get + { + object o = Registry.GetValue("HKEY_CURRENT_USER\\Software\\WinAwake", "ActiveOnStart", 0); + + if (null != o) + { + return (0 != (int)o); + } + + return false; + } + set + { + Registry.SetValue("HKEY_CURRENT_USER\\Software\\WinAwake", "ActiveOnStart", value ? 1 : 0, RegistryValueKind.DWord); + } + } +} diff --git a/WinAwake/SysTray.cs b/WinAwake/SysTray.cs index df458a5..987208d 100644 --- a/WinAwake/SysTray.cs +++ b/WinAwake/SysTray.cs @@ -6,57 +6,84 @@ class SysTray : Component { + private readonly ToolStripMenuItem activeToolStripMenuItem; + private readonly ToolStripMenuItem activeOnStartToolStripMenuItem; private readonly NotifyIcon notifyIcon; + internal event EventHandler ActiveActivated; + internal event EventHandler ActiveOnStartActivated; internal event EventHandler ExitActivated; - internal event EventHandler DoubleClicked; - internal SysTray(string initialAppendedTitleText, Icon initialIcon) + internal SysTray(string status, bool activeStatus, Icon icon) { + activeToolStripMenuItem = new ToolStripMenuItem("Active", null, OnActiveActivated); + + activeOnStartToolStripMenuItem = new ToolStripMenuItem("Active on Start", null, OnActiveOnStartActivated) + { + Checked = Settings.ActiveOnStart + }; + notifyIcon = new NotifyIcon() { ContextMenuStrip = new ContextMenuStrip() { Items = { - new ToolStripMenuItem("Exit", null, new EventHandler(OnExitActivated)) + activeToolStripMenuItem, + new ToolStripSeparator(), + activeOnStartToolStripMenuItem, + new ToolStripSeparator(), + new ToolStripMenuItem("Exit", null, OnExitActivated) } }, Visible = true }; - AppendedTitleText = initialAppendedTitleText; - - notifyIcon.DoubleClick += NotifyIcon_DoubleClick; + notifyIcon.Click += OnActiveActivated; - Icon = initialIcon; + SetStatus(status, activeStatus, icon); } - internal string AppendedTitleText + internal void SetStatus(string status, bool activeStatus, Icon icon) { - set + notifyIcon.Text = Assembly.GetExecutingAssembly().GetName().Name; + if (status?.Trim().Length > 0) { - notifyIcon.Text = Assembly.GetExecutingAssembly().GetName().Name; - - if (value?.Trim().Length > 0) - { - notifyIcon.Text += "\n" + value.Trim(); - } + notifyIcon.Text += "\n" + status.Trim(); } + + activeToolStripMenuItem.Checked = activeStatus; + + notifyIcon.Icon = icon; } - internal Icon Icon + internal bool ActiveChecked { - get => notifyIcon.Icon; - set => notifyIcon.Icon = value; + get => activeToolStripMenuItem.Checked; + } + + internal bool ActiveOnStartChecked + { + get => activeOnStartToolStripMenuItem.Checked; + set => activeOnStartToolStripMenuItem.Checked = value; + } + + internal void OnActiveActivated(object sender, EventArgs e) + { + if (e is MouseEventArgs args && MouseButtons.Right == args.Button) + { + return; + } + + ActiveActivated?.Invoke(this, EventArgs.Empty); } - private void NotifyIcon_DoubleClick(object sender, EventArgs e) + internal void OnActiveOnStartActivated(object sender, EventArgs e) { - DoubleClicked?.Invoke(this, EventArgs.Empty); + ActiveOnStartActivated?.Invoke(this, EventArgs.Empty); } - private void OnExitActivated(object sender, EventArgs e) + internal void OnExitActivated(object sender, EventArgs e) { ExitActivated?.Invoke(this, EventArgs.Empty); } diff --git a/WinAwake/WinAwake.csproj b/WinAwake/WinAwake.csproj index 7bd221f..696a83a 100644 --- a/WinAwake/WinAwake.csproj +++ b/WinAwake/WinAwake.csproj @@ -44,6 +44,7 @@ + Component