MainWindow.axaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.IO;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using fis.Mac;
  7. using fis.Win;
  8. using fis.Log;
  9. using Xilium.CefGlue;
  10. using Xilium.CefGlue.Avalonia;
  11. using Xilium.CefGlue.Common.Handlers;
  12. using System.Globalization;
  13. using System.Reflection;
  14. using System.Threading;
  15. using System.Runtime.InteropServices;
  16. namespace fis
  17. {
  18. public partial class MainWindow : Window
  19. {
  20. public static SynchronizationContext? MainThreadSyncContext;
  21. private TextBlock? _title;
  22. private AvaloniaCefBrowser? _browser;
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. ExtendClientAreaToDecorationsHint = true;
  27. ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
  28. ExtendClientAreaTitleBarHeightHint = -1;
  29. MainThreadSyncContext = SynchronizationContext.Current;
  30. #if DEBUG
  31. this.AttachDevTools();
  32. #endif
  33. }
  34. private void InitializeComponent()
  35. {
  36. AvaloniaXamlLoader.Load(this);
  37. var browserContainer = this.FindControl<Border>("BrowserBorder");
  38. var appHandler = new FileSystemHostHandler(ShellConfig.Instance.AppHost, ShellConfig.Instance.AppResourcePath);
  39. var resourceHandler = new FileSystemHostHandler(ShellConfig.Instance.LocalResourceHost, ShellConfig.Instance.LocalResourcePath);
  40. IPlatformService platformService;
  41. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  42. {
  43. this.FindControl<MacosTitleBar>("MacTitleBar").IsVisible = false;
  44. _title = this.FindControl<WindowsTitleBar>("WinTitleBar").FindControl<TextBlock>("WindowsTitle");
  45. platformService = new WinService();
  46. }
  47. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  48. {
  49. this.FindControl<WindowsTitleBar>("WinTitleBar").IsVisible = false;
  50. _title = this.FindControl<MacosTitleBar>("MacTitleBar").FindControl<TextBlock>("MacosTitle");
  51. platformService = new MacService();
  52. }
  53. else
  54. {
  55. throw new NotSupportedException($"Platform {Environment.OSVersion.Platform} is not suppoorted.");
  56. }
  57. var platformHandler = new JsonRpcHandler<IPlatformService>(ShellConfig.Instance.LocalApiHost, platformService);
  58. var handler = new HostRequestHandler();
  59. handler.RegisterHostHandler(appHandler);
  60. handler.RegisterHostHandler(resourceHandler);
  61. handler.RegisterHostHandler(platformHandler);
  62. _browser = new AvaloniaCefBrowser
  63. {
  64. RequestHandler = handler
  65. };
  66. //Make cross domain work.
  67. _browser.Settings.WebSecurity = CefState.Disabled;
  68. _browser.Address = "http://" + ShellConfig.Instance.AppHost + "/index.html";
  69. _browser.ContextMenuHandler = new TextContextMenuHandler(_browser);
  70. _browser.BrowserInitialized += () =>
  71. {
  72. var scriptObj = new FisBrowserScriptObject(_title, platformService);
  73. _browser.RegisterJavascriptObject(scriptObj, "FisShellApi");
  74. };
  75. browserContainer.Child = _browser;
  76. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  77. //WindowState = WindowState.Maximized;
  78. MinWidth = 1366;
  79. MinHeight = 768;
  80. var language = CultureInfo.CurrentCulture.Name;
  81. if (_title != null)
  82. {
  83. if (language == "zh-CN")
  84. {
  85. _title.Text = "杏聆荟";
  86. }
  87. else
  88. {
  89. _title.Text = "FLYINSONO";
  90. }
  91. }
  92. var assembly = Assembly.GetExecutingAssembly();
  93. Stream? resourceStream = null;
  94. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  95. {
  96. resourceStream = assembly.GetManifestResourceStream("fis.Win.flyinsono.ico");
  97. }
  98. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  99. {
  100. resourceStream = assembly.GetManifestResourceStream("fis.Mac.flyinsono.ico");
  101. }
  102. if (resourceStream != null)
  103. {
  104. Icon = new WindowIcon(resourceStream);
  105. }
  106. #if DEBUG
  107. System.Threading.Tasks.Task.Run(() =>
  108. {
  109. Thread.Sleep(1000);
  110. _browser.ShowDeveloperTools();
  111. });
  112. #endif
  113. }
  114. }
  115. internal class FisBrowserScriptObject
  116. {
  117. IPlatformService _platformService;
  118. TextBlock _title;
  119. internal FisBrowserScriptObject(TextBlock title, IPlatformService platformService)
  120. {
  121. _title = title;
  122. _platformService = platformService;
  123. }
  124. /// <summary>
  125. /// 获取平台名
  126. /// </summary>
  127. /// <returns></returns>
  128. public string GetPlatformName() => _platformService.GetPlatformName();
  129. /// <summary>
  130. /// 写日志
  131. /// </summary>
  132. /// <param name="message"></param>
  133. /// <returns></returns>
  134. public void WriteLog(string message) => Logger.Write(message);
  135. /// <summary>
  136. /// 设置窗体标题
  137. /// </summary>
  138. /// <param name="title">标题</param>
  139. public void SetTitle(string title)
  140. {
  141. MainWindow.MainThreadSyncContext?.Send(new SendOrPostCallback((args) =>
  142. {
  143. if(_title != null)
  144. {
  145. _title.Text = title;
  146. }
  147. }), this);
  148. }
  149. public int Age { get; set; }
  150. }
  151. }