Skip to content

Commit

Permalink
修复长时间连接后关闭窗口不弹出主界面的问题 #58
Browse files Browse the repository at this point in the history
  • Loading branch information
pdone committed Jan 19, 2024
1 parent 218eea0 commit 93cca99
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 68 deletions.
2 changes: 1 addition & 1 deletion FreeControl/Controller.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions FreeControl/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public Controller()
/// <summary>
/// 更新控制器位置
/// </summary>
public void UpdateLocation()
{
Action action = () =>
{
// 减去控制器自身默认宽度
Location = new Point(Main._Setting.ScrcpyPointX - 57, Main._Setting.ScrcpyPointY);
};
Invoke(action);
}
//public void UpdateLocation()
//{
// Action action = () =>
// {
// // 减去控制器自身默认宽度
// Location = new Point(Main._Setting.ScrcpyPointX - 57, Main._Setting.ScrcpyPointY);
// };
// Invoke(action);
//}

/// <summary>
/// 初始化窗口大小和位置
Expand Down
82 changes: 49 additions & 33 deletions FreeControl/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@ public class Info
public static string NameVersion { get; set; }
}

public MultiLanguage i18n;
/// <summary>
/// 多语言支持
/// </summary>
public MultiLanguage I18n;

public static IntPtr ControllerPtr = IntPtr.Zero;

/// <summary>
/// 是否启用输入法切换
/// 保持后台活跃 避免界面隐藏后被自动回收
/// </summary>
public readonly bool EnableSwitchIME = false;
private readonly System.Timers.Timer _Timer;
#endregion

#region 构造函数
Expand All @@ -99,12 +104,20 @@ public Main()
{
// 获取用户配置数据
_Setting = GetUserData();
LoadResEn();

// 加载语言资源
LoadLangRes();
// 设置语言
string lang = _Setting.Language.GetDesc();
i18n = new MultiLanguage(_Setting.Language);
I18n = new MultiLanguage(_Setting.Language);
System.Globalization.CultureInfo UICulture = new System.Globalization.CultureInfo(lang);
Thread.CurrentThread.CurrentUICulture = UICulture;
// 界面保活
_Timer = new System.Timers.Timer(_Setting.Heartbeat);
_Timer.Elapsed += (sender, e) =>
{
Logger.Info("", "alive");
};

InitializeComponent();
InitPdone();
IsInit = false;
Expand Down Expand Up @@ -135,10 +148,10 @@ public static Setting GetUserData()
Directory.CreateDirectory(UserDataPath);
if (!File.Exists(fullPath))
{
File.WriteAllText(fullPath, JsonHelper.json(tempData));
File.WriteAllText(fullPath, JsonHelper.Obj2Str(tempData));
}
StreamReader reader = File.OpenText(fullPath);
tempData = JsonHelper.jsonDes<Setting>(reader.ReadToEnd());
tempData = JsonHelper.Str2Obj<Setting>(reader.ReadToEnd());
reader.Close();
return tempData;
}
Expand All @@ -158,7 +171,7 @@ public static void SetUserData(Setting userData)
{
var fullPath = Path.Combine(UserDataPath, "config.json");
Directory.CreateDirectory(UserDataPath);
File.WriteAllText(fullPath, JsonHelper.json(userData));
File.WriteAllText(fullPath, JsonHelper.Obj2Str(userData));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -197,8 +210,8 @@ public void InitPdone()
}

#region 控件状态
uiLabel7.Visible = EnableSwitchIME;
linkIME.Visible = EnableSwitchIME;
uiLabel7.Visible = _Setting.EnableSwitchIME;
linkIME.Visible = _Setting.EnableSwitchIME;
#endregion

#region 事件绑定
Expand Down Expand Up @@ -280,9 +293,9 @@ public void InitPdone()
#endregion

#region 配置项默认值
comboPx.Items[0] = i18n.def;
comboMbps.Items[0] = i18n.def;
comboMaxFPS.Items[0] = i18n.def;
comboPx.Items[0] = I18n.def;
comboMbps.Items[0] = I18n.def;
comboMaxFPS.Items[0] = I18n.def;
comboPx.SelectedIndex = _Setting.PXIndex;
comboMbps.SelectedIndex = _Setting.BitRateIndex;
comboMaxFPS.SelectedIndex = _Setting.MaxFPSIndex;
Expand All @@ -305,9 +318,9 @@ public void InitPdone()
cbxShowTouches.Checked = _Setting.ShowTouches;
cbxReadOnly.Checked = _Setting.ReadOnly;
cbxAudioEnabled.Checked = _Setting.AudioEnabled;
linkIME.Text = i18n.imes[(InputMethod)_Setting.IME];
tbxIp.Watermark = i18n.tbxIpPlaceholder;
tbxPort.Watermark = i18n.tbxPortPlaceholder;
linkIME.Text = I18n.imes[(InputMethod)_Setting.IME];
tbxIp.Watermark = I18n.tbxIpPlaceholder;
tbxPort.Watermark = I18n.tbxPortPlaceholder;
#endregion
}

Expand All @@ -334,9 +347,9 @@ private void ExtractResource(bool reload = false)
}

/// <summary>
/// 加载英文资源
/// 加载语言资源 此资源控制界面布局
/// </summary>
public void LoadResEn()
public void LoadLangRes()
{
if (_Setting.Language == Lang.en)
{
Expand Down Expand Up @@ -365,7 +378,7 @@ private void StartButtonClick(object sender, EventArgs e)
if (_Setting.UseWireless &&
(string.IsNullOrWhiteSpace(_Setting.IPAddress) || string.IsNullOrWhiteSpace(_Setting.Port)))
{
ShowMessage(i18n.msgIpNull);
ShowMessage(I18n.msgIpNull);
return;
}

Expand Down Expand Up @@ -511,20 +524,20 @@ private void RunScrcpy()
scrcpy.Exited += (ss, ee) =>
{
SetUserData(_Setting);// 关闭scrcpy后保存一下配置文件
if (EnableSwitchIME && _Setting.IME != 0 && _Setting.IMEOrigin.IsNotNull())
if (_Setting.EnableSwitchIME && _Setting.IME != 0 && _Setting.IMEOrigin.IsNotNull())
{
ADB.Execute($"shell ime set {_Setting.IMEOrigin}");
}
MoveListener.StopListening();
FromHandle(false);
ButtonHandle(false);
LoadHistoryIPs(true);
ShowMessage(i18n.msgExit);
ShowMessage(I18n.msgExit);
};
scrcpy.BeginErrorReadLine();
scrcpy.BeginOutputReadLine();

if (EnableSwitchIME && _Setting.IME != 0)
if (_Setting.EnableSwitchIME && _Setting.IME != 0)
{
// 获取当前输入法
string strCurIME = ADB.Execute($"adb shell settings get secure default_input_method");
Expand Down Expand Up @@ -562,12 +575,12 @@ private void ButtonHandle(bool isStart)
if (isStart)
{
btnStart.Enabled = false;
btnStart.Text = i18n.btnStarting;
btnStart.Text = I18n.btnStarting;
}
else
{
btnStart.Enabled = true;
btnStart.Text = i18n.btnStartDef;
btnStart.Text = I18n.btnStartDef;
}
};
Invoke(action);
Expand All @@ -588,15 +601,18 @@ private void FromHandle(bool isStart)
if (_Setting.ControllerEnabled)
{
_Controller = new Controller();
ControllerPtr = _Controller.Handle;
_Controller.Show();
}
_Timer?.Start();
}
else
{
_Controller?.Dispose();
Show();
Activate();
Focus();
_Timer?.Stop();
}
};
Invoke(action);
Expand All @@ -621,7 +637,7 @@ void LoadHistoryIPs(bool isReload = false)
else
action();
}
#endregion
#endregion

#region 配置项改变事件
/// <summary>
Expand Down Expand Up @@ -903,7 +919,7 @@ private void linkEnabledADB_Click(object sender, EventArgs e)

private void linkSetPort_Click(object sender, EventArgs e)
{
if (UIMessageBox.Show(i18n.linkSetPort, i18n.linkSetPortTitle,
if (UIMessageBox.Show(I18n.linkSetPort, I18n.linkSetPortTitle,
_Setting.DarkMode ? UIStyle.Black : UIStyle.Gray, UIMessageBoxButtons.OKCancel, false))
{
var batPath = ScrcpyPath + "SetProt.bat";
Expand Down Expand Up @@ -972,27 +988,27 @@ private void lbAllShortcut_Click(object sender, EventArgs e)

private void linkIME_Click(object sender, EventArgs e)
{
var list = i18n.imes.Values.ToList();
var list = I18n.imes.Values.ToList();
int select = _Setting.IME;
if (UISelectDialog.ShowSelectDialog(this, ref select, list, i18n.linkImeTitle, i18n.linkImeContent))
if (UISelectDialog.ShowSelectDialog(this, ref select, list, I18n.linkImeTitle, I18n.linkImeContent))
{
_Setting.IME = select;
linkIME.Text = i18n.imes[(InputMethod)_Setting.IME];
linkIME.Text = I18n.imes[(InputMethod)_Setting.IME];
}
}

private void linkLang_Click(object sender, EventArgs e)
{
var list = i18n.langs;
var list = I18n.langs;
int select = (int)_Setting.Language;
if (UISelectDialog.ShowSelectDialog(this, ref select, list, i18n.linkLangTitle, i18n.linkLangContent))
if (UISelectDialog.ShowSelectDialog(this, ref select, list, I18n.linkLangTitle, I18n.linkLangContent))
{
if (select == (int)_Setting.Language)
{
return;
}
_Setting.Language = (Lang)select;
LoadResEn();
LoadLangRes();

System.Windows.Forms.Application.Restart();
}
Expand Down
4 changes: 2 additions & 2 deletions FreeControl/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.6.8")]
[assembly: AssemblyVersion("1.6.8")]
[assembly: AssemblyFileVersion("1.6.9")]
[assembly: AssemblyVersion("1.6.9")]
10 changes: 10 additions & 0 deletions FreeControl/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public List<string> ControllerButton
/// </summary>
public string Version { get; set; } = "1.0.0";

/// <summary>
/// 是否启用输入法切换功能
/// </summary>
public bool EnableSwitchIME { get; set; } = false;

/// <summary>
/// 启动Scrcpy时使用的输入法枚举值
/// </summary>
Expand All @@ -196,5 +201,10 @@ public List<string> ControllerButton
/// 主窗口 y坐标
/// </summary>
public int MainWindowY { get; set; } = 0;

/// <summary>
/// 心跳间隔 单位:毫秒
/// </summary>
public int Heartbeat { get; set; } = 60000;
}
}
4 changes: 4 additions & 0 deletions FreeControl/Update.en.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Free Control Update Record

## v1.6.9
- Fix bug
- Some optimized

## v1.6.8
- Fix bug
- Remove automatic switching IME
Expand Down
4 changes: 4 additions & 0 deletions FreeControl/Update.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Free Control 更新记录

## v1.6.9
- 修复了一些bug
- 优化了代码

## v1.6.8
- 修复了一些bug
- 移除自动切换输入法功能
Expand Down
2 changes: 1 addition & 1 deletion FreeControl/Utils/ADB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static bool Screenshot()
/// <returns></returns>
public static void ExecuteShell(string command)
{
Logger.Info($"{command}", $"ADB Shell");
Logger.Info($"{command}", $"adb shell");

var AdbProcessInfo = new ProcessStartInfo($"{ADBPath}adb.exe")
{
Expand Down
37 changes: 17 additions & 20 deletions FreeControl/Utils/JsonHelper.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
//using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.Script.Serialization;

namespace FreeControl.Utils
{
public class JsonHelper
{
/// <summary>
/// json反序列化
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static T jsonDes<T>(string input)
/// <summary>
/// Json字符串转对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <returns></returns>
public static T Str2Obj<T>(string input)
{
//return JsonConvert.DeserializeObject<T>(input);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
return javaScriptSerializer.Deserialize<T>(input);
return new JavaScriptSerializer().Deserialize<T>(input);
}
public static string json(object obj)

/// <summary>
/// 对象转Json字符串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string Obj2Str(object obj)
{
//return JsonConvert.SerializeObject(obj, Formatting.Indented);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
return javaScriptSerializer.Serialize(obj);
return new JavaScriptSerializer().Serialize(obj);
}
}
}
Loading

0 comments on commit 93cca99

Please sign in to comment.