From 555c202812cc0a0ede94f08c3214fcc74dfd32c1 Mon Sep 17 00:00:00 2001 From: Bagus Nur Listiyono Date: Sun, 19 Nov 2023 10:37:49 +0700 Subject: [PATCH] Add setting to control app behavior on game launch 3 options available: - Minimize (default) - Hide to tray (using H.NotifyIcon, same as Tray) - Do nothing Do note that Console will not get minimized, similar to how it is before --- .../Classes/Properties/InnerLauncherConfig.cs | 44 ++++++------- .../XAMLs/MainApp/Pages/HomePage.xaml.cs | 61 ++++++++++++++----- .../XAMLs/MainApp/Pages/SettingsPage.xaml | 20 ++++++ .../XAMLs/MainApp/Pages/SettingsPage.xaml.cs | 30 +++++++++ .../Shared/ClassStruct/StructsEnums.cs | 4 +- .../Classes/Shared/Region/LauncherConfig.cs | 35 +++++------ .../Lang/Locale/LangSettingsPage.cs | 5 ++ Hi3Helper.Core/Lang/en_US.json | 8 ++- 8 files changed, 151 insertions(+), 56 deletions(-) diff --git a/CollapseLauncher/Classes/Properties/InnerLauncherConfig.cs b/CollapseLauncher/Classes/Properties/InnerLauncherConfig.cs index 4621e48f9..92996b8d3 100644 --- a/CollapseLauncher/Classes/Properties/InnerLauncherConfig.cs +++ b/CollapseLauncher/Classes/Properties/InnerLauncherConfig.cs @@ -42,26 +42,26 @@ public enum AppMode StartOnTray } - public static AppMode m_appMode; - public static Arguments m_arguments = new Arguments(); - public static ushort[] w_windowsVersionNumbers; - public static Window m_window; + public static AppMode m_appMode; + public static Arguments m_arguments = new Arguments(); + public static ushort[] w_windowsVersionNumbers; + public static Window m_window; public static Microsoft.UI.WindowId m_windowID; - public static Rect m_windowPosSize; - public static IntPtr m_windowHandle; - public static AppWindow m_appWindow; - public static OverlappedPresenter m_presenter; - public static MainPage m_mainPage; - public static bool m_windowSupportCustomTitle = false; - public static Size m_actualMainFrameSize; - public static double m_appDPIScale; - public static string m_appCurrentFrameName; - public static NotificationPush NotificationData; - public static bool IsCustomBG = false; - public static bool IsSkippingUpdateCheck = false; - public static GameVersion AppCurrentVersion; - public static Color SystemAppTheme { get; set; } - public static AppThemeMode CurrentAppTheme; + public static Rect m_windowPosSize; + public static IntPtr m_windowHandle; + public static AppWindow m_appWindow; + public static OverlappedPresenter m_presenter; + public static MainPage m_mainPage; + public static bool m_windowSupportCustomTitle = false; + public static Size m_actualMainFrameSize; + public static double m_appDPIScale; + public static string m_appCurrentFrameName; + public static NotificationPush NotificationData; + public static bool IsCustomBG = false; + public static bool IsSkippingUpdateCheck = false; + public static GameVersion AppCurrentVersion; + public static Color SystemAppTheme { get; set; } + public static AppThemeMode CurrentAppTheme; public static bool IsAppThemeLight { get => CurrentAppTheme switch @@ -145,11 +145,11 @@ public static void LoadLocalNotificationData() new NotificationPush() .Serialize(InternalAppJSONContext.Default)); - string Data = File.ReadAllText(AppNotifIgnoreFile); + string Data = File.ReadAllText(AppNotifIgnoreFile); NotificationPush LocalNotificationData = Data.Deserialize(InternalAppJSONContext.Default); - NotificationData.AppPushIgnoreMsgIds = LocalNotificationData.AppPushIgnoreMsgIds; + NotificationData.AppPushIgnoreMsgIds = LocalNotificationData.AppPushIgnoreMsgIds; NotificationData.RegionPushIgnoreMsgIds = LocalNotificationData.RegionPushIgnoreMsgIds; - NotificationData.CurrentShowMsgIds = LocalNotificationData.CurrentShowMsgIds; + NotificationData.CurrentShowMsgIds = LocalNotificationData.CurrentShowMsgIds; NotificationData.EliminatePushList(); } diff --git a/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs b/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs index f0cb315cf..858e7d943 100644 --- a/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs +++ b/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs @@ -1189,10 +1189,24 @@ private async void StartGame(object sender, RoutedEventArgs e) WatchOutputLog = new CancellationTokenSource(); + GameRunningWatcher(); + if (GetAppConfigValue("EnableConsole").ToBool()) - { ReadOutputLog(); - GameLogWatcher(); + + switch (GetAppConfigValue("GameLaunchedBehavior").ToString()) + { + case "Minimize": + m_presenter.Minimize(); + break; + case "ToTray": + H.NotifyIcon.WindowExtensions.Hide(m_window); + break; + case "Nothing": + break; + default: + m_presenter.Minimize(); + break; } StartPlaytimeCounter(CurrentGameProperty._GameVersion.GamePreset.ConfigRegistryLocation, proc, CurrentGameProperty._GameVersion.GamePreset); @@ -1227,6 +1241,37 @@ private async void StartGame(object sender, RoutedEventArgs e) } } + // Use this method to do something when game is closed + private async void GameRunningWatcher() + { + await Task.Delay(5000); + while (CurrentGameProperty.IsGameRunning) + { + await Task.Delay(3000); + } + + // Stopping GameLogWatcher + if (GetAppConfigValue("EnableConsole").ToBool()) + WatchOutputLog.Cancel(); + + // Window manager on game closed + switch (GetAppConfigValue("GameLaunchedBehavior").ToString()) + { + case "Minimize": + m_presenter.Restore(); + break; + case "ToTray": + H.NotifyIcon.WindowExtensions.Show(m_window); + m_presenter.Restore(); + break; + case "Nothing": + break; + default: + m_presenter.Restore(); + break; + } + } + private void StopGame(PresetConfigV2 gamePreset) { try @@ -1440,7 +1485,6 @@ public async void ReadOutputLog() LogWriteLine($"{new string('=', barwidth)} GAME STARTED {new string('=', barwidth)}", LogType.Warning, true); try { - m_presenter.Minimize(); string logPath = Path.Combine(CurrentGameProperty._GameVersion.GameDirAppDataPath, CurrentGameProperty._GameVersion.GameOutputLogName); if (!Directory.Exists(Path.GetDirectoryName(logPath))) @@ -1479,17 +1523,6 @@ public async void ReadOutputLog() LogWriteLine($"{ex}", LogType.Error); } } - - private async void GameLogWatcher() - { - await Task.Delay(5000); - while (CurrentGameProperty.IsGameRunning) - { - await Task.Delay(3000); - } - - WatchOutputLog.Cancel(); - } #endregion #region Open Button Method diff --git a/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml b/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml index 44f8cf046..37ba674a2 100644 --- a/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml +++ b/CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml @@ -47,6 +47,26 @@ Text="{x:Bind helper:Locale.Lang._SettingsPage.AppLang_ApplyNeedRestart}" Visibility="Collapsed" /> + + + + + + + + + SetAndSaveConfigValue("LowerCollapsePrioOnGameLaunch", value); } + private int AppGameLaunchedBehaviorIndex + { + get => GetAppConfigValue("GameLaunchedBehavior").ToString() switch + { + "Minimize" => 0, + "ToTray" => 1, + "Nothing" => 2, + _ => 0 + }; + set + { + switch (value) + { + case 0: + SetAndSaveConfigValue("GameLaunchedBehavior", "Minimize"); + break; + case 1: + SetAndSaveConfigValue("GameLaunchedBehavior", "ToTray"); + break; + case 2: + SetAndSaveConfigValue("GameLaunchedBehavior", "Nothing"); + break; + default: + LogWriteLine("Invalid GameLaunchedBehavior selection! Reverting to default 'Minimize'", LogType.Error, true); + SetAndSaveConfigValue("GameLaunchedBehavior", "Minimize"); + break; + } + } + } + #region Keyboard Shortcuts private async void ShowKbScList_Click(Object sender, RoutedEventArgs e) => await Dialogs.KeyboardShortcuts.Dialog_ShowKbShortcuts(this); diff --git a/Hi3Helper.Core/Classes/Shared/ClassStruct/StructsEnums.cs b/Hi3Helper.Core/Classes/Shared/ClassStruct/StructsEnums.cs index ccf6060a6..db12c1dae 100644 --- a/Hi3Helper.Core/Classes/Shared/ClassStruct/StructsEnums.cs +++ b/Hi3Helper.Core/Classes/Shared/ClassStruct/StructsEnums.cs @@ -13,8 +13,8 @@ public struct AppIniStruct public enum AppThemeMode { Default = 0, - Light = 1, - Dark = 2, + Light = 1, + Dark = 2, } public enum GameInstallStateEnum diff --git a/Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs b/Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs index fe6ff0505..efe7cc89e 100644 --- a/Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs +++ b/Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs @@ -82,25 +82,25 @@ public static string AppExecutablePath return Path.Combine(dirPath, execName + ".exe"); } } - public static string AppExecutableName { get => Path.GetFileName(AppExecutablePath); } - public static string AppGameImgFolder { get => Path.Combine(AppGameFolder, "_img"); } + public static string AppExecutableName { get => Path.GetFileName(AppExecutablePath); } + public static string AppGameImgFolder { get => Path.Combine(AppGameFolder, "_img"); } public static string AppGameImgCachedFolder { get => Path.Combine(AppGameImgFolder, "cached"); } - public static string AppGameLogsFolder { get => Path.Combine(AppGameFolder, "_logs"); } - public static string AppConfigFile = Path.Combine(AppDataFolder, "config.ini"); - public static string AppNotifIgnoreFile = Path.Combine(AppDataFolder, "ignore_notif_ids.json"); + public static string AppGameLogsFolder { get => Path.Combine(AppGameFolder, "_logs"); } + public static string AppConfigFile = Path.Combine(AppDataFolder, "config.ini"); + public static string AppNotifIgnoreFile = Path.Combine(AppDataFolder, "ignore_notif_ids.json"); public static string AppCurrentVersionString { get; set; } public static string GamePathOnSteam; - public const long AppDiscordApplicationID = 1138126643592970251; + public const long AppDiscordApplicationID = 1138126643592970251; public const long AppDiscordApplicationID_HI3 = 1124126288370737314; - public const long AppDiscordApplicationID_GI = 1124137436650426509; + public const long AppDiscordApplicationID_GI = 1124137436650426509; public const long AppDiscordApplicationID_HSR = 1124153902959431780; //public const long AppDiscordApplicationID_ZZZ = 1124154024879456276; - public const string AppNotifURLPrefix = "/notification_{0}.json"; - public const string AppGameConfigV2URLPrefix = "/metadata/metadatav2_{0}.json"; + public const string AppNotifURLPrefix = "/notification_{0}.json"; + public const string AppGameConfigV2URLPrefix = "/metadata/metadatav2_{0}.json"; public const string AppGameRepairIndexURLPrefix = "/metadata/repair_indexes/{0}/{1}/index"; - public const string AppGameRepoIndexURLPrefix = "/metadata/repair_indexes/{0}/repo"; + public const string AppGameRepoIndexURLPrefix = "/metadata/repair_indexes/{0}/repo"; public static long AppGameConfigLastUpdate; public static int AppCurrentThread @@ -120,13 +120,13 @@ public static int AppCurrentThread public static DiscordPresenceManager AppDiscordPresence; #endif - public static bool RequireAdditionalDataDownload; - public static bool IsThisRegionInstalled = false; - public static bool IsPreview = false; - public static bool IsAppThemeNeedRestart = false; - public static bool IsAppLangNeedRestart = false; +public static bool RequireAdditionalDataDownload; + public static bool IsThisRegionInstalled = false; + public static bool IsPreview = false; + public static bool IsAppThemeNeedRestart = false; + public static bool IsAppLangNeedRestart = false; public static bool IsChangeRegionWarningNeedRestart = false; - public static bool IsFirstInstall = false; + public static bool IsFirstInstall = false; public static bool IsConsoleEnabled { get => GetAppConfigValue("EnableConsole").ToBoolNullable() ?? false; @@ -179,7 +179,8 @@ public static bool IsShowRegionChangeWarning { "LowerCollapsePrioOnGameLaunch", false }, { "EnableHTTPRepairOverride", false }, { "ForceGIHDREnable", false }, - { "HI3IgnoreMediaPack", false } + { "HI3IgnoreMediaPack", false }, + { "GameLaunchedBehavior", "Minimize" }, // Possible Values: "Minimize", "ToTray", and "Nothing" }; public static void LoadGamePreset() diff --git a/Hi3Helper.Core/Lang/Locale/LangSettingsPage.cs b/Hi3Helper.Core/Lang/Locale/LangSettingsPage.cs index 310fc1cb7..274c4703c 100644 --- a/Hi3Helper.Core/Lang/Locale/LangSettingsPage.cs +++ b/Hi3Helper.Core/Lang/Locale/LangSettingsPage.cs @@ -81,6 +81,11 @@ public sealed class LangSettingsPage public string KbShortcuts_Title { get; set; } = LangFallback?._SettingsPage.KbShortcuts_Title; public string KbShortcuts_ShowBtn { get; set; } = LangFallback?._SettingsPage.KbShortcuts_ShowBtn; public string KbShortcuts_ResetBtn { get; set; } = LangFallback?._SettingsPage.KbShortcuts_ResetBtn; + public string AppBehavior_Title { get; set; } = LangFallback?._SettingsPage.AppBehavior_Title; + public string AppBehavior_PostGameLaunch { get; set; } = LangFallback?._SettingsPage.AppBehavior_PostGameLaunch; + public string AppBehavior_PostGameLaunch_Minimize { get; set; } = LangFallback?._SettingsPage.AppBehavior_PostGameLaunch_Minimize; + public string AppBehavior_PostGameLaunch_ToTray { get; set; } = LangFallback?._SettingsPage.AppBehavior_PostGameLaunch_ToTray; + public string AppBehavior_PostGameLaunch_Nothing { get; set; } = LangFallback?._SettingsPage.AppBehavior_PostGameLaunch_Nothing; } } #endregion diff --git a/Hi3Helper.Core/Lang/en_US.json b/Hi3Helper.Core/Lang/en_US.json index 7c2064923..b89500637 100644 --- a/Hi3Helper.Core/Lang/en_US.json +++ b/Hi3Helper.Core/Lang/en_US.json @@ -399,7 +399,13 @@ "KbShortcuts_Title": "Keyboard Shortcuts", "KbShortcuts_ShowBtn": "Show Shortcuts", - "KbShortcuts_ResetBtn": "Reset to Default" + "KbShortcuts_ResetBtn": "Reset to Default", + + "AppBehavior_Title": "App Behavior", + "AppBehavior_PostGameLaunch": "After Launching Game", + "AppBehavior_PostGameLaunch_Minimize": "Minimize", + "AppBehavior_PostGameLaunch_ToTray": "Hide to tray", + "AppBehavior_PostGameLaunch_Nothing": "Do nothing" }, "_Misc": {