Browse Source

Add rotation context menu and related commands to macOS titlebar

Ruben 3 months ago
parent
commit
5349951325

+ 48 - 0
src/PicView.Avalonia.MacOS/Views/MacOSTitlebar.axaml

@@ -15,6 +15,54 @@
     <Design.DataContext>
         <viewModels:MainViewModel />
     </Design.DataContext>
+    <UserControl.ContextMenu>
+        <ContextMenu x:Name="RotationContextMenu">
+            <MenuItem
+                Command="{CompiledBinding Tools.RotateTask}"
+                CommandParameter="0"
+                GroupName="RotationGroup"
+                Header="0°"
+                ToggleType="Radio"
+                x:Name="Rotation0Item" />
+            <MenuItem
+                Command="{CompiledBinding Tools.RotateTask}"
+                CommandParameter="90"
+                GroupName="RotationGroup"
+                Header="90°"
+                ToggleType="Radio"
+                x:Name="Rotation90Item" />
+            <MenuItem
+                Command="{CompiledBinding Tools.RotateTask}"
+                CommandParameter="180"
+                GroupName="RotationGroup"
+                Header="180°"
+                ToggleType="Radio"
+                x:Name="Rotation180Item" />
+            <MenuItem
+                Command="{CompiledBinding Tools.RotateTask}"
+                CommandParameter="270"
+                GroupName="RotationGroup"
+                Header="270°"
+                ToggleType="Radio"
+                x:Name="Rotation270Item" />
+            <Separator />
+            <MenuItem Command="{CompiledBinding Tools.FlipCommand}" Header="{CompiledBinding Translation.IsFlipped.Value}">
+                <MenuItem.Icon>
+                    <Path
+                        Data="{StaticResource FlipGeometry}"
+                        Fill="{DynamicResource MainTextColor}"
+                        Height="12"
+                        Stretch="Fill"
+                        Width="12">
+                        <Path.RenderTransform>
+                            <ScaleTransform ScaleX="{CompiledBinding PicViewer.ScaleX.Value}" />
+                        </Path.RenderTransform>
+                    </Path>
+                </MenuItem.Icon>
+            </MenuItem>
+        </ContextMenu>
+    </UserControl.ContextMenu>
+
     <DockPanel
         Background="{DynamicResource MainBackgroundColor}"
         LastChildFill="True"

+ 57 - 0
src/PicView.Avalonia.MacOS/Views/MacOSTitlebar.axaml.cs

@@ -2,8 +2,10 @@ using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Input;
 using Avalonia.Media;
+using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.WindowBehavior;
+using R3;
 
 namespace PicView.Avalonia.MacOS.Views;
 
@@ -57,9 +59,64 @@ public partial class MacOSTitlebar : UserControl
                 catch (Exception) { }
                 #endif
             }
+            
+            Observable.EveryValueChanged(this, x => x.RotationContextMenu.IsOpen, UIHelper.GetFrameProvider)
+                .Subscribe(_ =>
+                {
+                    UpdateRotation();
+                });
+
+            RotateRightButton.PointerPressed += (_, e) =>
+            {
+                OpenContextMenu(e);
+            };
+            FlipButton.PointerPressed += (_, e) =>
+            {
+                OpenContextMenu(e);
+            };
         };
         PointerPressed += (_, e) => MoveWindow(e);
     }
+    
+    
+    private void OpenContextMenu(PointerPressedEventArgs e)
+    {
+        if (!e.GetCurrentPoint(this).Properties.IsRightButtonPressed)
+        {
+            return;
+        }
+
+        // Context menu doesn't want to be opened normally
+        RotationContextMenu.Open();
+    }
+
+    private void UpdateRotation()
+    {
+        if (DataContext is not MainViewModel vm)
+        {
+            return;
+        }
+        Rotation0Item.IsChecked = false;
+        Rotation90Item.IsChecked = false;
+        Rotation180Item.IsChecked = false;
+        Rotation270Item.IsChecked = false;
+        switch (vm.GlobalSettings.RotationAngle.CurrentValue)
+        {
+            case 0:
+                Rotation0Item.IsChecked = true;
+                break;
+            case 90:
+                Rotation90Item.IsChecked = true;
+                break;
+            case 180:
+                Rotation180Item.IsChecked = true;
+                break;
+            case 270:
+                Rotation270Item.IsChecked = true;
+                break;
+                    
+        }
+    }
 
     private void MoveWindow(PointerPressedEventArgs e)
     {