Skip to content

Commit

Permalink
Add setting to control app behavior on game launch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bagusnl committed Nov 19, 2023
1 parent eb81648 commit 555c202
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 56 deletions.
44 changes: 22 additions & 22 deletions CollapseLauncher/Classes/Properties/InnerLauncherConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<NotificationPush>(InternalAppJSONContext.Default);
NotificationData.AppPushIgnoreMsgIds = LocalNotificationData.AppPushIgnoreMsgIds;
NotificationData.AppPushIgnoreMsgIds = LocalNotificationData.AppPushIgnoreMsgIds;
NotificationData.RegionPushIgnoreMsgIds = LocalNotificationData.RegionPushIgnoreMsgIds;
NotificationData.CurrentShowMsgIds = LocalNotificationData.CurrentShowMsgIds;
NotificationData.CurrentShowMsgIds = LocalNotificationData.CurrentShowMsgIds;
NotificationData.EliminatePushList();
}

Expand Down
61 changes: 47 additions & 14 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@
Text="{x:Bind helper:Locale.Lang._SettingsPage.AppLang_ApplyNeedRestart}"
Visibility="Collapsed" />
</StackPanel>
<StackPanel Margin="0,0,0,16">
<TextBlock
Margin="0,0,0,8"
Style="{ThemeResource SubtitleTextBlockStyle}"
Text="{x:Bind helper:Locale.Lang._SettingsPage.AppBehavior_Title}"/>
<TextBlock
Margin="0,0,0,8"
Text="{x:Bind helper:Locale.Lang._SettingsPage.AppBehavior_PostGameLaunch}"/>
<ComboBox
Margin="0,0,0,0"
x:Name="GameLaunchedBehaviorSelector"
MinWidth="256"
CornerRadius="14"
MaxDropDownHeight="200"
SelectedIndex="{x:Bind AppGameLaunchedBehaviorIndex, Mode=TwoWay}">
<ComboBoxItem Content="{x:Bind helper:Locale.Lang._SettingsPage.AppBehavior_PostGameLaunch_Minimize}"/>
<ComboBoxItem Content="{x:Bind helper:Locale.Lang._SettingsPage.AppBehavior_PostGameLaunch_ToTray}"/>
<ComboBoxItem Content="{x:Bind helper:Locale.Lang._SettingsPage.AppBehavior_PostGameLaunch_Nothing}"/>
</ComboBox>
</StackPanel>
<TextBlock
Margin="0,10,0,16"
Style="{ThemeResource SubtitleTextBlockStyle}"
Expand Down
30 changes: 30 additions & 0 deletions CollapseLauncher/XAMLs/MainApp/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,36 @@ private bool IsLowerCollapsePriorityOnGameLaunch
set => 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);

Expand Down
4 changes: 2 additions & 2 deletions Hi3Helper.Core/Classes/Shared/ClassStruct/StructsEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public struct AppIniStruct
public enum AppThemeMode
{
Default = 0,
Light = 1,
Dark = 2,
Light = 1,
Dark = 2,
}

public enum GameInstallStateEnum
Expand Down
35 changes: 18 additions & 17 deletions Hi3Helper.Core/Classes/Shared/Region/LauncherConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions Hi3Helper.Core/Lang/Locale/LangSettingsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion Hi3Helper.Core/Lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 555c202

Please sign in to comment.