@@ -83,12 +83,17 @@ public class Info
83
83
public static string NameVersion { get ; set ; }
84
84
}
85
85
86
- public MultiLanguage i18n ;
86
+ /// <summary>
87
+ /// 多语言支持
88
+ /// </summary>
89
+ public MultiLanguage I18n ;
90
+
91
+ public static IntPtr ControllerPtr = IntPtr . Zero ;
87
92
88
93
/// <summary>
89
- /// 是否启用输入法切换
94
+ /// 保持后台活跃 避免界面隐藏后被自动回收
90
95
/// </summary>
91
- public readonly bool EnableSwitchIME = false ;
96
+ private readonly System . Timers . Timer _Timer ;
92
97
#endregion
93
98
94
99
#region 构造函数
@@ -99,12 +104,20 @@ public Main()
99
104
{
100
105
// 获取用户配置数据
101
106
_Setting = GetUserData ( ) ;
102
- LoadResEn ( ) ;
103
-
107
+ // 加载语言资源
108
+ LoadLangRes ( ) ;
109
+ // 设置语言
104
110
string lang = _Setting . Language . GetDesc ( ) ;
105
- i18n = new MultiLanguage ( _Setting . Language ) ;
111
+ I18n = new MultiLanguage ( _Setting . Language ) ;
106
112
System . Globalization . CultureInfo UICulture = new System . Globalization . CultureInfo ( lang ) ;
107
113
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
+
108
121
InitializeComponent ( ) ;
109
122
InitPdone ( ) ;
110
123
IsInit = false ;
@@ -135,10 +148,10 @@ public static Setting GetUserData()
135
148
Directory . CreateDirectory ( UserDataPath ) ;
136
149
if ( ! File . Exists ( fullPath ) )
137
150
{
138
- File . WriteAllText ( fullPath , JsonHelper . json ( tempData ) ) ;
151
+ File . WriteAllText ( fullPath , JsonHelper . Obj2Str ( tempData ) ) ;
139
152
}
140
153
StreamReader reader = File . OpenText ( fullPath ) ;
141
- tempData = JsonHelper . jsonDes < Setting > ( reader . ReadToEnd ( ) ) ;
154
+ tempData = JsonHelper . Str2Obj < Setting > ( reader . ReadToEnd ( ) ) ;
142
155
reader . Close ( ) ;
143
156
return tempData ;
144
157
}
@@ -158,7 +171,7 @@ public static void SetUserData(Setting userData)
158
171
{
159
172
var fullPath = Path . Combine ( UserDataPath , "config.json" ) ;
160
173
Directory . CreateDirectory ( UserDataPath ) ;
161
- File . WriteAllText ( fullPath , JsonHelper . json ( userData ) ) ;
174
+ File . WriteAllText ( fullPath , JsonHelper . Obj2Str ( userData ) ) ;
162
175
}
163
176
catch ( Exception ex )
164
177
{
@@ -197,8 +210,8 @@ public void InitPdone()
197
210
}
198
211
199
212
#region 控件状态
200
- uiLabel7 . Visible = EnableSwitchIME ;
201
- linkIME . Visible = EnableSwitchIME ;
213
+ uiLabel7 . Visible = _Setting . EnableSwitchIME ;
214
+ linkIME . Visible = _Setting . EnableSwitchIME ;
202
215
#endregion
203
216
204
217
#region 事件绑定
@@ -280,9 +293,9 @@ public void InitPdone()
280
293
#endregion
281
294
282
295
#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 ;
286
299
comboPx . SelectedIndex = _Setting . PXIndex ;
287
300
comboMbps . SelectedIndex = _Setting . BitRateIndex ;
288
301
comboMaxFPS . SelectedIndex = _Setting . MaxFPSIndex ;
@@ -305,9 +318,9 @@ public void InitPdone()
305
318
cbxShowTouches . Checked = _Setting . ShowTouches ;
306
319
cbxReadOnly . Checked = _Setting . ReadOnly ;
307
320
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 ;
311
324
#endregion
312
325
}
313
326
@@ -334,9 +347,9 @@ private void ExtractResource(bool reload = false)
334
347
}
335
348
336
349
/// <summary>
337
- /// 加载英文资源
350
+ /// 加载语言资源 此资源控制界面布局
338
351
/// </summary>
339
- public void LoadResEn ( )
352
+ public void LoadLangRes ( )
340
353
{
341
354
if ( _Setting . Language == Lang . en )
342
355
{
@@ -365,7 +378,7 @@ private void StartButtonClick(object sender, EventArgs e)
365
378
if ( _Setting . UseWireless &&
366
379
( string . IsNullOrWhiteSpace ( _Setting . IPAddress ) || string . IsNullOrWhiteSpace ( _Setting . Port ) ) )
367
380
{
368
- ShowMessage ( i18n . msgIpNull ) ;
381
+ ShowMessage ( I18n . msgIpNull ) ;
369
382
return ;
370
383
}
371
384
@@ -511,20 +524,20 @@ private void RunScrcpy()
511
524
scrcpy . Exited += ( ss , ee ) =>
512
525
{
513
526
SetUserData ( _Setting ) ; // 关闭scrcpy后保存一下配置文件
514
- if ( EnableSwitchIME && _Setting . IME != 0 && _Setting . IMEOrigin . IsNotNull ( ) )
527
+ if ( _Setting . EnableSwitchIME && _Setting . IME != 0 && _Setting . IMEOrigin . IsNotNull ( ) )
515
528
{
516
529
ADB . Execute ( $ "shell ime set { _Setting . IMEOrigin } ") ;
517
530
}
518
531
MoveListener . StopListening ( ) ;
519
532
FromHandle ( false ) ;
520
533
ButtonHandle ( false ) ;
521
534
LoadHistoryIPs ( true ) ;
522
- ShowMessage ( i18n . msgExit ) ;
535
+ ShowMessage ( I18n . msgExit ) ;
523
536
} ;
524
537
scrcpy . BeginErrorReadLine ( ) ;
525
538
scrcpy . BeginOutputReadLine ( ) ;
526
539
527
- if ( EnableSwitchIME && _Setting . IME != 0 )
540
+ if ( _Setting . EnableSwitchIME && _Setting . IME != 0 )
528
541
{
529
542
// 获取当前输入法
530
543
string strCurIME = ADB . Execute ( $ "adb shell settings get secure default_input_method") ;
@@ -562,12 +575,12 @@ private void ButtonHandle(bool isStart)
562
575
if ( isStart )
563
576
{
564
577
btnStart . Enabled = false ;
565
- btnStart . Text = i18n . btnStarting ;
578
+ btnStart . Text = I18n . btnStarting ;
566
579
}
567
580
else
568
581
{
569
582
btnStart . Enabled = true ;
570
- btnStart . Text = i18n . btnStartDef ;
583
+ btnStart . Text = I18n . btnStartDef ;
571
584
}
572
585
} ;
573
586
Invoke ( action ) ;
@@ -588,15 +601,18 @@ private void FromHandle(bool isStart)
588
601
if ( _Setting . ControllerEnabled )
589
602
{
590
603
_Controller = new Controller ( ) ;
604
+ ControllerPtr = _Controller . Handle ;
591
605
_Controller . Show ( ) ;
592
606
}
607
+ _Timer ? . Start ( ) ;
593
608
}
594
609
else
595
610
{
596
611
_Controller ? . Dispose ( ) ;
597
612
Show ( ) ;
598
613
Activate ( ) ;
599
614
Focus ( ) ;
615
+ _Timer ? . Stop ( ) ;
600
616
}
601
617
} ;
602
618
Invoke ( action ) ;
@@ -621,7 +637,7 @@ void LoadHistoryIPs(bool isReload = false)
621
637
else
622
638
action ( ) ;
623
639
}
624
- #endregion
640
+ #endregion
625
641
626
642
#region 配置项改变事件
627
643
/// <summary>
@@ -903,7 +919,7 @@ private void linkEnabledADB_Click(object sender, EventArgs e)
903
919
904
920
private void linkSetPort_Click ( object sender , EventArgs e )
905
921
{
906
- if ( UIMessageBox . Show ( i18n . linkSetPort , i18n . linkSetPortTitle ,
922
+ if ( UIMessageBox . Show ( I18n . linkSetPort , I18n . linkSetPortTitle ,
907
923
_Setting . DarkMode ? UIStyle . Black : UIStyle . Gray , UIMessageBoxButtons . OKCancel , false ) )
908
924
{
909
925
var batPath = ScrcpyPath + "SetProt.bat" ;
@@ -972,27 +988,27 @@ private void lbAllShortcut_Click(object sender, EventArgs e)
972
988
973
989
private void linkIME_Click ( object sender , EventArgs e )
974
990
{
975
- var list = i18n . imes . Values . ToList ( ) ;
991
+ var list = I18n . imes . Values . ToList ( ) ;
976
992
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 ) )
978
994
{
979
995
_Setting . IME = select ;
980
- linkIME . Text = i18n . imes [ ( InputMethod ) _Setting . IME ] ;
996
+ linkIME . Text = I18n . imes [ ( InputMethod ) _Setting . IME ] ;
981
997
}
982
998
}
983
999
984
1000
private void linkLang_Click ( object sender , EventArgs e )
985
1001
{
986
- var list = i18n . langs ;
1002
+ var list = I18n . langs ;
987
1003
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 ) )
989
1005
{
990
1006
if ( select == ( int ) _Setting . Language )
991
1007
{
992
1008
return ;
993
1009
}
994
1010
_Setting . Language = ( Lang ) select ;
995
- LoadResEn ( ) ;
1011
+ LoadLangRes ( ) ;
996
1012
997
1013
System . Windows . Forms . Application . Restart ( ) ;
998
1014
}
0 commit comments