Skip to content

Commit a92e523

Browse files
committed
Add option to start on boot in menu of system tray icon
1 parent 5981e36 commit a92e523

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ When the Dynamic Desktop feature was announced for macOS Mojave which shifts thr
77

88
## How do I use this?
99

10-
The first time you run WinDynamicDesktop, it will automatically download the macOS Mojave wallpapers from [here](https://files.rb.gd/mojave_dynamic.zip) and extract them to your disk. I have not included the files directly in this repository for copyright reasons.
10+
The first time you run WinDynamicDesktop, it will automatically download the macOS Mojave wallpapers from [here](https://files.rb.gd/mojave_dynamic.zip) and extract them to your disk. I have not included the files directly in this repository for copyright reasons. If you want to select a different set of images, see the next section for how to do so.
1111

1212
You will also need to input your location when running the program for the first time. This location is not used for any purpose other than to determine the times of sunrise and sunset where you live.
1313

14-
After you enter your location, you can minimize the program to your system tray and it will run in the background. If you ever want to change the location, right-click on the system tray icon and click *Update Location*. The program can also be exited via the right-click menu of the system tray icon.
15-
16-
The program does not yet have an option built-in to automatically start when Windows boots. To make it do this, create a shortcut to the EXE in the following folder: `%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup`.
14+
After you enter your location, you can minimize the program to your system tray and it will run in the background. Right-clicking on the system tray icon opens a menu with options to update the location, start WinDynamicDesktop when Windows boots, or exit the program.
1715

1816
## Can I customize the images?
1917

src/FormWrapper.cs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace WinDynamicDesktop
1212
{
1313
class FormWrapper : ApplicationContext
1414
{
15+
private string registryStartupLocation = @"Software\Microsoft\Windows\CurrentVersion\Run";
16+
private bool startOnBoot;
17+
1518
private ProgressDialog downloadDialog;
1619
private InputDialog locationDialog;
1720
private NotifyIcon notifyIcon;
@@ -23,10 +26,14 @@ public FormWrapper()
2326
Application.ApplicationExit += new EventHandler(OnApplicationExit);
2427
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);
2528

26-
InitializeComponent();
27-
2829
JsonConfig.LoadConfig();
2930

31+
RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(registryStartupLocation);
32+
startOnBoot = startupKey.GetValue("WinDynamicDesktop") != null;
33+
startupKey.Close();
34+
35+
InitializeComponent();
36+
3037
if (!Directory.Exists("images"))
3138
{
3239
DownloadImages();
@@ -43,28 +50,38 @@ public FormWrapper()
4350

4451
private void InitializeComponent()
4552
{
46-
notifyIcon = new NotifyIcon();
47-
notifyIcon.Visible = true;
48-
notifyIcon.Icon = Properties.Resources.AppIcon;
49-
notifyIcon.Text = "WinDynamicDesktop";
50-
notifyIcon.BalloonTipTitle = "WinDynamicDesktop";
53+
notifyIcon = new NotifyIcon
54+
{
55+
Visible = true,
56+
Icon = Properties.Resources.AppIcon,
57+
Text = "WinDynamicDesktop",
58+
BalloonTipTitle = "WinDynamicDesktop"
59+
};
5160

5261
notifyIcon.ContextMenu = new ContextMenu(new MenuItem[]
5362
{
5463
new MenuItem("WinDynamicDesktop"),
5564
new MenuItem("-"),
56-
new MenuItem("&Update Location", locationItem_Click),
57-
new MenuItem("E&xit", exitItem_Click)
65+
new MenuItem("&Update Location", OnLocationItemClick),
66+
new MenuItem("&Start on Boot", OnStartupItemClick),
67+
new MenuItem("E&xit", OnExitItemClick)
5868
});
69+
5970
notifyIcon.ContextMenu.MenuItems[0].Enabled = false;
71+
notifyIcon.ContextMenu.MenuItems[3].Checked = startOnBoot;
6072
}
6173

62-
private void locationItem_Click(object sender, EventArgs e)
74+
private void OnLocationItemClick(object sender, EventArgs e)
6375
{
6476
UpdateLocation();
6577
}
78+
79+
private void OnStartupItemClick(object sender, EventArgs e)
80+
{
81+
ToggleStartOnBoot();
82+
}
6683

67-
private void exitItem_Click(object sender, EventArgs e)
84+
private void OnExitItemClick(object sender, EventArgs e)
6885
{
6986
notifyIcon.Visible = false;
7087
Application.Exit();
@@ -83,7 +100,7 @@ public void DownloadImages()
83100
}
84101

85102
downloadDialog = new ProgressDialog();
86-
downloadDialog.FormClosed += downloadDialog_Closed;
103+
downloadDialog.FormClosed += OnDownloadDialogClosed;
87104
downloadDialog.Show();
88105

89106
using (WebClient client = new WebClient())
@@ -94,7 +111,7 @@ public void DownloadImages()
94111
}
95112
}
96113

97-
private void downloadDialog_Closed(object sender, EventArgs e)
114+
private void OnDownloadDialogClosed(object sender, EventArgs e)
98115
{
99116
downloadDialog = null;
100117

@@ -118,13 +135,12 @@ private void downloadDialog_Closed(object sender, EventArgs e)
118135
}
119136
}
120137

121-
public void UpdateLocation()
138+
public void UpdateLocation()
122139
{
123140
if (locationDialog == null)
124141
{
125-
locationDialog = new InputDialog();
126-
locationDialog.wcsService = wcsService;
127-
locationDialog.FormClosed += locationDialog_Closed;
142+
locationDialog = new InputDialog { wcsService = wcsService };
143+
locationDialog.FormClosed += OnLocationDialogClosed;
128144
locationDialog.Show();
129145
}
130146
else
@@ -133,7 +149,7 @@ public void UpdateLocation()
133149
}
134150
}
135151

136-
private void locationDialog_Closed(object sender, EventArgs e)
152+
private void OnLocationDialogClosed(object sender, EventArgs e)
137153
{
138154
locationDialog = null;
139155

@@ -149,9 +165,30 @@ private void locationDialog_Closed(object sender, EventArgs e)
149165
notifyIcon.BalloonTipText = "The app is still running in the background. " +
150166
"You can access it at any time by right-clicking on this icon.";
151167
notifyIcon.ShowBalloonTip(10000);
168+
169+
JsonConfig.firstRun = false; // Don't show this message again
152170
}
153171
}
154172

173+
private void ToggleStartOnBoot()
174+
{
175+
RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(registryStartupLocation, true);
176+
177+
if (!startOnBoot)
178+
{
179+
string exePath = Path.Combine(Directory.GetCurrentDirectory(),
180+
Environment.GetCommandLineArgs()[0]);
181+
startupKey.SetValue("WinDynamicDesktop", exePath);
182+
}
183+
else
184+
{
185+
startupKey.DeleteValue("WinDynamicDesktop");
186+
}
187+
188+
startOnBoot = !startOnBoot;
189+
notifyIcon.ContextMenu.MenuItems[3].Checked = startOnBoot;
190+
}
191+
155192
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
156193
{
157194
if (e.Mode == PowerModes.Suspend)

src/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static void Main()
1717
{
1818
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(
1919
OnUnhandledException);
20+
Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
2021

2122
Application.EnableVisualStyles();
2223
Application.SetCompatibleTextRenderingDefault(false);

src/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.1")]
36+
//[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)