Skip to content

Commit 93cca99

Browse files
committed
修复长时间连接后关闭窗口不弹出主界面的问题 #58
1 parent 218eea0 commit 93cca99

File tree

10 files changed

+214
-68
lines changed

10 files changed

+214
-68
lines changed

FreeControl/Controller.Designer.cs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeControl/Controller.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public Controller()
1919
/// <summary>
2020
/// 更新控制器位置
2121
/// </summary>
22-
public void UpdateLocation()
23-
{
24-
Action action = () =>
25-
{
26-
// 减去控制器自身默认宽度
27-
Location = new Point(Main._Setting.ScrcpyPointX - 57, Main._Setting.ScrcpyPointY);
28-
};
29-
Invoke(action);
30-
}
22+
//public void UpdateLocation()
23+
//{
24+
// Action action = () =>
25+
// {
26+
// // 减去控制器自身默认宽度
27+
// Location = new Point(Main._Setting.ScrcpyPointX - 57, Main._Setting.ScrcpyPointY);
28+
// };
29+
// Invoke(action);
30+
//}
3131

3232
/// <summary>
3333
/// 初始化窗口大小和位置

FreeControl/Main.cs

+49-33
Original file line numberDiff line numberDiff line change
@@ -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
}

FreeControl/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
3535
//[assembly: AssemblyVersion("1.0.0")]
36-
[assembly: AssemblyFileVersion("1.6.8")]
37-
[assembly: AssemblyVersion("1.6.8")]
36+
[assembly: AssemblyFileVersion("1.6.9")]
37+
[assembly: AssemblyVersion("1.6.9")]

FreeControl/Setting.cs

+10
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ public List<string> ControllerButton
173173
/// </summary>
174174
public string Version { get; set; } = "1.0.0";
175175

176+
/// <summary>
177+
/// 是否启用输入法切换功能
178+
/// </summary>
179+
public bool EnableSwitchIME { get; set; } = false;
180+
176181
/// <summary>
177182
/// 启动Scrcpy时使用的输入法枚举值
178183
/// </summary>
@@ -196,5 +201,10 @@ public List<string> ControllerButton
196201
/// 主窗口 y坐标
197202
/// </summary>
198203
public int MainWindowY { get; set; } = 0;
204+
205+
/// <summary>
206+
/// 心跳间隔 单位:毫秒
207+
/// </summary>
208+
public int Heartbeat { get; set; } = 60000;
199209
}
200210
}

FreeControl/Update.en.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Free Control Update Record
22

3+
## v1.6.9
4+
- Fix bug
5+
- Some optimized
6+
37
## v1.6.8
48
- Fix bug
59
- Remove automatic switching IME

FreeControl/Update.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Free Control 更新记录
22

3+
## v1.6.9
4+
- 修复了一些bug
5+
- 优化了代码
6+
37
## v1.6.8
48
- 修复了一些bug
59
- 移除自动切换输入法功能

FreeControl/Utils/ADB.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static bool Screenshot()
8888
/// <returns></returns>
8989
public static void ExecuteShell(string command)
9090
{
91-
Logger.Info($"{command}", $"ADB Shell");
91+
Logger.Info($"{command}", $"adb shell");
9292

9393
var AdbProcessInfo = new ProcessStartInfo($"{ADBPath}adb.exe")
9494
{

FreeControl/Utils/JsonHelper.cs

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
//using Newtonsoft.Json;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.IO;
5-
using System.Linq;
6-
using System.Text;
7-
using System.Web.Script.Serialization;
1+
using System.Web.Script.Serialization;
82

93
namespace FreeControl.Utils
104
{
115
public class JsonHelper
126
{
13-
/// <summary>
14-
/// json反序列化
15-
/// </summary>
16-
/// <param name="input"></param>
17-
/// <returns></returns>
18-
public static T jsonDes<T>(string input)
7+
/// <summary>
8+
/// Json字符串转对象
9+
/// </summary>
10+
/// <typeparam name="T"></typeparam>
11+
/// <param name="input"></param>
12+
/// <returns></returns>
13+
public static T Str2Obj<T>(string input)
1914
{
20-
//return JsonConvert.DeserializeObject<T>(input);
21-
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
22-
return javaScriptSerializer.Deserialize<T>(input);
15+
return new JavaScriptSerializer().Deserialize<T>(input);
2316
}
24-
public static string json(object obj)
17+
18+
/// <summary>
19+
/// 对象转Json字符串
20+
/// </summary>
21+
/// <param name="obj"></param>
22+
/// <returns></returns>
23+
public static string Obj2Str(object obj)
2524
{
26-
//return JsonConvert.SerializeObject(obj, Formatting.Indented);
27-
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
28-
return javaScriptSerializer.Serialize(obj);
25+
return new JavaScriptSerializer().Serialize(obj);
2926
}
3027
}
3128
}

0 commit comments

Comments
 (0)