Skip to content

Commit

Permalink
Add a keyboard shortcut that reloads the Home page (#324)
Browse files Browse the repository at this point in the history
* Support for a Kb shortcut that reloads the Home page

* Update en_US.json

* Fix shortcuts not working if the selector is changed to the destination game/region

* Shortcut now reloads the current page, localization changes and bump down H.Tray

* Ignore reload on the settings page

* Remove last item from history after refresh

* Rename localization variables
  • Loading branch information
gablm authored Dec 4, 2023
1 parent 0085c01 commit 72272a9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
52 changes: 51 additions & 1 deletion CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,13 @@ private void CreateKeyboardShortcutHandlers()
KeyboardHandler.KeyboardAccelerators.Add(keystroke);
}

KeyboardAccelerator keystrokeF5 = new KeyboardAccelerator()
{
Key = VirtualKey.F5
};
keystrokeF5.Invoked += RefreshPage_Invoked;
KeyboardHandler.KeyboardAccelerators.Add(keystrokeF5);

List<KeybindAction> actions = new()
{
// General
Expand All @@ -1403,7 +1410,9 @@ private void CreateKeyboardShortcutHandlers()

GoGameRepir_Invoked,
GoGameSettings_Invoked,
GoGameCaches_Invoked
GoGameCaches_Invoked,

RefreshPage_Invoked
};

foreach (KeybindAction func in actions)
Expand All @@ -1425,6 +1434,29 @@ private void CreateKeyboardShortcutHandlers()
}
}

private void RefreshPage_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
if (IsKbShortcutCannotChange || !(IsLoadRegionComplete || IsExplicitCancel))
return;

switch (PreviousTag)
{
case "launcher":
RestoreCurrentRegion();
ChangeRegionNoWarning(IsShowRegionChangeWarning ? ChangeRegionConfirmBtn : ChangeRegionConfirmBtnNoWarning, null);
return;
case "settings":
return;
default:
string Tag = PreviousTag;
PreviousTag = "Empty";
NavigateInnerSwitch(Tag);
LauncherFrame.BackStack.RemoveAt(LauncherFrame.BackStack.Count - 1);
PreviousTagString.RemoveAt(PreviousTagString.Count - 1);
return;
}
}

private void DeleteKeyboardShortcutHandlers() => KeyboardHandler.KeyboardAccelerators.Clear();

private async void ChangeTimer(int time = 500)
Expand All @@ -1438,10 +1470,27 @@ private async void ChangeTimer(int time = 500)
catch { }
}

private void RestoreCurrentRegion()
{
string GameCategory = GetAppConfigValue("GameCategory").ToString();

if (!GetConfigV2Regions(GameCategory))
GameCategory = ConfigV2GameCategory.FirstOrDefault();

int IndexCategory = ConfigV2GameCategory.IndexOf(GameCategory);
if (IndexCategory < 0) IndexCategory = 0;

int IndexRegion = GetPreviousGameRegion(GameCategory);

ComboBoxGameCategory.SelectedIndex = IndexCategory;
ComboBoxGameRegion.SelectedIndex = IndexRegion;
}

private void KeyboardGameShortcut_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
int index = (int)sender.Key; index -= index < 96 ? 49 : 97;

RestoreCurrentRegion();
if (IsKbShortcutCannotChange || !(IsLoadRegionComplete || IsExplicitCancel) || index >= ComboBoxGameCategory.Items.Count)
return;

Expand All @@ -1460,6 +1509,7 @@ private void KeyboardGameRegionShortcut_Invoked(KeyboardAccelerator sender, Keyb
{
int index = (int)sender.Key; index -= index < 96 ? 49 : 97;

RestoreCurrentRegion();
if (IsKbShortcutCannotChange || !(IsLoadRegionComplete || IsExplicitCancel) || index >= ComboBoxGameRegion.Items.Count)
return;

Expand Down
18 changes: 12 additions & 6 deletions CollapseLauncher/XAMLs/MainApp/Pages/Dialogs/KeyboardShortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ public static async Task<ContentDialogResult> Dialog_ShowKbShortcuts(UIElement C
genStack.Children.Add(GenerateShortcutBlock(keys[3], Lang._KbShortcuts.General_GoHome));
genStack.Children.Add(GenerateShortcutBlock(keys[4], Lang._KbShortcuts.General_GoSettings));
genStack.Children.Add(GenerateShortcutBlock(keys[5], Lang._KbShortcuts.General_OpenNotifTray));
genStack.Children.Add(GenerateShortcutBlock(keys[13], Lang._KbShortcuts.General_ReloadRegion, Lang._KbShortcuts.General_ReloadRegion_Desc));
genStack.Children.Add(new MenuFlyoutSeparator() { Margin = new Thickness(0, 10, 0, 8) });
pageNum++;

// Region/Game Shortcuts
StackPanel changeStack = new StackPanel() { Orientation = Orientation.Vertical, Visibility = Visibility.Collapsed };
changeStack.Children.Add(new TextBlock { Text = Lang._KbShortcuts.Switch_Title, FontSize = 16, FontWeight = FontWeights.Bold, Margin = new Thickness(0, 0, 0, 2) });
changeStack.Children.Add(new TextBlock { Text = Lang._KbShortcuts.Switch_Subtitle, FontSize = 11.5 });

string gameMod = keys[0][0];
string regionMod = keys[1][0];

Expand Down Expand Up @@ -180,9 +181,10 @@ private static Grid GenerateShortcutBlock(List<string> kbKeys, string descriptio
Orientation = Orientation.Horizontal
};

List<string> dataKeys = new List<string> { kbKeys[0], kbKeys[1], description, pageNum.ToString() };
if (enableSwapButton)

if (enableSwapButton && kbKeys.Count > 1)
{
List<string> dataKeys = new List<string> { kbKeys[0], kbKeys[1], description, pageNum.ToString() };
Button shortcutSwap = new Button()
{
Content = new TextBlock() { Text = "", FontSize = 12, Margin = new Thickness(-5, 0, -5, 0), FontFamily = Application.Current.Resources["FontAwesomeSolid"] as FontFamily },
Expand Down Expand Up @@ -555,7 +557,7 @@ private static bool ValidKeyCombination(List<string> keys)
new List<string> { "Ctrl", "1 - 3" }, // Game selection
new List<string> { "Shift", "1 - 6" }, // Region selection

new List<string> { "Ctrl", "Tab" }, // Keybind menu
new List<string> { "Ctrl", "Tab" }, // Keyboard shortcuts menu
new List<string> { "Ctrl", "H" }, // Home page
new List<string> { "Ctrl", "S" }, // Settings page
new List<string> { "Ctrl", "Q" }, // Notification panel
Expand All @@ -567,7 +569,9 @@ private static bool ValidKeyCombination(List<string> keys)

new List<string> { "Shift", "R" }, // Repair page
new List<string> { "Shift", "S" }, // Game settings page
new List<string> { "Shift", "C" } // Caches page
new List<string> { "Shift", "C" }, // Caches page

new List<string> { "Ctrl", "R" } // Reload region

};

Expand Down Expand Up @@ -602,7 +606,9 @@ public static List<List<string>> KeyList

if (resultList.Count < defaultKeyList.Count)
{
resultList.InsertRange(resultList.Count, defaultKeyList.GetRange(resultList.Count, defaultKeyList.Count - resultList.Count));
int newKeyCount = defaultKeyList.Count - resultList.Count;
resultList.InsertRange(resultList.Count, defaultKeyList.GetRange(resultList.Count, newKeyCount));
KeyList = resultList;
}

return resultList;
Expand Down
2 changes: 2 additions & 0 deletions Hi3Helper.Core/Lang/Locale/LangKeyboardShortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public sealed class LangKeyboardShortcuts
public string General_GoHome { get; set; } = LangFallback?._KbShortcuts.General_GoHome;
public string General_GoSettings { get; set; } = LangFallback?._KbShortcuts.General_GoSettings;
public string General_OpenNotifTray { get; set; } = LangFallback?._KbShortcuts.General_OpenNotifTray;
public string General_ReloadRegion { get; set; } = LangFallback?._KbShortcuts.General_ReloadRegion;
public string General_ReloadRegion_Desc { get; set; } = LangFallback?._KbShortcuts.General_ReloadRegion_Desc;

public string Switch_Title { get; set; } = LangFallback?._KbShortcuts.Switch_Title;
public string Switch_Subtitle { get; set; } = LangFallback?._KbShortcuts.Switch_Subtitle;
Expand Down
2 changes: 2 additions & 0 deletions Hi3Helper.Core/Lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,8 @@
"General_GoHome": "Go to the Home page",
"General_GoSettings": "Go to the Settings page",
"General_OpenNotifTray": "Open the Notification Tray",
"General_ReloadRegion": "Reload current page",
"General_ReloadRegion_Desc": "Pressing F5 is an alternative to this shortcut.\nPlease note that reloading the Home Page will reload the region.",

"Switch_Title": "Quick Game/Region change",
"Switch_Subtitle": "Note: The keybinds follow the selector order.",
Expand Down

0 comments on commit 72272a9

Please sign in to comment.