@@ -83,12 +83,17 @@ public class Info
8383 public static string NameVersion { get ; set ; }
8484 }
8585
86- public MultiLanguage i18n ;
86+ /// <summary>
87+ /// 多语言支持
88+ /// </summary>
89+ public MultiLanguage I18n ;
90+
91+ public static IntPtr ControllerPtr = IntPtr . Zero ;
8792
8893 /// <summary>
89- /// 是否启用输入法切换
94+ /// 保持后台活跃 避免界面隐藏后被自动回收
9095 /// </summary>
91- public readonly bool EnableSwitchIME = false ;
96+ private readonly System . Timers . Timer _Timer ;
9297 #endregion
9398
9499 #region 构造函数
@@ -99,12 +104,20 @@ public Main()
99104 {
100105 // 获取用户配置数据
101106 _Setting = GetUserData ( ) ;
102- LoadResEn ( ) ;
103-
107+ // 加载语言资源
108+ LoadLangRes ( ) ;
109+ // 设置语言
104110 string lang = _Setting . Language . GetDesc ( ) ;
105- i18n = new MultiLanguage ( _Setting . Language ) ;
111+ I18n = new MultiLanguage ( _Setting . Language ) ;
106112 System . Globalization . CultureInfo UICulture = new System . Globalization . CultureInfo ( lang ) ;
107113 Thread . CurrentThread . CurrentUICulture = UICulture ;
114+ // 界面保活
115+ _Timer = new System . Timers . Timer ( _Setting . Heartbeat ) ;
116+ _Timer . Elapsed += ( sender , e ) =>
117+ {
118+ Logger . Info ( "" , "alive" ) ;
119+ } ;
120+
108121 InitializeComponent ( ) ;
109122 InitPdone ( ) ;
110123 IsInit = false ;
@@ -135,10 +148,10 @@ public static Setting GetUserData()
135148 Directory . CreateDirectory ( UserDataPath ) ;
136149 if ( ! File . Exists ( fullPath ) )
137150 {
138- File . WriteAllText ( fullPath , JsonHelper . json ( tempData ) ) ;
151+ File . WriteAllText ( fullPath , JsonHelper . Obj2Str ( tempData ) ) ;
139152 }
140153 StreamReader reader = File . OpenText ( fullPath ) ;
141- tempData = JsonHelper . jsonDes < Setting > ( reader . ReadToEnd ( ) ) ;
154+ tempData = JsonHelper . Str2Obj < Setting > ( reader . ReadToEnd ( ) ) ;
142155 reader . Close ( ) ;
143156 return tempData ;
144157 }
@@ -158,7 +171,7 @@ public static void SetUserData(Setting userData)
158171 {
159172 var fullPath = Path . Combine ( UserDataPath , "config.json" ) ;
160173 Directory . CreateDirectory ( UserDataPath ) ;
161- File . WriteAllText ( fullPath , JsonHelper . json ( userData ) ) ;
174+ File . WriteAllText ( fullPath , JsonHelper . Obj2Str ( userData ) ) ;
162175 }
163176 catch ( Exception ex )
164177 {
@@ -197,8 +210,8 @@ public void InitPdone()
197210 }
198211
199212 #region 控件状态
200- uiLabel7 . Visible = EnableSwitchIME ;
201- linkIME . Visible = EnableSwitchIME ;
213+ uiLabel7 . Visible = _Setting . EnableSwitchIME ;
214+ linkIME . Visible = _Setting . EnableSwitchIME ;
202215 #endregion
203216
204217 #region 事件绑定
@@ -280,9 +293,9 @@ public void InitPdone()
280293 #endregion
281294
282295 #region 配置项默认值
283- comboPx . Items [ 0 ] = i18n . def ;
284- comboMbps . Items [ 0 ] = i18n . def ;
285- comboMaxFPS . Items [ 0 ] = i18n . def ;
296+ comboPx . Items [ 0 ] = I18n . def ;
297+ comboMbps . Items [ 0 ] = I18n . def ;
298+ comboMaxFPS . Items [ 0 ] = I18n . def ;
286299 comboPx . SelectedIndex = _Setting . PXIndex ;
287300 comboMbps . SelectedIndex = _Setting . BitRateIndex ;
288301 comboMaxFPS . SelectedIndex = _Setting . MaxFPSIndex ;
@@ -305,9 +318,9 @@ public void InitPdone()
305318 cbxShowTouches . Checked = _Setting . ShowTouches ;
306319 cbxReadOnly . Checked = _Setting . ReadOnly ;
307320 cbxAudioEnabled . Checked = _Setting . AudioEnabled ;
308- linkIME . Text = i18n . imes [ ( InputMethod ) _Setting . IME ] ;
309- tbxIp . Watermark = i18n . tbxIpPlaceholder ;
310- tbxPort . Watermark = i18n . tbxPortPlaceholder ;
321+ linkIME . Text = I18n . imes [ ( InputMethod ) _Setting . IME ] ;
322+ tbxIp . Watermark = I18n . tbxIpPlaceholder ;
323+ tbxPort . Watermark = I18n . tbxPortPlaceholder ;
311324 #endregion
312325 }
313326
@@ -334,9 +347,9 @@ private void ExtractResource(bool reload = false)
334347 }
335348
336349 /// <summary>
337- /// 加载英文资源
350+ /// 加载语言资源 此资源控制界面布局
338351 /// </summary>
339- public void LoadResEn ( )
352+ public void LoadLangRes ( )
340353 {
341354 if ( _Setting . Language == Lang . en )
342355 {
@@ -365,7 +378,7 @@ private void StartButtonClick(object sender, EventArgs e)
365378 if ( _Setting . UseWireless &&
366379 ( string . IsNullOrWhiteSpace ( _Setting . IPAddress ) || string . IsNullOrWhiteSpace ( _Setting . Port ) ) )
367380 {
368- ShowMessage ( i18n . msgIpNull ) ;
381+ ShowMessage ( I18n . msgIpNull ) ;
369382 return ;
370383 }
371384
@@ -511,20 +524,20 @@ private void RunScrcpy()
511524 scrcpy . Exited += ( ss , ee ) =>
512525 {
513526 SetUserData ( _Setting ) ; // 关闭scrcpy后保存一下配置文件
514- if ( EnableSwitchIME && _Setting . IME != 0 && _Setting . IMEOrigin . IsNotNull ( ) )
527+ if ( _Setting . EnableSwitchIME && _Setting . IME != 0 && _Setting . IMEOrigin . IsNotNull ( ) )
515528 {
516529 ADB . Execute ( $ "shell ime set { _Setting . IMEOrigin } ") ;
517530 }
518531 MoveListener . StopListening ( ) ;
519532 FromHandle ( false ) ;
520533 ButtonHandle ( false ) ;
521534 LoadHistoryIPs ( true ) ;
522- ShowMessage ( i18n . msgExit ) ;
535+ ShowMessage ( I18n . msgExit ) ;
523536 } ;
524537 scrcpy . BeginErrorReadLine ( ) ;
525538 scrcpy . BeginOutputReadLine ( ) ;
526539
527- if ( EnableSwitchIME && _Setting . IME != 0 )
540+ if ( _Setting . EnableSwitchIME && _Setting . IME != 0 )
528541 {
529542 // 获取当前输入法
530543 string strCurIME = ADB . Execute ( $ "adb shell settings get secure default_input_method") ;
@@ -562,12 +575,12 @@ private void ButtonHandle(bool isStart)
562575 if ( isStart )
563576 {
564577 btnStart . Enabled = false ;
565- btnStart . Text = i18n . btnStarting ;
578+ btnStart . Text = I18n . btnStarting ;
566579 }
567580 else
568581 {
569582 btnStart . Enabled = true ;
570- btnStart . Text = i18n . btnStartDef ;
583+ btnStart . Text = I18n . btnStartDef ;
571584 }
572585 } ;
573586 Invoke ( action ) ;
@@ -588,15 +601,18 @@ private void FromHandle(bool isStart)
588601 if ( _Setting . ControllerEnabled )
589602 {
590603 _Controller = new Controller ( ) ;
604+ ControllerPtr = _Controller . Handle ;
591605 _Controller . Show ( ) ;
592606 }
607+ _Timer ? . Start ( ) ;
593608 }
594609 else
595610 {
596611 _Controller ? . Dispose ( ) ;
597612 Show ( ) ;
598613 Activate ( ) ;
599614 Focus ( ) ;
615+ _Timer ? . Stop ( ) ;
600616 }
601617 } ;
602618 Invoke ( action ) ;
@@ -621,7 +637,7 @@ void LoadHistoryIPs(bool isReload = false)
621637 else
622638 action ( ) ;
623639 }
624- #endregion
640+ #endregion
625641
626642 #region 配置项改变事件
627643 /// <summary>
@@ -903,7 +919,7 @@ private void linkEnabledADB_Click(object sender, EventArgs e)
903919
904920 private void linkSetPort_Click ( object sender , EventArgs e )
905921 {
906- if ( UIMessageBox . Show ( i18n . linkSetPort , i18n . linkSetPortTitle ,
922+ if ( UIMessageBox . Show ( I18n . linkSetPort , I18n . linkSetPortTitle ,
907923 _Setting . DarkMode ? UIStyle . Black : UIStyle . Gray , UIMessageBoxButtons . OKCancel , false ) )
908924 {
909925 var batPath = ScrcpyPath + "SetProt.bat" ;
@@ -972,27 +988,27 @@ private void lbAllShortcut_Click(object sender, EventArgs e)
972988
973989 private void linkIME_Click ( object sender , EventArgs e )
974990 {
975- var list = i18n . imes . Values . ToList ( ) ;
991+ var list = I18n . imes . Values . ToList ( ) ;
976992 int select = _Setting . IME ;
977- if ( UISelectDialog . ShowSelectDialog ( this , ref select , list , i18n . linkImeTitle , i18n . linkImeContent ) )
993+ if ( UISelectDialog . ShowSelectDialog ( this , ref select , list , I18n . linkImeTitle , I18n . linkImeContent ) )
978994 {
979995 _Setting . IME = select ;
980- linkIME . Text = i18n . imes [ ( InputMethod ) _Setting . IME ] ;
996+ linkIME . Text = I18n . imes [ ( InputMethod ) _Setting . IME ] ;
981997 }
982998 }
983999
9841000 private void linkLang_Click ( object sender , EventArgs e )
9851001 {
986- var list = i18n . langs ;
1002+ var list = I18n . langs ;
9871003 int select = ( int ) _Setting . Language ;
988- if ( UISelectDialog . ShowSelectDialog ( this , ref select , list , i18n . linkLangTitle , i18n . linkLangContent ) )
1004+ if ( UISelectDialog . ShowSelectDialog ( this , ref select , list , I18n . linkLangTitle , I18n . linkLangContent ) )
9891005 {
9901006 if ( select == ( int ) _Setting . Language )
9911007 {
9921008 return ;
9931009 }
9941010 _Setting . Language = ( Lang ) select ;
995- LoadResEn ( ) ;
1011+ LoadLangRes ( ) ;
9961012
9971013 System . Windows . Forms . Application . Restart ( ) ;
9981014 }
0 commit comments