Skip to content

Commit

Permalink
Add advanced option to start the window hidden
Browse files Browse the repository at this point in the history
Resolves #50

Also will hide the window while it's loading in case the host is slow between instructions.
  • Loading branch information
danielchalmers committed Jul 15, 2024
1 parent fd639d4 commit 9df1417
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions DesktopClock/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 12 additions & 6 deletions DesktopClock/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/// <summary>
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion DesktopClock/Properties/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,18 @@ private Settings()
public int Height { get; set; } = 48;

/// <summary>
/// Runs the app on startup.
/// Runs the app when the user logs in.
/// </summary>
/// <remarks>
/// A registry key is created or deleted.
/// </remarks>
public bool RunOnStartup { get; set; } = false;

/// <summary>
/// Starts the app in the "Hide for now" state.
/// </summary>
public bool StartHidden { get; set; } = false;

/// <summary>
/// Allows moving the clock by dragging.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions DesktopClock/WindowUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Windows;

namespace DesktopClock;

public static class WindowUtil
{
/// <summary>
/// Hides the window until the user opens it through the taskbar, tray, alt-tab, etc.
/// </summary>
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;
}
}

0 comments on commit 9df1417

Please sign in to comment.