Skip to content

Commit

Permalink
Separate Saveand Save as commands, instead of having `Show file s…
Browse files Browse the repository at this point in the history
…aving dialog` option in settings. Fix Ctrl + Shift modifier keys not working properly.
  • Loading branch information
Ruben2776 committed Oct 23, 2024
1 parent 4c5b53a commit bc1570a
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 62 deletions.
41 changes: 22 additions & 19 deletions src/PicView.Avalonia/FileSystem/FileSaverHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Avalonia.Media.Imaging;
using PicView.Avalonia.ImageHandling;
using PicView.Avalonia.ViewModels;
using PicView.Core.Config;
using PicView.Core.ImageDecoding;

namespace PicView.Avalonia.FileSystem;
Expand All @@ -15,28 +14,32 @@ public static async Task SaveCurrentFile(MainViewModel vm)
return;
}

if (SettingsHelper.Settings.UIProperties.ShowFileSavingDialog)
if (vm.FileInfo is null)
{
if (vm.FileInfo is null)
{
await SaveFileAsync(null, vm.FileInfo.FullName, vm);
}
else
{
await FilePicker.PickAndSaveFileAsAsync(vm.FileInfo.FullName, vm);
}

await SaveFileAsync(null, vm.FileInfo.FullName, vm);
}
else
{
if (vm.FileInfo is null)
{
await SaveFileAsync(null, vm.FileInfo.FullName, vm);
}
else
{
await SaveFileAsync(vm.FileInfo.FullName, vm.FileInfo.FullName, vm);
}
await SaveFileAsync(vm.FileInfo.FullName, vm.FileInfo.FullName, vm);
}

//TODO: Add visual design to tell the user that file was saved
}

public static async Task SaveFileAs(MainViewModel vm)
{
if (vm is null)
{
return;
}

if (vm.FileInfo is null)
{
await SaveFileAsync(null, vm.FileInfo.FullName, vm);
}
else
{
await FilePicker.PickAndSaveFileAsAsync(vm.FileInfo.FullName, vm);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/PicView.Avalonia/Input/KeybindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static class KeybindingManager
"Ctrl+E": "OpenWith",
"Ctrl+R": "Reload",
"Ctrl+S": "Save",
"Ctrl+Shift+S": "SaveAs",
"F2": "Rename",
"Ctrl+C": "CopyFile",
"Ctrl+Alt+V": "CopyFilePath",
Expand Down Expand Up @@ -114,7 +115,7 @@ public static async Task UpdateKeyBindingsFile()
var json = JsonSerializer.Serialize(
CustomShortcuts.ToDictionary(kvp => kvp.Key.ToString(),
kvp => GetFunctionNameByFunction(kvp.Value)), typeof(Dictionary<string, string>),
SourceGenerationContext.Default).Replace("\\u002B", "+"); // Fix plus sign encoded to unicode
SourceGenerationContext.Default).Replace("\\u002B", "+"); // Fix plus sign encoded to Unicode
await KeybindingFunctions.SaveKeyBindingsFile(json).ConfigureAwait(false);
}
catch (Exception exception)
Expand Down
75 changes: 68 additions & 7 deletions src/PicView.Avalonia/Input/MainKeyboardShortcuts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public static async Task MainWindow_KeysDownAsync(KeyEventArgs e)
{
return;
}

CtrlDown = e.KeyModifiers == KeyModifiers.Control;
AltDown = e.KeyModifiers is KeyModifiers.Alt or KeyModifiers.Meta;
ShiftDown = e.KeyModifiers == KeyModifiers.Shift;

switch (e.Key)
{
#if DEBUG
Expand All @@ -63,31 +60,95 @@ public static async Task MainWindow_KeysDownAsync(KeyEventArgs e)

case Key.LeftShift:
case Key.RightShift:
ShiftDown = true;
return;
case Key.LeftCtrl:
case Key.RightCtrl:
CtrlDown = true;
return;
case Key.LeftAlt:
case Key.RightAlt:
AltDown = true;
return;
case Key.LWin:
case Key.RWin:
return;
}

if (CtrlDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Control);
if (ShiftDown)
{
if (AltDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Control | KeyModifiers.Alt | KeyModifiers.Shift);
}
else
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Control | KeyModifiers.Shift);
}
}
if (AltDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Alt | KeyModifiers.Control);
}
else
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Control);
}
}
else if (AltDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Alt);
if (CtrlDown)
{
if (ShiftDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Control | KeyModifiers.Shift | KeyModifiers.Alt);
}
else
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Shift | KeyModifiers.Alt);
}
}

if (ShiftDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Shift | KeyModifiers.Alt);
}
else
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Alt);
}
}
else if (ShiftDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Shift);
if (CtrlDown)
{
if (AltDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Control | KeyModifiers.Shift | KeyModifiers.Alt);
}
else
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Control | KeyModifiers.Shift);
}
}

if (AltDown)
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Shift | KeyModifiers.Alt);
}
else
{
CurrentKeys = new KeyGesture(e.Key, KeyModifiers.Shift);
}
}
else
{
CurrentKeys = new KeyGesture(e.Key);
}

// Note Ctrl + Alt + key is sometimes not working properly

if (_x >= ushort.MaxValue - 1)
{
Expand Down
6 changes: 6 additions & 0 deletions src/PicView.Avalonia/UI/FunctionsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static Task<Func<Task>> GetFunctionByName(string functionName)
"OpenWith" => OpenWith,
"OpenInExplorer" => OpenInExplorer,
"Save" => Save,
"SaveAs" => SaveAs,
"Print" => Print,
"Reload" => Reload,

Expand Down Expand Up @@ -626,6 +627,11 @@ public static async Task Save()
await FileSaverHelper.SaveCurrentFile(Vm);
}

public static async Task SaveAs()
{
await FileSaverHelper.SaveFileAs(Vm);
}

public static async Task DeleteFile()
{
if (Vm is null)
Expand Down
15 changes: 3 additions & 12 deletions src/PicView.Avalonia/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ public bool IsFillSquareMenuChecked
public ReactiveCommand<Unit, Unit>? LastCommand { get; }
public ReactiveCommand<Unit, Unit>? OpenFileCommand { get; }
public ReactiveCommand<Unit, Unit>? SaveFileCommand { get; }
public ReactiveCommand<Unit, Unit>? SaveFileAsCommand { get; }
public ReactiveCommand<Unit, Unit>? OpenLastFileCommand { get; }
public ReactiveCommand<Unit, Unit>? PasteCommand { get; }
public ReactiveCommand<string, Unit>? CopyFileCommand { get; }
Expand Down Expand Up @@ -668,18 +669,6 @@ public bool IsStayingCentered
}
}

private bool _isFileSavingDialogShown = SettingsHelper.Settings.UIProperties.ShowFileSavingDialog;

public bool IsFileSavingDialogShown
{
get => _isFileSavingDialogShown;
set
{
this.RaiseAndSetIfChanged(ref _isFileSavingDialogShown, value);
SettingsHelper.Settings.UIProperties.ShowFileSavingDialog = value;
}
}

private bool _isOpeningInSameWindow = SettingsHelper.Settings.UIProperties.OpenInSameWindow;

public bool IsOpeningInSameWindow
Expand Down Expand Up @@ -1876,6 +1865,8 @@ public MainViewModel(IPlatformSpecificService? platformSpecificService)

SaveFileCommand = ReactiveCommand.CreateFromTask(FunctionsHelper.Save);

SaveFileAsCommand = ReactiveCommand.CreateFromTask(FunctionsHelper.SaveAs);

CopyFileCommand = ReactiveCommand.CreateFromTask<string>(CopyFileTask);

CopyFilePathCommand = ReactiveCommand.CreateFromTask<string>(CopyFilePathTask);
Expand Down
17 changes: 0 additions & 17 deletions src/PicView.Avalonia/Views/GeneralSettingsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,6 @@
Mode=OneWay}" />
</ToggleButton>

<ToggleButton
Background="Transparent"
BorderThickness="0"
Classes="altHover"
IsChecked="{CompiledBinding IsFileSavingDialogShown}"
Margin="0,0,0,10"
Width="300">
<TextBlock
Classes="txt"
Foreground="{StaticResource SecondaryTextColor}"
Margin="0"
MaxWidth="240"
Padding="0,1,5,0"
Text="{CompiledBinding ShowFileSavingDialog,
Mode=OneWay}" />
</ToggleButton>

<ToggleButton
Background="Transparent"
BorderThickness="0"
Expand Down
Loading

0 comments on commit bc1570a

Please sign in to comment.