Browse Source

PixiEditorPopup auto ownership

Krzysztof Krysiński 1 year ago
parent
commit
bd2ba537ba

+ 4 - 2
src/PixiEditor.AvaloniaUI/Styles/PixiEditorPopupTemplate.axaml

@@ -18,13 +18,15 @@
                 <DockPanel>
                     <controls:DialogTitleBar 
                         DockPanel.Dock="Top"
+                        CanMinimize="{TemplateBinding CanMinimize}"
+                        CanFullscreen="{TemplateBinding CanResize}"
                         TitleKey="{TemplateBinding Title}"/>
-                    <Grid Background="{DynamicResource ThemeBackgroundBrush1}" Focusable="True" RowDefinitions="40, *">
+                    <Grid Background="{DynamicResource ThemeBackgroundBrush1}" Focusable="True">
                         <Interaction.Behaviors>
                             <behaviours:ClearFocusOnClickBehavior/>
                         </Interaction.Behaviors>
                         
-                        <ContentPresenter Grid.Row="1" DockPanel.Dock="Bottom" Content="{TemplateBinding Content}" />
+                        <ContentPresenter DockPanel.Dock="Bottom" Content="{TemplateBinding Content}" />
                     </Grid>
                 </DockPanel>
             </ControlTemplate>

+ 2 - 1
src/PixiEditor.AvaloniaUI/ViewModels/SubViewModels/WindowViewModel.cs

@@ -4,6 +4,7 @@ using Avalonia.Input;
 using CommunityToolkit.Mvvm.Input;
 using PixiEditor.AvaloniaUI.Models.Commands;
 using PixiEditor.AvaloniaUI.ViewModels.Document;
+using PixiEditor.AvaloniaUI.Views;
 using PixiEditor.AvaloniaUI.Views.Windows;
 using PixiEditor.Views.UserControls;
 using Command = PixiEditor.AvaloniaUI.Models.Commands.Attributes.Commands.Command;
@@ -155,7 +156,7 @@ internal class WindowViewModel : SubViewModel<ViewModelMain>
     [Command.Basic("PixiEditor.Window.OpenStartupWindow", "OPEN_STARTUP_WINDOW", "OPEN_STARTUP_WINDOW")]
     public void OpenHelloThereWindow()
     {
-        new HelloTherePopup(Owner.FileSubViewModel).Show();
+        new HelloTherePopup(Owner.FileSubViewModel).Show(MainWindow.Current);
     }
 
     [Command.Basic("PixiEditor.Window.OpenShortcutWindow", "OPEN_SHORTCUT_WINDOW", "OPEN_SHORTCUT_WINDOW", Key = Key.F1)]

+ 22 - 16
src/PixiEditor.AvaloniaUI/Views/Dialogs/DialogTitleBar.axaml

@@ -29,7 +29,7 @@
                     <Setter Property="Background" Value="Transparent"/>
                     <Setter Property="CornerRadius" Value="0"/>
                     <Setter Property="Width" Value="48"></Setter>
-                    <Setter Property="IsHitTestVisible" Value="True"></Setter>
+                    <Setter Property="IsHitTestVisible" Value="True"/>
                 </Style>
             </DockPanel.Styles>
             <Button 
@@ -39,22 +39,28 @@
                 Click="CloseWindow">
                 🗙
             </Button>
-            <Button
-                IsVisible="{Binding !$parent[Window].WindowState, Converter={converters:IsEqualConverter}, ConverterParameter={x:Static WindowState.Maximized}}"
-                DockPanel.Dock="Right"
-                ui:Translator.TooltipKey="MAXIMIZE"
-                Click="MaximizeWindow">
-                🗖
-            </Button>
-            <Button
-                IsVisible="{Binding $parent[Window].WindowState, Converter={converters:IsEqualConverter}, ConverterParameter={x:Static WindowState.Maximized}}"
-                DockPanel.Dock="Right"
-                ui:Translator.TooltipKey="RESTORE"
-                Click="RestoreWindow">
-                🗗
-            </Button>
+            <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" IsVisible="{Binding ElementName=uc, Path=CanFullscreen}">
+                <Button
+                    IsVisible="{Binding !$parent[Window].WindowState, Converter={converters:IsEqualConverter}, ConverterParameter={x:Static WindowState.Maximized}}"
+                    DockPanel.Dock="Right"
+                    ui:Translator.TooltipKey="MAXIMIZE"
+                    Name="maximizeButton"
+                    Click="MaximizeWindow">
+                    🗖
+                </Button>
+                <Button
+                    IsVisible="{Binding $parent[Window].WindowState, Converter={converters:IsEqualConverter}, ConverterParameter={x:Static WindowState.Maximized}}"
+                    DockPanel.Dock="Right"
+                    Name="restoreButton"
+                    ui:Translator.TooltipKey="RESTORE"
+                    Click="RestoreWindow">
+                    🗗
+                </Button>
+            </StackPanel>
             <Button 
-                DockPanel.Dock="Right" 
+                DockPanel.Dock="Right"
+                Name="minimizeButton"
+                IsVisible="{Binding ElementName=uc, Path=CanMinimize}"
                 ui:Translator.TooltipKey="MINIMIZE"
                 Click="MinimizeWindow">
                 🗕

+ 21 - 3
src/PixiEditor.AvaloniaUI/Views/Dialogs/DialogTitleBar.axaml.cs

@@ -10,6 +10,12 @@ namespace PixiEditor.AvaloniaUI.Views.Dialogs;
 
 internal partial class DialogTitleBar : UserControl, ICustomTranslatorElement
 {
+    public static readonly StyledProperty<bool> CanMinimizeProperty = AvaloniaProperty.Register<DialogTitleBar, bool>(
+        nameof(CanMinimize), defaultValue: true);
+
+    public static readonly StyledProperty<bool> CanFullscreenProperty = AvaloniaProperty.Register<DialogTitleBar, bool>(
+        nameof(CanFullscreen), defaultValue: true);
+
     public static readonly StyledProperty<string> TitleKeyProperty =
         AvaloniaProperty.Register<DialogTitleBar, string>(nameof(TitleKey), string.Empty);
 
@@ -30,6 +36,18 @@ internal partial class DialogTitleBar : UserControl, ICustomTranslatorElement
         get => GetValue(TitleKeyProperty);
         set => SetValue(TitleKeyProperty, value);
     }
+
+    public bool CanMinimize
+    {
+        get => GetValue(CanMinimizeProperty);
+        set => SetValue(CanMinimizeProperty, value);
+    }
+
+    public bool CanFullscreen
+    {
+        get => GetValue(CanFullscreenProperty);
+        set => SetValue(CanFullscreenProperty, value);
+    }
     
     public DialogTitleBar()
     {
@@ -59,21 +77,21 @@ internal partial class DialogTitleBar : UserControl, ICustomTranslatorElement
     
     private void MaximizeWindow(object? sender, RoutedEventArgs e)
     {
-        if (VisualRoot is not Window window)
+        if (VisualRoot is not Window window || !CanFullscreen)
             return;
         window.WindowState = WindowState.Maximized;
     }
     
     private void RestoreWindow(object? sender, RoutedEventArgs e)
     {
-        if (VisualRoot is not Window window)
+        if (VisualRoot is not Window window || !CanFullscreen)
             return;
         window.WindowState = WindowState.Normal;
     }
     
     private void MinimizeWindow(object? sender, RoutedEventArgs e)
     {
-        if (VisualRoot is not Window window)
+        if (VisualRoot is not Window window || !CanMinimize)
             return;
         window.WindowState = WindowState.Minimized;
     }

+ 6 - 3
src/PixiEditor.AvaloniaUI/Views/Dialogs/NewFilePopup.axaml

@@ -1,4 +1,5 @@
-<Window x:Class="PixiEditor.AvaloniaUI.Views.Dialogs.NewFilePopup"
+<dialogs:PixiEditorPopup
+    x:Class="PixiEditor.AvaloniaUI.Views.Dialogs.NewFilePopup"
         x:ClassModifier="internal"
         xmlns="https://github.com/avaloniaui"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -8,9 +9,11 @@
         xmlns:system="clr-namespace:System;assembly=System.Runtime"
         Width="300" Height="250"
         SizeToContent="Height"
+        CanResize="False"
+        CanMinimize="False"
         Name="popup"
         Title="CREATE_NEW_IMAGE">
-    <DockPanel Background="{DynamicResource ThemeBackgroundBrush1}" Focusable="True">
+    <DockPanel Background="{DynamicResource ThemeBackgroundBrush}" Focusable="True">
         <Button DockPanel.Dock="Bottom" Margin="0,15,0,15" HorizontalAlignment="Center"
                 IsDefault="True" ui:Translator.Key="CREATE" x:Name="createButton"
                 Classes="DarkRoundButton"
@@ -26,4 +29,4 @@
                           ChosenWidth="{Binding FileWidth, Mode=TwoWay, ElementName=popup}"
                           x:Name="sizePicker"/>
     </DockPanel>
-</Window>
+</dialogs:PixiEditorPopup>

+ 15 - 0
src/PixiEditor.AvaloniaUI/Views/Dialogs/PixiEditorPopup.cs

@@ -3,13 +3,28 @@ using Avalonia.Controls;
 using Avalonia.Controls.Primitives;
 using Avalonia.Styling;
 using CommunityToolkit.Mvvm.Input;
+using PixiEditor.AvaloniaUI.Helpers.Extensions;
 
 namespace PixiEditor.AvaloniaUI.Views.Dialogs;
 
 public partial class PixiEditorPopup : Window, IStyleable
 {
+    public static readonly StyledProperty<bool> CanMinimizeProperty = AvaloniaProperty.Register<PixiEditorPopup, bool>(
+        nameof(CanMinimize), defaultValue: true);
+
+    public bool CanMinimize
+    {
+        get => GetValue(CanMinimizeProperty);
+        set => SetValue(CanMinimizeProperty, value);
+    }
+
     Type IStyleable.StyleKey => typeof(PixiEditorPopup);
 
+    public override void Show()
+    {
+        Show(MainWindow.Current);
+    }
+
     [RelayCommand]
     public void SetResultAndCloseCommand()
     {

+ 4 - 8
src/PixiEditor.AvaloniaUI/Views/Windows/HelloTherePopup.axaml

@@ -1,4 +1,4 @@
-<Window x:Class="PixiEditor.AvaloniaUI.Views.Windows.HelloTherePopup"
+<dialogs:PixiEditorPopup x:Class="PixiEditor.AvaloniaUI.Views.Windows.HelloTherePopup"
         x:ClassModifier="internal"
         xmlns="https://github.com/avaloniaui"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -30,16 +30,12 @@
 
     <DockPanel>
         <Grid Background="{StaticResource ThemeBackgroundBrush}" x:Name="grid">
-            <Grid.RowDefinitions>
-                <RowDefinition Height="35" />
-                <RowDefinition Height="*"/>
-            </Grid.RowDefinitions>
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="*"/>
                 <ColumnDefinition Width="300"/>
             </Grid.ColumnDefinitions>
 
-            <ScrollViewer Grid.Column="0" Grid.Row="1" VerticalScrollBarVisibility="Auto" Margin="3,0">
+            <ScrollViewer Grid.Column="0"  VerticalScrollBarVisibility="Auto" Margin="3,0">
                 <Grid Grid.Row="1" Margin="0,30,0,0">
                     <Grid.RowDefinitions>
                         <RowDefinition Height="90"/>
@@ -294,7 +290,7 @@
                 </Grid>
             </ScrollViewer>
 
-            <ScrollViewer Grid.Row="1" Grid.Column="1"
+            <ScrollViewer Grid.Column="1"
                           IsVisible="{Binding !NewsPanelCollapsed}">
                 <Border Padding="5" BorderThickness="3 0 0 0" BorderBrush="{StaticResource ThemeBackgroundBrush}">
                     <Grid>
@@ -317,4 +313,4 @@
             </ScrollViewer>
         </Grid>
     </DockPanel>
-</Window>
+</dialogs:PixiEditorPopup>