Merge pull request #370 from irihitech/dialog-close

Dialog system improvement
This commit is contained in:
Zhang Dian
2024-08-26 18:06:09 +08:00
committed by GitHub
30 changed files with 991 additions and 853 deletions

View File

@@ -0,0 +1,83 @@
<UserControl
x:Class="Ursa.Demo.Dialogs.CustomDemoDialog"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dialogs="clr-namespace:Ursa.Demo.Dialogs"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
d:DesignHeight="450"
d:DesignWidth="800"
x:DataType="dialogs:CustomDemoDialogViewModel"
mc:Ignorable="d">
<UserControl.Background>
<LinearGradientBrush StartPoint="5%, 5%" EndPoint="80%, 80%">
<GradientStop Offset="0.0" Color="{DynamicResource SemiLightBlue0Color}" />
<GradientStop Offset="0.4" Color="{DynamicResource SemiLightBlue2Color}" />
<GradientStop Offset="0.9" Color="{DynamicResource SemiLightBlue1Color}" />
</LinearGradientBrush>
</UserControl.Background>
<Grid Margin="24" RowDefinitions="Auto, *, Auto" MinWidth="400">
<TextBlock
Grid.Row="0"
Margin="8"
FontSize="16"
FontWeight="600"
Text="Add New" />
<u:Form
Grid.Row="1"
Margin="24"
HorizontalAlignment="Stretch"
LabelPosition="Top">
<u:Form.ItemsPanel>
<ItemsPanelTemplate>
<Grid ColumnDefinitions="Auto, *" RowDefinitions="Auto, *" />
</ItemsPanelTemplate>
</u:Form.ItemsPanel>
<u:FormItem Label="Country/Region">
<ComboBox
Width="120"
ItemsSource="{Binding Cities}"
SelectedItem="{Binding City}" />
</u:FormItem>
<u:FormItem
Grid.Row="0"
Grid.Column="1"
Margin="32,8,0,8"
Label="Owner">
<TextBox u:FormItem.Label="Owner" Text="{Binding Owner}" />
</u:FormItem>
<u:FormItem
Grid.Row="1"
Grid.Column="0"
Label="Target">
<ComboBox
Width="120"
ItemsSource="{Binding Cities}"
SelectedItem="{Binding Target}" />
</u:FormItem>
<u:FormItem
Grid.Row="1"
Grid.Column="1"
Margin="32,8,0,8"
Label="Department">
<TextBox Text="{Binding Department}" />
</u:FormItem>
</u:Form>
<StackPanel
Grid.Row="2"
HorizontalAlignment="Right"
Orientation="Horizontal"
Spacing="8">
<Button Command="{Binding DialogCommand}" Content="Dialog" Theme="{DynamicResource SolidButton}" />
<Button Command="{Binding OKCommand}" Content="OK" Theme="{DynamicResource SolidButton}" Classes="Tertiary"/>
<Button Command="{Binding CancelCommand}" Content="Cancel" Theme="{DynamicResource SolidButton}" Classes="Tertiary"/>
<ComboBox>
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>
</StackPanel>
</Grid>
</UserControl>

View File

@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Ursa.Demo.Dialogs;
public partial class CustomDemoDialog : UserControl
{
public CustomDemoDialog()
{
InitializeComponent();
}
}

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input; using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
@@ -8,14 +9,30 @@ using Ursa.Controls;
namespace Ursa.Demo.Dialogs; namespace Ursa.Demo.Dialogs;
public partial class DialogWithActionViewModel: ObservableObject, IDialogContext public partial class CustomDemoDialogViewModel : ObservableObject, IDialogContext
{ {
[ObservableProperty] private string _title; [ObservableProperty] private string? _city;
[ObservableProperty] private DateTime _date; [ObservableProperty] private string? _department;
[ObservableProperty] private string? _owner;
[ObservableProperty] private string? _target;
public CustomDemoDialogViewModel()
{
Cities =
[
"Shanghai", "Beijing", "Hulunbuir", "Shenzhen", "Hangzhou", "Nanjing", "Chengdu", "Wuhan", "Chongqing",
"Suzhou", "Tianjin", "Xi'an", "Qingdao", "Dalian"
];
OKCommand = new RelayCommand(OK);
CancelCommand = new RelayCommand(Cancel);
DialogCommand = new AsyncRelayCommand(ShowDialog);
}
public ObservableCollection<string> Cities { get; set; }
public void Close() public void Close()
{ {
RequestClose?.Invoke(this, false); RequestClose?.Invoke(this, null);
} }
public event EventHandler<object?>? RequestClose; public event EventHandler<object?>? RequestClose;
@@ -25,15 +42,6 @@ public partial class DialogWithActionViewModel: ObservableObject, IDialogContext
public ICommand DialogCommand { get; set; } public ICommand DialogCommand { get; set; }
public DialogWithActionViewModel()
{
OKCommand = new RelayCommand(OK);
CancelCommand = new RelayCommand(Cancel);
DialogCommand = new AsyncRelayCommand(ShowDialog);
Title = "Please select a date";
Date = DateTime.Now;
}
private void OK() private void OK()
{ {
RequestClose?.Invoke(this, true); RequestClose?.Invoke(this, true);
@@ -46,6 +54,6 @@ public partial class DialogWithActionViewModel: ObservableObject, IDialogContext
private async Task ShowDialog() private async Task ShowDialog()
{ {
await OverlayDialog.ShowCustomModal<DialogWithAction, DialogWithActionViewModel, bool>(new DialogWithActionViewModel()); await OverlayDialog.ShowCustomModal<CustomDemoDialog, CustomDemoDialogViewModel, bool>(new CustomDemoDialogViewModel());
} }
} }

View File

@@ -0,0 +1,50 @@
<UserControl
x:Class="Ursa.Demo.Dialogs.DefaultDemoDialog"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dialogs="clr-namespace:Ursa.Demo.Dialogs"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
MinWidth="400"
d:DesignHeight="450"
d:DesignWidth="800"
x:DataType="dialogs:DefaultDemoDialogViewModel"
mc:Ignorable="d">
<u:Form HorizontalAlignment="Stretch" LabelPosition="Top">
<u:Form.ItemsPanel>
<ItemsPanelTemplate>
<Grid ColumnDefinitions="Auto, *" RowDefinitions="Auto, *" />
</ItemsPanelTemplate>
</u:Form.ItemsPanel>
<u:FormItem Label="Country/Region">
<ComboBox
Width="120"
ItemsSource="{Binding Cities}"
SelectedItem="{Binding City}" />
</u:FormItem>
<u:FormItem
Grid.Row="0"
Grid.Column="1"
Margin="32,8,0,8"
Label="Owner">
<TextBox u:FormItem.Label="Owner" Text="{Binding Owner}" />
</u:FormItem>
<u:FormItem
Grid.Row="1"
Grid.Column="0"
Label="Target">
<ComboBox
Width="120"
ItemsSource="{Binding Cities}"
SelectedItem="{Binding Target}" />
</u:FormItem>
<u:FormItem
Grid.Row="1"
Grid.Column="1"
Margin="32,8,0,8"
Label="Department">
<TextBox Text="{Binding Department}" />
</u:FormItem>
</u:Form>
</UserControl>

View File

@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Ursa.Demo.Dialogs;
public partial class DefaultDemoDialog : UserControl
{
public DefaultDemoDialog()
{
InitializeComponent();
}
}

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using Irihi.Avalonia.Shared.Contracts;
namespace Ursa.Demo.Dialogs;
public partial class DefaultDemoDialogViewModel: ObservableObject
{
public ObservableCollection<string> Cities { get; set; }
[ObservableProperty] private string? _owner;
[ObservableProperty] private string? _department;
[ObservableProperty] private string? _target;
[ObservableProperty] private string? _city;
public DefaultDemoDialogViewModel()
{
Cities =
[
"Shanghai", "Beijing", "Hulunbuir", "Shenzhen", "Hangzhou", "Nanjing", "Chengdu", "Wuhan", "Chongqing",
"Suzhou", "Tianjin", "Xi'an", "Qingdao", "Dalian"
];
}
}

View File

@@ -1,45 +0,0 @@
<UserControl
x:Class="Ursa.Demo.Dialogs.DialogWithAction"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Ursa.Demo.Dialogs"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
d:DesignHeight="450"
d:DesignWidth="800"
x:CompileBindings="True"
x:DataType="local:DialogWithActionViewModel"
Background="{DynamicResource SemiYellow1}"
mc:Ignorable="d">
<Grid Margin="24" RowDefinitions="Auto, *, Auto">
<TextBlock
Grid.Row="0"
Margin="8"
FontSize="16"
FontWeight="600"
Text="{Binding Title}" />
<Calendar
Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Top"
SelectedDate="{Binding Date}" />
<StackPanel
Grid.Row="2"
HorizontalAlignment="Right"
Orientation="Horizontal"
Spacing="8">
<Rectangle Width="10" Height="10" Fill="Red" u:DialogControlBase.CanClose="True"></Rectangle>
<Button Command="{Binding DialogCommand}" Content="Dialog" />
<Button Command="{Binding OKCommand}" Content="OK" />
<Button Command="{Binding CancelCommand}" Content="Cancel" />
<ComboBox>
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>
</StackPanel>
</Grid>
</UserControl>

View File

@@ -1,11 +0,0 @@
using Avalonia.Controls;
namespace Ursa.Demo.Dialogs;
public partial class DialogWithAction : UserControl
{
public DialogWithAction()
{
InitializeComponent();
}
}

View File

@@ -1,14 +0,0 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
xmlns:local="clr-namespace:Ursa.Demo.Dialogs"
x:DataType="local:PlainDialogViewModel"
x:CompileBindings="True"
x:Class="Ursa.Demo.Dialogs.PlainDialog">
<StackPanel>
<Calendar SelectedDate="{Binding Date}" ></Calendar>
<TextBlock TextWrapping="Wrap" Text="{Binding Text}"></TextBlock>
</StackPanel>
</UserControl>

View File

@@ -1,11 +0,0 @@
using Avalonia.Controls;
namespace Ursa.Demo.Dialogs;
public partial class PlainDialog : UserControl
{
public PlainDialog()
{
InitializeComponent();
}
}

View File

@@ -1,27 +0,0 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel;
namespace Ursa.Demo.Dialogs;
public class PlainDialogViewModel: ObservableObject
{
private DateTime? _date;
public DateTime? Date
{
get => _date;
set => SetProperty(ref _date, value);
}
private string? _text;
public string? Text
{
get => _text;
set => SetProperty(ref _text, value);
}
public PlainDialogViewModel()
{
Text = "I am PlainDialogViewModel!";
}
}

View File

@@ -6,115 +6,159 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa" xmlns:u="https://irihi.tech/ursa"
xmlns:vm="using:Ursa.Demo.ViewModels" xmlns:vm="using:Ursa.Demo.ViewModels"
d:DesignHeight="450" d:DesignHeight="1200"
d:DesignWidth="800" d:DesignWidth="800"
x:CompileBindings="True" x:CompileBindings="True"
x:DataType="vm:DialogDemoViewModel" x:DataType="vm:DialogDemoViewModel"
mc:Ignorable="d"> mc:Ignorable="d">
<Grid ColumnDefinitions="Auto, *"> <Grid ColumnDefinitions="Auto, *">
<TabControl Grid.Column="0" Width="300"> <TabControl Grid.Column="0" TabStripPlacement="Left">
<TabItem Header="Default"> <TabItem Header="Default Window Dialog">
<StackPanel> <u:Form
<ToggleSwitch Width="300"
Name="overlay" Margin="16,0"
Content="Window/Overlay" DataContext="{Binding DefaultWindowDialogDemoViewModel}"
IsChecked="{Binding IsWindow}" LabelPosition="Top">
OffContent="Overlay" <TextBox
OnContent="Window" /> HorizontalAlignment="Stretch"
<ToggleSwitch u:FormItem.Label="Title"
Content="Global/Local" Text="{Binding Title}" />
IsChecked="{Binding IsGlobal}" <u:EnumSelector
IsVisible="{Binding !#overlay.IsChecked}" HorizontalAlignment="Stretch"
OffContent="Local" u:FormItem.Label="Startup Location"
OnContent="Global" /> EnumType="WindowStartupLocation"
<ToggleSwitch Value="{Binding Location}" />
Name="defaultModal" <u:FormItem Label="PixelPosition">
Content="Modal/Regular" <UniformGrid Columns="2" Rows="1">
IsChecked="{Binding IsModal}" <u:NumericIntUpDown InnerLeftContent="X" Value="{Binding X}" />
IsVisible="{Binding !#overlay.IsChecked}" <u:NumericIntUpDown InnerLeftContent="Y" Value="{Binding Y}" />
OffContent="Regular" </UniformGrid>
OnContent="Modal" /> </u:FormItem>
<ToggleSwitch <u:EnumSelector
Content="HideInTaskBar/ShowInTaskBar" HorizontalAlignment="Stretch"
IsChecked="{Binding ShowInTaskBar}" u:FormItem.Label="Dialog Mode"
IsVisible="{Binding #overlay.IsChecked}" EnumType="u:DialogMode"
OffContent="HideInTaskBar" Value="{Binding Mode}" />
OnContent="ShowInTaskBar" /> <u:EnumSelector
<ToggleSwitch HorizontalAlignment="Stretch"
Content="ClickOnMaskToClose" u:FormItem.Label="Dialog Buttons"
IsChecked="{Binding CanLightDismiss}" EnumType="u:DialogButton"
OffContent="No" Value="{Binding Button}" />
OnContent="Yes" /> <CheckBox u:FormItem.Label="Show In Taskbar" IsChecked="{Binding ShowInTaskBar}" />
<ToggleSwitch <CheckBox
Content="FullScreen" u:FormItem.Label="Is Close Button Visible"
IsChecked="{Binding FullScreen}" IsChecked="{Binding IsCloseButtonVisible}"
OffContent="No" IsThreeState="True" />
OnContent="Yes" /> <Button
<StackPanel Orientation="Horizontal"> HorizontalAlignment="Left"
<TextBlock VerticalAlignment="Center" Text="Buttons" /> u:FormItem.NoLabel="True"
<u:EnumSelector EnumType="{x:Type u:DialogButton}" Value="{Binding SelectedButton}" /> Command="{Binding ShowDialogCommand}"
</StackPanel> Content="Show" />
<StackPanel Orientation="Horizontal"> </u:Form>
<TextBlock VerticalAlignment="Center" Text="Mode" />
<u:EnumSelector EnumType="{x:Type u:DialogMode}" Value="{Binding SelectedMode}" />
</StackPanel>
<Button Command="{Binding ShowDialogCommand}" Content="Show Dialog" />
<TextBlock>
<Run Text="Default Result: " />
<Run Text="{Binding DefaultResult, FallbackValue=''}" />
</TextBlock>
<TextBlock>
<Run Text="Dialog Date: " />
<Run Text="{Binding Date, FallbackValue=''}" />
</TextBlock>
</StackPanel>
</TabItem> </TabItem>
<TabItem Header="Custom"> <TabItem Header="Custom Window Dialog">
<StackPanel> <u:Form
Width="300"
Margin="16,0"
DataContext="{Binding CustomWindowDialogDemoViewModel}"
LabelPosition="Top">
<TextBox
HorizontalAlignment="Stretch"
u:FormItem.Label="Title"
Text="{Binding Title}" />
<u:EnumSelector
HorizontalAlignment="Stretch"
u:FormItem.Label="Startup Location"
EnumType="WindowStartupLocation"
Value="{Binding Location}" />
<u:FormItem Label="PixelPosition">
<UniformGrid Columns="2" Rows="1">
<u:NumericIntUpDown InnerLeftContent="X" Value="{Binding X}" />
<u:NumericIntUpDown InnerLeftContent="Y" Value="{Binding Y}" />
</UniformGrid>
</u:FormItem>
<CheckBox u:FormItem.Label="Show In Taskbar" IsChecked="{Binding ShowInTaskBar}" />
<CheckBox
u:FormItem.Label="Is Close Button Visible"
IsChecked="{Binding IsCloseButtonVisible}"
IsThreeState="True" />
<CheckBox u:FormItem.Label="Modal" IsChecked="{Binding IsModal}" />
<Button
HorizontalAlignment="Left"
u:FormItem.NoLabel="True"
Command="{Binding ShowDialogCommand}"
Content="Show" />
</u:Form>
</TabItem>
<TabItem Header="Default Overlay Dialog">
<u:Form
Width="300"
Margin="16,0"
DataContext="{Binding DefaultOverlayDialogDemoViewModel}"
LabelPosition="Top">
<TextBox
HorizontalAlignment="Stretch"
u:FormItem.Label="Title"
Text="{Binding Title}" />
<u:EnumSelector
HorizontalAlignment="Stretch"
u:FormItem.Label="Dialog Mode"
EnumType="u:DialogMode"
Value="{Binding Mode}" />
<u:EnumSelector
HorizontalAlignment="Stretch"
u:FormItem.Label="Dialog Buttons"
EnumType="u:DialogButton"
Value="{Binding Button}" />
<CheckBox
u:FormItem.Label="Is Close Button Visible"
IsChecked="{Binding IsCloseButtonVisible}"
IsThreeState="True" />
<CheckBox u:FormItem.Label="Modal" IsChecked="{Binding IsModal}" />
<CheckBox u:FormItem.Label="Can DragMove" IsChecked="{Binding CanDragMove}" />
<CheckBox u:FormItem.Label="Can LightDismiss" IsChecked="{Binding CanLightDismiss}" />
<CheckBox u:FormItem.Label="FullScreen" IsChecked="{Binding FullScreen}" />
<ToggleSwitch <ToggleSwitch
Name="overlay2" u:FormItem.Label="Global/Local OverlayHost"
Content="Window/Overlay" IsChecked="{Binding IsLocal}"
IsChecked="{Binding IsWindow}" OffContent="Global"
OffContent="Overlay" OnContent="Local" />
OnContent="Window" /> <Button
HorizontalAlignment="Left"
u:FormItem.NoLabel="True"
Command="{Binding ShowDialogCommand}"
Content="Show" />
</u:Form>
</TabItem>
<TabItem Header="Custom Overlay Dialog">
<u:Form
Width="300"
Margin="16,0"
DataContext="{Binding CustomOverlayDialogDemoViewModel}"
LabelPosition="Top">
<TextBox
HorizontalAlignment="Stretch"
u:FormItem.Label="Title"
Text="{Binding Title}" />
<CheckBox
u:FormItem.Label="Is Close Button Visible"
IsChecked="{Binding IsCloseButtonVisible}"
IsThreeState="True" />
<CheckBox u:FormItem.Label="Modal" IsChecked="{Binding IsModal}" />
<CheckBox u:FormItem.Label="Can DragMove" IsChecked="{Binding CanDragMove}" />
<CheckBox u:FormItem.Label="Can LightDismiss" IsChecked="{Binding CanLightDismiss}" />
<CheckBox u:FormItem.Label="FullScreen" IsChecked="{Binding FullScreen}" />
<ToggleSwitch <ToggleSwitch
Content="Global/Local" u:FormItem.Label="Global/Local OverlayHost"
IsChecked="{Binding IsGlobal}" IsChecked="{Binding IsLocal}"
IsVisible="{Binding !#overlay2.IsChecked}" OffContent="Global"
OffContent="Local" OnContent="Local" />
OnContent="Global" /> <Button
<ToggleSwitch HorizontalAlignment="Left"
Content="HideInTaskBar/ShowInTaskBar" u:FormItem.NoLabel="True"
IsChecked="{Binding ShowInTaskBar}" Command="{Binding ShowDialogCommand}"
IsVisible="{Binding #overlay2.IsChecked}" Content="Show" />
OffContent="HideInTaskBar" </u:Form>
OnContent="ShowInTaskBar" />
<ToggleSwitch
Name="modal"
Content="Modal/Regular"
IsChecked="{Binding IsModal}"
OffContent="Regular"
OnContent="Modal" />
<ToggleSwitch
Content="ClickOnMaskToClose"
IsChecked="{Binding CanLightDismiss}"
OffContent="No"
OnContent="Yes" />
<ToggleSwitch
Content="FullScreen"
IsChecked="{Binding FullScreen}"
OffContent="No"
OnContent="Yes" />
<Button Command="{Binding ShowCustomDialogCommand}" Content="Show Dialog" />
<TextBlock>
<Run Text="Custom Result: " />
<Run Text="{Binding Result}" />
</TextBlock>
<TextBlock>
<Run Text="Dialog Date: " />
<Run Text="{Binding Date}" />
</TextBlock>
</StackPanel>
</TabItem> </TabItem>
</TabControl> </TabControl>
<Grid Grid.Column="1"> <Grid Grid.Column="1">
@@ -122,8 +166,8 @@
BorderBrush="{DynamicResource SemiGrey1}" BorderBrush="{DynamicResource SemiGrey1}"
BorderThickness="1" BorderThickness="1"
ClipToBounds="True" ClipToBounds="True"
CornerRadius="20"> CornerRadius="12">
<u:OverlayDialogHost HostId="LocalHost"> <u:OverlayDialogHost HostId="{x:Static vm:DialogDemoViewModel.LocalHost}">
<u:OverlayDialogHost.DialogDataTemplates> <u:OverlayDialogHost.DialogDataTemplates>
<DataTemplate DataType="x:String"> <DataTemplate DataType="x:String">
<TextBlock <TextBlock

View File

@@ -13,86 +13,38 @@
x:DataType="vm:DrawerDemoViewModel" x:DataType="vm:DrawerDemoViewModel"
mc:Ignorable="d"> mc:Ignorable="d">
<Grid ColumnDefinitions="Auto, *"> <Grid ColumnDefinitions="Auto, *">
<TabControl Grid.Column="0" Width="300"> <u:Form Width="300" Grid.Column="0" LabelPosition="Top">
<TabItem Header="Default"> <TextBox u:FormItem.Label="Title" Text="{Binding Title}"/>
<StackPanel> <u:EnumSelector
<u:EnumSelector EnumType="common:Position" Value="{Binding SelectedPosition}" /> u:FormItem.Label="Position"
<ToggleSwitch EnumType="common:Position"
Content="Global/Local" Value="{Binding Position}"/>
IsChecked="{Binding IsGlobal}" <u:EnumSelector
OffContent="Local" u:FormItem.Label="Buttons"
OnContent="Global" /> EnumType="u:DialogButton"
<ToggleSwitch Value="{Binding Buttons}"/>
Content="Modal" <CheckBox u:FormItem.Label="Can LightDismiss" IsChecked="{Binding CanLightDismiss}"/>
IsChecked="{Binding IsModal}" <CheckBox u:FormItem.Label="Is Modal" IsChecked="{Binding IsModal}"/>
OffContent="No" <CheckBox u:FormItem.Label="Is Close Button Visible" IsChecked="{Binding IsCloseButtonVisible}" IsThreeState="True"/>
OnContent="Yes" /> <CheckBox u:FormItem.Label="Custom Dialog" IsChecked="{Binding Custom}"/>
<ToggleSwitch <ToggleSwitch
Content="CanLightDismiss" u:FormItem.Label="Global/Local OverlayHost"
IsChecked="{Binding CanLightDismiss}" IsChecked="{Binding IsLocal}"
OffContent="No" OffContent="Global"
OnContent="Yes" /> OnContent="Local" />
<StackPanel Orientation="Horizontal"> <Button
<TextBlock Text="Buttons" /> HorizontalAlignment="Left"
<u:EnumSelector EnumType="{x:Type u:DialogButton}" Value="{Binding SelectedButton}" /> u:FormItem.NoLabel="True"
</StackPanel> Command="{Binding ShowDialogCommand}"
<Button Command="{Binding ShowDialogCommand}" Content="Show Default Drawer" /> Content="Show" />
<TextBlock> </u:Form>
<Run Text="Default Result: " />
<Run Text="{Binding DefaultResult}" />
</TextBlock>
<TextBlock>
<Run Text="Dialog Date: " />
<Run Text="{Binding Date}" />
</TextBlock>
</StackPanel>
</TabItem>
<TabItem Header="Custom">
<StackPanel>
<u:EnumSelector EnumType="common:Position" Value="{Binding SelectedPosition}" />
<ToggleSwitch
Content="Global/Local"
IsChecked="{Binding IsGlobal}"
OffContent="Local"
OnContent="Global" />
<ToggleSwitch
Content="CanLightDismiss"
IsChecked="{Binding CanLightDismiss}"
OffContent="No"
OnContent="Yes" />
<ToggleSwitch
Content="Modal"
IsChecked="{Binding IsModal}"
OffContent="No"
OnContent="Yes" />
<Button Command="{Binding ShowCustomDialogCommand}" Content="Show Custom Drawer" />
<TextBlock>
<Run Text="Custom Result: " />
<Run Text="{Binding Result}" />
</TextBlock>
<TextBlock>
<Run Text="Dialog Date: " />
<Run Text="{Binding Date}" />
</TextBlock>
</StackPanel>
</TabItem>
</TabControl>
<Grid Grid.Column="1" ClipToBounds="True"> <Grid Grid.Column="1" ClipToBounds="True">
<Border <Border
BorderBrush="{DynamicResource SemiGrey1}" BorderBrush="{DynamicResource SemiGrey1}"
BorderThickness="1" BorderThickness="1"
ClipToBounds="True" ClipToBounds="True"
CornerRadius="20"> CornerRadius="20">
<u:OverlayDialogHost HostId="LocalHost"> <u:OverlayDialogHost HostId="LocalHost"/>
<u:OverlayDialogHost.DialogDataTemplates>
<DataTemplate DataType="x:String">
<TextBlock
Margin="24,24,48,24"
Foreground="Red"
Text="{Binding Path=.}" />
</DataTemplate>
</u:OverlayDialogHost.DialogDataTemplates>
</u:OverlayDialogHost>
</Border> </Border>
</Grid> </Grid>
</Grid> </Grid>

View File

@@ -1,7 +1,10 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Threading.Channels;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input; using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using Ursa.Controls; using Ursa.Controls;
@@ -11,129 +14,203 @@ namespace Ursa.Demo.ViewModels;
public partial class DialogDemoViewModel: ObservableObject public partial class DialogDemoViewModel: ObservableObject
{ {
public ICommand ShowDialogCommand { get; set; } public const string LocalHost = "LocalHost";
public ICommand ShowCustomDialogCommand { get; set; } public DefaultWindowDialogDemoViewModel DefaultWindowDialogDemoViewModel { get; set; } = new();
public CustomWindowDialogDemoViewModel CustomWindowDialogDemoViewModel { get; set; } = new();
public DefaultOverlayDialogDemoViewModel DefaultOverlayDialogDemoViewModel { get; set; } = new();
public CustomOverlayDialogDemoViewModel CustomOverlayDialogDemoViewModel { get; set; } = new();
}
[ObservableProperty] private DialogMode _selectedMode; public partial class DefaultWindowDialogDemoViewModel: ObservableObject
[ObservableProperty] private DialogButton _selectedButton; {
[ObservableProperty] private bool _isWindow; [ObservableProperty] private WindowStartupLocation _location;
[ObservableProperty] private bool _isGlobal; [ObservableProperty] private int? _x;
[ObservableProperty] private bool _isModal; [ObservableProperty] private int? _y;
[ObservableProperty] private bool _canLightDismiss; [ObservableProperty] private string? _title;
[ObservableProperty] private DialogResult? _defaultResult; [ObservableProperty] private DialogMode _mode;
[ObservableProperty] private bool _result; [ObservableProperty] private DialogButton _button;
[ObservableProperty] private DateTime? _date;
[ObservableProperty] private bool _fullScreen;
[ObservableProperty] private bool _showInTaskBar; [ObservableProperty] private bool _showInTaskBar;
[ObservableProperty] private bool? _isCloseButtonVisible;
public DialogDemoViewModel()
public ICommand ShowDialogCommand { get; }
public DefaultWindowDialogDemoViewModel()
{ {
ShowDialogCommand = new AsyncRelayCommand(ShowDialog); ShowDialogCommand = new AsyncRelayCommand(ShowDialog);
ShowCustomDialogCommand = new AsyncRelayCommand(ShowCustomDialog); Mode = DialogMode.None;
Button = DialogButton.OKCancel;
Location = WindowStartupLocation.CenterScreen;
}
private async Task ShowDialog()
{
var options = new DialogOptions()
{
Title = Title,
Mode = Mode,
Button = Button,
ShowInTaskBar = ShowInTaskBar,
IsCloseButtonVisible = IsCloseButtonVisible,
StartupLocation = Location,
};
if (X.HasValue && Y.HasValue)
{
options.Position = new PixelPoint(X.Value, Y.Value);
}
await Dialog.ShowModal<DefaultDemoDialog, DefaultDemoDialogViewModel>(new DefaultDemoDialogViewModel(), options: options);
}
}
public partial class CustomWindowDialogDemoViewModel: ObservableObject
{
[ObservableProperty] private WindowStartupLocation _location;
[ObservableProperty] private int? _x;
[ObservableProperty] private int? _y;
[ObservableProperty] private string? _title;
[ObservableProperty] private bool _showInTaskBar;
[ObservableProperty] private bool? _isCloseButtonVisible;
[ObservableProperty] private bool _isModal;
public ICommand ShowDialogCommand { get; }
public CustomWindowDialogDemoViewModel()
{
ShowDialogCommand = new AsyncRelayCommand(ShowDialog);
Location = WindowStartupLocation.CenterScreen;
IsModal = true;
}
private async Task ShowDialog()
{
var options = new DialogOptions()
{
Title = Title,
ShowInTaskBar = ShowInTaskBar,
IsCloseButtonVisible = IsCloseButtonVisible,
StartupLocation = Location,
};
if (X.HasValue && Y.HasValue)
{
options.Position = new PixelPoint(X.Value, Y.Value);
}
if (IsModal)
{
await Dialog.ShowCustomModal<CustomDemoDialog, CustomDemoDialogViewModel, object>(new CustomDemoDialogViewModel(),
options: options);
}
else
{
Dialog.ShowCustom<CustomDemoDialog, CustomDemoDialogViewModel>(new CustomDemoDialogViewModel(),
options: options);
}
}
}
public partial class DefaultOverlayDialogDemoViewModel : ObservableObject
{
[ObservableProperty] private HorizontalPosition _horizontalAnchor;
[ObservableProperty] private VerticalPosition _verticalAnchor;
[ObservableProperty] private double? _horizontalOffset;
[ObservableProperty] private double? _verticalOffset;
[ObservableProperty] private bool _fullScreen;
[ObservableProperty] private DialogMode _mode;
[ObservableProperty] private DialogButton _button;
[ObservableProperty] private string? _title;
[ObservableProperty] private bool _canLightDismiss;
[ObservableProperty] private bool _canDragMove;
[ObservableProperty] private bool? _isCloseButtonVisible;
[ObservableProperty] private bool _isModal;
[ObservableProperty] private bool _isLocal;
public ICommand ShowDialogCommand { get; }
public DefaultOverlayDialogDemoViewModel()
{
ShowDialogCommand = new AsyncRelayCommand(ShowDialog);
HorizontalAnchor = HorizontalPosition.Center;
VerticalAnchor = VerticalPosition.Center;
CanDragMove = true;
IsModal = true;
IsCloseButtonVisible = true;
Button = DialogButton.OKCancel;
}
private async Task ShowDialog()
{
var options = new OverlayDialogOptions()
{
FullScreen = FullScreen,
HorizontalAnchor = HorizontalAnchor,
VerticalAnchor = VerticalAnchor,
HorizontalOffset = HorizontalOffset,
VerticalOffset = VerticalOffset,
Mode = Mode,
Buttons = Button,
Title = Title,
CanLightDismiss = CanLightDismiss,
CanDragMove = CanDragMove,
IsCloseButtonVisible = IsCloseButtonVisible,
};
string? dialogHostId = IsLocal ? DialogDemoViewModel.LocalHost : null;
if (IsModal)
{
await OverlayDialog.ShowModal<DefaultDemoDialog, DefaultDemoDialogViewModel>(new DefaultDemoDialogViewModel(), dialogHostId, options: options);
}
else
{
OverlayDialog.Show<DefaultDemoDialog, DefaultDemoDialogViewModel>(new DefaultDemoDialogViewModel(), dialogHostId, options: options);
}
}
}
public partial class CustomOverlayDialogDemoViewModel: ObservableObject
{
[ObservableProperty] private HorizontalPosition _horizontalAnchor;
[ObservableProperty] private VerticalPosition _verticalAnchor;
[ObservableProperty] private double? _horizontalOffset;
[ObservableProperty] private double? _verticalOffset;
[ObservableProperty] private bool _fullScreen;
[ObservableProperty] private string? _title;
[ObservableProperty] private bool _canLightDismiss;
[ObservableProperty] private bool _canDragMove;
[ObservableProperty] private bool? _isCloseButtonVisible;
[ObservableProperty] private bool _isModal;
[ObservableProperty] private bool _isLocal;
public ICommand ShowDialogCommand { get; }
public CustomOverlayDialogDemoViewModel()
{
ShowDialogCommand = new AsyncRelayCommand(ShowDialog);
HorizontalAnchor = HorizontalPosition.Center;
VerticalAnchor = VerticalPosition.Center;
CanDragMove = true;
IsModal = true; IsModal = true;
IsGlobal = true;
ShowInTaskBar = false;
} }
private async Task ShowDialog() private async Task ShowDialog()
{ {
var vm = new PlainDialogViewModel(); var options = new OverlayDialogOptions()
if (IsWindow)
{ {
DefaultResult = await Dialog.ShowModal<PlainDialog, PlainDialogViewModel>( FullScreen = FullScreen,
vm, options: new DialogOptions() HorizontalAnchor = HorizontalAnchor,
{ VerticalAnchor = VerticalAnchor,
Title = "Please select a date", HorizontalOffset = HorizontalOffset,
Mode = SelectedMode, VerticalOffset = VerticalOffset,
Button = SelectedButton, Title = Title,
ShowInTaskBar = ShowInTaskBar, CanLightDismiss = CanLightDismiss,
}); CanDragMove = CanDragMove,
Date = vm.Date; IsCloseButtonVisible = IsCloseButtonVisible,
};
var dialogHostId = IsLocal ? DialogDemoViewModel.LocalHost : null;
if (IsModal)
{
await OverlayDialog.ShowCustomModal<CustomDemoDialog, CustomDemoDialogViewModel, object>(new CustomDemoDialogViewModel(), dialogHostId, options: options);
} }
else else
{ {
if (IsModal) OverlayDialog.ShowCustom<CustomDemoDialog, CustomDemoDialogViewModel>(new CustomDemoDialogViewModel(), dialogHostId, options: options);
{
DefaultResult = await OverlayDialog.ShowModal<PlainDialog, PlainDialogViewModel>(
vm,
IsGlobal ? null : "LocalHost",
new OverlayDialogOptions()
{
Title = "Please select a date",
Mode = SelectedMode,
Buttons = SelectedButton,
CanLightDismiss = CanLightDismiss,
HorizontalAnchor = HorizontalPosition.Right,
HorizontalOffset = 50,
VerticalAnchor = VerticalPosition.Top,
VerticalOffset = 50,
FullScreen = FullScreen,
}
);
Date = vm.Date;
}
else
{
OverlayDialog.Show<PlainDialog, PlainDialogViewModel>(
new PlainDialogViewModel(),
IsGlobal ? null : "LocalHost",
new OverlayDialogOptions()
{
Title = "Please select a date",
Mode = SelectedMode,
Buttons = SelectedButton,
CanLightDismiss = CanLightDismiss,
FullScreen = FullScreen,
});
}
} }
}
private async Task ShowCustomDialog()
{
var vm = new DialogWithActionViewModel();
if (IsWindow)
{
if (IsModal)
{
Result = await Dialog.ShowCustomModal<DialogWithAction, DialogWithActionViewModel, bool>(
vm,
options: new DialogOptions
{
ShowInTaskBar = ShowInTaskBar
});
Date = vm.Date;
}
else
{
Dialog.ShowCustom<DialogWithAction, DialogWithActionViewModel>(
vm,
options: new DialogOptions
{
ShowInTaskBar = ShowInTaskBar
});
}
}
else
{
if (IsModal)
{
Result = await OverlayDialog.ShowCustomModal<DialogWithAction, DialogWithActionViewModel, bool>(
vm, IsGlobal ? null : "LocalHost", options: new OverlayDialogOptions()
{
CanLightDismiss = CanLightDismiss,
FullScreen = FullScreen,
});
Date = vm.Date;
}
else
{
OverlayDialog.ShowCustom<DialogWithAction, DialogWithActionViewModel>(new DialogWithActionViewModel(),
IsGlobal ? null : "LocalHost",
options: new OverlayDialogOptions{ CanLightDismiss = CanLightDismiss, FullScreen = FullScreen});
}
}
} }
} }

View File

@@ -13,83 +13,61 @@ namespace Ursa.Demo.ViewModels;
public partial class DrawerDemoViewModel : ObservableObject public partial class DrawerDemoViewModel : ObservableObject
{ {
public ICommand ShowDialogCommand { get; set; } public ICommand ShowDialogCommand { get; set; }
public ICommand ShowCustomDialogCommand { get; set; }
[ObservableProperty] private Position _selectedPosition; [ObservableProperty] private Position _position;
[ObservableProperty] private DialogButton _selectedButton; [ObservableProperty] private DialogButton _buttons;
[ObservableProperty] private bool _isGlobal;
[ObservableProperty] private bool _canLightDismiss; [ObservableProperty] private bool _canLightDismiss;
[ObservableProperty] private DialogResult? _defaultResult;
[ObservableProperty] private bool _result;
[ObservableProperty] private bool _isModal; [ObservableProperty] private bool _isModal;
[ObservableProperty] private DateTime? _date; [ObservableProperty] private bool? _isCloseButtonVisible;
[ObservableProperty] private string? _title;
[ObservableProperty] private bool _custom;
[ObservableProperty] private bool _isLocal;
public DrawerDemoViewModel() public DrawerDemoViewModel()
{ {
ShowDialogCommand = new AsyncRelayCommand(ShowDefaultDialog); ShowDialogCommand = new AsyncRelayCommand(ShowDefaultDialog);
ShowCustomDialogCommand = new AsyncRelayCommand(ShowCustomDrawer); Position = Position.Right;
SelectedPosition = Position.Right;
IsGlobal = true;
IsModal = true; IsModal = true;
Title = "Add New";
} }
private async Task ShowDefaultDialog() private async Task ShowDefaultDialog()
{ {
var vm = new PlainDialogViewModel(); var options = new DrawerOptions()
if (IsModal)
{ {
DefaultResult = await Drawer.ShowModal<PlainDialog, PlainDialogViewModel>( Position = Position,
vm, Buttons = Buttons,
IsGlobal ? null : "LocalHost", CanLightDismiss = CanLightDismiss,
new DrawerOptions() IsCloseButtonVisible = IsCloseButtonVisible,
{ Title = Title,
Title = "Please select a date", };
Position = SelectedPosition, var hostId = IsLocal ? "LocalHost" : null;
Buttons = SelectedButton, if (Custom)
CanLightDismiss = CanLightDismiss, {
}); var vm = new CustomDemoDialogViewModel();
Date = vm.Date; if (IsModal)
{
await Drawer.ShowCustomModal<CustomDemoDialog, CustomDemoDialogViewModel, object?>(vm, hostId, options);
}
else
{
Drawer.ShowCustom<CustomDemoDialog, CustomDemoDialogViewModel>(vm, hostId, options);
}
} }
else else
{ {
Drawer.Show<PlainDialog, PlainDialogViewModel>( var vm = new DefaultDemoDialogViewModel();
vm, if (IsModal)
IsGlobal ? null : "LocalHost", {
new DrawerOptions() await Drawer.ShowModal<DefaultDemoDialog, DefaultDemoDialogViewModel>(vm, hostId, options);
{ }
Title = "Please select a date", else
Position = SelectedPosition, {
Buttons = SelectedButton, Drawer.Show<DefaultDemoDialog, DefaultDemoDialogViewModel>(vm, hostId, options);
CanLightDismiss = CanLightDismiss, }
});
}
}
private async Task ShowCustomDrawer()
{
var vm = new DialogWithActionViewModel();
if (IsModal)
{
Result = await Drawer.ShowCustomModal<DialogWithAction, DialogWithActionViewModel, bool>(
vm,
IsGlobal ? null : "LocalHost",
new DrawerOptions()
{
Position = SelectedPosition,
CanLightDismiss = CanLightDismiss,
});
Date = vm.Date;
}
else
{
Drawer.ShowCustom<DialogWithAction, DialogWithActionViewModel>(
vm,
IsGlobal ? null : "LocalHost",
new DrawerOptions()
{
Position = SelectedPosition,
CanLightDismiss = CanLightDismiss,
});
} }
} }
} }

View File

@@ -1,30 +1,27 @@
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Irihi.Avalonia.Shared.Contracts; using Irihi.Avalonia.Shared.Contracts;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls; namespace Ursa.Controls;
public class CustomDialogControl: DialogControlBase public class CustomDialogControl : DialogControlBase
{ {
internal bool IsCloseButtonVisible { get; set; }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
if (_closeButton is not null) var closeButtonVisible = IsCloseButtonVisible ??DataContext is IDialogContext;
IsHitTestVisibleProperty.SetValue(closeButtonVisible, _closeButton);
if (!closeButtonVisible)
{ {
_closeButton.IsVisible = IsCloseButtonVisible; OpacityProperty.SetValue(0, _closeButton);
} }
} }
public override void Close() public override void Close()
{ {
if (DataContext is IDialogContext context) if (DataContext is IDialogContext context)
{
context.Close(); context.Close();
}
else else
{
OnElementClosing(this, null); OnElementClosing(this, null);
}
} }
} }

View File

@@ -12,20 +12,30 @@ namespace Ursa.Controls;
[TemplatePart(PART_CancelButton, typeof(Button))] [TemplatePart(PART_CancelButton, typeof(Button))]
[TemplatePart(PART_YesButton, typeof(Button))] [TemplatePart(PART_YesButton, typeof(Button))]
[TemplatePart(PART_NoButton, typeof(Button))] [TemplatePart(PART_NoButton, typeof(Button))]
public class DefaultDialogControl: DialogControlBase public class DefaultDialogControl : DialogControlBase
{ {
public const string PART_OKButton = "PART_OKButton"; public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton"; public const string PART_CancelButton = "PART_CancelButton";
public const string PART_YesButton = "PART_YesButton"; public const string PART_YesButton = "PART_YesButton";
public const string PART_NoButton = "PART_NoButton"; public const string PART_NoButton = "PART_NoButton";
private Button? _okButton; public static readonly StyledProperty<string?> TitleProperty =
AvaloniaProperty.Register<DefaultDialogControl, string?>(
nameof(Title));
public static readonly StyledProperty<DialogButton> ButtonsProperty =
AvaloniaProperty.Register<DefaultDialogControl, DialogButton>(
nameof(Buttons));
public static readonly StyledProperty<DialogMode> ModeProperty =
AvaloniaProperty.Register<DefaultDialogControl, DialogMode>(
nameof(Mode));
private Button? _cancelButton; private Button? _cancelButton;
private Button? _yesButton;
private Button? _noButton; private Button? _noButton;
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DefaultDialogControl, string?>( private Button? _okButton;
nameof(Title)); private Button? _yesButton;
public string? Title public string? Title
{ {
@@ -33,24 +43,18 @@ public class DefaultDialogControl: DialogControlBase
set => SetValue(TitleProperty, value); set => SetValue(TitleProperty, value);
} }
public static readonly StyledProperty<DialogButton> ButtonsProperty = AvaloniaProperty.Register<DefaultDialogControl, DialogButton>(
nameof(Buttons));
public DialogButton Buttons public DialogButton Buttons
{ {
get => GetValue(ButtonsProperty); get => GetValue(ButtonsProperty);
set => SetValue(ButtonsProperty, value); set => SetValue(ButtonsProperty, value);
} }
public static readonly StyledProperty<DialogMode> ModeProperty = AvaloniaProperty.Register<DefaultDialogControl, DialogMode>(
nameof(Mode));
public DialogMode Mode public DialogMode Mode
{ {
get => GetValue(ModeProperty); get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value); set => SetValue(ModeProperty, value);
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
@@ -62,56 +66,51 @@ public class DefaultDialogControl: DialogControlBase
Button.ClickEvent.AddHandler(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton); Button.ClickEvent.AddHandler(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton);
SetButtonVisibility(); SetButtonVisibility();
} }
private void SetButtonVisibility() private void SetButtonVisibility()
{ {
bool isCloseButtonVisible = DataContext is IDialogContext || Buttons != DialogButton.YesNo; var closeButtonVisible = IsCloseButtonVisible ?? (DataContext is IDialogContext || Buttons != DialogButton.YesNo );
Button.IsVisibleProperty.SetValue(isCloseButtonVisible, _closeButton); IsHitTestVisibleProperty.SetValue(closeButtonVisible, _closeButton);
if (!closeButtonVisible)
{
OpacityProperty.SetValue(0, _closeButton);
}
switch (Buttons) switch (Buttons)
{ {
case DialogButton.None: case DialogButton.None:
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _okButton, _cancelButton, _yesButton, _noButton);
break; break;
case DialogButton.OK: case DialogButton.OK:
Button.IsVisibleProperty.SetValue(true, _okButton); IsVisibleProperty.SetValue(true, _okButton);
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
break; break;
case DialogButton.OKCancel: case DialogButton.OKCancel:
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton); IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _yesButton, _noButton);
break; break;
case DialogButton.YesNo: case DialogButton.YesNo:
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton); IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _yesButton, _noButton);
break; break;
case DialogButton.YesNoCancel: case DialogButton.YesNoCancel:
Button.IsVisibleProperty.SetValue(false, _okButton); IsVisibleProperty.SetValue(false, _okButton);
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
break; break;
} }
} }
private void DefaultButtonsClose(object? sender, RoutedEventArgs args) private void DefaultButtonsClose(object? sender, RoutedEventArgs args)
{ {
if (sender is Button button) if (sender is Button button)
{ {
if (button == _okButton) if (button == _okButton)
{
OnElementClosing(this, DialogResult.OK); OnElementClosing(this, DialogResult.OK);
}
else if (button == _cancelButton) else if (button == _cancelButton)
{
OnElementClosing(this, DialogResult.Cancel); OnElementClosing(this, DialogResult.Cancel);
}
else if (button == _yesButton) else if (button == _yesButton)
{
OnElementClosing(this, DialogResult.Yes); OnElementClosing(this, DialogResult.Yes);
} else if (button == _noButton) OnElementClosing(this, DialogResult.No);
else if (button == _noButton)
{
OnElementClosing(this, DialogResult.No);
}
} }
} }
@@ -123,7 +122,7 @@ public class DefaultDialogControl: DialogControlBase
} }
else else
{ {
DialogResult result = Buttons switch var result = Buttons switch
{ {
DialogButton.None => DialogResult.None, DialogButton.None => DialogResult.None,
DialogButton.OK => DialogResult.OK, DialogButton.OK => DialogResult.OK,

View File

@@ -12,22 +12,27 @@ namespace Ursa.Controls;
[TemplatePart(PART_NoButton, typeof(Button))] [TemplatePart(PART_NoButton, typeof(Button))]
[TemplatePart(PART_OKButton, typeof(Button))] [TemplatePart(PART_OKButton, typeof(Button))]
[TemplatePart(PART_CancelButton, typeof(Button))] [TemplatePart(PART_CancelButton, typeof(Button))]
public class DefaultDialogWindow: DialogWindow public class DefaultDialogWindow : DialogWindow
{ {
protected override Type StyleKeyOverride { get; } = typeof(DefaultDialogWindow);
public const string PART_YesButton = "PART_YesButton"; public const string PART_YesButton = "PART_YesButton";
public const string PART_NoButton = "PART_NoButton"; public const string PART_NoButton = "PART_NoButton";
public const string PART_OKButton = "PART_OKButton"; public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton"; public const string PART_CancelButton = "PART_CancelButton";
private Button? _yesButton; public static readonly StyledProperty<DialogButton> ButtonsProperty =
AvaloniaProperty.Register<DefaultDialogWindow, DialogButton>(
nameof(Buttons));
public static readonly StyledProperty<DialogMode> ModeProperty =
AvaloniaProperty.Register<DefaultDialogWindow, DialogMode>(
nameof(Mode));
private Button? _cancelButton;
private Button? _noButton; private Button? _noButton;
private Button? _okButton; private Button? _okButton;
private Button? _cancelButton;
public static readonly StyledProperty<DialogButton> ButtonsProperty = AvaloniaProperty.Register<DefaultDialogWindow, DialogButton>( private Button? _yesButton;
nameof(Buttons)); protected override Type StyleKeyOverride { get; } = typeof(DefaultDialogWindow);
public DialogButton Buttons public DialogButton Buttons
{ {
@@ -35,15 +40,12 @@ public class DefaultDialogWindow: DialogWindow
set => SetValue(ButtonsProperty, value); set => SetValue(ButtonsProperty, value);
} }
public static readonly StyledProperty<DialogMode> ModeProperty = AvaloniaProperty.Register<DefaultDialogWindow, DialogMode>(
nameof(Mode));
public DialogMode Mode public DialogMode Mode
{ {
get => GetValue(ModeProperty); get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value); set => SetValue(ModeProperty, value);
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
@@ -59,69 +61,49 @@ public class DefaultDialogWindow: DialogWindow
private void OnDefaultClose(object? sender, RoutedEventArgs e) private void OnDefaultClose(object? sender, RoutedEventArgs e)
{ {
if (Equals(sender, _yesButton)) if (Equals(sender, _yesButton))
{
Close(DialogResult.Yes); Close(DialogResult.Yes);
return; else if (Equals(sender, _noButton))
}
if(Equals(sender, _noButton))
{
Close(DialogResult.No); Close(DialogResult.No);
return; else if (Equals(sender, _okButton))
}
if(Equals(sender, _okButton))
{
Close(DialogResult.OK); Close(DialogResult.OK);
return; else if (Equals(sender, _cancelButton))
}
if(Equals(sender, _cancelButton))
{
Close(DialogResult.Cancel); Close(DialogResult.Cancel);
}
} }
private void SetButtonVisibility() private void SetButtonVisibility()
{ {
bool closeButtonVisible = DataContext is IDialogContext || Buttons != DialogButton.YesNo; // Close button should be hidden instead if invisible to retain layout.
SetVisibility(_closeButton, closeButtonVisible); IsVisibleProperty.SetValue(true, _closeButton);
var closeButtonVisible =
IsCloseButtonVisible ?? (DataContext is IDialogContext || Buttons != DialogButton.YesNo);
IsHitTestVisibleProperty.SetValue(closeButtonVisible, _closeButton);
if (!closeButtonVisible)
{
OpacityProperty.SetValue(0, _closeButton);
}
switch (Buttons) switch (Buttons)
{ {
case DialogButton.None: case DialogButton.None:
SetVisibility(_okButton, false); IsVisibleProperty.SetValue(false, _okButton, _cancelButton, _yesButton, _noButton);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break; break;
case DialogButton.OK: case DialogButton.OK:
SetVisibility(_okButton, true); IsVisibleProperty.SetValue(true, _okButton);
SetVisibility(_cancelButton, false); IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break; break;
case DialogButton.OKCancel: case DialogButton.OKCancel:
SetVisibility(_okButton, true); IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
SetVisibility(_cancelButton, true); IsVisibleProperty.SetValue(false, _yesButton, _noButton);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break; break;
case DialogButton.YesNo: case DialogButton.YesNo:
SetVisibility(_okButton, false); IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
SetVisibility(_cancelButton, false); IsVisibleProperty.SetValue(true, _yesButton, _noButton);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break; break;
case DialogButton.YesNoCancel: case DialogButton.YesNoCancel:
SetVisibility(_okButton, false); IsVisibleProperty.SetValue(false, _okButton);
SetVisibility(_cancelButton, true); IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break; break;
} }
} }
private void SetVisibility(Button? button, bool visible)
{
if (button is not null) button.IsVisible = visible;
}
protected override void OnCloseButtonClicked(object? sender, RoutedEventArgs args) protected override void OnCloseButtonClicked(object? sender, RoutedEventArgs args)
{ {
@@ -131,7 +113,7 @@ public class DefaultDialogWindow: DialogWindow
} }
else else
{ {
DialogResult result = Buttons switch var result = Buttons switch
{ {
DialogButton.None => DialogResult.None, DialogButton.None => DialogResult.None,
DialogButton.OK => DialogResult.OK, DialogButton.OK => DialogResult.OK,

View File

@@ -240,6 +240,7 @@ public static class Dialog
window.Buttons = options.Button; window.Buttons = options.Button;
window.Mode = options.Mode; window.Mode = options.Mode;
window.ShowInTaskbar = options.ShowInTaskBar; window.ShowInTaskbar = options.ShowInTaskBar;
window.IsCloseButtonVisible = options.IsCloseButtonVisible;
if (options.StartupLocation == WindowStartupLocation.Manual) if (options.StartupLocation == WindowStartupLocation.Manual)
{ {
if (options.Position is not null) if (options.Position is not null)

View File

@@ -20,6 +20,22 @@ public abstract class DialogControlBase : OverlayFeedbackElement
public const string PC_Modal = ":modal"; public const string PC_Modal = ":modal";
public const string PC_FullScreen = ":full-screen"; public const string PC_FullScreen = ":full-screen";
public static readonly DirectProperty<DialogControlBase, bool> IsFullScreenProperty =
AvaloniaProperty.RegisterDirect<DialogControlBase, bool>(
nameof(IsFullScreen), o => o.IsFullScreen, (o, v) => o.IsFullScreen = v);
protected internal Button? _closeButton;
private bool _isFullScreen;
private Panel? _titleArea;
static DialogControlBase()
{
CanDragMoveProperty.Changed.AddClassHandler<InputElement, bool>(OnCanDragMoveChanged);
CanCloseProperty.Changed.AddClassHandler<InputElement, bool>(OnCanCloseChanged);
IsFullScreenProperty.AffectsPseudoClass<DialogControlBase>(PC_FullScreen);
}
internal HorizontalPosition HorizontalAnchor { get; set; } = HorizontalPosition.Center; internal HorizontalPosition HorizontalAnchor { get; set; } = HorizontalPosition.Center;
internal VerticalPosition VerticalAnchor { get; set; } = VerticalPosition.Center; internal VerticalPosition VerticalAnchor { get; set; } = VerticalPosition.Center;
internal HorizontalPosition ActualHorizontalAnchor { get; set; } internal HorizontalPosition ActualHorizontalAnchor { get; set; }
@@ -29,11 +45,7 @@ public abstract class DialogControlBase : OverlayFeedbackElement
internal double? HorizontalOffsetRatio { get; set; } internal double? HorizontalOffsetRatio { get; set; }
internal double? VerticalOffsetRatio { get; set; } internal double? VerticalOffsetRatio { get; set; }
internal bool CanLightDismiss { get; set; } internal bool CanLightDismiss { get; set; }
internal bool? IsCloseButtonVisible { get; set; }
private bool _isFullScreen;
public static readonly DirectProperty<DialogControlBase, bool> IsFullScreenProperty = AvaloniaProperty.RegisterDirect<DialogControlBase, bool>(
nameof(IsFullScreen), o => o.IsFullScreen, (o, v) => o.IsFullScreen = v);
public bool IsFullScreen public bool IsFullScreen
{ {
@@ -41,113 +53,6 @@ public abstract class DialogControlBase : OverlayFeedbackElement
set => SetAndRaise(IsFullScreenProperty, ref _isFullScreen, value); set => SetAndRaise(IsFullScreenProperty, ref _isFullScreen, value);
} }
protected internal Button? _closeButton;
private Panel? _titleArea;
#region Layer Management
public static readonly RoutedEvent<DialogLayerChangeEventArgs> LayerChangedEvent =
RoutedEvent.Register<CustomDialogControl, DialogLayerChangeEventArgs>(
nameof(LayerChanged), RoutingStrategies.Bubble);
public event EventHandler<DialogLayerChangeEventArgs> LayerChanged
{
add => AddHandler(LayerChangedEvent, value);
remove => RemoveHandler(LayerChangedEvent, value);
}
public void UpdateLayer(object? o)
{
if (o is DialogLayerChangeType t)
{
RaiseEvent(new DialogLayerChangeEventArgs(LayerChangedEvent, t));
}
}
#endregion
#region DragMove AttachedPropert
public static readonly AttachedProperty<bool> CanDragMoveProperty =
AvaloniaProperty.RegisterAttached<DialogControlBase, InputElement, bool>("CanDragMove");
public static void SetCanDragMove(InputElement obj, bool value) => obj.SetValue(CanDragMoveProperty, value);
public static bool GetCanDragMove(InputElement obj) => obj.GetValue(CanDragMoveProperty);
private static void OnCanDragMoveChanged(InputElement arg1, AvaloniaPropertyChangedEventArgs<bool> arg2)
{
if (arg2.NewValue.Value)
{
arg1.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Bubble);
arg1.AddHandler(PointerMovedEvent, OnPointerMoved, RoutingStrategies.Bubble);
arg1.AddHandler(PointerReleasedEvent, OnPointerReleased, RoutingStrategies.Bubble);
}
else
{
arg1.RemoveHandler(PointerPressedEvent, OnPointerPressed);
arg1.RemoveHandler(PointerMovedEvent, OnPointerMoved);
arg1.RemoveHandler(PointerReleasedEvent, OnPointerReleased);
}
void OnPointerPressed(InputElement sender, PointerPressedEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog)
{
e.Source = dialog;
}
}
void OnPointerMoved(InputElement sender, PointerEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog)
{
e.Source = dialog;
}
}
void OnPointerReleased(InputElement sender, PointerReleasedEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog)
{
e.Source = dialog;
}
}
}
#endregion
#region Close AttachedProperty
public static readonly AttachedProperty<bool> CanCloseProperty =
AvaloniaProperty.RegisterAttached<DialogControlBase, InputElement, bool>("CanClose");
public static void SetCanClose(InputElement obj, bool value) => obj.SetValue(CanCloseProperty, value);
public static bool GetCanClose(InputElement obj) => obj.GetValue(CanCloseProperty);
private static void OnCanCloseChanged(InputElement arg1, AvaloniaPropertyChangedEventArgs<bool> arg2)
{
if (arg2.NewValue.Value)
{
arg1.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Bubble);
}
void OnPointerPressed(InputElement sender, PointerPressedEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog)
{
dialog.Close();
}
}
}
#endregion
static DialogControlBase()
{
CanDragMoveProperty.Changed.AddClassHandler<InputElement, bool>(OnCanDragMoveChanged);
CanCloseProperty.Changed.AddClassHandler<InputElement, bool>(OnCanCloseChanged);
IsFullScreenProperty.AffectsPseudoClass<DialogControlBase>(PC_FullScreen);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
@@ -188,10 +93,107 @@ public abstract class DialogControlBase : OverlayFeedbackElement
e.Source = this; e.Source = this;
} }
private void OnCloseButtonClick(object? sender, RoutedEventArgs args) => Close(); private void OnCloseButtonClick(object? sender, RoutedEventArgs args)
{
Close();
}
internal void SetAsModal(bool modal) internal void SetAsModal(bool modal)
{ {
PseudoClasses.Set(PC_Modal, modal); PseudoClasses.Set(PC_Modal, modal);
} }
#region Layer Management
public static readonly RoutedEvent<DialogLayerChangeEventArgs> LayerChangedEvent =
RoutedEvent.Register<CustomDialogControl, DialogLayerChangeEventArgs>(
nameof(LayerChanged), RoutingStrategies.Bubble);
public event EventHandler<DialogLayerChangeEventArgs> LayerChanged
{
add => AddHandler(LayerChangedEvent, value);
remove => RemoveHandler(LayerChangedEvent, value);
}
public void UpdateLayer(object? o)
{
if (o is DialogLayerChangeType t) RaiseEvent(new DialogLayerChangeEventArgs(LayerChangedEvent, t));
}
#endregion
#region DragMove AttachedPropert
public static readonly AttachedProperty<bool> CanDragMoveProperty =
AvaloniaProperty.RegisterAttached<DialogControlBase, InputElement, bool>("CanDragMove");
public static void SetCanDragMove(InputElement obj, bool value)
{
obj.SetValue(CanDragMoveProperty, value);
}
public static bool GetCanDragMove(InputElement obj)
{
return obj.GetValue(CanDragMoveProperty);
}
private static void OnCanDragMoveChanged(InputElement arg1, AvaloniaPropertyChangedEventArgs<bool> arg2)
{
if (arg2.NewValue.Value)
{
arg1.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Bubble);
arg1.AddHandler(PointerMovedEvent, OnPointerMoved, RoutingStrategies.Bubble);
arg1.AddHandler(PointerReleasedEvent, OnPointerReleased, RoutingStrategies.Bubble);
}
else
{
arg1.RemoveHandler(PointerPressedEvent, OnPointerPressed);
arg1.RemoveHandler(PointerMovedEvent, OnPointerMoved);
arg1.RemoveHandler(PointerReleasedEvent, OnPointerReleased);
}
void OnPointerPressed(InputElement sender, PointerPressedEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog) e.Source = dialog;
}
void OnPointerMoved(InputElement sender, PointerEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog) e.Source = dialog;
}
void OnPointerReleased(InputElement sender, PointerReleasedEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog) e.Source = dialog;
}
}
#endregion
#region Close AttachedProperty
public static readonly AttachedProperty<bool> CanCloseProperty =
AvaloniaProperty.RegisterAttached<DialogControlBase, InputElement, bool>("CanClose");
public static void SetCanClose(InputElement obj, bool value)
{
obj.SetValue(CanCloseProperty, value);
}
public static bool GetCanClose(InputElement obj)
{
return obj.GetValue(CanCloseProperty);
}
private static void OnCanCloseChanged(InputElement arg1, AvaloniaPropertyChangedEventArgs<bool> arg2)
{
if (arg2.NewValue.Value) arg1.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Bubble);
void OnPointerPressed(InputElement sender, PointerPressedEventArgs e)
{
if (sender.FindLogicalAncestorOfType<DialogControlBase>() is { } dialog) dialog.Close();
}
}
#endregion
} }

View File

@@ -11,33 +11,29 @@ namespace Ursa.Controls;
[TemplatePart(PART_CloseButton, typeof(Button))] [TemplatePart(PART_CloseButton, typeof(Button))]
[TemplatePart(PART_TitleArea, typeof(Panel))] [TemplatePart(PART_TitleArea, typeof(Panel))]
public class DialogWindow: Window public class DialogWindow : Window
{ {
public const string PART_CloseButton = "PART_CloseButton"; public const string PART_CloseButton = "PART_CloseButton";
public const string PART_TitleArea = "PART_TitleArea"; public const string PART_TitleArea = "PART_TitleArea";
protected override Type StyleKeyOverride { get; } = typeof(DialogWindow);
protected internal Button? _closeButton; protected internal Button? _closeButton;
private Panel? _titleArea; private Panel? _titleArea;
internal bool IsCloseButtonVisible { get; set; }
static DialogWindow() static DialogWindow()
{ {
DataContextProperty.Changed.AddClassHandler<DialogWindow, object?>((o, e) => o.OnDataContextChange(e)); DataContextProperty.Changed.AddClassHandler<DialogWindow, object?>((window, e) =>
window.OnDataContextChange(e));
} }
protected override Type StyleKeyOverride { get; } = typeof(DialogWindow);
internal bool? IsCloseButtonVisible { get; set; }
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args) private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
{ {
if (args.OldValue.Value is IDialogContext oldContext) if (args.OldValue.Value is IDialogContext oldContext) oldContext.RequestClose -= OnContextRequestClose;
{
oldContext.RequestClose-= OnContextRequestClose;
}
if (args.NewValue.Value is IDialogContext newContext) if (args.NewValue.Value is IDialogContext newContext) newContext.RequestClose += OnContextRequestClose;
{
newContext.RequestClose += OnContextRequestClose;
}
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
@@ -46,11 +42,10 @@ public class DialogWindow: Window
Button.ClickEvent.RemoveHandler(OnCloseButtonClicked, _closeButton); Button.ClickEvent.RemoveHandler(OnCloseButtonClicked, _closeButton);
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed); _titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton); _closeButton = e.NameScope.Find<Button>(PART_CloseButton);
Button.IsVisibleProperty.SetValue(IsCloseButtonVisible, _closeButton); IsVisibleProperty.SetValue(IsCloseButtonVisible ?? true, _closeButton);
Button.ClickEvent.AddHandler(OnCloseButtonClicked, _closeButton); Button.ClickEvent.AddHandler(OnCloseButtonClicked, _closeButton);
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea); _titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
} }
private void OnContextRequestClose(object? sender, object? args) private void OnContextRequestClose(object? sender, object? args)
@@ -61,17 +56,13 @@ public class DialogWindow: Window
protected virtual void OnCloseButtonClicked(object? sender, RoutedEventArgs args) protected virtual void OnCloseButtonClicked(object? sender, RoutedEventArgs args)
{ {
if (DataContext is IDialogContext context) if (DataContext is IDialogContext context)
{
context.Close(); context.Close();
}
else else
{
Close(null); Close(null);
}
} }
private void OnTitlePointerPressed(object? sender, PointerPressedEventArgs e) private void OnTitlePointerPressed(object? sender, PointerPressedEventArgs e)
{ {
this.BeginMoveDrag(e); BeginMoveDrag(e);
} }
} }

View File

@@ -25,7 +25,7 @@ public class DialogOptions
public DialogButton Button { get; set; } = DialogButton.OKCancel; public DialogButton Button { get; set; } = DialogButton.OKCancel;
public bool IsCloseButtonVisible { get; set; } = true; public bool? IsCloseButtonVisible { get; set; } = true;
public bool ShowInTaskBar { get; set; } = true; public bool ShowInTaskBar { get; set; } = true;
} }

View File

@@ -16,40 +16,50 @@ public enum VerticalPosition
public class OverlayDialogOptions public class OverlayDialogOptions
{ {
internal static OverlayDialogOptions Default { get; } = new OverlayDialogOptions(); internal static OverlayDialogOptions Default { get; } = new();
public bool FullScreen { get; set; } public bool FullScreen { get; set; }
public HorizontalPosition HorizontalAnchor { get; set; } = HorizontalPosition.Center; public HorizontalPosition HorizontalAnchor { get; set; } = HorizontalPosition.Center;
public VerticalPosition VerticalAnchor { get; set; } = VerticalPosition.Center; public VerticalPosition VerticalAnchor { get; set; } = VerticalPosition.Center;
/// <summary> /// <summary>
/// This attribute is only used when HorizontalAnchor is not Center /// This attribute is only used when HorizontalAnchor is not Center
/// </summary> /// </summary>
public double? HorizontalOffset { get; set; } = null; public double? HorizontalOffset { get; set; } = null;
/// <summary> /// <summary>
/// This attribute is only used when VerticalAnchor is not Center /// This attribute is only used when VerticalAnchor is not Center
/// </summary> /// </summary>
public double? VerticalOffset { get; set; } = null; public double? VerticalOffset { get; set; } = null;
/// <summary> /// <summary>
/// Only works for DefaultDialogControl /// Only works for DefaultDialogControl
/// </summary> /// </summary>
public DialogMode Mode { get; set; } = DialogMode.None; public DialogMode Mode { get; set; } = DialogMode.None;
/// <summary> /// <summary>
/// Only works for DefaultDialogControl /// Only works for DefaultDialogControl
/// </summary> /// </summary>
public DialogButton Buttons { get; set; } = DialogButton.OKCancel; public DialogButton Buttons { get; set; } = DialogButton.OKCancel;
/// <summary> /// <summary>
/// Only works for DefaultDialogControl /// Only works for DefaultDialogControl
/// </summary> /// </summary>
public string? Title { get; set; } = null; public string? Title { get; set; } = null;
/// <summary> /// <summary>
/// Only works for CustomDialogControl /// Only works for CustomDialogControl
/// </summary> /// </summary>
public bool IsCloseButtonVisible { get; set; } = true; public bool? IsCloseButtonVisible { get; set; } = true;
[Obsolete()]
public bool ShowCloseButton { get; set; } = true; [Obsolete] public bool ShowCloseButton { get; set; } = true;
public bool CanLightDismiss { get; set; } public bool CanLightDismiss { get; set; }
public bool CanDragMove { get; set; } = true; public bool CanDragMove { get; set; } = true;
/// <summary> /// <summary>
/// The hash code of the top level dialog host. This is used to identify the dialog host if there are multiple dialog hosts with the same id. If this is not provided, the dialog will be added to the first dialog host with the same id. /// The hash code of the top level dialog host. This is used to identify the dialog host if there are multiple dialog
/// hosts with the same id. If this is not provided, the dialog will be added to the first dialog host with the same
/// id.
/// </summary> /// </summary>
public int? TopLevelHashCode { get; set; } public int? TopLevelHashCode { get; set; }
} }

View File

@@ -221,6 +221,7 @@ public static class OverlayDialog
control.Buttons = options.Buttons; control.Buttons = options.Buttons;
control.Title = options.Title; control.Title = options.Title;
control.CanLightDismiss = options.CanLightDismiss; control.CanLightDismiss = options.CanLightDismiss;
control.IsCloseButtonVisible = options.IsCloseButtonVisible;
DialogControlBase.SetCanDragMove(control, options.CanDragMove); DialogControlBase.SetCanDragMove(control, options.CanDragMove);
} }

View File

@@ -10,7 +10,7 @@ public class CustomDrawerControl: DrawerControlBase
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
if (_closeButton is not null) if (_closeButton is not null)
{ {
_closeButton.IsVisible = IsCloseButtonVisible; _closeButton.IsVisible = IsCloseButtonVisible ?? true;
} }
} }

View File

@@ -13,20 +13,30 @@ namespace Ursa.Controls;
[TemplatePart(PART_NoButton, typeof(Button))] [TemplatePart(PART_NoButton, typeof(Button))]
[TemplatePart(PART_OKButton, typeof(Button))] [TemplatePart(PART_OKButton, typeof(Button))]
[TemplatePart(PART_CancelButton, typeof(Button))] [TemplatePart(PART_CancelButton, typeof(Button))]
public class DefaultDrawerControl: DrawerControlBase public class DefaultDrawerControl : DrawerControlBase
{ {
public const string PART_YesButton = "PART_YesButton"; public const string PART_YesButton = "PART_YesButton";
public const string PART_NoButton = "PART_NoButton"; public const string PART_NoButton = "PART_NoButton";
public const string PART_OKButton = "PART_OKButton"; public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton"; public const string PART_CancelButton = "PART_CancelButton";
private Button? _yesButton; public static readonly StyledProperty<DialogButton> ButtonsProperty =
AvaloniaProperty.Register<DefaultDrawerControl, DialogButton>(
nameof(Buttons), DialogButton.OKCancel);
public static readonly StyledProperty<DialogMode> ModeProperty =
AvaloniaProperty.Register<DefaultDrawerControl, DialogMode>(
nameof(Mode), DialogMode.None);
public static readonly StyledProperty<string?> TitleProperty =
AvaloniaProperty.Register<DefaultDrawerControl, string?>(
nameof(Title));
private Button? _cancelButton;
private Button? _noButton; private Button? _noButton;
private Button? _okButton; private Button? _okButton;
private Button? _cancelButton;
public static readonly StyledProperty<DialogButton> ButtonsProperty = AvaloniaProperty.Register<DefaultDrawerControl, DialogButton>( private Button? _yesButton;
nameof(Buttons), DialogButton.OKCancel);
public DialogButton Buttons public DialogButton Buttons
{ {
@@ -34,24 +44,18 @@ public class DefaultDrawerControl: DrawerControlBase
set => SetValue(ButtonsProperty, value); set => SetValue(ButtonsProperty, value);
} }
public static readonly StyledProperty<DialogMode> ModeProperty = AvaloniaProperty.Register<DefaultDrawerControl, DialogMode>(
nameof(Mode), DialogMode.None);
public DialogMode Mode public DialogMode Mode
{ {
get => GetValue(ModeProperty); get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value); set => SetValue(ModeProperty, value);
} }
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DefaultDrawerControl, string?>(
nameof(Title));
public string? Title public string? Title
{ {
get => GetValue(TitleProperty); get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value); set => SetValue(TitleProperty, value);
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
@@ -63,58 +67,54 @@ public class DefaultDrawerControl: DrawerControlBase
Button.ClickEvent.AddHandler(OnDefaultButtonClick, _yesButton, _noButton, _okButton, _cancelButton); Button.ClickEvent.AddHandler(OnDefaultButtonClick, _yesButton, _noButton, _okButton, _cancelButton);
SetButtonVisibility(); SetButtonVisibility();
} }
private void SetButtonVisibility() private void SetButtonVisibility()
{ {
bool isCloseButtonVisible = DataContext is IDialogContext || Buttons != DialogButton.YesNo; var closeButtonVisible =
Button.IsVisibleProperty.SetValue(isCloseButtonVisible, _closeButton); IsCloseButtonVisible ?? (DataContext is IDialogContext || Buttons != DialogButton.YesNo);
IsHitTestVisibleProperty.SetValue(closeButtonVisible, _closeButton);
if (!closeButtonVisible)
{
OpacityProperty.SetValue(0, _closeButton);
}
switch (Buttons) switch (Buttons)
{ {
case DialogButton.None: case DialogButton.None:
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _okButton, _cancelButton, _yesButton, _noButton);
break; break;
case DialogButton.OK: case DialogButton.OK:
Button.IsVisibleProperty.SetValue(true, _okButton); IsVisibleProperty.SetValue(true, _okButton);
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
break; break;
case DialogButton.OKCancel: case DialogButton.OKCancel:
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton); IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _yesButton, _noButton);
break; break;
case DialogButton.YesNo: case DialogButton.YesNo:
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton); IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _yesButton, _noButton);
break; break;
case DialogButton.YesNoCancel: case DialogButton.YesNoCancel:
Button.IsVisibleProperty.SetValue(false, _okButton); IsVisibleProperty.SetValue(false, _okButton);
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
break; break;
} }
} }
private void OnDefaultButtonClick(object? sender, RoutedEventArgs e) private void OnDefaultButtonClick(object? sender, RoutedEventArgs e)
{ {
if (sender is Button button) if (sender is Button button)
{ {
if (button == _okButton) if (button == _okButton)
{
OnElementClosing(this, DialogResult.OK); OnElementClosing(this, DialogResult.OK);
}
else if (button == _cancelButton) else if (button == _cancelButton)
{
OnElementClosing(this, DialogResult.Cancel); OnElementClosing(this, DialogResult.Cancel);
}
else if (button == _yesButton) else if (button == _yesButton)
{
OnElementClosing(this, DialogResult.Yes); OnElementClosing(this, DialogResult.Yes);
} else if (button == _noButton) OnElementClosing(this, DialogResult.No);
else if (button == _noButton)
{
OnElementClosing(this, DialogResult.No);
}
} }
} }
public override void Close() public override void Close()
{ {
if (DataContext is IDialogContext context) if (DataContext is IDialogContext context)
@@ -123,7 +123,7 @@ public class DefaultDrawerControl: DrawerControlBase
} }
else else
{ {
DialogResult result = Buttons switch var result = Buttons switch
{ {
DialogButton.None => DialogResult.None, DialogButton.None => DialogResult.None,
DialogButton.OK => DialogResult.OK, DialogButton.OK => DialogResult.OK,

View File

@@ -37,15 +37,7 @@ public abstract class DrawerControlBase: OverlayFeedbackElement
set => SetValue(IsOpenProperty, value); set => SetValue(IsOpenProperty, value);
} }
public static readonly StyledProperty<bool> IsCloseButtonVisibleProperty = internal bool? IsCloseButtonVisible { get; set; }
AvaloniaProperty.Register<DrawerControlBase, bool>(
nameof(IsCloseButtonVisible), defaultValue: true);
public bool IsCloseButtonVisible
{
get => GetValue(IsCloseButtonVisibleProperty);
set => SetValue(IsCloseButtonVisibleProperty, value);
}
protected internal bool CanLightDismiss { get; set; } protected internal bool CanLightDismiss { get; set; }

View File

@@ -7,7 +7,7 @@ public class DrawerOptions
internal static DrawerOptions Default => new (); internal static DrawerOptions Default => new ();
public Position Position { get; set; } = Position.Right; public Position Position { get; set; } = Position.Right;
public bool CanLightDismiss { get; set; } = true; public bool CanLightDismiss { get; set; } = true;
public bool IsCloseButtonVisible { get; set; } = true; public bool? IsCloseButtonVisible { get; set; } = true;
public double? MinWidth { get; set; } = null; public double? MinWidth { get; set; } = null;
public double? MinHeight { get; set; } = null; public double? MinHeight { get; set; } = null;
public double? MaxWidth { get; set; } = null; public double? MaxWidth { get; set; } = null;

View File

@@ -8,57 +8,60 @@ using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls; namespace Ursa.Controls;
/// <summary> /// <summary>
/// The messageBox used to display in OverlayDialogHost. /// The messageBox used to display in OverlayDialogHost.
/// </summary> /// </summary>
[TemplatePart(PART_NoButton, typeof(Button))] [TemplatePart(PART_NoButton, typeof(Button))]
[TemplatePart(PART_OKButton, typeof(Button))] [TemplatePart(PART_OKButton, typeof(Button))]
[TemplatePart(PART_CancelButton, typeof(Button))] [TemplatePart(PART_CancelButton, typeof(Button))]
[TemplatePart(PART_YesButton, typeof(Button))] [TemplatePart(PART_YesButton, typeof(Button))]
public class MessageBoxControl: DialogControlBase public class MessageBoxControl : DialogControlBase
{ {
public const string PART_YesButton = "PART_YesButton"; public const string PART_YesButton = "PART_YesButton";
public const string PART_NoButton = "PART_NoButton"; public const string PART_NoButton = "PART_NoButton";
public const string PART_OKButton = "PART_OKButton"; public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton"; public const string PART_CancelButton = "PART_CancelButton";
private Button? _yesButton;
private Button? _noButton;
private Button? _okButton;
private Button? _cancelButton;
public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty = public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty =
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>( AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
nameof(MessageIcon)); nameof(MessageIcon));
public static readonly StyledProperty<MessageBoxButton> ButtonsProperty =
AvaloniaProperty.Register<MessageBoxControl, MessageBoxButton>(
nameof(Buttons));
public static readonly StyledProperty<string?> TitleProperty =
AvaloniaProperty.Register<MessageBoxControl, string?>(
nameof(Title));
private Button? _cancelButton;
private Button? _noButton;
private Button? _okButton;
private Button? _yesButton;
static MessageBoxControl()
{
ButtonsProperty.Changed.AddClassHandler<MessageBoxControl>((o, _) => { o.SetButtonVisibility(); });
}
public MessageBoxIcon MessageIcon public MessageBoxIcon MessageIcon
{ {
get => GetValue(MessageIconProperty); get => GetValue(MessageIconProperty);
set => SetValue(MessageIconProperty, value); set => SetValue(MessageIconProperty, value);
} }
public static readonly StyledProperty<MessageBoxButton> ButtonsProperty = AvaloniaProperty.Register<MessageBoxControl, MessageBoxButton>(
nameof(Buttons));
public MessageBoxButton Buttons public MessageBoxButton Buttons
{ {
get => GetValue(ButtonsProperty); get => GetValue(ButtonsProperty);
set => SetValue(ButtonsProperty, value); set => SetValue(ButtonsProperty, value);
} }
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<MessageBoxControl, string?>(
nameof(Title));
public string? Title public string? Title
{ {
get => GetValue(TitleProperty); get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value); set => SetValue(TitleProperty, value);
} }
static MessageBoxControl()
{
ButtonsProperty.Changed.AddClassHandler<MessageBoxControl>((o, _) => { o.SetButtonVisibility(); });
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
@@ -71,55 +74,66 @@ public class MessageBoxControl: DialogControlBase
SetButtonVisibility(); SetButtonVisibility();
} }
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
var defaultButton = Buttons switch
{
MessageBoxButton.OK => _okButton,
MessageBoxButton.OKCancel => _cancelButton,
MessageBoxButton.YesNo => _yesButton,
MessageBoxButton.YesNoCancel => _cancelButton,
_ => null
};
defaultButton?.Focus();
}
private void DefaultButtonsClose(object? sender, RoutedEventArgs e) private void DefaultButtonsClose(object? sender, RoutedEventArgs e)
{ {
if (sender is Button button) if (sender is not Button button) return;
var result = button switch
{ {
if (button == _okButton) _ when button == _okButton => MessageBoxResult.OK,
{ _ when button == _cancelButton => MessageBoxResult.Cancel,
OnElementClosing(this, MessageBoxResult.OK); _ when button == _yesButton => MessageBoxResult.Yes,
} _ when button == _noButton => MessageBoxResult.No,
else if (button == _cancelButton) _ => MessageBoxResult.None
{ };
OnElementClosing(this, MessageBoxResult.Cancel); OnElementClosing(this, result);
}
else if (button == _yesButton)
{
OnElementClosing(this, MessageBoxResult.Yes);
}
else if (button == _noButton)
{
OnElementClosing(this, MessageBoxResult.No);
}
}
} }
private void SetButtonVisibility() private void SetButtonVisibility()
{ {
var closeButtonVisible = Buttons != MessageBoxButton.YesNo;
IsVisibleProperty.SetValue(closeButtonVisible, _closeButton);
switch (Buttons) switch (Buttons)
{ {
case MessageBoxButton.OK: case MessageBoxButton.OK:
Button.IsVisibleProperty.SetValue(true, _okButton); IsVisibleProperty.SetValue(true, _okButton);
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
Button.IsDefaultProperty.SetValue(true, _okButton);
Button.IsDefaultProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
break; break;
case MessageBoxButton.OKCancel: case MessageBoxButton.OKCancel:
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton); IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _yesButton, _noButton);
Button.IsDefaultProperty.SetValue(true, _okButton);
Button.IsDefaultProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
break; break;
case MessageBoxButton.YesNo: case MessageBoxButton.YesNo:
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton); IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _yesButton, _noButton);
break; break;
case MessageBoxButton.YesNoCancel: case MessageBoxButton.YesNoCancel:
Button.IsVisibleProperty.SetValue(false, _okButton); IsVisibleProperty.SetValue(false, _okButton);
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
break; break;
} }
} }
public override void Close() public override void Close()
{ {
MessageBoxResult result = Buttons switch var result = Buttons switch
{ {
MessageBoxButton.OK => MessageBoxResult.OK, MessageBoxButton.OK => MessageBoxResult.OK,
MessageBoxButton.OKCancel => MessageBoxResult.Cancel, MessageBoxButton.OKCancel => MessageBoxResult.Cancel,

View File

@@ -21,28 +21,29 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
public const string PART_OKButton = "PART_OKButton"; public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton"; public const string PART_CancelButton = "PART_CancelButton";
private Button? _closeButton;
private Button? _yesButton;
private Button? _noButton;
private Button? _okButton;
private Button? _cancelButton;
protected override Type StyleKeyOverride => typeof(MessageBoxWindow);
public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty = public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty =
AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>( AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
nameof(MessageIcon)); nameof(MessageIcon));
private Button? _closeButton;
private Button? _cancelButton;
private Button? _noButton;
private Button? _okButton;
private Button? _yesButton;
public MessageBoxWindow() : this(MessageBoxButton.OK)
{
}
protected override Type StyleKeyOverride => typeof(MessageBoxWindow);
public MessageBoxIcon MessageIcon public MessageBoxIcon MessageIcon
{ {
get => GetValue(MessageIconProperty); get => GetValue(MessageIconProperty);
set => SetValue(MessageIconProperty, value); set => SetValue(MessageIconProperty, value);
} }
public MessageBoxWindow() : this(MessageBoxButton.OK)
{
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
@@ -60,33 +61,32 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
private void SetButtonVisibility() private void SetButtonVisibility()
{ {
var closeButtonVisible = buttons != MessageBoxButton.YesNo;
IsVisibleProperty.SetValue(closeButtonVisible, _closeButton);
switch (buttons) switch (buttons)
{ {
case MessageBoxButton.OK: case MessageBoxButton.OK:
Button.IsVisibleProperty.SetValue(true, _okButton); IsVisibleProperty.SetValue(true, _okButton);
Button.IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _cancelButton, _yesButton, _noButton);
break; break;
case MessageBoxButton.OKCancel: case MessageBoxButton.OKCancel:
Button.IsVisibleProperty.SetValue(true, _okButton, _cancelButton); IsVisibleProperty.SetValue(true, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(false, _yesButton, _noButton); IsVisibleProperty.SetValue(false, _yesButton, _noButton);
break; break;
case MessageBoxButton.YesNo: case MessageBoxButton.YesNo:
Button.IsVisibleProperty.SetValue(false, _okButton, _cancelButton); IsVisibleProperty.SetValue(false, _okButton, _cancelButton);
Button.IsVisibleProperty.SetValue(true, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _yesButton, _noButton);
break; break;
case MessageBoxButton.YesNoCancel: case MessageBoxButton.YesNoCancel:
Button.IsVisibleProperty.SetValue(false, _okButton); IsVisibleProperty.SetValue(false, _okButton);
Button.IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton); IsVisibleProperty.SetValue(true, _cancelButton, _yesButton, _noButton);
break; break;
} }
} }
private void OnCloseButtonClick(object? sender, RoutedEventArgs e) private void OnCloseButtonClick(object? sender, RoutedEventArgs e)
{ {
if (buttons == MessageBoxButton.OK) if (buttons == MessageBoxButton.OK) Close(MessageBoxResult.OK);
{
Close(MessageBoxResult.OK);
}
Close(MessageBoxResult.Cancel); Close(MessageBoxResult.Cancel);
} }
@@ -94,29 +94,29 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
private void OnDefaultButtonClick(object? sender, RoutedEventArgs e) private void OnDefaultButtonClick(object? sender, RoutedEventArgs e)
{ {
if (Equals(sender, _okButton)) if (Equals(sender, _okButton))
{
Close(MessageBoxResult.OK); Close(MessageBoxResult.OK);
}
else if (Equals(sender, _cancelButton)) else if (Equals(sender, _cancelButton))
{
Close(MessageBoxResult.Cancel); Close(MessageBoxResult.Cancel);
}
else if (Equals(sender, _yesButton)) else if (Equals(sender, _yesButton))
{
Close(MessageBoxResult.Yes); Close(MessageBoxResult.Yes);
} else if (Equals(sender, _noButton)) Close(MessageBoxResult.No);
else if (Equals(sender, _noButton))
{
Close(MessageBoxResult.No);
}
} }
protected override void OnKeyUp(KeyEventArgs e) protected override void OnKeyUp(KeyEventArgs e)
{ {
base.OnKeyUp(e); base.OnKeyUp(e);
if (e.Key == Key.Escape && buttons == MessageBoxButton.OK) if (e.Key != Key.Escape) return;
switch (buttons)
{ {
Close(MessageBoxResult.OK); case MessageBoxButton.OK:
Close(MessageBoxResult.OK);
break;
case MessageBoxButton.OKCancel:
Close(MessageBoxResult.Cancel);
break;
case MessageBoxButton.YesNoCancel:
Close(MessageBoxResult.Cancel);
break;
} }
} }
@@ -124,4 +124,19 @@ public class MessageBoxWindow(MessageBoxButton buttons) : Window
{ {
BeginMoveDrag(e); BeginMoveDrag(e);
} }
protected override void OnLoaded(RoutedEventArgs e)
{
base.OnLoaded(e);
var defaultButton = buttons switch
{
MessageBoxButton.OK => _okButton,
MessageBoxButton.OKCancel => _cancelButton,
MessageBoxButton.YesNo => _yesButton,
MessageBoxButton.YesNoCancel => _cancelButton,
_ => null
};
Button.IsDefaultProperty.SetValue(true, defaultButton);
defaultButton?.Focus();
}
} }