feat: implement buttons in different conditions.

This commit is contained in:
rabbitism
2024-01-11 21:03:37 +08:00
parent 66894aa569
commit 5eeebb020f
5 changed files with 156 additions and 18 deletions

View File

@@ -9,5 +9,9 @@
x:Class="Ursa.Demo.Pages.MessageBoxDemo"> x:Class="Ursa.Demo.Pages.MessageBoxDemo">
<StackPanel HorizontalAlignment="Left"> <StackPanel HorizontalAlignment="Left">
<Button Content="Default" Command="{Binding DefaultMessageBoxCommand}"></Button> <Button Content="Default" Command="{Binding DefaultMessageBoxCommand}"></Button>
<Button Content="OK" Command="{Binding OkCommand}" ></Button>
<Button Content="OKCancel" Command="{Binding OkCancelCommand}" ></Button>
<Button Content="YesNo" Command="{Binding YesNoCommand}" ></Button>
<Button Content="YesNoCancel" Command="{Binding YesNoCancelCommand}" ></Button>
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -9,14 +9,42 @@ namespace Ursa.Demo.ViewModels;
public class MessageBoxDemoViewModel: ObservableObject public class MessageBoxDemoViewModel: ObservableObject
{ {
public ICommand DefaultMessageBoxCommand { get; set; } public ICommand DefaultMessageBoxCommand { get; set; }
public ICommand OkCommand { get; set; }
public ICommand YesNoCommand { get; set; }
public ICommand YesNoCancelCommand { get; set; }
public ICommand OkCancelCommand { get; set; }
public MessageBoxDemoViewModel() public MessageBoxDemoViewModel()
{ {
DefaultMessageBoxCommand = new AsyncRelayCommand(OnDefaultMessageAsync); DefaultMessageBoxCommand = new AsyncRelayCommand(OnDefaultMessageAsync);
OkCommand = new AsyncRelayCommand(OnOkAsync);
YesNoCommand = new AsyncRelayCommand(OnYesNoAsync);
YesNoCancelCommand = new AsyncRelayCommand(OnYesNoCancelAsync);
OkCancelCommand = new AsyncRelayCommand(OnOkCancelAsync);
} }
private async Task OnDefaultMessageAsync() private async Task OnDefaultMessageAsync()
{ {
var result = await MessageBox.ShowAsync("Hello Message Box"); var result = await MessageBox.ShowAsync("Hello Message Box");
} }
private async Task OnOkAsync()
{
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.OK);
}
private async Task OnYesNoAsync()
{
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.YesNo);
}
private async Task OnYesNoCancelAsync()
{
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.YesNoCancel);
}
private async Task OnOkCancelAsync()
{
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.OKCancel);
}
} }

View File

@@ -4,7 +4,7 @@
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here --> <!-- Add Resources Here -->
<ControlTheme x:Key="{x:Type u:MessageBoxWindow}" TargetType="u:MessageBoxWindow"> <ControlTheme x:Key="{x:Type u:MessageBoxWindow}" TargetType="u:MessageBoxWindow">
<Setter Property="Title" Value="{x:Null}"></Setter> <Setter Property="Title" Value="{x:Null}" />
<Setter Property="Background" Value="{DynamicResource WindowDefaultBackground}" /> <Setter Property="Background" Value="{DynamicResource WindowDefaultBackground}" />
<Setter Property="TransparencyBackgroundFallback" Value="{DynamicResource WindowDefaultBackground}" /> <Setter Property="TransparencyBackgroundFallback" Value="{DynamicResource WindowDefaultBackground}" />
<Setter Property="Foreground" Value="{DynamicResource WindowDefaultForeground}" /> <Setter Property="Foreground" Value="{DynamicResource WindowDefaultForeground}" />
@@ -24,13 +24,30 @@
<Border Background="{TemplateBinding Background}" IsHitTestVisible="False" /> <Border Background="{TemplateBinding Background}" IsHitTestVisible="False" />
<Panel Margin="{TemplateBinding WindowDecorationMargin}" Background="Transparent" /> <Panel Margin="{TemplateBinding WindowDecorationMargin}" Background="Transparent" />
<Grid RowDefinitions="Auto, *, Auto"> <Grid RowDefinitions="Auto, *, Auto">
<Grid ColumnDefinitions="*, Auto" Grid.Row="0"> <Grid Grid.Row="0" ColumnDefinitions="*, Auto">
<TextBlock Grid.Column="0" Text="{TemplateBinding Title}" /> <TextBlock Grid.Column="0" Text="{TemplateBinding Title}" Margin="8 8 0 0" FontWeight="Bold" FontSize="14" />
<!-- A temporary style copied from Semi. Will replace when I get time -->
<Button <Button
Grid.Column="1"
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}" Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Content="X" Grid.Column="1"
/> Margin="0,4,4,0"
Background="{DynamicResource CaptionButtonClosePointeroverBackground}"
BorderBrush="{DynamicResource CaptionButtonClosePressedBackground}"
Theme="{DynamicResource CaptionButton}">
<Button.Styles>
<Style Selector="Button:pointerover">
<Setter Property="Foreground" Value="White" />
</Style>
<Style Selector="Button:pressed">
<Setter Property="Foreground" Value="White" />
</Style>
</Button.Styles>
<PathIcon
Width="12"
Height="12"
Data="{DynamicResource WindowCloseIconGlyph}"
Foreground="{Binding $parent[Button].Foreground}" />
</Button>
</Grid> </Grid>
<ContentPresenter <ContentPresenter
Name="PART_ContentPresenter" Name="PART_ContentPresenter"
@@ -40,11 +57,31 @@
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}" Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" /> ContentTemplate="{TemplateBinding ContentTemplate}" />
<StackPanel Grid.Row="2" Orientation="Horizontal"> <StackPanel
<Button Name="{x:Static u:MessageBoxWindow.PART_YesButton}" Classes="Primary" Content="Yes" /> Grid.Row="2"
<Button Name="{x:Static u:MessageBoxWindow.PART_NoButton}" Classes="Danger" Content="No" /> Margin="0,0,8,8"
<Button Name="{x:Static u:MessageBoxWindow.PART_OKButton}" Classes="Primary" Content="OK" /> HorizontalAlignment="Right"
<Button Name="{x:Static u:MessageBoxWindow.PART_CancelButton}" Classes="Tertiary" Content="Cancel" /> Orientation="Horizontal">
<Button
Name="{x:Static u:MessageBoxWindow.PART_YesButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="Yes" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_NoButton}"
Margin="8,0,0,0"
Classes="Danger"
Content="No" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_OKButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="OK" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_CancelButton}"
Margin="8,0,0,0"
Classes="Tertiary"
Content="Cancel" />
</StackPanel> </StackPanel>
</Grid> </Grid>
</Panel> </Panel>

View File

@@ -31,4 +31,32 @@ public static class MessageBox
return MessageBoxResult.None; return MessageBoxResult.None;
} }
} }
public static async Task<MessageBoxResult> ShowAsync(string message, string title, MessageBoxButton button)
{
var messageWindow = new MessageBoxWindow(button)
{
Content = message,
Title = title
};
var lifetime = Application.Current?.ApplicationLifetime;
if (lifetime is IClassicDesktopStyleApplicationLifetime classLifetime)
{
var main = classLifetime.MainWindow;
if (main is null)
{
messageWindow.Show();
return MessageBoxResult.None;
}
else
{
var result = await messageWindow.ShowDialog<MessageBoxResult>(main);
return result;
}
}
else
{
return MessageBoxResult.None;
}
}
} }

View File

@@ -93,39 +93,80 @@ public class MessageBoxWindow: Window
{ {
_cancelButton.Click += OnCancelButtonClick; _cancelButton.Click += OnCancelButtonClick;
} }
SetButtonVisibility();
}
private void SetButtonVisibility()
{
if (_buttonConfigs == MessageBoxButton.OK)
{
if (_closeButton != null) _closeButton.IsVisible = true;
if (_yesButton != null) _yesButton.IsVisible = false;
if (_noButton != null) _noButton.IsVisible = false;
if (_okButton != null) _okButton.IsVisible = true;
if (_cancelButton != null) _cancelButton.IsVisible = false;
}
else if (_buttonConfigs == MessageBoxButton.OKCancel)
{
if (_closeButton != null) _closeButton.IsVisible = true;
if (_yesButton != null) _yesButton.IsVisible = false;
if (_noButton != null) _noButton.IsVisible = false;
if (_okButton != null) _okButton.IsVisible = true;
if (_cancelButton != null) _cancelButton.IsVisible = true;
}
else if (_buttonConfigs == MessageBoxButton.YesNo)
{
if (_closeButton != null) _closeButton.IsVisible = false;
if (_yesButton != null) _yesButton.IsVisible = true;
if (_noButton != null) _noButton.IsVisible = true;
if (_okButton != null) _okButton.IsVisible = false;
if (_cancelButton != null) _cancelButton.IsVisible = false;
}
else if (_buttonConfigs == MessageBoxButton.YesNoCancel)
{
if (_closeButton != null) _closeButton.IsVisible = true;
if (_yesButton != null) _yesButton.IsVisible = true;
if (_noButton != null) _noButton.IsVisible = true;
if (_okButton != null) _okButton.IsVisible = false;
if (_cancelButton != null) _cancelButton.IsVisible = true;
}
} }
private void OnCloseButtonClick(object sender, RoutedEventArgs e) private void OnCloseButtonClick(object sender, RoutedEventArgs e)
{ {
this.Close(MessageBoxResult.None); if (_buttonConfigs == MessageBoxButton.OK)
{
Close(MessageBoxResult.OK);
}
Close(MessageBoxResult.Cancel);
} }
private void OnYesButtonClick(object sender, RoutedEventArgs e) private void OnYesButtonClick(object sender, RoutedEventArgs e)
{ {
this.Close(MessageBoxResult.Yes); Close(MessageBoxResult.Yes);
} }
private void OnNoButtonClick(object sender, RoutedEventArgs e) private void OnNoButtonClick(object sender, RoutedEventArgs e)
{ {
this.Close(MessageBoxResult.No); Close(MessageBoxResult.No);
} }
private void OnOKButtonClick(object sender, RoutedEventArgs e) private void OnOKButtonClick(object sender, RoutedEventArgs e)
{ {
this.Close(MessageBoxResult.OK); Close(MessageBoxResult.OK);
} }
private void OnCancelButtonClick(object sender, RoutedEventArgs e) private void OnCancelButtonClick(object sender, RoutedEventArgs e)
{ {
this.Close(MessageBoxResult.Cancel); Close(MessageBoxResult.Cancel);
} }
protected override void OnKeyUp(KeyEventArgs e) protected override void OnKeyUp(KeyEventArgs e)
{ {
base.OnKeyUp(e); base.OnKeyUp(e);
if (e.Key == Key.Escape) if (e.Key == Key.Escape && _buttonConfigs == MessageBoxButton.OK)
{ {
this.Close(MessageBoxResult.None); Close(MessageBoxResult.OK);
} }
} }
} }