diff --git a/src/UniGetUI/App.xaml.cs b/src/UniGetUI/App.xaml.cs index 8dd47ad16..cd1cd9e12 100644 --- a/src/UniGetUI/App.xaml.cs +++ b/src/UniGetUI/App.xaml.cs @@ -2,6 +2,7 @@ using System.Text.RegularExpressions; using Windows.ApplicationModel.Activation; using CommunityToolkit.WinUI.Helpers; +using H.NotifyIcon; using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -349,14 +350,9 @@ private async Task CheckForMissingDependencies() await MainWindow.HandleMissingDependencies(missing_deps); } - protected override async void OnLaunched(LaunchActivatedEventArgs args) + protected override void OnLaunched(LaunchActivatedEventArgs args) { - if (!CoreData.IsDaemon) - { - await ShowMainWindowFromLaunchAsync(); - } - - CoreData.IsDaemon = false; + MainWindow?.Activate(); } public async Task ShowMainWindowFromRedirectAsync(AppActivationArguments rawArgs) @@ -390,14 +386,6 @@ public async Task ShowMainWindowFromRedirectAsync(AppActivationArguments rawArgs MainWindow.DispatcherQueue.TryEnqueue(MainWindow.Activate); } - public async Task ShowMainWindowFromLaunchAsync() - { - while (MainWindow is null) - await Task.Delay(100); - - MainWindow.DispatcherQueue.TryEnqueue(MainWindow.Activate); - } - public async void DisposeAndQuit(int outputCode = 0) { Logger.Warn("Quitting UniGetUI"); diff --git a/src/UniGetUI/MainWindow.xaml.cs b/src/UniGetUI/MainWindow.xaml.cs index 73160cfbb..7b6b383e5 100644 --- a/src/UniGetUI/MainWindow.xaml.cs +++ b/src/UniGetUI/MainWindow.xaml.cs @@ -19,6 +19,7 @@ using UniGetUI.PackageEngine.Classes.Manager.Classes; using UniGetUI.PackageEngine.Interfaces; using Windows.ApplicationModel.DataTransfer; +using H.NotifyIcon.EfficiencyMode; using Microsoft.Windows.AppNotifications; using UniGetUI.Core.Classes; using UniGetUI.Interface.Enums; @@ -125,6 +126,27 @@ public MainWindow() DWMThreadHelper.ChangeState_XAML(false); } }; + + if (CoreData.IsDaemon) + { + try + { + TrayIcon?.ForceCreate(true); + } + catch (Exception ex) + { + TrayIcon?.ForceCreate(false); + Logger.Error("Could not create taskbar tray with efficiency mode enabled"); + Logger.Error(ex); + } + DWMThreadHelper.ChangeState_DWM(true); + DWMThreadHelper.ChangeState_XAML(true); + CoreData.IsDaemon = false; + } + else + { + Activate(); + } } private static void TransferOldSettingsFormats() @@ -206,21 +228,19 @@ public async void HandleClosingEvent(AppWindow sender, AppWindowClosingEventArgs args.Cancel = true; DWMThreadHelper.ChangeState_DWM(true); DWMThreadHelper.ChangeState_XAML(true); + try { - // this.Hide(enableEfficiencyMode: true); - AppWindow.Hide(); - MainContentFrame.Content = null; + EfficiencyModeUtilities.SetEfficiencyMode(true); } catch (Exception ex) { - // Somewhere, Sometimes, MS Window Efficiency mode just crashes - Logger.Debug("Windows efficiency mode API crashed, but this was [kinda] expected"); - Logger.Debug(ex); - // this.Hide(enableEfficiencyMode: false); - AppWindow.Hide(); - MainContentFrame.Content = null; + Logger.Error("Could not disable efficiency mode"); + Logger.Error(ex); } + + MainContentFrame.Content = null; + AppWindow.Hide(); } else { @@ -369,6 +389,16 @@ public void ProcessCommandLineParameters() public new void Activate() { + try + { + EfficiencyModeUtilities.SetEfficiencyMode(false); + } + catch (Exception ex) + { + Logger.Error("Could not disable efficiency mode"); + Logger.Error(ex); + } + DWMThreadHelper.ChangeState_DWM(false); DWMThreadHelper.ChangeState_XAML(false);