Skip to content

Commit

Permalink
feat: option to remember Enabled state between reruns (#7)
Browse files Browse the repository at this point in the history
- added option to remember Enabled state between reruns
- code refactoring - a small cleanup
- bug fixed - Enabling NoSleep didn't immediately apply the state
- moved app-wide settings out of the code, to native app.config
- removed compiled binary (see releases) and outdated readme.html
  • Loading branch information
CHerSun committed Dec 28, 2024
1 parent d4ea602 commit 048a43c
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 305 deletions.
Binary file removed Compiled binary/NoSleep.exe
Binary file not shown.
25 changes: 25 additions & 0 deletions Sources/NoSleep/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="NoSleep.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="NoSleep.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
Expand All @@ -16,6 +19,28 @@
<setting name="UpgradeRequired" serializeAs="String">
<value>True</value>
</setting>
<setting name="SaveEnabledState" serializeAs="String">
<value>False</value>
</setting>
<setting name="EnabledState" serializeAs="String">
<value>True</value>
</setting>
</NoSleep.Properties.Settings>
</userSettings>
<applicationSettings>
<NoSleep.Properties.Settings>
<setting name="AppName" serializeAs="String">
<value>NoSleep</value>
</setting>
<setting name="AppStartupName" serializeAs="String">
<value>NoSleep</value>
</setting>
<setting name="AppMutexGuid" serializeAs="String">
<value>8b2caf22-dc35-4e70-88df-35933ab63f69</value>
</setting>
<setting name="RefreshIntervalMs" serializeAs="String">
<value>10000</value>
</setting>
</NoSleep.Properties.Settings>
</applicationSettings>
</configuration>
3 changes: 1 addition & 2 deletions Sources/NoSleep/NoSleep.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
Expand All @@ -53,6 +51,7 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tools.cs" />
<Compile Include="TrayIcon.cs" />
<Compile Include="WinU.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
Expand Down
8 changes: 4 additions & 4 deletions Sources/NoSleep/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ static class Program
[STAThread]
static void Main()
{
using (Mutex mutex = new Mutex(false, TrayIcon.AppGuid))
using (Mutex mutex = new Mutex(false, Properties.Settings.Default.AppMutexGuid))
{
if (!mutex.WaitOne(0, false))
{
MessageBox.Show($"{TrayIcon.AppName} instance is already running.", TrayIcon.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show($"{Properties.Settings.Default.AppName} instance is already running.", Properties.Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

Expand All @@ -29,7 +29,8 @@ static void Main()
}

/// <summary>
/// If no settings were found - it's possible the program version has changed, and there are settings from a previous version. This will upgrade the settings to the current version.
/// If no settings were found - it's possible that the program version has changed and there are settings from a previous version.
/// This function will upgrade the settings to the current version - this preserves user settings.
/// </summary>
static void InitUpgradeSettings()
{
Expand All @@ -40,6 +41,5 @@ static void InitUpgradeSettings()
Properties.Settings.Default.Save();
}
}

}
}
4 changes: 2 additions & 2 deletions Sources/NoSleep/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
62 changes: 61 additions & 1 deletion Sources/NoSleep/Properties/Settings.Designer.cs

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

18 changes: 18 additions & 0 deletions Sources/NoSleep/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,23 @@
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="SaveEnabledState" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="EnabledState" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="AppName" Type="System.String" Scope="Application">
<Value Profile="(Default)">NoSleep</Value>
</Setting>
<Setting Name="AppStartupName" Type="System.String" Scope="Application">
<Value Profile="(Default)">NoSleep</Value>
</Setting>
<Setting Name="AppMutexGuid" Type="System.String" Scope="Application">
<Value Profile="(Default)">8b2caf22-dc35-4e70-88df-35933ab63f69</Value>
</Setting>
<Setting Name="RefreshIntervalMs" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">10000</Value>
</Setting>
</Settings>
</SettingsFile>
66 changes: 66 additions & 0 deletions Sources/NoSleep/Tools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.IO;
using System.Windows.Forms;

namespace NoSleep
{
public static class Tools
{
/// <summary> Create a shortcut at given path with given link.</summary>
/// <param name="targetPath"> Where to target the shortcut, i.e. what to run on shortcut usage.</param>
/// <param name="shortcutPath"> Where to create the shortcut and how to name it.</param>
/// <exception cref="Exception"> On IO related issues, including permissions.</exception>
public static void CreateShortcut(string targetPath, string shortcutPath)
{
var shell = new IWshRuntimeLibrary.WshShell();
var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = targetPath;
shortcut.Save();
}
/// <summary> Path to the autostart shortcut. </summary>
private static readonly string autostartPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), $"{Properties.Settings.Default.AppStartupName}.lnk");

/// <summary> Check application startup state </summary>
/// <returns><see langword="true"/> if application startup is enabled.</returns>
internal static bool AutostartCheck()
=> File.Exists(autostartPath);

/// <summary> Disable application startup on user login. </summary>
/// <returns><see langword="true"/> if shortcut is no longer present.</returns>
internal static bool AutostartDisable()
{
// If autostart shortcut exists - try to remove it
if (File.Exists(autostartPath))
{
try
{
File.Delete(autostartPath);
}
catch (Exception e)
{
MessageBox.Show($"Wasn't able to remove autostart shortcut from '{autostartPath}'. Error: {e.Message}",
caption: Properties.Settings.Default.AppName, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
return false;
}
}
return true;
}

/// <summary> Enable application startup on user login </summary>
/// <returns><see langword="true"/> if shortcut was created.</returns>
internal static bool AutostartEnable()
{
try
{
CreateShortcut(Application.ExecutablePath, autostartPath);
}
catch (Exception e)
{
MessageBox.Show($"Wasn't able to create autostart shortcut at '{autostartPath}'. Error: {e.Message}",
caption: Properties.Settings.Default.AppName, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
return false;
}
return true;
}
}
}
Loading

0 comments on commit 048a43c

Please sign in to comment.