From a92e5232129083b43ca55e4dce35f9f2ba052d0c Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Sat, 16 Jun 2018 22:14:37 -0400 Subject: [PATCH] Add option to start on boot in menu of system tray icon --- README.md | 6 +-- src/FormWrapper.cs | 73 +++++++++++++++++++++++++--------- src/Program.cs | 1 + src/Properties/AssemblyInfo.cs | 4 +- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index f0e3deb..6d120a0 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,11 @@ When the Dynamic Desktop feature was announced for macOS Mojave which shifts thr ## How do I use this? -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. +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. 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. -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. - -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`. +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. ## Can I customize the images? diff --git a/src/FormWrapper.cs b/src/FormWrapper.cs index 9d81b12..17d88ff 100644 --- a/src/FormWrapper.cs +++ b/src/FormWrapper.cs @@ -12,6 +12,9 @@ namespace WinDynamicDesktop { class FormWrapper : ApplicationContext { + private string registryStartupLocation = @"Software\Microsoft\Windows\CurrentVersion\Run"; + private bool startOnBoot; + private ProgressDialog downloadDialog; private InputDialog locationDialog; private NotifyIcon notifyIcon; @@ -23,10 +26,14 @@ public FormWrapper() Application.ApplicationExit += new EventHandler(OnApplicationExit); SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged); - InitializeComponent(); - JsonConfig.LoadConfig(); + RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(registryStartupLocation); + startOnBoot = startupKey.GetValue("WinDynamicDesktop") != null; + startupKey.Close(); + + InitializeComponent(); + if (!Directory.Exists("images")) { DownloadImages(); @@ -43,28 +50,38 @@ public FormWrapper() private void InitializeComponent() { - notifyIcon = new NotifyIcon(); - notifyIcon.Visible = true; - notifyIcon.Icon = Properties.Resources.AppIcon; - notifyIcon.Text = "WinDynamicDesktop"; - notifyIcon.BalloonTipTitle = "WinDynamicDesktop"; + notifyIcon = new NotifyIcon + { + Visible = true, + Icon = Properties.Resources.AppIcon, + Text = "WinDynamicDesktop", + BalloonTipTitle = "WinDynamicDesktop" + }; notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { new MenuItem("WinDynamicDesktop"), new MenuItem("-"), - new MenuItem("&Update Location", locationItem_Click), - new MenuItem("E&xit", exitItem_Click) + new MenuItem("&Update Location", OnLocationItemClick), + new MenuItem("&Start on Boot", OnStartupItemClick), + new MenuItem("E&xit", OnExitItemClick) }); + notifyIcon.ContextMenu.MenuItems[0].Enabled = false; + notifyIcon.ContextMenu.MenuItems[3].Checked = startOnBoot; } - private void locationItem_Click(object sender, EventArgs e) + private void OnLocationItemClick(object sender, EventArgs e) { UpdateLocation(); } + + private void OnStartupItemClick(object sender, EventArgs e) + { + ToggleStartOnBoot(); + } - private void exitItem_Click(object sender, EventArgs e) + private void OnExitItemClick(object sender, EventArgs e) { notifyIcon.Visible = false; Application.Exit(); @@ -83,7 +100,7 @@ public void DownloadImages() } downloadDialog = new ProgressDialog(); - downloadDialog.FormClosed += downloadDialog_Closed; + downloadDialog.FormClosed += OnDownloadDialogClosed; downloadDialog.Show(); using (WebClient client = new WebClient()) @@ -94,7 +111,7 @@ public void DownloadImages() } } - private void downloadDialog_Closed(object sender, EventArgs e) + private void OnDownloadDialogClosed(object sender, EventArgs e) { downloadDialog = null; @@ -118,13 +135,12 @@ private void downloadDialog_Closed(object sender, EventArgs e) } } - public void UpdateLocation() + public void UpdateLocation() { if (locationDialog == null) { - locationDialog = new InputDialog(); - locationDialog.wcsService = wcsService; - locationDialog.FormClosed += locationDialog_Closed; + locationDialog = new InputDialog { wcsService = wcsService }; + locationDialog.FormClosed += OnLocationDialogClosed; locationDialog.Show(); } else @@ -133,7 +149,7 @@ public void UpdateLocation() } } - private void locationDialog_Closed(object sender, EventArgs e) + private void OnLocationDialogClosed(object sender, EventArgs e) { locationDialog = null; @@ -149,9 +165,30 @@ private void locationDialog_Closed(object sender, EventArgs e) notifyIcon.BalloonTipText = "The app is still running in the background. " + "You can access it at any time by right-clicking on this icon."; notifyIcon.ShowBalloonTip(10000); + + JsonConfig.firstRun = false; // Don't show this message again } } + private void ToggleStartOnBoot() + { + RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(registryStartupLocation, true); + + if (!startOnBoot) + { + string exePath = Path.Combine(Directory.GetCurrentDirectory(), + Environment.GetCommandLineArgs()[0]); + startupKey.SetValue("WinDynamicDesktop", exePath); + } + else + { + startupKey.DeleteValue("WinDynamicDesktop"); + } + + startOnBoot = !startOnBoot; + notifyIcon.ContextMenu.MenuItems[3].Checked = startOnBoot; + } + private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) { if (e.Mode == PowerModes.Suspend) diff --git a/src/Program.cs b/src/Program.cs index df96d89..8a9b21f 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -17,6 +17,7 @@ static void Main() { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( OnUnhandledException); + Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index 9fb3466..4c595ff 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -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.1")] +//[assembly: AssemblyFileVersion("1.0.0.0")]