Skip to content

Commit

Permalink
Configurable startup state; Solved crash on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenSilveira committed Jul 16, 2023
1 parent aca60f8 commit d39dc5d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 37 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
38 changes: 25 additions & 13 deletions WinAwake/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 23 additions & 0 deletions WinAwake/Settings.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
69 changes: 48 additions & 21 deletions WinAwake/SysTray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions WinAwake/WinAwake.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="App.cs" />
<Compile Include="Settings.cs" />
<Compile Include="SysTray.cs">
<SubType>Component</SubType>
</Compile>
Expand Down

0 comments on commit d39dc5d

Please sign in to comment.