Эх сурвалжийг харах

设置窗体标题 Script & 统一文本文件存取接口

melon.yin 3 жил өмнө
parent
commit
0fd892e940

+ 15 - 0
fis/IPlatformService.cs

@@ -33,6 +33,21 @@ namespace fis
 
         /// 获取配置
         string? GetConfig();
+
+        /// <summary>
+        /// 保存文本
+        /// </summary>
+        /// <param name="name">文件名</param>
+        /// <param name="text">文本</param>
+        /// <returns></returns>
+        bool SaveText(string name, string text);
+
+        /// <summary>
+        /// 获取文本
+        /// </summary>
+        /// <param name="name">文件名</param>
+        /// <returns></returns>
+        string? GetText(string name);
     }
 
 }

+ 1 - 1
fis/Log/LogEngine.cs

@@ -54,7 +54,7 @@ namespace fis.Log
         {
             var today = DateTime.Today;
 
-            var parentLogDirectory = Path.Combine(ShellConfig.Instance.DataPath, "Logs");
+            var parentLogDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
 
             var logDirectory = Path.Combine(parentLogDirectory, today.Year.ToString(), today.Month.ToString("D2"), today.Day.ToString("D2"));
 

+ 4 - 0
fis/Mac/MacService.cs

@@ -26,6 +26,10 @@ namespace fis.Mac
         public bool SaveConfig(string text) => AppManager.Get<IConfigManager>().SaveConfigAsync(text).Result;
 
         public string? GetConfig() => AppManager.Get<IConfigManager>().GetConfigAsync().Result;
+
+        public bool SaveText(string name, string text) => AppManager.Get<ITextFileManager>().SaveTextAsync(name, text).Result;
+
+        public string? GetText(string name) => AppManager.Get<ITextFileManager>().GetTextAsync(name).Result;
     }
 
 }

+ 12 - 4
fis/MainWindow.axaml.cs

@@ -59,7 +59,7 @@ namespace fis
             _browser.ContextMenuHandler = new TextContextMenuHandler(_browser);
             _browser.BrowserInitialized += () =>
             {
-                var scriptObj = new FisBrowserScriptObject(platformService);
+                var scriptObj = new FisBrowserScriptObject(this, platformService);
                 _browser.RegisterJavascriptObject(scriptObj, "FisShellApi");
             };
             browserContainer.Child = _browser;
@@ -80,11 +80,11 @@ namespace fis
 
             var assembly = Assembly.GetExecutingAssembly();
             Stream? resourceStream = null;
-            if(Environment.OSVersion.Platform == PlatformID.Win32NT)
+            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
             {
                 resourceStream = assembly.GetManifestResourceStream("fis.Win.flyinsono.ico");
             }
-            else if(Environment.OSVersion.Platform == PlatformID.Unix)
+            else if (Environment.OSVersion.Platform == PlatformID.Unix)
             {
                 resourceStream = assembly.GetManifestResourceStream("fis.Mac.flyinsono.ico");
             }
@@ -107,8 +107,10 @@ namespace fis
     internal class FisBrowserScriptObject
     {
         IPlatformService _platformService;
-        internal FisBrowserScriptObject(IPlatformService platformService)
+        Window _window;
+        internal FisBrowserScriptObject(Window window, IPlatformService platformService)
         {
+            _window = window;
             _platformService = platformService;
         }
 
@@ -125,6 +127,12 @@ namespace fis
         /// <returns></returns>
         public void WriteLog(string message) => Logger.Write(message);
 
+        /// <summary>
+        /// 设置窗体标题
+        /// </summary>
+        /// <param name="title">标题</param>
+        public void SetTitle(string title) => _window.Title = title;
+
         public int Age { get; set; }
     }
 }

+ 1 - 0
fis/Managers/AppManager.cs

@@ -75,6 +75,7 @@ namespace fis.Managers
             if (_initialized) return;
 
             RegisterManager<IThemeManager>(new ThemeManager());
+            RegisterManager<ITextFileManager>(new TextFileManager());
             RegisterManager<IConfigManager>(new ConfigManager());
 
             _initialized = true;

+ 3 - 45
fis/Managers/ConfigManager.cs

@@ -13,52 +13,10 @@ namespace fis.Managers
 {
     internal class ConfigManager : FisManager, IConfigManager
     {
-        public async Task<bool> SaveConfigAsync(string jsonText)
-        {
-            try
-            {
-                string filePath = GetFilePath();
-                using (var stream = new FileStream(
-                    filePath,
-                    FileMode.OpenOrCreate,
-                    FileAccess.ReadWrite,
-                    FileShare.None,
-                    bufferSize: 4096,
-                    useAsync: true)
-                )
-                {
-                    byte[] buffer = Encoding.UTF8.GetBytes(jsonText);
-                    await stream.WriteAsync(buffer, 0, buffer.Length);
-                }
-                await File.WriteAllTextAsync(filePath, jsonText, Encoding.UTF8);
-                return true;
-            }
-            catch (Exception ex)
-            {
-                Logger.WriteShellLog("保存配置异常", ex);
-            }
-            return false;
-        }
+        const string FILE_NAME = "config.conf";
 
-        public async Task<string?> GetConfigAsync()
-        {
-            try
-            {
-                string filePath = GetFilePath();
-                string text = await File.ReadAllTextAsync(filePath, Encoding.UTF8);
-                return text;
-            }
-            catch (Exception ex)
-            {
-                Logger.WriteShellLog("获取配置异常", ex);
-            }
-            return null;
-        }
+        public async Task<bool> SaveConfigAsync(string jsonText) => await AppManager.Get<ITextFileManager>().SaveTextAsync(FILE_NAME, jsonText);
 
-        private string GetFilePath()
-        {
-            string dir = AppDomain.CurrentDomain.BaseDirectory;
-            return Path.Combine(dir, $"config.conf");
-        }
+        public async Task<string?> GetConfigAsync() => await AppManager.Get<ITextFileManager>().GetTextAsync(FILE_NAME);
     }
 }

+ 27 - 0
fis/Managers/Interfaces/ITextFileManager.cs

@@ -0,0 +1,27 @@
+using fis.Managers.Interfaces;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace fis.Managers.Interfaces
+{
+    internal interface ITextFileManager : IFisManager
+    {
+        /// <summary>
+        /// 保存文本
+        /// </summary>
+        /// <param name="name">文件名</param>
+        /// <param name="text">文本内容</param>
+        /// <returns></returns>
+        Task<bool> SaveTextAsync(string name, string text);
+
+        /// <summary>
+        /// 获取文本
+        /// </summary>
+        /// <param name="name">文件名</param>
+        /// <returns></returns>
+        Task<string?> GetTextAsync(string name);
+    }
+}

+ 67 - 0
fis/Managers/TextFileManager.cs

@@ -0,0 +1,67 @@
+using fis.Log;
+using fis.Managers;
+using fis.Managers.Interfaces;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace fis.Managers
+{
+    internal class TextFileManager : FisManager, ITextFileManager
+    {
+        public async Task<bool> SaveTextAsync(string name, string text)
+        {
+            try
+            {
+                string filePath = GetFilePath(name);
+                using (var stream = new FileStream(
+                    filePath,
+                    FileMode.OpenOrCreate,
+                    FileAccess.ReadWrite,
+                    FileShare.None,
+                    bufferSize: 4096,
+                    useAsync: true)
+                )
+                {
+                    byte[] buffer = Encoding.UTF8.GetBytes(text);
+                    await stream.WriteAsync(buffer, 0, buffer.Length);
+                }
+                //await File.WriteAllTextAsync(filePath, text, Encoding.UTF8);
+                return true;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteShellLog("保存文本文件异常", ex);
+            }
+            return false;
+        }
+
+        public async Task<string?> GetTextAsync(string name)
+        {
+            try
+            {
+                string filePath = GetFilePath(name);
+                string text = await File.ReadAllTextAsync(filePath, Encoding.UTF8);
+                return text;
+            }
+            catch (Exception ex)
+            {
+                Logger.WriteShellLog("获取文本文件异常", ex);
+            }
+            return null;
+        }
+
+        private string GetFilePath(string name)
+        {
+            string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppStorage");
+            if (!Directory.Exists(dir))
+            {
+                Directory.CreateDirectory(dir);
+            }
+            return Path.Combine(dir, name);
+        }
+    }
+}

+ 4 - 0
fis/Win/WinService.cs

@@ -26,6 +26,10 @@ namespace fis.Win
         public bool SaveConfig(string text) => AppManager.Get<IConfigManager>().SaveConfigAsync(text).Result;
 
         public string? GetConfig() => AppManager.Get<IConfigManager>().GetConfigAsync().Result;
+
+        public bool SaveText(string name, string text) => AppManager.Get<ITextFileManager>().SaveTextAsync(name, text).Result;
+
+        public string? GetText(string name) => AppManager.Get<ITextFileManager>().GetTextAsync(name).Result;
     }
 
 }