MessageWindow.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 BitmapImage WarningImage => _warning;
  173. public static MessageWindow New()
  174. {
  175. return new MessageWindow();
  176. }
  177. public static MessageWindow NewMessage(string message, string title, ImageSource? image = null)
  178. {
  179. return new MessageWindow()
  180. .Title(title)
  181. .Message(message)
  182. .Image(image)
  183. .AddOKButton();
  184. }
  185. public static void ShowMessage(string message, string title, ImageSource? image = null)
  186. {
  187. NewMessage(message, title, image).Display();
  188. }
  189. public static MessageWindow NewOKCancel(string message, string title, ImageSource? image = null)
  190. {
  191. return new MessageWindow()
  192. .Title(title)
  193. .Message(message)
  194. .Image(image)
  195. .AddOKButton()
  196. .AddCancelButton();
  197. }
  198. public static bool ShowOKCancel(string message, string title, ImageSource? image = null)
  199. {
  200. return NewOKCancel(message, title, image)
  201. .Display()
  202. .Result == MessageWindowResult.OK;
  203. }
  204. public static MessageWindow NewYesNo(string message, string title, ImageSource? image = null)
  205. {
  206. return new MessageWindow()
  207. .Title(title)
  208. .Message(message)
  209. .Image(image)
  210. .AddYesButton()
  211. .AddNoButton();
  212. }
  213. public static bool ShowYesNo(string message, string title, ImageSource? image = null)
  214. {
  215. return NewYesNo(message, title, image)
  216. .Display()
  217. .Result == MessageWindowResult.Yes;
  218. }
  219. public static MessageWindow NewYesNoCancel(string message, string title, ImageSource? image = null)
  220. {
  221. return new MessageWindow()
  222. .Title(title)
  223. .Message(message)
  224. .Image(image)
  225. .AddYesButton()
  226. .AddNoButton()
  227. .AddCancelButton();
  228. }
  229. public static MessageWindowResult ShowYesNoCancel(string message, string title, ImageSource? image = null)
  230. {
  231. return NewYesNoCancel(message, title, image)
  232. .Display().Result;
  233. }
  234. /// <summary>
  235. /// Display a message box for an exception, giving options to view the logs.
  236. /// </summary>
  237. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  238. /// <param name="exception"></param>
  239. /// <param name="title"></param>
  240. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  241. public static MessageWindow NewError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  242. {
  243. if (shouldLog)
  244. {
  245. CoreUtils.LogException(ClientFactory.UserID, exception);
  246. }
  247. var window = new MessageWindow()
  248. .Message(message ?? exception.Message)
  249. .Title(title)
  250. .Details(CoreUtils.FormatException(exception))
  251. .Image(image ?? _warning)
  252. .AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
  253. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  254. {
  255. win.ShowDetails = !win.ShowDetails;
  256. button.Content = win.ShowDetails
  257. ? "Hide Details"
  258. : "Show Details";
  259. }, MessageWindowButtonPosition.Left);
  260. return window.AddButton(showDetailsButton)
  261. .AddOKButton();
  262. }
  263. /// <summary>
  264. /// Display a message box for a non-exception error, giving options to view the logs.
  265. /// </summary>
  266. /// <param name="message">The message to display. Set to <see langword="null"/> to default to the exception message.</param>
  267. /// <param name="details"></param>
  268. /// <param name="title"></param>
  269. /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
  270. public static MessageWindow NewError(string message, string? details = null, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  271. {
  272. if (shouldLog)
  273. {
  274. Logger.Send(LogType.Error, ClientFactory.UserID, details ?? message);
  275. }
  276. var window = new MessageWindow()
  277. .Message(message)
  278. .Title(title);
  279. if(details is not null)
  280. {
  281. window.Details(details);
  282. }
  283. window.Image(image ?? _warning)
  284. .AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));
  285. if(details is not null)
  286. {
  287. var showDetailsButton = new MessageWindowButton("Show Details", (win, button) =>
  288. {
  289. win.ShowDetails = !win.ShowDetails;
  290. button.Content = win.ShowDetails
  291. ? "Hide Details"
  292. : "Show Details";
  293. }, MessageWindowButtonPosition.Left);
  294. window.AddButton(showDetailsButton);
  295. }
  296. return window.AddOKButton();
  297. }
  298. public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  299. {
  300. NewError(message, exception, title, shouldLog, image).Display();
  301. }
  302. public static void ShowError(string message, string details, string title = "Error", bool shouldLog = true, ImageSource? image = null)
  303. {
  304. NewError(message, details, title, shouldLog, image).Display();
  305. }
  306. private static void ShowLogs_Click(MessageWindow window, MessageWindowButton button)
  307. {
  308. var console = new MessageWindowConsole("Logs", Path.Combine(CoreUtils.GetPath(), string.Format("{0:yyyy-MM-dd}.log", DateTime.Today)));
  309. console.ShowDialog();
  310. }
  311. #endregion
  312. }
  313. public static class MessageWindowBuilder
  314. {
  315. public static MessageWindow Title(this MessageWindow window, string title)
  316. {
  317. window.Title = title;
  318. return window;
  319. }
  320. public static MessageWindow Message(this MessageWindow window, string message)
  321. {
  322. window.Message = message;
  323. return window;
  324. }
  325. public static MessageWindow Image(this MessageWindow window, ImageSource? image)
  326. {
  327. window.Image = image;
  328. return window;
  329. }
  330. public static MessageWindow Icon(this MessageWindow window, ImageSource image)
  331. {
  332. window.Icon = image;
  333. return window;
  334. }
  335. public static MessageWindow Details(this MessageWindow window, string details)
  336. {
  337. window.Details = details;
  338. return window;
  339. }
  340. public static MessageWindow Display(this MessageWindow window)
  341. {
  342. window.ShowDialog();
  343. return window;
  344. }
  345. }
  346. public class MessageWindowConsole : Console.Console
  347. {
  348. public string FileName { get; set; }
  349. public MessageWindowConsole(string description, string file) : base(description)
  350. {
  351. FileName = file;
  352. ConsoleControl.AllowLoadLogButton = false;
  353. }
  354. protected override void OnLoaded()
  355. {
  356. base.OnLoaded();
  357. if (File.Exists(FileName))
  358. {
  359. var lines = File.ReadLines(FileName);
  360. ConsoleControl.LoadLogEntries(lines);
  361. }
  362. }
  363. protected override string GetLogDirectory()
  364. {
  365. return CoreUtils.GetPath();
  366. }
  367. }