You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.0 KiB

  1. // Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Threading.Tasks;
  22. using ICSharpCode.ILSpy.TextView;
  23. namespace ICSharpCode.ILSpy.ViewModels
  24. {
  25. public class TabPageModel : PaneModel
  26. {
  27. private readonly Dictionary<Language, LanguageVersion> languageVersionHistory = new Dictionary<Language, LanguageVersion>();
  28. public TabPageModel()
  29. {
  30. this.Title = Properties.Resources.NewTab;
  31. }
  32. private Language language;
  33. public Language Language {
  34. get => language;
  35. set {
  36. if (language != value)
  37. {
  38. if (language != null && language.HasLanguageVersions)
  39. {
  40. languageVersionHistory[language] = languageVersion;
  41. }
  42. language = value;
  43. RaisePropertyChanged(nameof(Language));
  44. if (language.HasLanguageVersions)
  45. {
  46. if (languageVersionHistory.TryGetValue(value, out var version))
  47. {
  48. LanguageVersion = version;
  49. }
  50. else
  51. {
  52. LanguageVersion = Language.LanguageVersions.Last();
  53. }
  54. }
  55. else
  56. {
  57. LanguageVersion = default;
  58. }
  59. }
  60. }
  61. }
  62. private LanguageVersion languageVersion;
  63. public LanguageVersion LanguageVersion {
  64. get => languageVersion;
  65. set {
  66. if (languageVersion != value)
  67. {
  68. languageVersion = value;
  69. if (language.HasLanguageVersions)
  70. {
  71. languageVersionHistory[language] = languageVersion;
  72. }
  73. RaisePropertyChanged(nameof(LanguageVersion));
  74. }
  75. }
  76. }
  77. private bool supportsLanguageSwitching = true;
  78. public bool SupportsLanguageSwitching {
  79. get => supportsLanguageSwitching;
  80. set {
  81. if (supportsLanguageSwitching != value)
  82. {
  83. supportsLanguageSwitching = value;
  84. RaisePropertyChanged(nameof(SupportsLanguageSwitching));
  85. }
  86. }
  87. }
  88. private object content;
  89. public object Content {
  90. get => content;
  91. set {
  92. if (content != value)
  93. {
  94. content = value;
  95. RaisePropertyChanged(nameof(Content));
  96. }
  97. }
  98. }
  99. public ViewState GetState()
  100. {
  101. return (Content as IHaveState)?.GetState();
  102. }
  103. }
  104. public static class TabPageModelExtensions
  105. {
  106. public static Task<T> ShowTextViewAsync<T>(this TabPageModel tabPage, Func<DecompilerTextView, Task<T>> action)
  107. {
  108. if (!(tabPage.Content is DecompilerTextView textView))
  109. {
  110. textView = new DecompilerTextView();
  111. tabPage.Content = textView;
  112. }
  113. return action(textView);
  114. }
  115. public static Task ShowTextViewAsync(this TabPageModel tabPage, Func<DecompilerTextView, Task> action)
  116. {
  117. if (!(tabPage.Content is DecompilerTextView textView))
  118. {
  119. textView = new DecompilerTextView();
  120. tabPage.Content = textView;
  121. }
  122. return action(textView);
  123. }
  124. public static void ShowTextView(this TabPageModel tabPage, Action<DecompilerTextView> action)
  125. {
  126. if (!(tabPage.Content is DecompilerTextView textView))
  127. {
  128. textView = new DecompilerTextView();
  129. tabPage.Content = textView;
  130. }
  131. action(textView);
  132. }
  133. }
  134. public interface IHaveState
  135. {
  136. ViewState GetState();
  137. }
  138. }