Skip to content

Commit 22a2e26

Browse files
Minimize on Launch (#17)
* Add MinimizeOnLaunch Config Value * Add Minimize On Launch Button to the settings menu * Implemented Logic to Hide the Window on Launch
1 parent 62c9462 commit 22a2e26

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

Desktop/Config/AppConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace OpenShock.Desktop.Config;
55
public sealed class AppConfig
66
{
77
public bool CloseToTray { get; set; } = true;
8+
public bool MinimizeOnLaunch { get; set; } = false;
89
public bool DiscordPreview { get; set; } = false;
910

1011
public UpdateChannel UpdateChannel { get; set; } = UpdateChannel.Release;

Desktop/MauiProgram.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ public static Microsoft.Maui.Hosting.MauiApp CreateMauiApp()
2222
{
2323
Console.Write(RuntimeInformation.RuntimeIdentifier);
2424
return CreateMauiAppInternal();
25-
} catch (Exception e)
25+
}
26+
catch (Exception e)
2627
{
2728
Console.WriteLine(e);
2829
throw;
2930
}
3031
}
31-
32+
3233
private static Microsoft.Maui.Hosting.MauiApp CreateMauiAppInternal()
3334
{
3435
var builder = Microsoft.Maui.Hosting.MauiApp.CreateBuilder();
@@ -49,14 +50,27 @@ private static Microsoft.Maui.Hosting.MauiApp CreateMauiAppInternal()
4950
windowsLifecycleBuilder.OnWindowCreated(window =>
5051
{
5152
var appWindow = WindowUtils.GetAppWindow(window);
53+
54+
// On first launch, hide the window if the user has chosen to start minimized
55+
if (_config?.App.MinimizeOnLaunch ?? false)
56+
{
57+
void HideOnFirstLaunch(object? sender, Microsoft.UI.Xaml.WindowActivatedEventArgs args)
58+
{
59+
appWindow.Hide();
60+
61+
window.Activated -= HideOnFirstLaunch;
62+
}
63+
64+
window.Activated += HideOnFirstLaunch;
65+
}
5266

5367
_pipeServerService?.OnMessageReceived.SubscribeAsync(_ =>
5468
{
5569
appWindow.ShowOnTop();
5670

5771
return Task.CompletedTask;
5872
}).AsTask().Wait();
59-
73+
6074
//When user execute the closing method, we can push a display alert. If user click Yes, close this application, if click the cancel, display alert will dismiss.
6175
appWindow.Closing += async (s, e) =>
6276
{
@@ -71,9 +85,9 @@ private static Microsoft.Maui.Hosting.MauiApp CreateMauiAppInternal()
7185
if (Application.Current == null) return;
7286

7387
var page = Application.Current.Windows[0].Page;
74-
75-
if(page == null) return;
76-
88+
89+
if (page == null) return;
90+
7791
var result = await page.DisplayAlert(
7892
"Close?",
7993
"Do you want to close OpenShock?",
@@ -96,7 +110,7 @@ private static Microsoft.Maui.Hosting.MauiApp CreateMauiAppInternal()
96110

97111
_config = app.Services.GetRequiredService<ConfigManager>().Config;
98112
_pipeServerService = app.Services.GetRequiredService<PipeServerService>();
99-
113+
100114
app.Services.StartOpenShockDesktopServices(false);
101115

102116
return app;

Desktop/Ui/Pages/Dash/Tabs/AppSettingsTab.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
<div class="d-flex gap-5" style="align-items: center">
2323

24+
<MudCheckBox Class="option-width option-checkbox-height" @bind-Value="ConfigManager.Config.App.MinimizeOnLaunch"
25+
Label="Minimize On Launch" @bind-Value:after="OnSettingsValueChange"/>
2426
<MudCheckBox Class="option-width option-checkbox-height" @bind-Value="ConfigManager.Config.App.CloseToTray"
2527
Label="Close to Tray" @bind-Value:after="OnSettingsValueChange"/>
2628
<MudCheckBox Class="option-width option-checkbox-height" @bind-Value="ConfigManager.Config.App.DiscordPreview"

0 commit comments

Comments
 (0)