SlaveWindow.axaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.Threading;
  5. using fis.Mac;
  6. using fis.Managers;
  7. using fis.Utilities;
  8. using fis.Win;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Reflection;
  14. using System.Runtime.InteropServices;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using Xilium.CefGlue.Avalonia;
  18. namespace fis
  19. {
  20. public partial class SlaveWindow : Window
  21. {
  22. public static SynchronizationContext? MainThreadSyncContext;
  23. private TextBlock? _title;
  24. private AvaloniaCefBrowser _browser;
  25. private string _host;
  26. private WindowType _windowType;
  27. private Dictionary<string, string> _parameters;
  28. public bool ForceClose { get; set; } = false;
  29. public SlaveWindow()
  30. {
  31. ExtendClientAreaToDecorationsHint = true;
  32. ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
  33. ExtendClientAreaTitleBarHeightHint = -1;
  34. MainThreadSyncContext = SynchronizationContext.Current;
  35. _browser = BrowserManager.SlaveBrowser;
  36. #if DEBUG
  37. this.AttachDevTools();
  38. #endif
  39. Closing += SlaveWindow_Closing;
  40. }
  41. /// <summary>
  42. /// 初始化参数和组件
  43. /// </summary>
  44. /// <param name="host"></param>
  45. /// <param name="dictionary"></param>
  46. public void init(string host, Dictionary<string, string> dictionary, WindowType windowType)
  47. {
  48. _windowType = windowType;
  49. _parameters = dictionary;
  50. _host = host;
  51. InitializeComponent();
  52. }
  53. /// <summary>
  54. /// 修改页面内容
  55. /// </summary>
  56. /// <param name="host"></param>
  57. /// <param name="keyValuePairs"></param>
  58. internal async Task ChangeContentViewAsync(string host, Dictionary<string, string> keyValuePairs, WindowType windowType)
  59. {
  60. TargetMethodName targetMethodName;
  61. if (windowType == WindowType.TemplateDesigner) {
  62. //_browser.Address = host;
  63. //return;
  64. }
  65. _host = host;
  66. _parameters = keyValuePairs;
  67. var arguments = new List<string>();
  68. foreach (var p in _parameters)
  69. {
  70. arguments.Add(p.Value);
  71. }
  72. await Dispatcher.UIThread.InvokeAsync(() =>
  73. {
  74. if (WindowState == WindowState.Minimized)
  75. {
  76. WindowState = WindowState.Maximized;
  77. }
  78. if (!ShowInTaskbar)
  79. {
  80. ShowInTaskbar = true;
  81. ShowActivated = true;
  82. }
  83. Show();
  84. });
  85. Thread.Sleep(600);
  86. if (windowType == WindowType.TemplateDesigner) {
  87. targetMethodName = TargetMethodName.OpenReportDesignerPage;
  88. }
  89. else if (windowType == WindowType.Measure)
  90. {
  91. targetMethodName = TargetMethodName.OpenMeasurePage;
  92. }
  93. else {
  94. if (arguments.Count == 3) {
  95. targetMethodName = TargetMethodName.OpenReportPreviewPage;
  96. }
  97. else
  98. {
  99. targetMethodName = TargetMethodName.OpenReportEditPage;
  100. }
  101. }
  102. BrowserManager.ExecuteJS(BrowserManager.NotificationName, targetMethodName, arguments, false);
  103. }
  104. private void SlaveWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
  105. {
  106. if (!ForceClose)
  107. {
  108. e.Cancel = true;
  109. Dispatcher.UIThread.InvokeAsync(() =>
  110. {
  111. ShowActivated = false;
  112. Hide();
  113. });
  114. GC.Collect();
  115. }
  116. }
  117. private void InitializeComponent()
  118. {
  119. AvaloniaXamlLoader.Load(this);
  120. var browserContainer = this.FindControl<Border>("BrowserBorder");
  121. IPlatformService platformService;
  122. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  123. {
  124. this.FindControl<MacosTitleBar>("MacTitleBar").IsVisible = false;
  125. _title = this.FindControl<WindowsTitleBar>("WinTitleBar").FindControl<TextBlock>("WindowsTitle");
  126. platformService = new WinService();
  127. }
  128. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  129. {
  130. this.FindControl<WindowsTitleBar>("WinTitleBar").IsVisible = false;
  131. _title = this.FindControl<MacosTitleBar>("MacTitleBar").FindControl<TextBlock>("MacosTitle");
  132. platformService = new MacService();
  133. }
  134. else
  135. {
  136. throw new NotSupportedException($"Platform {Environment.OSVersion.Platform} is not suppoorted.");
  137. }
  138. var parameter = "";
  139. var index = 0;
  140. foreach (var p in _parameters)
  141. {
  142. parameter += p.Key + "=" + p.Value;
  143. if (index < _parameters.Count - 1) {
  144. parameter += "&";
  145. }
  146. index++;
  147. }
  148. //Make cross domain work.
  149. if (parameter.Length > 0)
  150. {
  151. var url = "http://" + ShellConfig.Instance.AppHost + "/index.html?page=measure&" + parameter;
  152. _browser.Address = url;
  153. }
  154. else
  155. {
  156. _browser.Address = _host;
  157. }
  158. _browser.BrowserInitialized += () =>
  159. {
  160. #if DEBUG
  161. _browser!.ShowDeveloperTools();
  162. #endif
  163. };
  164. var language = CultureInfo.CurrentCulture.Name;
  165. if (_title != null)
  166. {
  167. if (language == "zh-CN")
  168. {
  169. _title.Text = "杏聆荟";
  170. }
  171. else
  172. {
  173. _title.Text = "FLYINSONO";
  174. }
  175. var scriptObj = new FisBrowserScriptObject(_title, platformService);
  176. _browser.RegisterJavascriptObject(scriptObj, "FisShellApi");
  177. }
  178. browserContainer.Child = _browser;
  179. WindowStartupLocation = WindowStartupLocation.Manual;
  180. Position = new PixelPoint();
  181. WindowState = WindowState.Maximized;
  182. MinWidth = 1366;
  183. MinHeight = 768;
  184. var assembly = Assembly.GetExecutingAssembly();
  185. Stream? resourceStream = null;
  186. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  187. {
  188. resourceStream = assembly.GetManifestResourceStream("fis.Win.flyinsono.ico");
  189. }
  190. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  191. {
  192. resourceStream = assembly.GetManifestResourceStream("fis.Mac.flyinsono.ico");
  193. }
  194. if (resourceStream != null)
  195. {
  196. Icon = new WindowIcon(resourceStream);
  197. }
  198. if (_windowType == WindowType.Measure)
  199. {
  200. BrowserManager.MeasureWindowBrowser = _browser;
  201. }
  202. if (_windowType == WindowType.ReportEdit)
  203. {
  204. BrowserManager.ReportWindowBrowser = _browser;
  205. }
  206. }
  207. }
  208. }