소스 검색

Fluent builder for message box

Kenric Nugteren 1 년 전
부모
커밋
c9d0320759
2개의 변경된 파일99개의 추가작업 그리고 45개의 파일을 삭제
  1. 2 1
      inabox.wpf/Forms/MessageWindow.xaml
  2. 97 44
      inabox.wpf/Forms/MessageWindow.xaml.cs

+ 2 - 1
inabox.wpf/Forms/MessageWindow.xaml

@@ -6,6 +6,7 @@
         xmlns:local="clr-namespace:InABox.Wpf"
         mc:Ignorable="d"
         Width="280"
+        MinWidth="280"
         SizeToContent="Height"
         x:Name="Window"
         DataContext="{Binding ElementName=Window}">
@@ -19,7 +20,7 @@
             </Button>
         </DataTemplate>
     </Window.Resources>
-    <Grid>
+    <Grid x:Name="Grid">
         <Grid.RowDefinitions>
             <RowDefinition Height="*" MinHeight="100"/>
             <RowDefinition Height="Auto"/>

+ 97 - 44
inabox.wpf/Forms/MessageWindow.xaml.cs

@@ -141,19 +141,22 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
         OnPropertyChanged(nameof(RightButtons));
     }
 
-    public void AddButton(MessageWindowButton button)
+    public MessageWindow AddButton(MessageWindowButton button)
     {
         Buttons.Add(button);
+        return this;
     }
 
-    public void AddOKButton(string content = "OK")
+    public MessageWindow AddOKButton(string content = "OK")
     {
         Buttons.Add(new MessageWindowButton(content, OKButton_Click, MessageWindowButtonPosition.Right));
+        return this;
     }
 
-    public void AddCancelButton(string content = "Cancel")
+    public MessageWindow AddCancelButton(string content = "Cancel")
     {
         Buttons.Add(new MessageWindowButton(content, CancelButton_Click, MessageWindowButtonPosition.Right));
+        return this;
     }
 
     private void CancelButton_Click(MessageWindow window, MessageWindowButton button)
@@ -179,44 +182,54 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
 
     public static readonly BitmapImage _warning = InABox.Wpf.Resources.warning.AsBitmapImage();
 
+    public static MessageWindow New()
+    {
+        return new MessageWindow();
+    }
+
+    public static MessageWindow NewMessage(string message, string title, ImageSource? image = null)
+    {
+        return new MessageWindow()
+            .Title(title)
+            .Message(message)
+            .Image(image)
+            .AddOKButton();
+    }
     public static void ShowMessage(string message, string title, ImageSource? image = null)
     {
-        var window = new MessageWindow
-        {
-            Message = message,
-            Title = title,
-            Image = image
-        };
-        window.AddOKButton();
-        window.ShowDialog();
+        NewMessage(message, title, image).Display();
     }
 
+    public static MessageWindow NewOKCancel(string message, string title, ImageSource? image = null)
+    {
+        return new MessageWindow()
+            .Title(title)
+            .Message(message)
+            .Image(image)
+            .AddOKButton()
+            .AddCancelButton();
+    }
     public static bool ShowOKCancel(string message, string title, ImageSource? image = null)
     {
-        var window = new MessageWindow
-        {
-            Message = message,
-            Title = title,
-            Image = image
-        };
-        window.AddOKButton();
-        window.AddCancelButton();
-        window.ShowDialog();
-        return window.Result == MessageWindowResult.OK;
+        return NewOKCancel(message, title, image)
+            .Display()
+            .Result == MessageWindowResult.OK;
     }
 
+    public static MessageWindow NewYesNo(string message, string title, ImageSource? image = null)
+    {
+        return new MessageWindow()
+            .Title(title)
+            .Message(message)
+            .Image(image)
+            .AddOKButton("Yes")
+            .AddCancelButton("No");
+    }
     public static bool ShowYesNo(string message, string title, ImageSource? image = null)
     {
-        var window = new MessageWindow
-        {
-            Message = message,
-            Title = title,
-            Image = image
-        };
-        window.AddOKButton("Yes");
-        window.AddCancelButton("No");
-        window.ShowDialog();
-        return window.Result == MessageWindowResult.OK;
+        return NewYesNo(message, title, image)
+            .Display()
+            .Result == MessageWindowResult.OK;
     }
 
     /// <summary>
@@ -226,22 +239,19 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
     /// <param name="exception"></param>
     /// <param name="title"></param>
     /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
-    public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
+    public static MessageWindow NewError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
     {
         if (shouldLog)
         {
             CoreUtils.LogException(ClientFactory.UserID, exception);
         }
 
-        var window = new MessageWindow
-        {
-            Message = message ?? exception.Message,
-            Title = title,
-            Details = CoreUtils.FormatException(exception),
-            Image = image ?? _warning
-        };
-
-        window.AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
+        var window = new MessageWindow()
+            .Message(message ?? exception.Message)
+            .Title(title)
+            .Details(CoreUtils.FormatException(exception))
+            .Image(image ?? _warning)
+            .AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
 
         var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
         {
@@ -250,10 +260,14 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
                 ? "Hide Details"
                 : "Show Details";
         }, MessageWindowButtonPosition.Left);
-        window.AddButton(showDetailsButton);
 
-        window.AddOKButton();
-        window.ShowDialog();
+        return window.AddButton(showDetailsButton)
+            .AddOKButton();
+    }
+
+    public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
+    {
+        NewError(message, exception, title, shouldLog, image).Display();
     }
 
     private static void ShowLogs_Click(MessageWindow window, MessageWindowButton button)
@@ -265,6 +279,45 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
     #endregion
 }
 
+public static class MessageWindowBuilder
+{
+    public static MessageWindow Title(this MessageWindow window, string title)
+    {
+        window.Title = title;
+        return window;
+    }
+
+    public static MessageWindow Message(this MessageWindow window, string message)
+    {
+        window.Message = message;
+        return window;
+    }
+
+    public static MessageWindow Image(this MessageWindow window, ImageSource? image)
+    {
+        window.Image = image;
+        return window;
+    }
+
+    public static MessageWindow Icon(this MessageWindow window, ImageSource image)
+    {
+        window.Icon = image;
+        return window;
+    }
+
+    public static MessageWindow Details(this MessageWindow window, string details)
+    {
+        window.Details = details;
+        return window;
+    }
+
+    public static MessageWindow Display(this MessageWindow window)
+    {
+        window.ShowDialog();
+        return window;
+    }
+}
+
 public class MessageWindowConsole : Console.Console
 {
     public string FileName { get; set; }