PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Studio/Features/Input/AskUser.cs

http://github.com/ayende/ravendb
C# | 203 lines | 161 code | 42 blank | 0 comment | 14 complexity | 2feceb187a054155e6883dc51a6517bb MD5 | raw file
Possible License(s): GPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Apache-2.0, BSD-3-Clause, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace Raven.Studio.Features.Input
  7. {
  8. public static class AskUser
  9. {
  10. public static Task<T> ShowAsync<T>(this T window) where T : ChildWindow
  11. {
  12. var tcs = new TaskCompletionSource<T>();
  13. window.Closed += (sender, args) =>
  14. {
  15. if (window.DialogResult == true)
  16. tcs.SetResult(window);
  17. else
  18. tcs.SetCanceled();
  19. };
  20. window.Show();
  21. return tcs.Task;
  22. }
  23. public static Task<string> QuestionAsync(string title, string question)
  24. {
  25. var dataContext = new InputModel
  26. {
  27. Title = title,
  28. Question = question
  29. };
  30. var inputWindow = new InputWindow
  31. {
  32. DataContext = dataContext
  33. };
  34. var tcs = new TaskCompletionSource<string>();
  35. inputWindow.Closed += (sender, args) =>
  36. {
  37. if (inputWindow.DialogResult == true)
  38. tcs.SetResult(dataContext.Answer);
  39. else
  40. tcs.SetCanceled();
  41. };
  42. inputWindow.Show();
  43. return tcs.Task;
  44. }
  45. public static Task<string> QuestionWithSuggestionAsync(string title, string question, Func<string, Task<IList<object>>> provideSuggestions)
  46. {
  47. var dataContext = new InputModelWithSuggestion(provideSuggestions)
  48. {
  49. Title = title,
  50. Question = question
  51. };
  52. var inputWindow = new InputWindowWithSuggestion
  53. {
  54. DataContext = dataContext
  55. };
  56. var tcs = new TaskCompletionSource<string>();
  57. inputWindow.Closed += (sender, args) =>
  58. {
  59. if (inputWindow.DialogResult == true)
  60. tcs.SetResult(dataContext.Answer);
  61. else
  62. tcs.SetCanceled();
  63. };
  64. inputWindow.Show();
  65. return tcs.Task;
  66. }
  67. public static Task<string> SelectItem(string title, string question, Func<Task<IList<string>>> provideList)
  68. {
  69. var dataContext = new SelectModel(provideList)
  70. {
  71. Title = title,
  72. Question = question
  73. };
  74. var inputWindow = new SelectWindow
  75. {
  76. DataContext = dataContext
  77. };
  78. var tcs = new TaskCompletionSource<string>();
  79. inputWindow.Closed += (sender, args) =>
  80. {
  81. if (inputWindow.DialogResult == true)
  82. tcs.SetResult(dataContext.Answer);
  83. else
  84. tcs.SetCanceled();
  85. };
  86. inputWindow.Show();
  87. return tcs.Task;
  88. }
  89. public static Task<bool> ConfirmationAsync(string title, string question)
  90. {
  91. var dataContext = new ConfirmModel
  92. {
  93. Title = title,
  94. Question = question
  95. };
  96. var inputWindow = new ConfirmWindow
  97. {
  98. DataContext = dataContext
  99. };
  100. var tcs = new TaskCompletionSource<bool>();
  101. inputWindow.Closed += (sender, args) =>
  102. {
  103. if (inputWindow.DialogResult != null)
  104. tcs.SetResult(inputWindow.DialogResult.Value);
  105. else
  106. tcs.SetCanceled();
  107. };
  108. inputWindow.Show();
  109. return tcs.Task;
  110. }
  111. public static void ConfirmationWithEvent(string title, string question, Action onOkay)
  112. {
  113. var dataContext = new ConfirmModel
  114. {
  115. Title = title,
  116. Question = question
  117. };
  118. var inputWindow = new ConfirmWindow
  119. {
  120. DataContext = dataContext
  121. };
  122. inputWindow.Closed += (sender, args) =>
  123. {
  124. if (inputWindow.DialogResult == true)
  125. onOkay();
  126. };
  127. inputWindow.Show();
  128. }
  129. public static Task<T> ConfirmationWithContinuation<T>(string title, string question, Func<Task<T>> onOkay, Func<Task<T>> onCancelled)
  130. {
  131. var dataContext = new ConfirmModel
  132. {
  133. Title = title,
  134. Question = question
  135. };
  136. var inputWindow = new ConfirmWindow
  137. {
  138. DataContext = dataContext
  139. };
  140. var tcs = new TaskCompletionSource<T>();
  141. inputWindow.Closed += async (sender, args) =>
  142. {
  143. try
  144. {
  145. var result = inputWindow.DialogResult == true ? await onOkay() : await onCancelled();
  146. tcs.SetResult(result);
  147. }
  148. catch (Exception ex)
  149. {
  150. tcs.SetException(ex);
  151. }
  152. };
  153. inputWindow.Show();
  154. return tcs.Task;
  155. }
  156. public static bool Confirmation(string title, string question)
  157. {
  158. return MessageBox.Show(question, title, MessageBoxButton.OKCancel) == MessageBoxResult.OK;
  159. }
  160. }
  161. }