MessageWindow.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using InABox.WPF;
  16. namespace InABox.Wpf;
  17. public enum MessageWindowButtonPosition
  18. {
  19. Left,
  20. Right
  21. }
  22. public enum MessageWindowResult
  23. {
  24. None,
  25. OK,
  26. Cancel,
  27. Yes,
  28. No,
  29. Other
  30. }
  31. public class MessageWindowButton : INotifyPropertyChanged
  32. {
  33. public delegate void MessageWindowButtonDelegate(MessageWindow window, MessageWindowButton button);
  34. public MessageWindowButtonPosition Position { get; set; }
  35. private string _content;
  36. public string Content
  37. {
  38. get => _content;
  39. [MemberNotNull(nameof(_content))]
  40. set
  41. {
  42. _content = value;
  43. OnPropertyChanged();
  44. }
  45. }
  46. public MessageWindowButtonDelegate Action { get; set; }
  47. public MessageWindowButton(string content, MessageWindowButtonDelegate action, MessageWindowButtonPosition position)
  48. {
  49. Content = content;
  50. Action = action;
  51. Position = position;
  52. }
  53. public event PropertyChangedEventHandler? PropertyChanged;
  54. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  55. {
  56. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  57. }
  58. }
  59. /// <summary>
  60. /// Interaction logic for MessageWindow.xaml
  61. /// </summary>
  62. public partial class MessageWindow : Window, INotifyPropertyChanged
  63. {
  64. public ObservableCollection<MessageWindowButton> Buttons { get; private set; } = new();
  65. public IEnumerable<MessageWindowButton> LeftButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Left);
  66. public IEnumerable<MessageWindowButton> RightButtons => Buttons.Where(x => x.Position == MessageWindowButtonPosition.Right);
  67. private string _message = "";
  68. public string Message
  69. {
  70. get => _message;
  71. set
  72. {
  73. _message = value;
  74. OnPropertyChanged();
  75. }
  76. }
  77. public ImageSource? _image = null;
  78. public ImageSource? Image
  79. {
  80. get => _image;
  81. set
  82. {
  83. _image = value;
  84. OnPropertyChanged();
  85. }
  86. }
  87. private string _details = "";
  88. public string Details
  89. {
  90. get => _details;
  91. set
  92. {
  93. _details = value;
  94. OnPropertyChanged();
  95. }
  96. }
  97. public static readonly DependencyProperty ShowDetailsProperty = DependencyProperty.Register(nameof(ShowDetails), typeof(bool), typeof(MessageWindow));
  98. public bool ShowDetails
  99. {
  100. get => (bool)GetValue(ShowDetailsProperty);
  101. set => SetValue(ShowDetailsProperty, value);
  102. }
  103. public MessageWindowResult Result { get; set; } = MessageWindowResult.None;
  104. public object? OtherResult { get; set; }
  105. public MessageWindow()
  106. {
  107. InitializeComponent();
  108. Buttons.CollectionChanged += Buttons_CollectionChanged;
  109. }
  110. private void Button_Click(object sender, RoutedEventArgs e)
  111. {
  112. if (sender is not Button button || button.Tag is not MessageWindowButton winButton) return;
  113. winButton.Action(this, winButton);
  114. }
  115. private void Buttons_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  116. {
  117. OnPropertyChanged(nameof(LeftButtons));
  118. OnPropertyChanged(nameof(RightButtons));
  119. }
  120. public MessageWindow AddButton(MessageWindowButton button)
  121. {
  122. Buttons.Add(button);
  123. return this;
  124. }
  125. public MessageWindow AddOKButton(string content = "OK")
  126. {
  127. Buttons.Add(new MessageWindowButton(content, OKButton_Click, MessageWindowButtonPosition.Right));
  128. return this;
  129. }
  130. public MessageWindow AddCancelButton(string content = "Cancel")
  131. {
  132. Buttons.Add(new MessageWindowButton(content, CancelButton_Click, MessageWindowButtonPosition.Right));
  133. return this;
  134. }
  135. public MessageWindow AddYesButton(string content = "Yes")
  136. {
  137. Buttons.Add(new MessageWindowButton(content, YesButton_Click, MessageWindowButtonPosition.Right));
  138. return this;
  139. }
  140. public MessageWindow AddNoButton(string content = "No")
  141. {
  142. Buttons.Add(new MessageWindowButton(content, NoButton_Click, MessageWindowButtonPosition.Right));
  143. return this;
  144. }
  145. private void YesButton_Click(MessageWindow window, MessageWindowButton button)
  146. {
  147. Result = MessageWindowResult.Yes;
  148. Close();
  149. }
  150. private void NoButton_Click(MessageWindow window, MessageWindowButton button)
  151. {
  152. Result = MessageWindowResult.No;
  153. Close();
  154. }
  155. private void CancelButton_Click(MessageWindow window, MessageWindowButton button)
  156. {
  157. Result = MessageWindowResult.Cancel;
  158. Close();
  159. }
  160. private void OKButton_Click(MessageWindow window, MessageWindowButton button)
  161. {
  162. Result = MessageWindowResult.OK;
  163. Close();
  164. }
  165. public event PropertyChangedEventHandler? PropertyChanged;
  166. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  167. {
  168. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  169. }
  170. #region Static Constructors
  171. public static readonly BitmapImage _warning = InABox.Wpf.Resources.warning.AsBitmapImage();
  172. public static MessageWindow New()
  173. {
  174. return new MessageWindow();
  175. }
  176. public static MessageWindow NewMessage(string message, string title, ImageSource? image = null)
  177. {
  178. return new MessageWindow()
  179. .Title(title)
  180. .Message(message)
  181. .Image(image)
  182. .AddOKButton();
  183. }
  184. public static void ShowMessage(string message, string title, ImageSource? image = null)
  185. {
  186. NewMessage(message, title, image).Display();
  187. }
  188. public static MessageWindow NewOKCancel(string message, string title, ImageSource? image = null)
  189. {
  190. return new MessageWindow()
  191. .Title(title)
  192. .Message(message)
  193. .Image(image)
  194. .AddOKButton()
  195. .AddCancelButton();
  196. }
  197. public static bool ShowOKCancel(string message, string title, ImageSource? image = null)
  198. {
  199. return NewOKCancel(message, title, image)
  200. .Display()
  201. .Result == MessageWindowResult.OK;
  202. }
  203. public static MessageWindow NewYesNo(string message, string title, ImageSource? image = null)
  204. {
  205. return new MessageWindow()
  206. .Title(title)
  207. .Message(message)
  208. .Image(image)
  209. .AddYesButton()
  210. .AddNoButton();
  211. }
  212. public static bool ShowYesNo(string message, string title, ImageSource? image = null)
  213. {
  214. return NewYesNo(message, title, image)
  215. .Display()
  216. .Result == MessageWindowResult.Yes;
  217. }
  218. public static MessageWindow NewYesNoCancel(string message, string title, ImageSource? image = null)
  219. {
  220. return new MessageWindow()
  221. .Title(title)
  222. .Message(message)
  223. .Image(image)
  224. .AddYesButton()
  225. .AddNoButton()
  226. .AddCancelButton();
  227. }
  228. public static MessageWindowResult ShowYesNoCancel(string message, string title, ImageSource? image = null)
  229. {
  230. return NewYesNoCancel(message, title, image)
  231. .Display().Result;
  232. }
  233. /// <summary>
  234. /// Display a message box for an exception, giving options to view the logs.
  235. /// </summary>
  236. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  237. /// <param name="exception"></param>
  238. /// <param name="title"></param>
  239. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  240. public static MessageWindow NewError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  241. {
  242. if (shouldLog)
  243. {
  244. CoreUtils.LogException(ClientFactory.UserID, exception);
  245. }
  246. var window = new MessageWindow()
  247. .Message(message ?? exception.Message)
  248. .Title(title)
  249. .Details(CoreUtils.FormatException(exception))
  250. .Image(image ?? _warning)
  251. .AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
  252. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  253. {
  254. win.ShowDetails = !win.ShowDetails;
  255. button.Content = win.ShowDetails
  256. ? "Hide Details"
  257. : "Show Details";
  258. }, MessageWindowButtonPosition.Left);
  259. return window.AddButton(showDetailsButton)
  260. .AddOKButton();
  261. }
  262. /// <summary>
  263. /// Display a message box for a non-exception error, giving options to view the logs.
  264. /// </summary>
  265. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  266. /// <param name="details"></param>
  267. /// <param name="title"></param>
  268. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  269. public static MessageWindow NewError(string message, string? details = null, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  270. {
  271. if (shouldLog)
  272. {
  273. Logger.Send(LogType.Error, ClientFactory.UserID, details ?? message);
  274. }
  275. var window = new MessageWindow()
  276. .Message(message)
  277. .Title(title);
  278. if(details is not null)
  279. {
  280. window.Details(details);
  281. }
  282. window.Image(image ?? _warning)
  283. .AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
  284. if(details is not null)
  285. {
  286. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  287. {
  288. win.ShowDetails = !win.ShowDetails;
  289. button.Content = win.ShowDetails
  290. ? "Hide Details"
  291. : "Show Details";
  292. }, MessageWindowButtonPosition.Left);
  293. window.AddButton(showDetailsButton);
  294. }
  295. return window.AddOKButton();
  296. }
  297. public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  298. {
  299. NewError(message, exception, title, shouldLog, image).Display();
  300. }
  301. public static void ShowError(string message, string details, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  302. {
  303. NewError(message, details, title, shouldLog, image).Display();
  304. }
  305. private static void ShowLogs_Click(MessageWindow window, MessageWindowButton button)
  306. {
  307. var console = new MessageWindowConsole("Logs", Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today)));
  308. console.ShowDialog();
  309. }
  310. #endregion
  311. }
  312. public static class MessageWindowBuilder
  313. {
  314. public static MessageWindow Title(this MessageWindow window, string title)
  315. {
  316. window.Title = title;
  317. return window;
  318. }
  319. public static MessageWindow Message(this MessageWindow window, string message)
  320. {
  321. window.Message = message;
  322. return window;
  323. }
  324. public static MessageWindow Image(this MessageWindow window, ImageSource? image)
  325. {
  326. window.Image = image;
  327. return window;
  328. }
  329. public static MessageWindow Icon(this MessageWindow window, ImageSource image)
  330. {
  331. window.Icon = image;
  332. return window;
  333. }
  334. public static MessageWindow Details(this MessageWindow window, string details)
  335. {
  336. window.Details = details;
  337. return window;
  338. }
  339. public static MessageWindow Display(this MessageWindow window)
  340. {
  341. window.ShowDialog();
  342. return window;
  343. }
  344. }
  345. public class MessageWindowConsole : Console.Console
  346. {
  347. public string FileName { get; set; }
  348. public MessageWindowConsole(string description, string file) : base(description)
  349. {
  350. FileName = file;
  351. ConsoleControl.AllowLoadLogButton = false;
  352. }
  353. protected override void OnLoaded()
  354. {
  355. base.OnLoaded();
  356. if (File.Exists(FileName))
  357. {
  358. var lines = File.ReadLines(FileName);
  359. ConsoleControl.LoadLogEntries(lines);
  360. }
  361. }
  362. protected override string GetLogDirectory()
  363. {
  364. return CoreUtils.GetPath();
  365. }
  366. }