Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard Shortcut improvements #416

Merged
merged 19 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CollapseLauncher/Classes/RegionManagement/RegionManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async Task<bool> LoadRegionFromCurrentConfigV2(PresetConfigV2 preset)

// Set IsLoadRegionComplete to false
IsLoadRegionComplete = true;
ChangeTimer();
DisableKbShortcuts();

return true;
}
Expand Down Expand Up @@ -587,7 +587,7 @@ private void CancelRegionLoadingHandler(object sender, RoutedEventArgs args)
InvokeLoadingRegionPopup(false);
LoadingMessageHelper.HideActionButton();

ChangeTimer();
DisableKbShortcuts();
}

private async void WatchAndCancelIfTimeout(CancellationTokenSourceWrapper TokenSource, uint Timeout)
Expand All @@ -602,7 +602,7 @@ private async void WatchAndCancelIfTimeout(CancellationTokenSourceWrapper TokenS
if (!IsLoadRegionComplete && !TokenSource.IsDisposed)
{
TokenSource.Cancel();
ChangeTimer();
DisableKbShortcuts();
}
}

Expand Down
95 changes: 47 additions & 48 deletions CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
private bool IsLoadFrameCompleted = true;
private bool IsTitleIconForceShow;
private bool IsNotificationPanelShow;
private bool IsKbShortcutCannotChange = true;
private int CurrentGameCategory = -1;
private int CurrentGameRegion = -1;

Expand Down Expand Up @@ -1422,7 +1421,7 @@
if (GetAppConfigValue("EnableShortcuts").ToBoolNullable() == null)
{
SetAndSaveConfigValue("EnableShortcuts", true);
KeyList = null;
KbShortcutList = null;

SpawnNotificationPush(
Lang._AppNotification.NotifKbShortcutTitle,
Expand All @@ -1446,15 +1445,13 @@
{
try
{
List<List<string>> keys = KeyList;

int keysIndex = 0;
if (KbShortcutList == null || KbShortcutList.Count == 0)
LoadKbShortcuts();

int numIndex = 0;
VirtualKeyModifiers keyModifier = StrToVKeyModifier(keys[keysIndex][0]);
foreach (StackPanel gameTitlePanel in ComboBoxGameCategory.Items.OfType<StackPanel>())
VirtualKeyModifiers keyModifier = KbShortcutList["GameSelection"].Modifier;

Check warning on line 1452 in CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Possible 'System.NullReferenceException'

Possible 'System.NullReferenceException'
for (; numIndex <= ConfigV2.GameCount; numIndex++)
{
string game = GetComboBoxGameRegionValue(gameTitlePanel);
KeyboardAccelerator keystroke = new KeyboardAccelerator()
{
Modifiers = keyModifier,
Expand All @@ -1465,15 +1462,15 @@

KeyboardAccelerator keystrokeNP = new KeyboardAccelerator()
{
Key = VirtualKey.NumberPad1 + numIndex++,
Key = VirtualKey.NumberPad1 + numIndex,
};
keystrokeNP.Invoked += KeyboardGameShortcut_Invoked;
KeyboardHandler.KeyboardAccelerators.Add(keystrokeNP);
}

numIndex = 0;
keyModifier = StrToVKeyModifier(keys[++keysIndex][0]);
while (numIndex < 6)
keyModifier = KbShortcutList["RegionSelection"].Modifier;
while (numIndex < ConfigV2.MaxRegionCount)
{
KeyboardAccelerator keystroke = new KeyboardAccelerator()
{
Expand All @@ -1491,49 +1488,49 @@
keystrokeF5.Invoked += RefreshPage_Invoked;
KeyboardHandler.KeyboardAccelerators.Add(keystrokeF5);

List<KeybindAction> actions = new()
Dictionary<string, KeybindAction> actions = new()
{
// General
ShowKeybinds_Invoked,
GoHome_Invoked,
GoSettings_Invoked,
OpenNotify_Invoked,
{ "KbShortcutsMenu", ShowKeybinds_Invoked },
{ "HomePage", GoHome_Invoked },
{ "SettingsPage", GoSettings_Invoked },
{ "NotificationPanel", OpenNotify_Invoked },

// Game Related
OpenScreenshot_Invoked,
OpenGameFolder_Invoked,
OpenGameCacheFolder_Invoked,
ForceCloseGame_Invoked,
{ "ScreenshotFolder", OpenScreenshot_Invoked},
{ "GameFolder", OpenGameFolder_Invoked },
{ "CacheFolder", OpenGameCacheFolder_Invoked },
{ "ForceCloseGame", ForceCloseGame_Invoked },

GoGameRepir_Invoked,
GoGameSettings_Invoked,
GoGameCaches_Invoked,
{ "RepairPage", GoGameRepir_Invoked },
{ "GameSettingsPage", GoGameSettings_Invoked },
{ "CachesPage", GoGameCaches_Invoked },

RefreshPage_Invoked
{ "ReloadRegion", RefreshPage_Invoked }
};

foreach (KeybindAction func in actions)
foreach (var func in actions)
{
KeyboardAccelerator kbfunc = new KeyboardAccelerator()
{
Modifiers = StrToVKeyModifier(keys[++keysIndex][0]),
Key = StrToVKey(keys[keysIndex][1])
Modifiers = KbShortcutList[func.Key].Modifier,
Key = KbShortcutList[func.Key].Key
};
kbfunc.Invoked += func;
kbfunc.Invoked += func.Value;
KeyboardHandler.KeyboardAccelerators.Add(kbfunc);
}
}
catch (Exception error)
{
LogWriteLine(error.ToString());
KeyList = null;
KbShortcutList = null;
CreateKeyboardShortcutHandlers();
}
}

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

switch (PreviousTag)
Expand All @@ -1558,13 +1555,13 @@

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

private async void ChangeTimer(int time = 500)
private async void DisableKbShortcuts(int time = 500)
{
try
{
IsKbShortcutCannotChange = true;
CannotUseKbShortcuts = true;
await Task.Delay(time);
IsKbShortcutCannotChange = false;
CannotUseKbShortcuts = false;
}
catch { }
}
Expand All @@ -1590,7 +1587,7 @@
int index = (int)sender.Key; index -= index < 96 ? 49 : 97;

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

if (ComboBoxGameCategory.SelectedValue != ComboBoxGameCategory.Items[index])
Expand All @@ -1600,7 +1597,7 @@
ChangeRegionNoWarning(ChangeRegionConfirmBtn, null);
ChangeRegionConfirmBtn.IsEnabled = false;
ChangeRegionConfirmBtnNoWarning.IsEnabled = false;
IsKbShortcutCannotChange = true;
CannotUseKbShortcuts = true;
}
}

Expand All @@ -1609,7 +1606,7 @@
int index = (int)sender.Key; index -= index < 96 ? 49 : 97;

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

if (ComboBoxGameRegion.SelectedValue != ComboBoxGameRegion.Items[index])
Expand All @@ -1618,38 +1615,40 @@
ChangeRegionNoWarning(ChangeRegionConfirmBtn, null);
ChangeRegionConfirmBtn.IsEnabled = false;
ChangeRegionConfirmBtnNoWarning.IsEnabled = false;
IsKbShortcutCannotChange = true;
CannotUseKbShortcuts = true;
}
}

private async void ShowKeybinds_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
if (CannotUseKbShortcuts)
return;
await Dialogs.KeyboardShortcuts.Dialog_ShowKbShortcuts(this);
}

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

if (NavigationViewControl.SelectedItem == NavigationViewControl.MenuItems[0])
return;

ChangeTimer();
DisableKbShortcuts();
NavigationViewControl.SelectedItem = NavigationViewControl.MenuItems[0];
NavigateInnerSwitch("launcher");

}

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

if (NavigationViewControl.SelectedItem == NavigationViewControl.SettingsItem)
return;

ChangeTimer();
DisableKbShortcuts();
NavigationViewControl.SelectedItem = NavigationViewControl.SettingsItem;
Navigate(typeof(SettingsPage), "settings");
}
Expand Down Expand Up @@ -1742,38 +1741,38 @@
}
private void GoGameRepir_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
if (!(IsLoadRegionComplete || IsExplicitCancel) || IsKbShortcutCannotChange)
if (!(IsLoadRegionComplete || IsExplicitCancel) || CannotUseKbShortcuts)
return;

if (NavigationViewControl.SelectedItem == NavigationViewControl.MenuItems[2])
return;

ChangeTimer();
DisableKbShortcuts();
NavigationViewControl.SelectedItem = NavigationViewControl.MenuItems[2];
NavigateInnerSwitch("repair");
}

private void GoGameCaches_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
if (!(IsLoadRegionComplete || IsExplicitCancel) || IsKbShortcutCannotChange)
if (!(IsLoadRegionComplete || IsExplicitCancel) || CannotUseKbShortcuts)
return;
if (NavigationViewControl.SelectedItem == NavigationViewControl.MenuItems[3])
return;

ChangeTimer();
DisableKbShortcuts();
NavigationViewControl.SelectedItem = NavigationViewControl.MenuItems[3];
NavigateInnerSwitch("caches");
}

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

if (NavigationViewControl.SelectedItem == NavigationViewControl.MenuItems.Last())
return;

ChangeTimer();
DisableKbShortcuts();
NavigationViewControl.SelectedItem = NavigationViewControl.MenuItems.Last();
switch (CurrentGameProperty._GamePreset.GameType)
{
Expand Down Expand Up @@ -1867,7 +1866,7 @@

DispatcherQueue.TryEnqueue(async () => {

if (!(IsLoadRegionComplete || IsExplicitCancel) || IsKbShortcutCannotChange)
if (!(IsLoadRegionComplete || IsExplicitCancel) || CannotUseKbShortcuts)
return;

bool sameRegion = SetActivatedRegion();
Expand Down
Loading
Loading