Browse Source

:art: Support minimize the main window after auto launch on Windows/macOS https://github.com/siyuan-note/siyuan/issues/11145

Daniel 1 year ago
parent
commit
b64a9a3ad2

+ 3 - 0
app/appearance/langs/en_US.json

@@ -1,4 +1,7 @@
 {
+  "autoLaunchMode0": "Do not launch automatically",
+  "autoLaunchMode1": "Auto launch after booting",
+  "autoLaunchMode2": "Auto launch and minimize the interface after booting",
   "editorMarkdownInlineSup": "Markdown inline superscript syntax",
   "editorMarkdownInlineSupTip": "After enabling, <code class='fn__code'>^foo^</code> inline superscript syntax input will be supported",
   "editorMarkdownInlineSub": "Markdown inline subscript syntax",

+ 3 - 0
app/appearance/langs/es_ES.json

@@ -1,4 +1,7 @@
 {
+  "autoLaunchMode0": "No iniciar automáticamente",
+  "autoLaunchMode1": "Inicio automático después del arranque",
+  "autoLaunchMode2": "Iniciar automáticamente y minimizar la interfaz después del arranque",
   "editorMarkdownInlineSup": "Sintaxis de superíndice en línea de Markdown",
   "editorMarkdownInlineSupTip": "Después de habilitar, se admitirá la entrada de sintaxis de superíndice en línea <code class='fn__code'>^foo^</code>",
   "editorMarkdownInlineSub": "Sintaxis del subíndice en línea de Markdown",

+ 3 - 0
app/appearance/langs/fr_FR.json

@@ -1,4 +1,7 @@
 {
+  "autoLaunchMode0": "Ne pas lancer automatiquement",
+  "autoLaunchMode1": "Lancement automatique après le démarrage",
+  "autoLaunchMode2": "Lancement automatique et réduction de l'interface après le démarrage",
   "editorMarkdownInlineSup": "Syntaxe exposant Markdown en ligne",
   "editorMarkdownInlineSupTip": "Après l'activation, la saisie de la syntaxe en exposant <code class='fn__code'>^foo^</code> en ligne sera prise en charge",
   "editorMarkdownInlineSub": "Syntaxe d'indice en ligne Markdown",

+ 3 - 0
app/appearance/langs/zh_CHT.json

@@ -1,4 +1,7 @@
 {
+  "autoLaunchMode0": "不自動啟動",
+  "autoLaunchMode1": "開機自動啟動",
+  "autoLaunchMode2": "開機自動啟動並最小化介面",
   "editorMarkdownInlineSup": "Markdown 行級上標語法",
   "editorMarkdownInlineSupTip": "啟用後將支援 <code class='fn__code'>^foo^</code> 行級上標語法輸入",
   "editorMarkdownInlineSub": "Markdown 行級下標語法",

+ 3 - 0
app/appearance/langs/zh_CN.json

@@ -1,4 +1,7 @@
 {
+  "autoLaunchMode0": "不自动启动",
+  "autoLaunchMode1": "开机后自动启动",
+  "autoLaunchMode2": "开机后自动启动并最小化界面",
   "editorMarkdownInlineSup": "Markdown 行级上标语法",
   "editorMarkdownInlineSupTip": "启用后将支持 <code class='fn__code'>^foo^</code> 行级上标语法输入",
   "editorMarkdownInlineSub": "Markdown 行级下标语法",

+ 6 - 6
app/electron/main.js

@@ -230,7 +230,7 @@ const writeLog = (out) => {
     }
 };
 
-const boot = () => {
+const initMainWindow = () => {
     let windowStateInitialized = true;
     // 恢复主窗体状态
     let oldWindowState = {};
@@ -1019,7 +1019,7 @@ app.whenReady().then(() => {
         if (!foundWorkspace) {
             initKernel(data.workspace, "", "").then((isSucc) => {
                 if (isSucc) {
-                    boot();
+                    initMainWindow();
                 }
             });
         }
@@ -1149,7 +1149,7 @@ app.whenReady().then(() => {
         ipcMain.on("siyuan-first-init", (event, data) => {
             initKernel(data.workspace, "", data.lang).then((isSucc) => {
                 if (isSucc) {
-                    boot();
+                    initMainWindow();
                 }
             });
             firstOpenWindow.destroy();
@@ -1173,7 +1173,7 @@ app.whenReady().then(() => {
         }
         initKernel(workspace, port, "").then((isSucc) => {
             if (isSucc) {
-                boot();
+                initMainWindow();
             }
         });
     }
@@ -1280,7 +1280,7 @@ app.on("second-instance", (event, argv) => {
     if (workspace) {
         initKernel(workspace, port, "").then((isSucc) => {
             if (isSucc) {
-                boot();
+                initMainWindow();
             }
         });
         return;
@@ -1306,7 +1306,7 @@ app.on("activate", () => {
         }
     }
     if (BrowserWindow.getAllWindows().length === 0) {
-        boot();
+        initMainWindow();
     }
 });
 

+ 12 - 7
app/src/config/about.ts

@@ -16,14 +16,18 @@ import {setKey} from "../sync/syncGuide";
 export const about = {
     element: undefined as Element,
     genHTML: () => {
-        return `<label class="fn__flex b3-label${isBrowser() || window.siyuan.config.system.isMicrosoftStore || "std" !== window.siyuan.config.system.container || "linux" === window.siyuan.config.system.os ? " fn__none" : ""}">
+        return `<div class="fn__flex b3-label config__item${isBrowser() || window.siyuan.config.system.isMicrosoftStore || "std" !== window.siyuan.config.system.container || "linux" === window.siyuan.config.system.os ? " fn__none" : ""}">
     <div class="fn__flex-1">
         ${window.siyuan.languages.autoLaunch}
         <div class="b3-label__text">${window.siyuan.languages.autoLaunchTip}</div>
     </div>
-    <div class="fn__space"></div>
-    <input class="b3-switch fn__flex-center" id="autoLaunch" type="checkbox"${window.siyuan.config.system.autoLaunch ? " checked" : ""}>
-</label>
+    <span class="fn__space"></span>
+    <select class="b3-select fn__flex-center fn__size200" id="autoLaunch">
+      <option value="0" ${window.siyuan.config.system.autoLaunch === 0 ? "selected" : ""}>${window.siyuan.languages.autoLaunchMode0}</option>
+      <option value="1" ${window.siyuan.config.system.autoLaunch === 1 ? "selected" : ""}>${window.siyuan.languages.autoLaunchMode1}</option>
+      <option value="2" ${window.siyuan.config.system.autoLaunch === 2 ? "selected" : ""}>${window.siyuan.languages.autoLaunchMode2}</option>
+    </select>    
+</div>
 <label class="fn__flex b3-label${isBrowser() || window.siyuan.config.system.isMicrosoftStore || window.siyuan.config.system.container !== "std" ? " fn__none" : ""}">
     <div class="fn__flex-1">
         ${window.siyuan.languages.autoDownloadUpdatePkg}
@@ -348,9 +352,10 @@ export const about = {
         /// #if !BROWSER
         const autoLaunchElement = about.element.querySelector("#autoLaunch") as HTMLInputElement;
         autoLaunchElement.addEventListener("change", () => {
-            fetchPost("/api/system/setAutoLaunch", {autoLaunch: autoLaunchElement.checked}, () => {
-                window.siyuan.config.system.autoLaunch = autoLaunchElement.checked;
-                ipcRenderer.send(Constants.SIYUAN_AUTO_LAUNCH, {openAtLogin: autoLaunchElement.checked});
+            const autoLaunchMode = parseInt(autoLaunchElement.value)
+            fetchPost("/api/system/setAutoLaunch", {autoLaunch: autoLaunchMode}, () => {
+                window.siyuan.config.system.autoLaunch = autoLaunchMode;
+                ipcRenderer.send(Constants.SIYUAN_AUTO_LAUNCH, {openAtLogin: 0 != autoLaunchMode});
             });
         });
         /// #endif

+ 4 - 1
app/src/types/config.d.ts

@@ -1351,8 +1351,11 @@ declare namespace Config {
         appDir: string;
         /**
          * Boot automatically
+         * - `0`: Do not boot automatically
+         * - `1`: Boot automatically
+         * - `2`: Boot automatically + Minimize UI
          */
-        autoLaunch: boolean;
+        autoLaunch: number;
         /**
          * The absolute path of the `conf` directory of the current workspace
          */

+ 37 - 2
kernel/api/system.go

@@ -421,9 +421,44 @@ func setAutoLaunch(c *gin.Context) {
 		return
 	}
 
-	autoLaunch := arg["autoLaunch"].(bool)
-	model.Conf.System.AutoLaunch = autoLaunch
+	autoLaunch := int(arg["autoLaunch"].(float64))
+	model.Conf.System.AutoLaunch2 = autoLaunch
 	model.Conf.Save()
+
+	windowStateConf := filepath.Join(util.ConfDir, "windowState.json")
+	windowState := map[string]interface{}{}
+	if gulu.File.IsExist(windowStateConf) {
+		succ := false
+		const maxRetry = 7
+		for i := 0; i < maxRetry; i++ {
+			data, err := os.ReadFile(windowStateConf)
+			if nil != err {
+				logging.LogErrorf("read [windowState.json] failed [%d/%d]: %s", i, maxRetry, err)
+				time.Sleep(time.Second)
+			} else {
+				if err = gulu.JSON.UnmarshalJSON(data, &windowState); nil != err {
+					logging.LogErrorf("unmarshal [windowState.json] failed: %s", err)
+				} else {
+					succ = true
+					break
+				}
+			}
+		}
+		if !succ {
+			logging.LogErrorf("read [windowState.json] failed")
+		}
+	}
+	windowState["autoLaunch"] = autoLaunch
+
+	data, err := gulu.JSON.MarshalJSON(windowState)
+	if nil != err {
+		logging.LogErrorf("marshal [windowState.json] failed: %s", err)
+		return
+	}
+
+	if err = gulu.File.WriteFileSafer(windowStateConf, data, 0644); nil != err {
+		logging.LogErrorf("create [windowState.json] failed: %s", err)
+	}
 }
 
 func setDownloadInstallPkg(c *gin.Context) {

+ 1 - 1
kernel/conf/system.go

@@ -42,7 +42,7 @@ type System struct {
 	UploadErrLog           bool `json:"uploadErrLog"`
 	DisableGoogleAnalytics bool `json:"disableGoogleAnalytics"`
 	DownloadInstallPkg     bool `json:"downloadInstallPkg"`
-	AutoLaunch             bool `json:"autoLaunch"`
+	AutoLaunch2            int  `json:"autoLaunch2"`    // 0:不自动启动,1:自动启动,2:自动启动+隐藏主窗口
 	LockScreenMode         int  `json:"lockScreenMode"` // 0:手动,1:手动+跟随系统 https://github.com/siyuan-note/siyuan/issues/9087
 }