1+ const path = require ( 'path' ) ;
2+ const log = require ( '../../modules/logger' ) ;
3+ const { readConfig } = require ( '../../modules/fn_config/config' ) ;
4+ const { restoreCookies } = require ( '../../modules/fn_config/cookie' ) ;
5+
6+ /**
7+ * 设置窗口为半屏
8+ * @param {Electron.BrowserWindow } mainWindow - 主窗口实例
9+ */
10+ function setHalfScreen ( mainWindow ) {
11+ if ( ! mainWindow ) return ;
12+
13+ mainWindow . setSize ( 1200 , 800 ) ;
14+ mainWindow . center ( ) ;
15+ mainWindow . unmaximize ( ) ;
16+ }
17+
18+ /**
19+ * 设置窗口为全屏
20+ * @param {Electron.BrowserWindow } mainWindow - 主窗口实例
21+ */
22+ function setFullScreen ( mainWindow ) {
23+ if ( mainWindow ) mainWindow . maximize ( ) ;
24+ }
25+
26+ /**
27+ * 设置全屏切换
28+ * @param {Electron.BrowserWindow } mainWindow - 主窗口实例
29+ */
30+ function setupFullScreenToggle ( mainWindow ) {
31+ let isFullScreen = false ;
32+ mainWindow . webContents . on ( 'before-input-event' , ( event , input ) => {
33+ if ( input . type === 'keyDown' && input . key === 'F11' ) {
34+ if ( isFullScreen ) {
35+ setHalfScreen ( ) ;
36+ } else {
37+ setFullScreen ( ) ;
38+ }
39+ isFullScreen = ! isFullScreen ;
40+ event . preventDefault ( ) ;
41+ }
42+ } ) ;
43+ }
44+
45+ /**
46+ * 设置输入法相关功能
47+ * @param {Electron.BrowserWindow } mainWindow - 主窗口实例
48+ */
49+ function setupInputMethodDisable ( mainWindow ) {
50+ // 禁用输入法相关功能
51+ mainWindow . webContents . on ( 'dom-ready' , ( ) => {
52+ // 注入CSS来禁用输入法自动切换
53+ mainWindow . webContents . insertCSS ( `
54+ * {
55+ ime-mode: disabled !important;
56+ -webkit-ime-mode: disabled !important;
57+ }
58+ input, textarea {
59+ ime-mode: inactive !important;
60+ -webkit-ime-mode: inactive !important;
61+ }
62+ ` ) ;
63+ } ) ;
64+ }
65+
66+ /**
67+ * 设置窗口显示事件
68+ * @param {Electron.BrowserWindow } mainWindow - 主窗口实例
69+ */
70+ function setupWindowShowEvents ( mainWindow ) {
71+ mainWindow . once ( 'ready-to-show' , ( ) => mainWindow . show ( ) ) ;
72+ }
73+
74+ /**
75+ * 设置 cookie 恢复
76+ * @param {Electron.BrowserWindow } mainWindow - 主窗口实例
77+ */
78+ function setupCookieRestore ( mainWindow ) {
79+ // 从配置中恢复 cookie
80+ const savedConfig = readConfig ( ) ;
81+ if ( ! savedConfig || ! savedConfig . token || ! savedConfig . domain ) {
82+ log . warn ( '没有找到已保存的配置,无法恢复 cookie' ) ;
83+ mainWindow . loadFile ( path . join ( __dirname , '../../public/login.html' ) ) ;
84+ return ;
85+ }
86+
87+ // 恢复 cookie 并跳转到对应的 URL
88+ log . info ( '恢复登录状态,即将跳转到主页面, domain:' , savedConfig . domain , ' token:' , savedConfig . token ) ;
89+
90+ // 恢复 cookie
91+ restoreCookies ( savedConfig . domain , savedConfig . token ) . then ( ( result ) => {
92+ if ( result === true ) {
93+ // cookie 恢复成功,跳转到主页面
94+ mainWindow . loadURL ( `${ savedConfig . domain } /v` ) ;
95+ return ;
96+ }
97+
98+ // cookie 恢复失败,跳转到登录页面
99+ log . warn ( 'Cookie 恢复失败,跳转到登录页面' ) ;
100+ mainWindow . loadFile ( path . join ( __dirname , '../../public/login.html' ) ) ;
101+ } ) . catch ( ( error ) => {
102+ // 出现异常,也跳转到登录页面
103+ log . error ( 'Cookie 恢复过程中出现异常:' , error ) ;
104+ mainWindow . loadFile ( path . join ( __dirname , '../../public/login.html' ) ) ;
105+ } ) ;
106+ }
107+
108+ module . exports = {
109+ setHalfScreen,
110+ setFullScreen,
111+ setupFullScreenToggle,
112+ setupInputMethodDisable,
113+ setupWindowShowEvents,
114+ setupCookieRestore,
115+ } ;
0 commit comments