/source/Playnite.DesktopApp/Windows/MessageBoxWindow.xaml.cs

https://github.com/JosefNemec/Playnite · C# · 360 lines · 318 code · 39 blank · 3 comment · 33 complexity · e312918392a70b477f0cad3c9ce09542 MD5 · raw file

  1. using Playnite.SDK;
  2. using Playnite.Controls;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using Playnite.Windows;
  18. namespace Playnite.DesktopApp.Windows
  19. {
  20. /// <summary>
  21. /// Interaction logic for MessageBoxWindow.xaml
  22. /// </summary>
  23. public partial class MessageBoxWindow : WindowBase
  24. {
  25. private MessageBoxResult result;
  26. private object resultCustom;
  27. private string text = string.Empty;
  28. public string Text
  29. {
  30. get => text;
  31. set
  32. {
  33. text = value;
  34. OnPropertyChanged(nameof(Text));
  35. }
  36. }
  37. private string caption = string.Empty;
  38. public string Caption
  39. {
  40. get => caption;
  41. set
  42. {
  43. caption = value;
  44. OnPropertyChanged(nameof(Caption));
  45. }
  46. }
  47. private bool showOKButton = false;
  48. public bool ShowOKButton
  49. {
  50. get => showOKButton;
  51. set
  52. {
  53. showOKButton = value;
  54. OnPropertyChanged(nameof(ShowOKButton));
  55. }
  56. }
  57. private bool showYesButton = false;
  58. public bool ShowYesButton
  59. {
  60. get => showYesButton;
  61. set
  62. {
  63. showYesButton = value;
  64. OnPropertyChanged(nameof(ShowYesButton));
  65. }
  66. }
  67. private bool showNoButton = false;
  68. public bool ShowNoButton
  69. {
  70. get => showNoButton;
  71. set
  72. {
  73. showNoButton = value;
  74. OnPropertyChanged(nameof(ShowNoButton));
  75. }
  76. }
  77. private bool showCancelButton = false;
  78. public bool ShowCancelButton
  79. {
  80. get => showCancelButton;
  81. set
  82. {
  83. showCancelButton = value;
  84. OnPropertyChanged(nameof(ShowCancelButton));
  85. }
  86. }
  87. private bool showInputField = false;
  88. public bool ShowInputField
  89. {
  90. get => showInputField;
  91. set
  92. {
  93. showInputField = value;
  94. OnPropertyChanged(nameof(ShowInputField));
  95. }
  96. }
  97. private string inputText = string.Empty;
  98. public string InputText
  99. {
  100. get => inputText;
  101. set
  102. {
  103. inputText = value;
  104. OnPropertyChanged(nameof(InputText));
  105. }
  106. }
  107. private MessageBoxImage displayIcon;
  108. public MessageBoxImage DisplayIcon
  109. {
  110. get => displayIcon;
  111. set
  112. {
  113. displayIcon = value;
  114. OnPropertyChanged(nameof(DisplayIcon));
  115. }
  116. }
  117. private bool isTextReadOnly = false;
  118. public bool IsTextReadOnly
  119. {
  120. get => isTextReadOnly;
  121. set
  122. {
  123. isTextReadOnly = value;
  124. OnPropertyChanged(nameof(IsTextReadOnly));
  125. }
  126. }
  127. public MessageBoxWindow() : base()
  128. {
  129. InitializeComponent();
  130. }
  131. public void ShowInputReadOnly(
  132. Window owner,
  133. string messageBoxText,
  134. string caption,
  135. string inputText)
  136. {
  137. if (owner == null || owner == this)
  138. {
  139. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  140. }
  141. if (this != owner)
  142. {
  143. Owner = owner;
  144. }
  145. WindowManager.NotifyChildOwnershipChanges();
  146. TextInputText.Focus();
  147. SetStrings(messageBoxText, caption);
  148. ShowInputField = true;
  149. ShowOKButton = true;
  150. InputText = inputText ?? string.Empty;
  151. IsTextReadOnly = true;
  152. ShowDialog();
  153. WindowManager.NotifyChildOwnershipChanges();
  154. }
  155. public StringSelectionDialogResult ShowInput(
  156. Window owner,
  157. string messageBoxText,
  158. string caption,
  159. string defaultInput)
  160. {
  161. if (owner == null || owner == this)
  162. {
  163. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  164. }
  165. if (this != owner)
  166. {
  167. Owner = owner;
  168. }
  169. WindowManager.NotifyChildOwnershipChanges();
  170. TextInputText.Focus();
  171. SetStrings(messageBoxText, caption);
  172. ShowInputField = true;
  173. ShowOKButton = true;
  174. ShowCancelButton = true;
  175. InputText = defaultInput ?? string.Empty;
  176. ShowDialog();
  177. WindowManager.NotifyChildOwnershipChanges();
  178. if (result == MessageBoxResult.Cancel)
  179. {
  180. return new StringSelectionDialogResult(false, InputText);
  181. }
  182. else
  183. {
  184. return new StringSelectionDialogResult(true, InputText);
  185. }
  186. }
  187. public MessageBoxResult Show(
  188. Window owner,
  189. string messageBoxText,
  190. string caption,
  191. MessageBoxButton button,
  192. MessageBoxImage icon,
  193. MessageBoxResult defaultResult,
  194. MessageBoxOptions options)
  195. {
  196. if (owner == null || owner == this)
  197. {
  198. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  199. }
  200. if (this != owner)
  201. {
  202. Owner = owner;
  203. }
  204. WindowManager.NotifyChildOwnershipChanges();
  205. result = defaultResult;
  206. SetStrings(messageBoxText, caption);
  207. DisplayIcon = icon;
  208. switch (button)
  209. {
  210. case MessageBoxButton.OK:
  211. ShowOKButton = true;
  212. break;
  213. case MessageBoxButton.OKCancel:
  214. ShowOKButton = true;
  215. ShowCancelButton = true;
  216. break;
  217. case MessageBoxButton.YesNoCancel:
  218. ShowYesButton = true;
  219. ShowNoButton = true;
  220. ShowCancelButton = true;
  221. break;
  222. case MessageBoxButton.YesNo:
  223. ShowYesButton = true;
  224. ShowNoButton = true;
  225. break;
  226. default:
  227. ShowOKButton = true;
  228. break;
  229. }
  230. if (ShowOKButton)
  231. {
  232. ButtonOK.Focus();
  233. }
  234. ShowDialog();
  235. WindowManager.NotifyChildOwnershipChanges();
  236. return result;
  237. }
  238. public object ShowCustom(
  239. Window owner,
  240. string messageBoxText,
  241. string caption,
  242. MessageBoxImage icon,
  243. List<object> options,
  244. List<string> optionsTitles)
  245. {
  246. if (owner == null || owner == this)
  247. {
  248. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  249. }
  250. if (this != owner)
  251. {
  252. Owner = owner;
  253. }
  254. WindowManager.NotifyChildOwnershipChanges();
  255. SetStrings(messageBoxText, caption);
  256. DisplayIcon = icon;
  257. ShowOKButton = false;
  258. ShowYesButton = false;
  259. ShowNoButton = false;
  260. ShowCancelButton = false;
  261. ShowInputField = false;
  262. for (int i = 0; i < options.Count; i++)
  263. {
  264. var option = options[i];
  265. var title = optionsTitles[i];
  266. var button = new Button();
  267. button.Content = title;
  268. button.Style = ResourceProvider.GetResource("BottomButton") as Style;
  269. button.Tag = option;
  270. button.Click += (s, __) =>
  271. {
  272. resultCustom = (s as Button).Tag;
  273. Close();
  274. };
  275. StackButtons.Children.Add(button);
  276. }
  277. ShowDialog();
  278. WindowManager.NotifyChildOwnershipChanges();
  279. return resultCustom;
  280. }
  281. private void SetStrings(string messageText, string messageCaption)
  282. {
  283. if (messageText?.StartsWith("LOC") == true)
  284. {
  285. Text = ResourceProvider.GetString(messageText);
  286. }
  287. else
  288. {
  289. Text = messageText;
  290. }
  291. if (messageCaption?.StartsWith("LOC") == true)
  292. {
  293. Caption = ResourceProvider.GetString(messageCaption);
  294. }
  295. else
  296. {
  297. Caption = messageCaption;
  298. }
  299. }
  300. private void ButtonOK_Click(object sender, RoutedEventArgs e)
  301. {
  302. result = MessageBoxResult.OK;
  303. Close();
  304. }
  305. private void ButtonYes_Click(object sender, RoutedEventArgs e)
  306. {
  307. result = MessageBoxResult.Yes;
  308. Close();
  309. }
  310. private void ButtonNo_Click(object sender, RoutedEventArgs e)
  311. {
  312. result = MessageBoxResult.No;
  313. Close();
  314. }
  315. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  316. {
  317. result = MessageBoxResult.Cancel;
  318. Close();
  319. }
  320. }
  321. }