Browse Source

代码优化,重复代码合并

loki.wu 2 years ago
parent
commit
c81ec22da2

+ 108 - 0
fis/BaseWindow.cs

@@ -0,0 +1,108 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+using fis.Mac;
+using fis.Managers;
+using fis.Utilities;
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Xilium.CefGlue.Avalonia;
+
+namespace fis.Win.Utilities
+{
+    public class BaseWindow : Window
+    {
+        private TextBlock? _title;
+        private AvaloniaCefBrowser _browser;
+
+        /// <summary>
+        /// 是否主窗口
+        /// </summary>
+        public bool IsMainWindow = true;
+
+        public static SynchronizationContext? MainThreadSyncContext;
+
+        public BaseWindow() 
+        {
+            MainThreadSyncContext = SynchronizationContext.Current;
+            ExtendClientAreaToDecorationsHint = true;
+            ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
+            ExtendClientAreaTitleBarHeightHint = -1;
+            _browser = BrowserManager.MainBrowser;
+#if DEBUG
+            this.AttachDevTools();
+#endif
+        }
+
+        public void InitializeComponent() 
+        {
+            if (!IsMainWindow)
+            {
+                _browser = BrowserManager.SlaveBrowser;
+            }
+            var browserContainer = this.FindControl<Border>("BrowserBorder");
+            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.");
+            }
+            if (_title != null)
+            {
+                var language = CultureInfo.CurrentCulture.Name;
+                if (language == "zh-CN")
+                {
+                    _title.Text = "杏聆荟";
+                }
+                else
+                {
+                    _title.Text = "FLYINSONO";
+                }
+                var scriptObj = new FisBrowserScriptObject(_title, platformService);
+                _browser.RegisterJavascriptObject(scriptObj, "FisShellApi");
+            }
+            _browser.BrowserInitialized += () =>
+            {
+#if DEBUG
+                _browser!.ShowDeveloperTools();
+#endif
+            };
+            MinWidth = 1366;
+            MinHeight = 768;
+            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);
+            }
+            browserContainer.Child = _browser;
+        }
+    }
+}

+ 5 - 74
fis/MainWindow.axaml.cs

@@ -12,59 +12,26 @@ using fis.Managers;
 using fis.Managers.Interfaces;
 using Avalonia.Threading;
 using fis.Utilities;
+using fis.Win.Utilities;
 
 namespace fis
 {
-    public partial class MainWindow : Window
+    public partial class MainWindow : BaseWindow
     {
         private const string ThisWindowName = "Main";
-        private TextBlock? _title;
-
-        public static SynchronizationContext? MainThreadSyncContext;
 
         public MainWindow()
         {
+            AvaloniaXamlLoader.Load(this);
             InitializeComponent();
-            ExtendClientAreaToDecorationsHint = true;
-            ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
-            ExtendClientAreaTitleBarHeightHint = -1;
-            MainThreadSyncContext = SynchronizationContext.Current;
-#if DEBUG
-            this.AttachDevTools();
-#endif
+            InitializeMainComponent();
         }
 
-        private void InitializeComponent()
+        private void InitializeMainComponent()
         {
-            AvaloniaXamlLoader.Load(this);
-            var browserContainer = this.FindControl<Border>("BrowserBorder");
-
-
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
-            {
-                this.FindControl<MacosTitleBar>("MacTitleBar").IsVisible = false;
-                _title = this.FindControl<WindowsTitleBar>("WinTitleBar").FindControl<TextBlock>("WindowsTitle");
-
-            }
-            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
-            {
-                this.FindControl<WindowsTitleBar>("WinTitleBar").IsVisible = false;
-                _title = this.FindControl<MacosTitleBar>("MacTitleBar").FindControl<TextBlock>("MacosTitle");
-
-            }
-            else
-            {
-                throw new NotSupportedException($"Platform {Environment.OSVersion.Platform} is not suppoorted.");
-            }
             //Make cross domain work.
             BrowserManager.MainBrowser.Address = "http://" + ShellConfig.Instance.AppHost + "/index.html";
             BrowserManager.MainBrowser.ContextMenuHandler = new TextContextMenuHandler(BrowserManager.MainBrowser);
-            BrowserManager.MainBrowser.BrowserInitialized += () =>
-            {
-#if DEBUG
-                BrowserManager.MainBrowser!.ShowDeveloperTools();
-#endif
-            };
             if (BrowserManager.MainBrowser is WinFisBrowser winFisBrowser)
             {
                 WindowDragingHelper.StartMouseMonitor();
@@ -108,48 +75,12 @@ namespace fis
                 };
                 winFisBrowser.WindowClosed += OnBrowserCloseTheWindow;
             }
-            browserContainer.Child = BrowserManager.MainBrowser;
-
-
             WindowStartupLocation = WindowStartupLocation.CenterScreen;
 #if DEBUG
 
 #else
             WindowState = WindowState.Maximized;
 #endif
-            MinWidth = 1366;
-            MinHeight = 768;
-
-            var language = CultureInfo.CurrentCulture.Name;
-            if (_title != null)
-            {
-                if (language == "zh-CN")
-                {
-                    _title.Text = "杏聆荟";
-                }
-                else
-                {
-                    _title.Text = "FLYINSONO";
-                }
-                var scriptObj = new FisBrowserScriptObject(_title, BrowserManager.PlatformService);
-                BrowserManager.MainBrowser.RegisterJavascriptObject(scriptObj, "FisShellApi");
-            }
-            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);
-            }
-
             AppManager.ParentWindow = this;
             Closed += OnMainWindowClosed;
         }

+ 0 - 1
fis/Managers/AppManager.cs

@@ -1,7 +1,6 @@
 using Avalonia.Controls;
 using fis.Managers.Interfaces;
 using fis.Managers;
-using fis.Managers.Interfaces;
 using System;
 using System.Collections.Concurrent;
 using System.Data;

+ 0 - 1
fis/Managers/FileExporterManager.cs

@@ -295,7 +295,6 @@ namespace fis.Managers
                 {
                     return false;
                 }
-                return true;
             }
             catch (Exception)
             {

+ 12 - 7
fis/Managers/Interfaces/ISecondaryScreenManager.cs

@@ -6,6 +6,18 @@ namespace fis.Managers.Interfaces
 {
     internal interface ISecondaryScreenManager : IFisManager
     {
+        SlaveWindow? SlaveWindow { get; }
+
+        /// <summary>
+        /// 是否使用第二窗口
+        /// </summary>
+        bool IsUseSecondWindow { get; }
+
+        /// <summary>
+        /// 第二窗口是否显示
+        /// </summary>
+        bool IsVisible { get; }
+
         /// <summary>
         /// 弹出新窗口
         /// </summary>
@@ -14,12 +26,5 @@ namespace fis.Managers.Interfaces
         /// <param name="keyValuePairs"></param>
         /// <returns></returns>
         Task ShowWindowByTypeAsync(WindowType windowType, string host, Dictionary<string, string> keyValuePairs, bool isShowWindow = true);
-
-        /// <summary>
-        /// 获取Window
-        /// </summary>
-        /// <param name="windowType"></param>
-        /// <returns></returns>
-        SlaveWindow? GetSlaveWindow();
     }
 }

+ 24 - 22
fis/Managers/SecondaryScreenManager.cs

@@ -12,58 +12,60 @@ namespace fis.Managers
 {
     internal class SecondaryScreenManager: ISecondaryScreenManager
     {
-        private SlaveWindow? _window;
+        public SlaveWindow? SlaveWindow { get; private set; }
+
+        public bool IsUseSecondWindow { get; private set; } = false;
+
+        public bool IsVisible { get; private set; } = false;
 
         public async Task ShowWindowByTypeAsync(WindowType windowType, string host, Dictionary<string, string> keyValuePairs,bool isShowWindow = true) 
         {
-            if (_window != null)
+            if (SlaveWindow != null)
             {
-                await _window.ChangeContentViewAsync(host, keyValuePairs, windowType);
+                await SlaveWindow.ChangeContentViewAsync(host, keyValuePairs, windowType);
+                IsVisible = true;
             }
             else 
             {
+                IsUseSecondWindow = true;
                 await Dispatcher.UIThread.InvokeAsync(() =>
                 {
-                    _window = new SlaveWindow();
-                    _window.init(host, keyValuePairs, windowType);
-                    if (_window.Screens.ScreenCount > 1)
+                    SlaveWindow = new SlaveWindow();
+                    SlaveWindow.Closed += SlaveWindow_Closed;
+                    SlaveWindow.init(host, keyValuePairs);
+                    if (SlaveWindow.Screens.ScreenCount > 1)
                     {
-                        var screen = _window.Screens.All.FirstOrDefault(x => x.Primary == false);
+                        var screen = SlaveWindow.Screens.All.FirstOrDefault<Avalonia.Platform.Screen>(x => x.Primary == false);
                         if (screen != null)
                         {
-                            _window.Position = new PixelPoint(screen.Bounds.X, screen.Bounds.Y);
+                            SlaveWindow.Position = new PixelPoint(screen.Bounds.X, screen.Bounds.Y);
                         }
                     }
-                    _window.Closed += Window_Closed;
                     if (!isShowWindow)
                     {
-                        _window.ShowInTaskbar = false;
-                        _window.Show();
-                        _window.Hide();
+                        SlaveWindow.ShowInTaskbar = false;
+                        SlaveWindow.Show();///Show一下是为了让Flutter程序初始化Splash页
+                        SlaveWindow.Hide();
                     }
                     else
                     {
-                        _window.Show();
+                        SlaveWindow.Show();
                     }
                 });
             }
         }
 
-        public SlaveWindow? GetSlaveWindow() {
-            return _window;
-        }
-
-        private void Window_Closed(object? sender, EventArgs e)
+        private void SlaveWindow_Closed(object? sender, EventArgs e)
         {
-            
+            IsVisible = false;
         }
 
         public void Dispose()
         {
-            if (_window != null)
+            if (SlaveWindow != null)
             {
-                _window.ForceClose = true;
-                _window.Close();
+                SlaveWindow.ForceClose = true;
+                SlaveWindow.Close();
             }
         }
     }

+ 3 - 3
fis/PlatformService.cs

@@ -39,7 +39,7 @@ namespace fis
         /// </summary>
         public void CloseSlaveWindow() {
             var manager = AppManager.Get<ISecondaryScreenManager>();
-            var window = manager.GetSlaveWindow();
+            var window = manager.SlaveWindow;
             if (window != null)
             {
                 window.Close();
@@ -131,8 +131,8 @@ namespace fis
         public void SaveReportTemplate(string templateId, string name, string useObject, string templateJson) 
         {
             var manager = AppManager.Get<ISecondaryScreenManager>();
-            var window =  manager.GetSlaveWindow();
-            List<String> args = new List<string>();
+            var window =  manager.SlaveWindow;
+            var args = new List<string>();
             args.Add(templateId);
             args.Add(name);
             args.Add(useObject);

+ 1 - 1
fis/ShellConfig.cs

@@ -14,7 +14,7 @@ namespace fis
         public int windowsNum = 0;
 
         //Gets or sets the app host which is the web App's access url
-        public string AppHost { get; set; } = "app.fis.plus";
+        public string AppHost { get; set; } = "127.0.0.1:8080";
 
         /// <summary>
         /// 模板设计器

+ 39 - 101
fis/SlaveWindow.axaml.cs

@@ -6,6 +6,7 @@ using fis.Mac;
 using fis.Managers;
 using fis.Utilities;
 using fis.Win;
+using fis.Win.Utilities;
 using System;
 using System.Collections.Generic;
 using System.Globalization;
@@ -18,28 +19,25 @@ using Xilium.CefGlue.Avalonia;
 
 namespace fis
 {
-    public partial class SlaveWindow : Window
+    public partial class SlaveWindow : BaseWindow
     {
-        public static SynchronizationContext? MainThreadSyncContext;
-        private TextBlock? _title;
+       
         private AvaloniaCefBrowser _browser;
-        private string _host;
-        private WindowType _windowType;
+        private string _host = "";
 
-        private Dictionary<string, string> _parameters;
+        private Dictionary<string, string> _parameters = new Dictionary<string, string>();
 
+        /// <summary>
+        /// 副窗口是否强制关闭,false则是点击关闭时隐藏,true则是窗口直接关闭
+        /// </summary>
         public bool ForceClose { get; set; } = false;
 
         public SlaveWindow()
         {
-            ExtendClientAreaToDecorationsHint = true;
-            ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
-            ExtendClientAreaTitleBarHeightHint = -1;
-            MainThreadSyncContext = SynchronizationContext.Current;
+            IsMainWindow = false;
+            AvaloniaXamlLoader.Load(this);
+            InitializeComponent();
             _browser = BrowserManager.SlaveBrowser;
-#if DEBUG
-            this.AttachDevTools();
-#endif
             Closing += SlaveWindow_Closing;
         }
 
@@ -48,12 +46,34 @@ namespace fis
         /// </summary>
         /// <param name="host"></param>
         /// <param name="dictionary"></param>
-        public void init(string host, Dictionary<string, string> dictionary, WindowType windowType)
+        public void init(string host, Dictionary<string, string> dictionary)
         {
-            _windowType = windowType;
             _parameters = dictionary;
             _host = host;
-            InitializeComponent();
+            var parameter = "";
+            var index = 0;
+            foreach (var p in _parameters)
+            {
+                parameter += p.Key + "=" + p.Value;
+                if (index < _parameters.Count - 1)
+                {
+                    parameter += "&";
+                }
+                index++;
+            }
+            //Make cross domain work.
+            if (parameter.Length > 0)
+            {
+                var url = "http://" + ShellConfig.Instance.AppHost + "/index.html?page=measure&" + parameter;
+                _browser.Address = url;
+            }
+            else
+            {
+                _browser.Address = _host;
+            }
+            WindowStartupLocation = WindowStartupLocation.Manual;
+            Position = new PixelPoint();
+            WindowState = WindowState.Maximized;
         }
 
         /// <summary>
@@ -84,6 +104,7 @@ namespace fis
                 }
                 Show();
             });
+            ///窗口在hidden变成show的时候容易卡死,加个Sleep能缓解这种情况
             Thread.Sleep(600);
             if (windowType == WindowType.TemplateDesigner) {
                 targetMethodName = TargetMethodName.OpenReportDesignerPage;
@@ -106,6 +127,8 @@ namespace fis
 
         private void SlaveWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
         {
+            ///若非强制关闭副窗口,则隐藏副窗口
+            ///用于保持Flutter在内存中不被销毁
             if (!ForceClose)
             {
                 e.Cancel = true;
@@ -117,90 +140,5 @@ namespace fis
                 GC.Collect();
             }
         }
-
-        private void InitializeComponent()
-        {
-            AvaloniaXamlLoader.Load(this);
-            var browserContainer = this.FindControl<Border>("BrowserBorder");
-            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 parameter = "";
-            var index = 0;
-            foreach (var p in _parameters)
-            {
-                parameter += p.Key + "=" + p.Value;
-                if (index < _parameters.Count - 1) {
-                    parameter += "&";
-                }
-                index++;
-            }
-            //Make cross domain work.
-            if (parameter.Length > 0)
-            {
-                var url = "http://" + ShellConfig.Instance.AppHost + "/index.html?page=measure&" + parameter;
-                _browser.Address = url;
-            }
-            else
-            {
-                _browser.Address = _host;
-            }
-            _browser.BrowserInitialized += () =>
-            {
-#if DEBUG
-               _browser!.ShowDeveloperTools();
-#endif
-            };
-            var language = CultureInfo.CurrentCulture.Name;
-            if (_title != null)
-            {
-                if (language == "zh-CN")
-                {
-                    _title.Text = "杏聆荟";
-                }
-                else
-                {
-                    _title.Text = "FLYINSONO";
-                }
-                var scriptObj = new FisBrowserScriptObject(_title, platformService);
-                _browser.RegisterJavascriptObject(scriptObj, "FisShellApi");
-            }
-            browserContainer.Child = _browser;
-            WindowStartupLocation = WindowStartupLocation.Manual;
-            Position = new PixelPoint();
-            WindowState = WindowState.Maximized;
-            MinWidth = 1366;
-            MinHeight = 768;
-            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);
-            }
-       
-        }
     }
 }

+ 1 - 1
fis/Utilities/FisBrowserScriptObject.cs

@@ -340,7 +340,7 @@ namespace fis.Utilities
             var manager = AppManager.Get<ISecondaryScreenManager>();
              Dispatcher.UIThread.InvokeAsync(() =>
              {
-                 var slaveWindow = manager.GetSlaveWindow();
+                 var slaveWindow = manager.SlaveWindow;
                  slaveWindow?.Close();
 
              });