123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System;
- using System.IO;
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Markup.Xaml;
- using fis.Mac;
- using fis.Win;
- using fis.Log;
- using Xilium.CefGlue;
- using Xilium.CefGlue.Avalonia;
- using Xilium.CefGlue.Common.Handlers;
- using System.Globalization;
- using System.Reflection;
- using System.Threading;
- using System.Runtime.InteropServices;
- namespace fis
- {
- public partial class MainWindow : Window
- {
- public static SynchronizationContext? MainThreadSyncContext;
- private TextBlock? _title;
- private AvaloniaCefBrowser? _browser;
- public MainWindow()
- {
- InitializeComponent();
- ExtendClientAreaToDecorationsHint = true;
- ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
- ExtendClientAreaTitleBarHeightHint = -1;
- MainThreadSyncContext = SynchronizationContext.Current;
- #if DEBUG
- this.AttachDevTools();
- #endif
- }
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- var browserContainer = this.FindControl<Border>("BrowserBorder");
- var appHandler = new FileSystemHostHandler(ShellConfig.Instance.AppHost, ShellConfig.Instance.AppResourcePath);
- var resourceHandler = new FileSystemHostHandler(ShellConfig.Instance.LocalResourceHost, ShellConfig.Instance.LocalResourcePath);
- IPlatformService platformService;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- this.FindControl<MacosTitleBar>("MacTitleBar").IsVisible = false;
- _title = this.FindControl<WindowsTitleBar>("WinTitleBar").FindControl<TextBlock>("WindowsTitle");
- platformService = new WinService();
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- this.FindControl<WindowsTitleBar>("WinTitleBar").IsVisible = false;
- _title = this.FindControl<MacosTitleBar>("MacTitleBar").FindControl<TextBlock>("MacosTitle");
- platformService = new MacService();
- }
- else
- {
- throw new NotSupportedException($"Platform {Environment.OSVersion.Platform} is not suppoorted.");
- }
- var platformHandler = new JsonRpcHandler<IPlatformService>(ShellConfig.Instance.LocalApiHost, platformService);
- var handler = new HostRequestHandler();
- handler.RegisterHostHandler(appHandler);
- handler.RegisterHostHandler(resourceHandler);
- handler.RegisterHostHandler(platformHandler);
- _browser = new AvaloniaCefBrowser
- {
- RequestHandler = handler
- };
- //Make cross domain work.
- _browser.Settings.WebSecurity = CefState.Disabled;
- _browser.Address = "http://" + ShellConfig.Instance.AppHost + "/index.html";
- _browser.ContextMenuHandler = new TextContextMenuHandler(_browser);
- _browser.BrowserInitialized += () =>
- {
- var scriptObj = new FisBrowserScriptObject(_title, platformService);
- _browser.RegisterJavascriptObject(scriptObj, "FisShellApi");
- };
- browserContainer.Child = _browser;
- WindowStartupLocation = WindowStartupLocation.CenterScreen;
- //WindowState = WindowState.Maximized;
- MinWidth = 1366;
- MinHeight = 768;
- var language = CultureInfo.CurrentCulture.Name;
- if (_title != null)
- {
- if (language == "zh-CN")
- {
- _title.Text = "杏聆荟";
- }
- else
- {
- _title.Text = "FLYINSONO";
- }
- }
- var assembly = Assembly.GetExecutingAssembly();
- Stream? resourceStream = null;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- resourceStream = assembly.GetManifestResourceStream("fis.Win.flyinsono.ico");
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- resourceStream = assembly.GetManifestResourceStream("fis.Mac.flyinsono.ico");
- }
- if (resourceStream != null)
- {
- Icon = new WindowIcon(resourceStream);
- }
- #if DEBUG
- System.Threading.Tasks.Task.Run(() =>
- {
- Thread.Sleep(1000);
- _browser.ShowDeveloperTools();
- });
- #endif
- }
- }
- internal class FisBrowserScriptObject
- {
- IPlatformService _platformService;
- TextBlock _title;
- internal FisBrowserScriptObject(TextBlock title, IPlatformService platformService)
- {
- _title = title;
- _platformService = platformService;
- }
- /// <summary>
- /// 获取平台名
- /// </summary>
- /// <returns></returns>
- public string GetPlatformName() => _platformService.GetPlatformName();
- /// <summary>
- /// 写日志
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- public void WriteLog(string message) => Logger.Write(message);
- /// <summary>
- /// 设置窗体标题
- /// </summary>
- /// <param name="title">标题</param>
- public void SetTitle(string title)
- {
- MainWindow.MainThreadSyncContext?.Send(new SendOrPostCallback((args) =>
- {
- if(_title != null)
- {
- _title.Text = title;
- }
- }), this);
- }
- public int Age { get; set; }
- }
- }
|