优化状态更新和图像处理流程,按照规范实现状态变更和UI更新

This commit is contained in:
zqm
2025-10-30 13:36:59 +08:00
parent 8f8c32879d
commit 2c51fe83f4

View File

@@ -113,9 +113,10 @@ namespace JoyD.Windows.CS.Toprie
bool isDisconnected = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Disconnected; bool isDisconnected = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Disconnected;
bool isReconnecting = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Reconnecting; bool isReconnecting = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Reconnecting;
bool isPaused = _isPaused; // 使用_isPaused标志判断暂停状态 bool isPaused = _isPaused; // 使用_isPaused标志判断暂停状态
bool isPingFailed = !IsDevicePingable;
// 如果需要显示信息创建InfoImage // 如果需要显示信息创建InfoImage
if (isDisconnected || isReconnecting || isPaused) if (isDisconnected || isReconnecting || isPaused || isPingFailed)
{ {
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT); _infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
using (Graphics g = Graphics.FromImage(_infoImage)) using (Graphics g = Graphics.FromImage(_infoImage))
@@ -130,11 +131,25 @@ namespace JoyD.Windows.CS.Toprie
string text = ""; string text = "";
Color textColor = Color.White; Color textColor = Color.White;
if (isDisconnected || isReconnecting) // 根据要求的优先级显示信息先检查Ping状态和连接状态最后检查暂停状态
if (isPingFailed || isDisconnected || isReconnecting)
{ {
// 断开或重连状态 // Ping不通或断开连接状态
text = isReconnecting ? "正在重连..." : "连接断开"; if (isReconnecting)
textColor = isReconnecting ? Color.Yellow : Color.Red; {
text = "正在重连...";
textColor = Color.Yellow;
}
else if (isDisconnected)
{
text = "连接断开";
textColor = Color.Red;
}
else if (isPingFailed)
{
text = "Ping不通";
textColor = Color.Red;
}
} }
else if (isPaused) else if (isPaused)
{ {
@@ -142,10 +157,6 @@ namespace JoyD.Windows.CS.Toprie
text = "暂停"; text = "暂停";
textColor = Color.Red; textColor = Color.Red;
} }
// 添加Ping状态信息
string pingStatus = IsDevicePingable ? "Ping通" : "Ping不通";
string pingText = $"设备{pingStatus}";
Color pingColor = IsDevicePingable ? Color.Green : Color.Red;
// 绘制文本 // 绘制文本
using (Font font = new Font("Arial", 48, FontStyle.Bold)) using (Font font = new Font("Arial", 48, FontStyle.Bold))
@@ -154,22 +165,9 @@ namespace JoyD.Windows.CS.Toprie
StringFormat format = new StringFormat(); StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center; format.Alignment = StringAlignment.Center;
// 将主文本位置调整到上方 // 将主文本居中显示
g.DrawString(text, font, textBrush, g.DrawString(text, font, textBrush,
new RectangleF(0, BUFFER_HEIGHT / 4, BUFFER_WIDTH, BUFFER_HEIGHT / 3), new RectangleF(0, BUFFER_HEIGHT / 3, BUFFER_WIDTH, BUFFER_HEIGHT / 3),
format);
}
// 绘制Ping状态信息
using (Font pingFont = new Font("Arial", 36, FontStyle.Regular))
using (SolidBrush pingBrush = new SolidBrush(pingColor))
{
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
// 将Ping状态文本位置放在主文本下方
g.DrawString(pingText, pingFont, pingBrush,
new RectangleF(0, BUFFER_HEIGHT * 2 / 3, BUFFER_WIDTH, BUFFER_HEIGHT / 3),
format); format);
} }
} }
@@ -630,12 +628,8 @@ namespace JoyD.Windows.CS.Toprie
try try
{ {
// 连接状态检查 - 只在设备实际连接时处理图像 // 移除连接状态检查确保在任何状态下都能更新UI
if (_deviceManager.ConnectionStatus != ConnectionStatus.Connected) // 根据流程要求需要在断开或Ping不通时也能显示相关信息
{
Console.WriteLine("设备未连接,跳过图像更新");
return;
}
// 检查图像缓冲区是否有效 // 检查图像缓冲区是否有效
if (_imageBuffer == null) if (_imageBuffer == null)
@@ -868,6 +862,9 @@ namespace JoyD.Windows.CS.Toprie
case ConnectionStatus.Connected: case ConnectionStatus.Connected:
Console.WriteLine("设备已连接"); Console.WriteLine("设备已连接");
// 连接时调用更新Info
UpdateInfo();
// 仅在首次连接时设置为热图模式,重连时保留之前的模式 // 仅在首次连接时设置为热图模式,重连时保留之前的模式
if (!_isReceivingImage) // 首次连接时_isReceivingImage为false if (!_isReceivingImage) // 首次连接时_isReceivingImage为false
{ {