feat: support multiple classes, improve unit tests.
This commit is contained in:
@@ -216,7 +216,8 @@ public static class Dialog
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(options.StyleClass))
|
||||
{
|
||||
window.Classes.Add(options.StyleClass);
|
||||
var styles = options.StyleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
window.Classes.AddRange(styles);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,7 +247,8 @@ public static class Dialog
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(options.StyleClass))
|
||||
{
|
||||
window.Classes.Add(options.StyleClass);
|
||||
var styles = options.StyleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
window.Classes.AddRange(styles);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,7 +207,8 @@ public static class OverlayDialog
|
||||
control.CanResize = options.CanResize;
|
||||
if (!string.IsNullOrWhiteSpace(options.StyleClass))
|
||||
{
|
||||
control.Classes.Add(options.StyleClass);
|
||||
var styles = options.StyleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
control.Classes.AddRange(styles);
|
||||
}
|
||||
DialogControlBase.SetCanDragMove(control, options.CanDragMove);
|
||||
}
|
||||
@@ -237,7 +238,8 @@ public static class OverlayDialog
|
||||
control.CanResize = options.CanResize;
|
||||
if (!string.IsNullOrWhiteSpace(options.StyleClass))
|
||||
{
|
||||
control.Classes.Add(options.StyleClass);
|
||||
var styles = options.StyleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
control.Classes.AddRange(styles);
|
||||
}
|
||||
DialogControlBase.SetCanDragMove(control, options.CanDragMove);
|
||||
}
|
||||
|
||||
@@ -220,7 +220,8 @@ public static class Drawer
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(options.StyleClass))
|
||||
{
|
||||
drawer.Classes.Add(options.StyleClass!);
|
||||
var styles = options.StyleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
drawer.Classes.AddRange(styles);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,7 +247,8 @@ public static class Drawer
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(options.StyleClass))
|
||||
{
|
||||
drawer.Classes.Add(options.StyleClass!);
|
||||
var styles = options.StyleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
drawer.Classes.AddRange(styles);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,8 @@ public static class MessageBox
|
||||
};
|
||||
if (!string.IsNullOrWhiteSpace(styleClass))
|
||||
{
|
||||
messageWindow.Classes.Add(styleClass!);
|
||||
var styles = styleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
messageWindow.Classes.AddRange(styles);
|
||||
}
|
||||
var lifetime = Application.Current?.ApplicationLifetime;
|
||||
if (lifetime is not IClassicDesktopStyleApplicationLifetime classLifetime) return MessageBoxResult.None;
|
||||
@@ -53,7 +54,8 @@ public static class MessageBox
|
||||
};
|
||||
if (!string.IsNullOrWhiteSpace(styleClass))
|
||||
{
|
||||
messageWindow.Classes.Add(styleClass!);
|
||||
var styles = styleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
messageWindow.Classes.AddRange(styles!);
|
||||
}
|
||||
var result = await messageWindow.ShowDialog<MessageBoxResult>(owner);
|
||||
return result;
|
||||
@@ -80,7 +82,8 @@ public static class MessageBox
|
||||
};
|
||||
if (!string.IsNullOrWhiteSpace(styleClass))
|
||||
{
|
||||
messageControl.Classes.Add(styleClass!);
|
||||
var styles = styleClass!.Split([' '], StringSplitOptions.RemoveEmptyEntries);
|
||||
messageControl.Classes.AddRange(styles!);
|
||||
}
|
||||
host.AddModalDialog(messageControl);
|
||||
var result = await messageControl.ShowAsync<MessageBoxResult>();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -10,6 +11,7 @@ internal static class OverlayDialogManager
|
||||
|
||||
public static void RegisterHost(OverlayDialogHost host, string? id, int? hash)
|
||||
{
|
||||
Debug.WriteLine("Count: "+Hosts.Count);
|
||||
Hosts.TryAdd(new HostKey(id, hash), host);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)"/>
|
||||
<PackageReference Include="Irihi.Avalonia.Shared" Version="0.2.0" />
|
||||
<PackageReference Include="Irihi.Avalonia.Shared.Contracts" Version="0.2.0" />
|
||||
<PackageReference Include="Irihi.Avalonia.Shared" Version="0.2.1" />
|
||||
<PackageReference Include="Irihi.Avalonia.Shared.Contracts" Version="0.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Headless.XUnit;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
using Ursa.Controls;
|
||||
using Xunit;
|
||||
|
||||
namespace HeadlessTest.Ursa.Controls.DialogTests.StyleClassTests;
|
||||
|
||||
@@ -16,7 +18,8 @@ public class StyleClassTests
|
||||
DataContext = vm,
|
||||
};
|
||||
ursaWindow.Show();
|
||||
vm.InvokeDialog();
|
||||
vm.InvokeDialog("Custom", ursaWindow.GetHashCode());
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
var dialog = ursaWindow.GetVisualDescendants().OfType<DefaultDialogControl>().SingleOrDefault();
|
||||
Assert.NotNull(dialog);
|
||||
Assert.Contains("Custom", dialog.Classes);
|
||||
@@ -26,5 +29,58 @@ public class StyleClassTests
|
||||
var cancelButton = dialog.GetVisualDescendants().OfType<Button>().SingleOrDefault(a=>a.Name == DefaultDialogControl.PART_CancelButton);
|
||||
Assert.NotNull(cancelButton);
|
||||
Assert.Equal("取消", cancelButton.Content);
|
||||
ursaWindow.Close();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void StyleClass_Changes_Button_Content_With_Multiple_Classes()
|
||||
{
|
||||
var vm = new TestViewModel();
|
||||
var ursaWindow = new TestWindow()
|
||||
{
|
||||
DataContext = vm,
|
||||
};
|
||||
ursaWindow.Show();
|
||||
vm.InvokeDialog("Custom Custom2", ursaWindow.GetHashCode());
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
var dialog = ursaWindow.GetVisualDescendants().OfType<DefaultDialogControl>().SingleOrDefault();
|
||||
Assert.NotNull(dialog);
|
||||
Assert.Contains("Custom", dialog.Classes);
|
||||
Assert.Contains("Custom2", dialog.Classes);
|
||||
var okButton = dialog.GetVisualDescendants().OfType<Button>().SingleOrDefault(a=>a.Name == DefaultDialogControl.PART_OKButton);
|
||||
Assert.NotNull(okButton);
|
||||
Assert.Equal("CUSTOM", okButton.Content);
|
||||
Assert.Contains("Warning", okButton.Classes);
|
||||
Assert.Contains("Small", okButton.Classes);
|
||||
var cancelButton = dialog.GetVisualDescendants().OfType<Button>().SingleOrDefault(a=>a.Name == DefaultDialogControl.PART_CancelButton);
|
||||
Assert.NotNull(cancelButton);
|
||||
Assert.Equal("取消", cancelButton.Content);
|
||||
ursaWindow.Close();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
}
|
||||
|
||||
[AvaloniaFact]
|
||||
public void StyleClass_Changes_Button_Content_With_No_StyleClass()
|
||||
{
|
||||
var vm = new TestViewModel();
|
||||
var ursaWindow = new TestWindow()
|
||||
{
|
||||
DataContext = vm,
|
||||
};
|
||||
ursaWindow.Show();
|
||||
vm.InvokeDialog(null, ursaWindow.GetHashCode());
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
var dialog = ursaWindow.GetVisualDescendants().OfType<DefaultDialogControl>().SingleOrDefault();
|
||||
Assert.NotNull(dialog);
|
||||
Assert.DoesNotContain("Custom", dialog.Classes);
|
||||
var okButton = dialog.GetVisualDescendants().OfType<Button>().SingleOrDefault(a=>a.Name == DefaultDialogControl.PART_OKButton);
|
||||
Assert.NotNull(okButton);
|
||||
Assert.Equal("确认", okButton.Content);
|
||||
var cancelButton = dialog.GetVisualDescendants().OfType<Button>().SingleOrDefault(a=>a.Name == DefaultDialogControl.PART_CancelButton);
|
||||
Assert.NotNull(cancelButton);
|
||||
Assert.Equal("取消", cancelButton.Content);
|
||||
ursaWindow.Close();
|
||||
Dispatcher.UIThread.RunJobs();
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,13 @@ namespace HeadlessTest.Ursa.Controls.DialogTests.StyleClassTests;
|
||||
|
||||
public class TestViewModel: ObservableObject
|
||||
{
|
||||
public void InvokeDialog()
|
||||
public void InvokeDialog(string? classes, int? hash)
|
||||
{
|
||||
OverlayDialog.Show<TextBlock, string>("Hello World", options: new OverlayDialogOptions()
|
||||
{
|
||||
Buttons = DialogButton.OKCancel,
|
||||
StyleClass = "Custom",
|
||||
StyleClass = classes,
|
||||
TopLevelHashCode = hash,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:u="https://irihi.tech/ursa"
|
||||
xmlns:helpers="clr-namespace:Irihi.Avalonia.Shared.Helpers;assembly=Irihi.Avalonia.Shared"
|
||||
mc:Ignorable="d" d:DesignWidth="800"
|
||||
d:DesignHeight="450"
|
||||
x:Class="HeadlessTest.Ursa.Controls.DialogTests.StyleClassTests.TestWindow"
|
||||
@@ -13,6 +14,11 @@
|
||||
<Setter Property="Content" Value="CUSTOM"/>
|
||||
</Style>
|
||||
</Style>
|
||||
<Style Selector="u|DefaultDialogControl.Custom2">
|
||||
<Style Selector="^ /template/ Button#PART_OKButton">
|
||||
<Setter Property="helpers:ClassHelper.Classes" Value="Warning Small" />
|
||||
</Style>
|
||||
</Style>
|
||||
</u:UrsaWindow.Styles>
|
||||
Welcome to Avalonia!
|
||||
|
||||
|
||||
Reference in New Issue
Block a user