MessageWindow.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.ComponentModel;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.IO;
  13. namespace InABox.Wpf;
  14. public enum MessageWindowButtonPosition
  15. {
  16. Left,
  17. Right
  18. }
  19. public enum MessageWindowResult
  20. {
  21. None,
  22. OK,
  23. Cancel,
  24. Other
  25. }
  26. public class MessageWindowButton : INotifyPropertyChanged
  27. {
  28. public delegate void MessageWindowButtonDelegate(MessageWindow window, MessageWindowButton button);
  29. public MessageWindowButtonPosition Position { get; set; }
  30. private string _content;
  31. public string Content
  32. {
  33. get => _content;
  34. [MemberNotNull(nameof(_content))]
  35. set
  36. {
  37. _content = value;
  38. OnPropertyChanged();
  39. }
  40. }
  41. public MessageWindowButtonDelegate Action { get; set; }
  42. public MessageWindowButton(string content, MessageWindowButtonDelegate action, MessageWindowButtonPosition position)
  43. {
  44. Content = content;
  45. Action = action;
  46. Position = position;
  47. }
  48. public event PropertyChangedEventHandler? PropertyChanged;
  49. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  50. {
  51. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  52. }
  53. }
  54. /// <summary>
  55. /// Interaction logic for MessageWindow.xaml
  56. /// </summary>
  57. public partial class MessageWindow : Window, INotifyPropertyChanged
  58. {
  59. public ObservableCollection<MessageWindowButton> Buttons { get; private set; } = new();
  60. public IEnumerable<MessageWindowButton> LeftButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Left);
  61. public IEnumerable<MessageWindowButton> RightButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Right);
  62. private string _message = "";
  63. public string Message
  64. {
  65. get => _message;
  66. set
  67. {
  68. _message = value;
  69. OnPropertyChanged();
  70. }
  71. }
  72. private string _details = "";
  73. public string Details
  74. {
  75. get => _details;
  76. set
  77. {
  78. _details = value;
  79. OnPropertyChanged();
  80. }
  81. }
  82. public static readonly DependencyProperty ShowDetailsProperty = DependencyProperty.Register(nameof(ShowDetails), typeof(bool), typeof(MessageWindow));
  83. public bool ShowDetails
  84. {
  85. get => (bool)GetValue(ShowDetailsProperty);
  86. set => SetValue(ShowDetailsProperty, value);
  87. }
  88. public MessageWindowResult Result { get; set; } = MessageWindowResult.None;
  89. public object? OtherResult { get; set; }
  90. public MessageWindow()
  91. {
  92. InitializeComponent();
  93. Buttons.CollectionChanged += Buttons_CollectionChanged;
  94. }
  95. private void Button_Click(object sender, RoutedEventArgs e)
  96. {
  97. if (sender is not Button button || button.Tag is not MessageWindowButton winButton) return;
  98. winButton.Action(this, winButton);
  99. }
  100. private void Buttons_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  101. {
  102. OnPropertyChanged(nameof(LeftButtons));
  103. OnPropertyChanged(nameof(RightButtons));
  104. }
  105. public void AddButton(MessageWindowButton button)
  106. {
  107. Buttons.Add(button);
  108. }
  109. public void AddOKButton(string content = "OK")
  110. {
  111. Buttons.Add(new MessageWindowButton(content, OKButton_Click, MessageWindowButtonPosition.Right));
  112. }
  113. public void AddCancelButton(string content = "Cancel")
  114. {
  115. Buttons.Add(new MessageWindowButton(content, CancelButton_Click, MessageWindowButtonPosition.Right));
  116. }
  117. private void CancelButton_Click(MessageWindow window, MessageWindowButton button)
  118. {
  119. Result = MessageWindowResult.Cancel;
  120. Close();
  121. }
  122. private void OKButton_Click(MessageWindow window, MessageWindowButton button)
  123. {
  124. Result = MessageWindowResult.OK;
  125. Close();
  126. }
  127. public event PropertyChangedEventHandler? PropertyChanged;
  128. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  129. {
  130. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  131. }
  132. #region Static Constructors
  133. public static void ShowMessage(string message, string title)
  134. {
  135. var window = new MessageWindow
  136. {
  137. Message = message,
  138. Title = title
  139. };
  140. window.AddOKButton();
  141. window.ShowDialog();
  142. }
  143. public static bool ShowOKCancel(string message, string title)
  144. {
  145. var window = new MessageWindow
  146. {
  147. Message = message,
  148. Title = title
  149. };
  150. window.AddOKButton();
  151. window.AddCancelButton();
  152. window.ShowDialog();
  153. return window.Result == MessageWindowResult.OK;
  154. }
  155. public static bool ShowYesNo(string message, string title)
  156. {
  157. var window = new MessageWindow
  158. {
  159. Message = message,
  160. Title = title
  161. };
  162. window.AddOKButton("Yes");
  163. window.AddCancelButton("No");
  164. window.ShowDialog();
  165. return window.Result == MessageWindowResult.OK;
  166. }
  167. /// <summary>
  168. /// Display a message box for an exception, giving options to view the logs.
  169. /// </summary>
  170. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  171. /// <param name="exception"></param>
  172. /// <param name="title"></param>
  173. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  174. public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true)
  175. {
  176. if (shouldLog)
  177. {
  178. CoreUtils.LogException(ClientFactory.UserID, exception);
  179. }
  180. var window = new MessageWindow
  181. {
  182. Message = message ?? exception.Message,
  183. Title = title,
  184. Details = CoreUtils.FormatException(exception)
  185. };
  186. window.AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
  187. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  188. {
  189. win.ShowDetails = !win.ShowDetails;
  190. button.Content = win.ShowDetails
  191. ? "Hide Details"
  192. : "Show Details";
  193. }, MessageWindowButtonPosition.Left);
  194. window.AddButton(showDetailsButton);
  195. window.AddOKButton();
  196. window.ShowDialog();
  197. }
  198. private static void ShowLogs_Click(MessageWindow window, MessageWindowButton button)
  199. {
  200. var console = new MessageWindowConsole("Logs", Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today)));
  201. console.ShowDialog();
  202. }
  203. #endregion
  204. }
  205. public class MessageWindowConsole : Console.Console
  206. {
  207. public string FileName { get; set; }
  208. public MessageWindowConsole(string description, string file) : base(description)
  209. {
  210. FileName = file;
  211. ConsoleControl.AllowLoadLogButton = false;
  212. }
  213. protected override void OnLoaded()
  214. {
  215. base.OnLoaded();
  216. if (File.Exists(FileName))
  217. {
  218. var lines = File.ReadLines(FileName);
  219. ConsoleControl.LoadLogEntries(lines);
  220. }
  221. }
  222. protected override string GetLogDirectory()
  223. {
  224. return CoreUtils.GetPath();
  225. }
  226. }