1+ using Avalonia . Controls ;
2+ using Avalonia . Media ;
3+ using Avalonia . Layout ;
4+ using System . Threading . Tasks ;
5+
6+ namespace VisualPairCoding . AvaloniaUI
7+ {
8+ public static class MessageBoxHelper
9+ {
10+ private static async Task < bool > ShowDialog ( Window owner , string title , string message , bool isError )
11+ {
12+ var dialog = new Window
13+ {
14+ Title = title ,
15+ Width = 420 ,
16+ Height = 220 ,
17+ WindowStartupLocation = WindowStartupLocation . CenterOwner ,
18+ CanResize = false ,
19+ ShowInTaskbar = false ,
20+ SystemDecorations = SystemDecorations . BorderOnly ,
21+ ExtendClientAreaToDecorationsHint = true ,
22+ ExtendClientAreaTitleBarHeightHint = - 1 ,
23+ Background = new SolidColorBrush ( Color . FromRgb ( 245 , 245 , 245 ) )
24+ } ;
25+
26+ // Main container with border
27+ var border = new Border
28+ {
29+ BorderBrush = new SolidColorBrush ( isError ? Color . FromRgb ( 220 , 53 , 69 ) : Color . FromRgb ( 40 , 167 , 69 ) ) ,
30+ BorderThickness = new Avalonia . Thickness ( 0 , 3 , 0 , 0 ) ,
31+ Background = Brushes . White ,
32+ CornerRadius = new Avalonia . CornerRadius ( 0 )
33+ } ;
34+
35+ var mainPanel = new DockPanel
36+ {
37+ LastChildFill = true
38+ } ;
39+
40+ // Title bar
41+ var titlePanel = new Border
42+ {
43+ Background = new SolidColorBrush ( Color . FromRgb ( 240 , 240 , 240 ) ) ,
44+ Height = 40 ,
45+ [ DockPanel . DockProperty ] = Dock . Top
46+ } ;
47+
48+ var titleText = new TextBlock
49+ {
50+ Text = title ,
51+ FontSize = 14 ,
52+ FontWeight = FontWeight . SemiBold ,
53+ VerticalAlignment = VerticalAlignment . Center ,
54+ Margin = new Avalonia . Thickness ( 15 , 0 , 0 , 0 )
55+ } ;
56+
57+ titlePanel . Child = titleText ;
58+ mainPanel . Children . Add ( titlePanel ) ;
59+
60+ // Content area
61+ var contentPanel = new Grid
62+ {
63+ Margin = new Avalonia . Thickness ( 25 , 20 , 25 , 20 ) ,
64+ RowDefinitions = new RowDefinitions ( "*, Auto" )
65+ } ;
66+
67+ // Icon and message panel
68+ var messagePanel = new DockPanel
69+ {
70+ [ Grid . RowProperty ] = 0 ,
71+ Margin = new Avalonia . Thickness ( 0 , 0 , 0 , 20 )
72+ } ;
73+
74+ // Icon
75+ var iconBorder = new Border
76+ {
77+ Width = 48 ,
78+ Height = 48 ,
79+ CornerRadius = new Avalonia . CornerRadius ( 24 ) ,
80+ Background = new SolidColorBrush ( isError ? Color . FromRgb ( 255 , 235 , 238 ) : Color . FromRgb ( 232 , 246 , 234 ) ) ,
81+ [ DockPanel . DockProperty ] = Dock . Left ,
82+ Margin = new Avalonia . Thickness ( 0 , 0 , 15 , 0 )
83+ } ;
84+
85+ var iconText = new TextBlock
86+ {
87+ Text = isError ? "!" : "✓" ,
88+ FontSize = 24 ,
89+ FontWeight = FontWeight . Bold ,
90+ Foreground = new SolidColorBrush ( isError ? Color . FromRgb ( 220 , 53 , 69 ) : Color . FromRgb ( 40 , 167 , 69 ) ) ,
91+ HorizontalAlignment = HorizontalAlignment . Center ,
92+ VerticalAlignment = VerticalAlignment . Center
93+ } ;
94+
95+ iconBorder . Child = iconText ;
96+ messagePanel . Children . Add ( iconBorder ) ;
97+
98+ // Message text
99+ var messageText = new TextBlock
100+ {
101+ Text = message ,
102+ TextWrapping = TextWrapping . Wrap ,
103+ FontSize = 13 ,
104+ VerticalAlignment = VerticalAlignment . Center ,
105+ LineHeight = 20
106+ } ;
107+
108+ messagePanel . Children . Add ( messageText ) ;
109+ contentPanel . Children . Add ( messagePanel ) ;
110+
111+ // Button panel
112+ var buttonPanel = new StackPanel
113+ {
114+ Orientation = Orientation . Horizontal ,
115+ HorizontalAlignment = HorizontalAlignment . Right ,
116+ [ Grid . RowProperty ] = 1
117+ } ;
118+
119+ var button = new Button
120+ {
121+ Content = "OK" ,
122+ Width = 90 ,
123+ Height = 32 ,
124+ HorizontalContentAlignment = HorizontalAlignment . Center ,
125+ VerticalContentAlignment = VerticalAlignment . Center ,
126+ Background = new SolidColorBrush ( isError ? Color . FromRgb ( 220 , 53 , 69 ) : Color . FromRgb ( 40 , 167 , 69 ) ) ,
127+ Foreground = Brushes . White ,
128+ FontWeight = FontWeight . Medium ,
129+ CornerRadius = new Avalonia . CornerRadius ( 4 )
130+ } ;
131+
132+ button . Classes . Add ( "primary" ) ;
133+ button . Click += ( s , e ) => dialog . Close ( ) ;
134+
135+ buttonPanel . Children . Add ( button ) ;
136+ contentPanel . Children . Add ( buttonPanel ) ;
137+
138+ mainPanel . Children . Add ( contentPanel ) ;
139+ border . Child = mainPanel ;
140+ dialog . Content = border ;
141+
142+ await dialog . ShowDialog ( owner ) ;
143+ return true ;
144+ }
145+
146+ public static async Task < bool > ShowError ( Window owner , string title , string message )
147+ {
148+ return await ShowDialog ( owner , title , message , true ) ;
149+ }
150+
151+ public static async Task < bool > ShowInfo ( Window owner , string title , string message )
152+ {
153+ return await ShowDialog ( owner , title , message , false ) ;
154+ }
155+ }
156+ }
0 commit comments