/main/src/addins/MonoDevelop.DotNetCore/MonoDevelop.DotNetCore.Gui/DotNetCoreSdkLocationWidget.UI.cs

https://github.com/directhex/monodevelop · C# · 246 lines · 171 code · 41 blank · 34 comment · 9 complexity · 1d857743187c524a28aeac0ed9911da9 MD5 · raw file

  1. //
  2. // DotNetCoreSdkLocationWidget.UI.cs
  3. //
  4. // Author:
  5. // Matt Ward <matt.ward@microsoft.com>
  6. //
  7. // Copyright (c) 2018 Microsoft
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using MonoDevelop.Components.AtkCocoaHelper;
  30. using MonoDevelop.Components.Extensions;
  31. using MonoDevelop.Core;
  32. using Xwt;
  33. namespace MonoDevelop.DotNetCore.Gui
  34. {
  35. partial class DotNetCoreSdkLocationWidget : Widget
  36. {
  37. CustomFileSelector locationFileSelector;
  38. Label commandLineFoundLabel;
  39. ImageView commandLineFoundIcon;
  40. Label sdkFoundLabel;
  41. ImageView sdkFoundIcon;
  42. ListBox sdkVersionsListBox;
  43. Label runtimeFoundLabel;
  44. ImageView runtimeFoundIcon;
  45. ListBox runtimeVersionsListBox;
  46. void Build ()
  47. {
  48. var mainVBox = new VBox ();
  49. mainVBox.Spacing = 12;
  50. // .NET Core command line section.
  51. var titleLabel = new Label ();
  52. titleLabel.Markup = GetBoldMarkup (GettextCatalog.GetString (".NET Core Command Line"));
  53. mainVBox.PackStart (titleLabel);
  54. var commandLineVBox = new VBox ();
  55. commandLineVBox.Spacing = 6;
  56. commandLineVBox.MarginLeft = 24;
  57. mainVBox.PackStart (commandLineVBox);
  58. var commandLineFoundHBox = new HBox ();
  59. commandLineFoundHBox.Spacing = 6;
  60. commandLineVBox.PackStart (commandLineFoundHBox, false, false);
  61. commandLineFoundIcon = new ImageView ();
  62. commandLineFoundHBox.PackStart (commandLineFoundIcon, false, false);
  63. commandLineFoundLabel = new Label ();
  64. commandLineFoundHBox.PackStart (commandLineFoundLabel, true, true);
  65. var locationBox = new HBox ();
  66. locationBox.Spacing = 6;
  67. commandLineVBox.PackStart (locationBox, false, false);
  68. var locationLabel = new Label ();
  69. locationLabel.Text = GettextCatalog.GetString ("Location:");
  70. locationBox.PackStart (locationLabel, false, false);
  71. locationFileSelector = new CustomFileSelector ();
  72. locationBox.PackStart (locationFileSelector, true, true);
  73. // .NET Core SDK section.
  74. var sdkVersionsTitleLabel = new Label ();
  75. sdkVersionsTitleLabel.Markup = GetBoldMarkup (GettextCatalog.GetString (".NET Core SDK"));
  76. mainVBox.PackStart (sdkVersionsTitleLabel);
  77. var sdkVersionsVBox = new VBox ();
  78. sdkVersionsVBox.Spacing = 6;
  79. sdkVersionsVBox.MarginLeft = 24;
  80. mainVBox.PackStart (sdkVersionsVBox, true, true);
  81. var sdkFoundHBox = new HBox ();
  82. sdkFoundHBox.Spacing = 6;
  83. sdkFoundHBox.MarginBottom = 6;
  84. sdkVersionsVBox.PackStart (sdkFoundHBox, false, false);
  85. sdkFoundIcon = new ImageView ();
  86. sdkFoundHBox.PackStart (sdkFoundIcon, false, false);
  87. sdkFoundLabel = new Label ();
  88. sdkFoundHBox.PackStart (sdkFoundLabel, false, false);
  89. sdkVersionsListBox = new ListBox ();
  90. sdkVersionsVBox.PackStart (sdkVersionsListBox, true, true);
  91. // .NET Core runtime section
  92. var runtimeVersionsTitleLabel = new Label ();
  93. runtimeVersionsTitleLabel.Markup = GetBoldMarkup (GettextCatalog.GetString (".NET Core Runtime"));
  94. mainVBox.PackStart (runtimeVersionsTitleLabel);
  95. var runtimeVersionsVBox = new VBox ();
  96. runtimeVersionsVBox.Spacing = 6;
  97. runtimeVersionsVBox.MarginLeft = 24;
  98. mainVBox.PackStart (runtimeVersionsVBox, true, true);
  99. var runtimeFoundHBox = new HBox ();
  100. runtimeFoundHBox.Spacing = 6;
  101. runtimeFoundHBox.MarginBottom = 6;
  102. runtimeVersionsVBox.PackStart (runtimeFoundHBox, false, false);
  103. runtimeFoundIcon = new ImageView ();
  104. runtimeFoundHBox.PackStart (runtimeFoundIcon, false, false);
  105. runtimeFoundLabel = new Label ();
  106. runtimeFoundHBox.PackStart (runtimeFoundLabel, false, false);
  107. runtimeVersionsListBox = new ListBox ();
  108. runtimeVersionsVBox.PackStart (runtimeVersionsListBox, true, true);
  109. Content = mainVBox;
  110. }
  111. static string GetBoldMarkup (string text)
  112. {
  113. return "<b>" + GLib.Markup.EscapeText (text) + "</b>";
  114. }
  115. void UpdateSdkIconAccessibility (bool found)
  116. {
  117. sdkFoundIcon.SetCommonAccessibilityAttributes (
  118. "DotNetCoreSdkFoundImage",
  119. found ? GettextCatalog.GetString ("A Tick") : GettextCatalog.GetString ("A Cross"),
  120. found ? GettextCatalog.GetString ("The .NET Core SDK was found") : GettextCatalog.GetString ("The .NET Core SDK was not found"));
  121. }
  122. void UpdateCommandLineIconAccessibility (bool found)
  123. {
  124. sdkFoundIcon.SetCommonAccessibilityAttributes (
  125. "DotNetCoreCommandLineFoundImage",
  126. found ? GettextCatalog.GetString ("A Tick") : GettextCatalog.GetString ("A Cross"),
  127. found ? GettextCatalog.GetString ("The .NET Core command line was found") : GettextCatalog.GetString ("The .NET Core command line was not found"));
  128. }
  129. void UpdateRuntimeIconAccessibility (bool found)
  130. {
  131. runtimeFoundIcon.SetCommonAccessibilityAttributes (
  132. "DotNetCoreRuntimeFoundImage",
  133. found ? GettextCatalog.GetString ("A Tick") : GettextCatalog.GetString ("A Cross"),
  134. found ? GettextCatalog.GetString ("A .NET Core runtime was found") : GettextCatalog.GetString ("A .NET Core runtime was not found"));
  135. }
  136. /// <summary>
  137. /// This is slightly different from the standard FileSelector. When the select file
  138. /// dialog is cancelled the current directory is not remembered. This keeps the
  139. /// behaviour consistent with the other SDK pages that use the FileSelector from
  140. /// MonoDevelop.Components.
  141. /// </summary>
  142. class CustomFileSelector : Widget
  143. {
  144. TextEntry entry;
  145. FileDialog dialog;
  146. string currentFolder;
  147. string title;
  148. public CustomFileSelector ()
  149. {
  150. var box = new HBox ();
  151. entry = new TextEntry ();
  152. entry.Changed += EntryChanged;
  153. box.PackStart (entry, true);
  154. var browseButton = new Button ("…");
  155. box.PackStart (browseButton);
  156. browseButton.Clicked += BrowseButtonClicked;
  157. Content = box;
  158. }
  159. public string CurrentFolder {
  160. get {
  161. return dialog != null ? dialog.CurrentFolder : currentFolder;
  162. }
  163. set {
  164. if (dialog != null)
  165. dialog.CurrentFolder = value;
  166. currentFolder = value;
  167. }
  168. }
  169. public string FileName {
  170. get { return dialog != null ? dialog.FileName : entry.Text; }
  171. set { entry.Text = value; }
  172. }
  173. public string Title {
  174. get {
  175. return title;
  176. }
  177. set {
  178. title = value;
  179. if (dialog != null)
  180. dialog.Title = value;
  181. }
  182. }
  183. public event EventHandler FileChanged;
  184. void EntryChanged (object sender, EventArgs e)
  185. {
  186. FileChanged?.Invoke (sender, e);
  187. }
  188. void BrowseButtonClicked (object sender, EventArgs e)
  189. {
  190. dialog = new OpenFileDialog ();
  191. try {
  192. if (!string.IsNullOrEmpty (currentFolder))
  193. dialog.CurrentFolder = currentFolder;
  194. if (!string.IsNullOrEmpty (title))
  195. dialog.Title = title;
  196. if (dialog.Run (ParentWindow))
  197. FileName = dialog.FileName;
  198. } finally {
  199. dialog.Dispose ();
  200. dialog = null;
  201. }
  202. }
  203. }
  204. }
  205. }