From 9df1417e588330947b6d6d7a50449417bd08056b Mon Sep 17 00:00:00 2001 From: Daniel Chalmers Date: Mon, 15 Jul 2024 12:38:28 -0500 Subject: [PATCH] Add advanced option to start the window hidden Resolves #50 Also will hide the window while it's loading in case the host is slow between instructions. --- DesktopClock/MainWindow.xaml | 1 + DesktopClock/MainWindow.xaml.cs | 18 ++++++++++++------ DesktopClock/Properties/Settings.cs | 10 +++++++++- DesktopClock/WindowUtil.cs | 19 +++++++++++++++++++ 4 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 DesktopClock/WindowUtil.cs diff --git a/DesktopClock/MainWindow.xaml b/DesktopClock/MainWindow.xaml index c4f2e1f..45ebc90 100644 --- a/DesktopClock/MainWindow.xaml +++ b/DesktopClock/MainWindow.xaml @@ -14,6 +14,7 @@ WindowStyle="None" ResizeMode="NoResize" SizeToContent="WidthAndHeight" + Opacity="0" Topmost="{Binding Topmost, Source={x:Static p:Settings.Default}, Mode=TwoWay}" FontFamily="{Binding FontFamily, Source={x:Static p:Settings.Default}, Mode=OneWay}" MouseDown="Window_MouseDown" diff --git a/DesktopClock/MainWindow.xaml.cs b/DesktopClock/MainWindow.xaml.cs index ca3d55e..45458be 100644 --- a/DesktopClock/MainWindow.xaml.cs +++ b/DesktopClock/MainWindow.xaml.cs @@ -53,7 +53,7 @@ public MainWindow() Settings.Default.PropertyChanged += (s, e) => Dispatcher.Invoke(() => Settings_PropertyChanged(s, e)); - // Not done through binding due to what's explained in the comment in HideForNow(). + // Not done through binding due to what's explained in the comment in WindowUtil.HideFromScreen(). ShowInTaskbar = Settings.Default.ShowInTaskbar; CurrentTimeOrCountdownString = Settings.Default.LastDisplay; @@ -88,11 +88,7 @@ public void HideForNow() Settings.Default.TipsShown |= TeachingTips.HideForNow; } - // Minimize the window and update the ShowInTaskbar property to keep it hidden if needed. - // https://stackoverflow.com/a/28239057. - ShowInTaskbar = true; - WindowState = WindowState.Minimized; - ShowInTaskbar = Settings.Default.ShowInTaskbar; + this.HideFromScreen(); } /// @@ -446,6 +442,16 @@ private void Window_SourceInitialized(object sender, EventArgs e) // Now that everything's been initially rendered and laid out, we can start listening for changes to the size to keep the window right-aligned. SizeChanged += Window_SizeChanged; + + if (Settings.Default.StartHidden) + { + _trayIcon?.ShowNotification("Started hidden", "Icon is in the tray"); + this.HideFromScreen(); + } + + // Show the window now that it's finished loading. + // This was mainly done to stop the StartHidden option from flashing the window briefly. + Opacity = 1; } private void Window_ContentRendered(object sender, EventArgs e) diff --git a/DesktopClock/Properties/Settings.cs b/DesktopClock/Properties/Settings.cs index 3123f16..8f5a21d 100644 --- a/DesktopClock/Properties/Settings.cs +++ b/DesktopClock/Properties/Settings.cs @@ -151,10 +151,18 @@ private Settings() public int Height { get; set; } = 48; /// - /// Runs the app on startup. + /// Runs the app when the user logs in. /// + /// + /// A registry key is created or deleted. + /// public bool RunOnStartup { get; set; } = false; + /// + /// Starts the app in the "Hide for now" state. + /// + public bool StartHidden { get; set; } = false; + /// /// Allows moving the clock by dragging. /// diff --git a/DesktopClock/WindowUtil.cs b/DesktopClock/WindowUtil.cs new file mode 100644 index 0000000..7301ec1 --- /dev/null +++ b/DesktopClock/WindowUtil.cs @@ -0,0 +1,19 @@ +using System.Windows; + +namespace DesktopClock; + +public static class WindowUtil +{ + /// + /// Hides the window until the user opens it through the taskbar, tray, alt-tab, etc. + /// + public static void HideFromScreen(this Window window) + { + // Minimize the window and update the ShowInTaskbar property to keep it hidden if needed. + // https://stackoverflow.com/a/28239057. + var wasShownInTaskbar = window.ShowInTaskbar; + window.ShowInTaskbar = true; + window.WindowState = WindowState.Minimized; + window.ShowInTaskbar = wasShownInTaskbar; + } +} \ No newline at end of file