浏览代码

Add contextmenu for the settings view

Ruben 5 月之前
父节点
当前提交
dc5151594a
共有 2 个文件被更改,包括 114 次插入58 次删除
  1. 49 0
      src/PicView.Avalonia/Views/SettingsView.axaml
  2. 65 58
      src/PicView.Avalonia/Views/SettingsView.axaml.cs

+ 49 - 0
src/PicView.Avalonia/Views/SettingsView.axaml

@@ -651,6 +651,55 @@
             </DrawingGroup>
         </DrawingImage>
     </UserControl.Resources>
+    <UserControl.ContextMenu>
+        <ContextMenu x:Name="ContextMenu">
+            <MenuItem
+                Command="{CompiledBinding ShowSettingsFileCommand}"
+                Foreground="{DynamicResource MainTextColor}"
+                Header="UserSettings.json">
+                <MenuItem.Icon>
+                    <Image
+                        Height="12"
+                        Source="{StaticResource BracesImage}"
+                        Width="12" />
+                </MenuItem.Icon>
+            </MenuItem>
+            <MenuItem
+                Command="{CompiledBinding ShowKeybindingsFileCommand}"
+                Foreground="{DynamicResource MainTextColor}"
+                Header="keybindings.json">
+                <MenuItem.Icon>
+                    <Image
+                        Height="12"
+                        Source="{StaticResource BracesImage}"
+                        Width="12" />
+                </MenuItem.Icon>
+            </MenuItem>
+            <MenuItem
+                Command="{CompiledBinding ResetSettingsCommand}"
+                Foreground="{DynamicResource MainTextColor}"
+                Header="{CompiledBinding Translation.ResetButtonText,
+                                         Mode=OneWay}">
+                <MenuItem.Icon>
+                    <Image
+                        Height="12"
+                        Source="{StaticResource ResetImage}"
+                        Width="12" />
+                </MenuItem.Icon>
+            </MenuItem>
+            <Separator />
+            <MenuItem Header="{CompiledBinding Translation.Close}" x:Name="CloseItem">
+                <MenuItem.Icon>
+                    <Path
+                        Data="{StaticResource CloseGeometry}"
+                        Fill="{DynamicResource MainTextColor}"
+                        Height="12"
+                        Stretch="Fill"
+                        Width="12" />
+                </MenuItem.Icon>
+            </MenuItem>
+        </ContextMenu>
+    </UserControl.ContextMenu>
     <Panel Background="Transparent" Focusable="True">
 
         <TabControl

+ 65 - 58
src/PicView.Avalonia/Views/SettingsView.axaml.cs

@@ -15,128 +15,135 @@ public partial class SettingsView : UserControl
     private readonly Stack<TabItem?> _backStack = new();
     private readonly Stack<TabItem?> _forwardStack = new();
     private TabItem? _currentTab;
-    
+
     public SettingsView()
     {
         InitializeComponent();
-        Loaded += delegate
-        {
-            Height = MainTabControl.MaxHeight = ScreenHelper.GetWindowMaxHeight() - SizeDefaults.TopBorderHeight;
-            if (!Settings.Theme.Dark)
-            {
-                MainTabControl.Background = Brushes.Transparent;
-            }
-            MainTabControl.SelectionChanged += TabSelectionChanged;
-            PointerPressed += OnMouseButtonDown;
-
-            if (DataContext is not MainViewModel vm)
-            {
-                return;
-            }
-            
-            vm.SettingsViewModel.GoBackCommand = ReactiveCommand.Create(
-                GoBack, 
-                vm.SettingsViewModel.WhenAnyValue(x => x.IsBackButtonEnabled)
-            );
-            
-            vm.SettingsViewModel.GoForwardCommand = ReactiveCommand.Create(
-                GoForward, 
-                vm.SettingsViewModel.WhenAnyValue(x => x.IsForwardButtonEnabled)
-            );
-        };
+        Loaded += OnLoaded;
     }
 
-    private void TabSelectionChanged(object? sender, SelectionChangedEventArgs e)
+    private MainViewModel? ViewModel => DataContext as MainViewModel;
+
+    private void OnLoaded(object? sender, EventArgs e)
     {
-        if (sender is not TabControl tabControl)
+        CloseItem.Click += (_, _) => (VisualRoot as Window)?.Close();
+        SetupUI();
+        AttachEventHandlers();
+        SetupCommands();
+    }
+
+    private void SetupUI()
+    {
+        Height = MainTabControl.MaxHeight = ScreenHelper.GetWindowMaxHeight() - SizeDefaults.TopBorderHeight;
+        if (!Settings.Theme.Dark)
         {
-            return;
+            MainTabControl.Background = Brushes.Transparent;
         }
+    }
+
+    private void AttachEventHandlers()
+    {
+        MainTabControl.SelectionChanged += OnTabSelectionChanged;
+        PointerPressed += OnPointerPressed;
+    }
 
-        if (tabControl.SelectedItem is not TabItem tabItem)
+    private void SetupCommands()
+    {
+        if (ViewModel is not { SettingsViewModel: { } svm })
         {
             return;
         }
-        
-        if (_currentTab == tabItem)
+
+        svm.GoBackCommand = ReactiveCommand.Create(GoBack, svm.WhenAnyValue(x => x.IsBackButtonEnabled));
+        svm.GoForwardCommand = ReactiveCommand.Create(GoForward, svm.WhenAnyValue(x => x.IsForwardButtonEnabled));
+    }
+
+    private void OnTabSelectionChanged(object? sender, SelectionChangedEventArgs e)
+    {
+        if (MainTabControl.SelectedItem is TabItem selectedTab && _currentTab != selectedTab)
         {
-            return;
+            HandleTabSelection(selectedTab);
         }
-
-        OnTabSelected(tabItem);
     }
 
-    public void OnTabSelected(TabItem? selectedTab)
+    private void HandleTabSelection(TabItem selectedTab)
     {
         if (_currentTab != null)
         {
             _backStack.Push(_currentTab);
-            // Clear forward stack when a new tab is selected directly
-            _forwardStack.Clear();
+            _forwardStack.Clear(); // Clear forward history
         }
-    
+
         _currentTab = selectedTab;
         SelectTab(_currentTab);
         UpdateNavigationButtons();
     }
-    
-    public void GoBack()
+
+    private void GoBack()
     {
-        if (_backStack.Count <= 0)
+        if (!_backStack.TryPop(out var previousTab))
         {
             return;
         }
 
         _forwardStack.Push(_currentTab);
-        _currentTab = _backStack.Pop();
+        _currentTab = previousTab;
         SelectTab(_currentTab);
         UpdateNavigationButtons();
     }
 
-    public void GoForward()
+    private void GoForward()
     {
-        if (_forwardStack.Count <= 0)
+        if (!_forwardStack.TryPop(out var nextTab))
         {
             return;
         }
 
         _backStack.Push(_currentTab);
-        _currentTab = _forwardStack.Pop();
+        _currentTab = nextTab;
         SelectTab(_currentTab);
         UpdateNavigationButtons();
     }
 
+    private void SelectTab(TabItem? tab)
+    {
+        MainTabControl.SelectedItem = tab;
+    }
+
     private void UpdateNavigationButtons()
     {
-        if (DataContext is not MainViewModel vm)
+        if (ViewModel?.SettingsViewModel is not { } svm)
         {
             return;
         }
-        
-        vm.SettingsViewModel.IsBackButtonEnabled = _backStack.Count > 0;
-        vm.SettingsViewModel.IsForwardButtonEnabled = _forwardStack.Count > 0;
-    }
 
-    private void SelectTab(TabItem? tab)
-    {
-        MainTabControl.SelectedItem = tab;
+        svm.IsBackButtonEnabled = _backStack.Count > 0;
+        svm.IsForwardButtonEnabled = _forwardStack.Count > 0;
     }
 
-    public void OnMouseButtonDown(object? sender, PointerPressedEventArgs e)
+    private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
     {
         if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
         {
             return;
         }
+
         var topLevel = TopLevel.GetTopLevel(desktop.MainWindow);
-        var prop = e.GetCurrentPoint(topLevel).Properties;
-        if (prop.IsXButton1Pressed)  // Back button
+        var properties = e.GetCurrentPoint(topLevel).Properties;
+
+        if (properties.IsXButton1Pressed)
         {
             GoBack();
         }
-        else if (prop.IsXButton2Pressed)  // Forward button
+
+        if (properties.IsXButton2Pressed)
         {
             GoForward();
         }
+
+        if (properties.IsRightButtonPressed)
+        {
+            ContextMenu.Open();
+        }
     }
 }