/** * Created by Administrator on 2016/11/23. * 頁面對窗口的一些操做封裝,用於渲染進程 */ "use strict"; const Common = require('../../window/common.js'); const { ipcRenderer, remote } = require('electron'); const WinReg = require('winreg'); const RUN_LOCATION = '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run'; const file = process.execPath; let flashTrayTimer = null; class WindowUtil{ // 窗口最小化 static minWindow() { remote.getCurrentWindow().minimize(); } // 窗口最大化 static maxWindow(isMaxed) { const browserWindow = remote.getCurrentWindow(); if(!isMaxed) { browserWindow.unmaximize(); } else { browserWindow.maximize(); } } // 設置窗口是否能改變大小,參數true/false static setResizable(resizable) { remote.getCurrentWindow().setResizable(resizable); } // 下載文件 static download(url){ remote.getCurrentWebContents().downloadURL(url); } // 隱藏窗口 static hide(){ const browserWindow = remote.getCurrentWindow(); browserWindow.hide(); } // 顯示窗口 static show(){ const browserWindow = remote.getCurrentWindow(); browserWindow.show(); } // 窗口閃爍 static flashFrame(){ const browserWindow = remote.getCurrentWindow(); // if(browserWindow.isFocused() || browserWindow.isVisible()) if(!browserWindow.isFocused()) { browserWindow.showInactive(); browserWindow.flashFrame(true); } } // 設置窗口最前端顯示 static setAlwaysOnTop(top){ const browserWindow = remote.getCurrentWindow(); browserWindow.setAlwaysOnTop(top); } // 設置開機啓動 static enableAutoStart(callback) { let key = new WinReg({hive: WinReg.HKCU, key: RUN_LOCATION}); key.set('EUC', WinReg.REG_SZ, file, (err)=> { console.log('設置自動啓動'+err); callback(err); }); } // 取消開機啓動 static disableAutoStart(callback) { let key = new WinReg({hive: WinReg.HKCU, key: RUN_LOCATION}); key.remove('EUC', (err)=>{ console.log('取消自動啓動'+err); callback(err); }); } // 獲取是否開機啓動 static getAutoStartValue(callback) { let key = new WinReg({hive: WinReg.HKCU, key: RUN_LOCATION}); key.get('EUC', function (error, result) { console.log("查詢自動啓動:"+JSON.stringify(result)); console.log("file:"+file); if (result) { callback(true); } else { callback(false); } }); } /** * 托盤圖標閃爍 * @param flash true:閃爍;false:中止 */ static flashTray(flash) { let hasIcon = false; const tayIcon = './imgs/logo.ico'; const tayIcon1 = './imgs/empty.png'; if (flash) { if (flashTrayTimer) { return; } flashTrayTimer = window.setInterval(() => { ipcRenderer.send('ChangeTrayIcon', hasIcon ? tayIcon : tayIcon1); hasIcon = !hasIcon; }, 500); } else { if(flashTrayTimer) { window.clearInterval(flashTrayTimer); flashTrayTimer = null; } ipcRenderer.send('ChangeTrayIcon', tayIcon); } } } module.exports = WindowUtil;