Skip to content

Commit

Permalink
Set default keybindings per platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Oct 23, 2024
1 parent fb057b2 commit e2acd3d
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 95 deletions.
2 changes: 2 additions & 0 deletions src/PicView.Avalonia.MacOS/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,6 @@ public void EnableScreensaver()
{
// TODO: Implement EnableScreensaver
}

public string DefaultJsonKeyMap { get; init; } = MacOsKeybindings.DefaultKeybindings;
}
72 changes: 72 additions & 0 deletions src/PicView.Avalonia.MacOS/macOSKeybindings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace PicView.Avalonia.MacOS;

public static class MacOsKeybindings
{
public const string DefaultKeybindings = """
{
"D": "Next",
"Right": "Next",
"Cmd+Right": "Last",
"Cmd+D": "Last",
"Cmd+Left": "First",
"Cmd+A": "First",
"Shift+D": "NextFolder",
"Shift+Right": "NextFolder",
"Shift+A": "PrevFolder",
"Shift+Left": "PrevFolder",
"A": "Prev",
"Left": "Prev",
"W": "Up",
"Up": "Up",
"S": "Down",
"Down": "Down",
"PageUp": "ScrollUp",
"PageDown": "ScrollDown",
"Add": "ZoomIn",
"OemPlus": "ZoomIn",
"OemMinus": "ZoomOut",
"Subtract": "ZoomOut",
"Scroll": "ToggleScroll",
"Home": "ScrollToTop",
"End": "ScrollToBottom",
"G": "ToggleGallery",
"F": "Flip",
"J": "ResizeImage",
"L": "ToggleLooping",
"C": "Crop",
"E": "GalleryClick",
"Enter": "GalleryClick",
"I": "ImageInfoWindow",
"F6": "EffectsWindow",
"F1": "AboutWindow",
"F3": "OpenInExplorer",
"F4": "SettingsWindow",
"F5": "Slideshow",
"F11": "Fullscreen",
"F12": "Fullscreen",
"B": "ChangeBackground",
"Space": "Center",
"K": "KeybindingsWindow",
"D0": "Set0Star",
"D1": "Set1Star",
"D2": "Set2Star",
"D3": "Set3Star",
"D4": "Set4Star",
"D5": "Set5Star",
"Cmd+O": "Open",
"Cmd+E": "OpenWith",
"Cmd+R": "Reload",
"Cmd+S": "Save",
"Cmd+Shift+S": "SaveAs",
"F2": "Rename",
"Cmd+C": "CopyFile",
"Cmd+Alt+V": "CopyFilePath",
"Cmd+Shift+C": "CopyImage",
"Cmd+X": "CutFile",
"Cmd+V": "Paste",
"Cmd+P": "Print",
"Alt+Z": "ToggleInterface",
"Delete": "DeleteFile"
}
""";
}
4 changes: 3 additions & 1 deletion src/PicView.Avalonia.Win32/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ public async Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDi
{
return await ArchiveExtractionHelper.ExtractWithLocalSoftwareAsync(path, tempDirectory);
}


public string DefaultJsonKeyMap { get; init; } = WindowsKeybindings.DefaultKeybindings;

public void DisableScreensaver()
{
NativeMethods.DisableScreensaver();
Expand Down
72 changes: 72 additions & 0 deletions src/PicView.Avalonia.Win32/WindowsKeybindings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace PicView.Avalonia.Win32;

public static class WindowsKeybindings
{
public const string DefaultKeybindings = """
{
"D": "Next",
"Right": "Next",
"Ctrl+Right": "Last",
"Ctrl+D": "Last",
"Ctrl+Left": "First",
"Ctrl+A": "First",
"Shift+D": "NextFolder",
"Shift+Right": "NextFolder",
"Shift+A": "PrevFolder",
"Shift+Left": "PrevFolder",
"A": "Prev",
"Left": "Prev",
"W": "Up",
"Up": "Up",
"S": "Down",
"Down": "Down",
"PageUp": "ScrollUp",
"PageDown": "ScrollDown",
"Add": "ZoomIn",
"OemPlus": "ZoomIn",
"OemMinus": "ZoomOut",
"Subtract": "ZoomOut",
"Scroll": "ToggleScroll",
"Home": "ScrollToTop",
"End": "ScrollToBottom",
"G": "ToggleGallery",
"F": "Flip",
"J": "ResizeImage",
"L": "ToggleLooping",
"C": "Crop",
"E": "GalleryClick",
"Enter": "GalleryClick",
"I": "ImageInfoWindow",
"F6": "EffectsWindow",
"F1": "AboutWindow",
"F3": "OpenInExplorer",
"F4": "SettingsWindow",
"F5": "Slideshow",
"F11": "Fullscreen",
"F12": "Fullscreen",
"B": "ChangeBackground",
"Space": "Center",
"K": "KeybindingsWindow",
"D0": "Set0Star",
"D1": "Set1Star",
"D2": "Set2Star",
"D3": "Set3Star",
"D4": "Set4Star",
"D5": "Set5Star",
"Ctrl+O": "Open",
"Ctrl+E": "OpenWith",
"Ctrl+R": "Reload",
"Ctrl+S": "Save",
"Ctrl+Shift+S": "SaveAs",
"F2": "Rename",
"Ctrl+C": "CopyFile",
"Ctrl+Alt+V": "CopyFilePath",
"Ctrl+Shift+C": "CopyImage",
"Ctrl+X": "CutFile",
"Ctrl+V": "Paste",
"Ctrl+P": "Print",
"Alt+Z": "ToggleInterface",
"Delete": "DeleteFile"
}
""";
}
78 changes: 7 additions & 71 deletions src/PicView.Avalonia/Input/KeybindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Avalonia.Input;
using PicView.Avalonia.Interfaces;
using PicView.Avalonia.UI;
using PicView.Core.Keybindings;

Expand All @@ -14,77 +15,11 @@ internal partial class SourceGenerationContext : JsonSerializerContext;
public static class KeybindingManager
{
// TODO move to an interface, use this as default for Windows and make a macOS default
private const string DefaultKeybindings = """
{
"D": "Next",
"Right": "Next",
"Ctrl+Right": "Last",
"Ctrl+D": "Last",
"Ctrl+Left": "First",
"Ctrl+A": "First",
"Shift+D": "NextFolder",
"Shift+Right": "NextFolder",
"Shift+A": "PrevFolder",
"Shift+Left": "PrevFolder",
"A": "Prev",
"Left": "Prev",
"W": "Up",
"Up": "Up",
"S": "Down",
"Down": "Down",
"PageUp": "ScrollUp",
"PageDown": "ScrollDown",
"Add": "ZoomIn",
"OemPlus": "ZoomIn",
"OemMinus": "ZoomOut",
"Subtract": "ZoomOut",
"Scroll": "ToggleScroll",
"Home": "ScrollToTop",
"End": "ScrollToBottom",
"G": "ToggleGallery",
"F": "Flip",
"J": "ResizeImage",
"L": "ToggleLooping",
"C": "Crop",
"E": "GalleryClick",
"Enter": "GalleryClick",
"I": "ImageInfoWindow",
"F6": "EffectsWindow",
"F1": "AboutWindow",
"F3": "OpenInExplorer",
"F4": "SettingsWindow",
"F5": "Slideshow",
"F11": "Fullscreen",
"F12": "Fullscreen",
"B": "ChangeBackground",
"Space": "Center",
"K": "KeybindingsWindow",
"D0": "Set0Star",
"D1": "Set1Star",
"D2": "Set2Star",
"D3": "Set3Star",
"D4": "Set4Star",
"D5": "Set5Star",
"Ctrl+O": "Open",
"Ctrl+E": "OpenWith",
"Ctrl+R": "Reload",
"Ctrl+S": "Save",
"Ctrl+Shift+S": "SaveAs",
"F2": "Rename",
"Ctrl+C": "CopyFile",
"Ctrl+Alt+V": "CopyFilePath",
"Ctrl+Shift+C": "CopyImage",
"Ctrl+X": "CutFile",
"Ctrl+V": "Paste",
"Ctrl+P": "Print",
"Alt+Z": "ToggleInterface",
"Delete": "DeleteFile"
}
""";


public static Dictionary<KeyGesture, Func<Task>>? CustomShortcuts { get; private set; }

public static async Task LoadKeybindings()
public static async Task LoadKeybindings(IPlatformSpecificService platformSpecificService)
{
try
{
Expand All @@ -93,7 +28,7 @@ public static async Task LoadKeybindings()
}
catch (Exception)
{
await SetDefaultKeybindings().ConfigureAwait(false);
await SetDefaultKeybindings(platformSpecificService).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -150,7 +85,7 @@ private static async Task Loop(Dictionary<string, string> keyValues)
}
}

internal static async Task SetDefaultKeybindings()
internal static async Task SetDefaultKeybindings(IPlatformSpecificService platformSpecificService)
{
if (CustomShortcuts is not null)
{
Expand All @@ -160,8 +95,9 @@ internal static async Task SetDefaultKeybindings()
{
CustomShortcuts = new Dictionary<KeyGesture, Func<Task>>();
}
var defaultKeybindings = platformSpecificService.DefaultJsonKeyMap;
var keyValues = JsonSerializer.Deserialize(
DefaultKeybindings, typeof(Dictionary<string, string>), SourceGenerationContext.Default)
defaultKeybindings, typeof(Dictionary<string, string>), SourceGenerationContext.Default)
as Dictionary<string, string>;

await Loop(keyValues).ConfigureAwait(false);
Expand Down
19 changes: 2 additions & 17 deletions src/PicView.Avalonia/Input/MainKeyboardShortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,10 @@ public static async Task MainWindow_KeysDownAsync(KeyEventArgs e)
{
if (func is null)
{
try
{
await KeybindingManager.SetDefaultKeybindings().ConfigureAwait(false);
if (KeybindingManager.CustomShortcuts.TryGetValue(CurrentKeys, out var retryFunc))
{
await retryFunc.Invoke().ConfigureAwait(false);
}
else
{
return;
}
}
catch (Exception)
{
// TODO: Display error to user
// TODO: Display error to user
#if DEBUG
Trace.WriteLine($"[{nameof(MainWindow_KeysDownAsync)}] error \n{e}");
Trace.WriteLine($"[{nameof(MainWindow_KeysDownAsync)}] error \n{e}");
#endif
}
return;
}
// Execute the associated action
Expand Down
2 changes: 2 additions & 0 deletions src/PicView.Avalonia/Interfaces/IPlatformSpecificService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public interface IPlatformSpecificService
bool CutFile(string path);

Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory);

string DefaultJsonKeyMap { get; }
}
9 changes: 8 additions & 1 deletion src/PicView.Avalonia/StartUp/StartUpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS

HandleThemeUpdates(vm);

Task.Run(KeybindingManager.LoadKeybindings);
if (settingsExists)
{
Task.Run(() => KeybindingManager.LoadKeybindings(vm.PlatformService));
}
else
{
Task.Run(() => KeybindingManager.SetDefaultKeybindings(vm.PlatformService));
}

SetWindowEventHandlers(window);

Expand Down
11 changes: 6 additions & 5 deletions src/PicView.Avalonia/Views/ShortcutsView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public ShortcutsView()

private async Task SetDefault()
{
await KeybindingManager.SetDefaultKeybindings().ConfigureAwait(false);
if (DataContext is not MainViewModel vm)
{
return;
}

await KeybindingManager.SetDefaultKeybindings(vm.PlatformService).ConfigureAwait(false);
await Dispatcher.UIThread.InvokeAsync(() =>
{
var topLevel = TopLevel.GetTopLevel(this);
Expand All @@ -48,10 +53,6 @@ await Dispatcher.UIThread.InvokeAsync(() =>
}
window.Close();
});
if (DataContext is not MainViewModel vm)
{
return;
}

await FunctionsHelper.KeybindingsWindow();
}
Expand Down

0 comments on commit e2acd3d

Please sign in to comment.