Files
community/Ink Canvas/MainWindow_cs/MW_UriHandler.cs
T

247 lines
11 KiB
C#
Raw Normal View History

2026-02-04 11:09:49 +08:00
using Ink_Canvas.Helpers;
2026-02-23 13:46:13 +08:00
using Newtonsoft.Json;
2026-02-04 11:09:49 +08:00
using System;
2026-02-22 20:52:14 +08:00
using System.IO;
2026-02-04 11:09:49 +08:00
using System.Windows;
namespace Ink_Canvas
{
2026-02-22 20:52:14 +08:00
/// <summary>
/// 处理 icc: URL 协议命令
2026-02-23 13:46:13 +08:00
/// 支持:收纳/展开/切换、彻底隐藏、点名/计时器/白板、工具状态切换与查询、配置方案列表与切换。
/// 配置方案:icc://config-profile/list 输出列表到 %TEMP%\InkCanvasConfigProfileList.json
/// icc://config-profile/switch?name=方案名 切换方案,结果写入 %TEMP%\InkCanvasConfigProfileSwitchResult.txt。
2026-02-22 20:52:14 +08:00
/// </summary>
2026-02-04 11:09:49 +08:00
public partial class MainWindow
{
public void HandleUriCommand(string uri)
{
try
{
if (string.IsNullOrEmpty(uri)) return;
if (!Settings.Advanced.IsEnableUriScheme)
{
2026-02-22 20:52:14 +08:00
LogHelper.WriteLogToFile($"URI 协议已禁用,忽略请求: {uri}", LogHelper.LogType.Warning);
return;
}
2026-02-22 20:52:14 +08:00
LogHelper.WriteLogToFile($"正在处理 URI 命令: {uri}", LogHelper.LogType.Event);
2026-02-04 11:09:49 +08:00
2026-02-22 20:52:14 +08:00
string command = ParseUriCommand(uri);
if (string.IsNullOrEmpty(command)) return;
2026-02-22 20:52:14 +08:00
string path = command;
string pathLower = path.ToLowerInvariant();
2026-02-22 20:52:14 +08:00
switch (pathLower)
2026-02-04 11:09:49 +08:00
{
case "fold":
if (!isFloatingBarFolded)
{
FoldFloatingBar_MouseUp(new object(), null);
ShowNotification("已进入收纳模式");
}
2026-02-22 20:52:14 +08:00
return;
2026-02-04 11:09:49 +08:00
case "unfold":
2026-02-22 20:52:14 +08:00
case "show":
2026-02-04 11:09:49 +08:00
if (isFloatingBarFolded)
{
UnFoldFloatingBar_MouseUp(new object(), null);
ShowNotification("已退出收纳模式");
}
2026-02-22 20:52:14 +08:00
return;
2026-02-04 11:09:49 +08:00
case "toggle":
if (isFloatingBarFolded)
{
UnFoldFloatingBar_MouseUp(new object(), null);
ShowNotification("已退出收纳模式");
}
else
{
FoldFloatingBar_MouseUp(new object(), null);
ShowNotification("已进入收纳模式");
}
2026-02-22 20:52:14 +08:00
return;
case "thoroughhideon":
Settings.Automation.ThoroughlyHideWhenFolded = true;
SaveSettingsToFile();
ShowNotification("已开启:收起时彻底隐藏");
if (isFloatingBarFolded)
this.Visibility = Visibility.Hidden;
2026-02-22 20:52:14 +08:00
return;
case "thoroughhideoff":
Settings.Automation.ThoroughlyHideWhenFolded = false;
SaveSettingsToFile();
ShowNotification("已关闭:收起时彻底隐藏");
this.Visibility = Visibility.Visible;
2026-02-22 20:52:14 +08:00
return;
case "thoroughhidetoggle":
Settings.Automation.ThoroughlyHideWhenFolded = !Settings.Automation.ThoroughlyHideWhenFolded;
SaveSettingsToFile();
ShowNotification(Settings.Automation.ThoroughlyHideWhenFolded ? "已开启:收起时彻底隐藏" : "已关闭:收起时彻底隐藏");
if (isFloatingBarFolded)
this.Visibility = Settings.Automation.ThoroughlyHideWhenFolded ? Visibility.Hidden : Visibility.Visible;
2026-02-22 20:52:14 +08:00
return;
case "randone":
SymbolIconRandOne_MouseUp(null, null);
2026-02-22 20:52:14 +08:00
return;
case "rand":
SymbolIconRand_MouseUp(null, null);
2026-02-22 20:52:14 +08:00
return;
case "timer":
ImageCountdownTimer_MouseUp(null, null);
2026-02-22 20:52:14 +08:00
return;
case "whiteboard":
case "board":
ImageBlackboard_MouseUp(null, null);
2026-02-22 20:52:14 +08:00
return;
}
2026-02-22 20:52:14 +08:00
if (pathLower == "tool/state")
{
string state = GetCurrentSelectedMode() ?? "cursor";
string stateFile = Path.Combine(Path.GetTempPath(), "InkCanvasToolState.txt");
File.WriteAllText(stateFile, state, System.Text.Encoding.UTF8);
return;
}
if (pathLower.StartsWith("tool/"))
{
string tool = pathLower.Length > 5 ? pathLower.Substring(5).TrimEnd('/') : "";
switch (tool)
{
case "pen":
case "color":
PenIcon_Click(null, null);
break;
case "cursor":
CursorIcon_Click(null, null);
break;
case "eraser":
2026-03-03 16:04:20 +08:00
PenIcon_Click(null, null);
2026-02-22 20:52:14 +08:00
EraserIcon_Click(null, null);
break;
case "eraserbystrokes":
case "eraserstroke":
2026-03-03 16:04:20 +08:00
PenIcon_Click(null, null);
2026-02-22 20:52:14 +08:00
EraserIconByStrokes_Click(EraserByStrokes_Icon, null);
break;
default:
LogHelper.WriteLogToFile($"未知的 URI 工具: {tool}", LogHelper.LogType.Warning);
break;
}
return;
2026-02-04 11:09:49 +08:00
}
2026-02-22 20:52:14 +08:00
2026-02-23 13:46:13 +08:00
if (pathLower == "config-profile/list")
{
WriteConfigProfileListToTemp();
return;
}
if (pathLower.StartsWith("config-profile/switch"))
{
string profileName = GetUriQueryValue(uri, "name");
HandleUriConfigProfileSwitch(profileName);
return;
}
2026-02-22 20:52:14 +08:00
LogHelper.WriteLogToFile($"未知的 URI 命令: {command}", LogHelper.LogType.Warning);
2026-02-04 11:09:49 +08:00
}
catch (Exception ex)
{
2026-02-22 20:52:14 +08:00
LogHelper.WriteLogToFile($"处理 URI 命令时出错: {ex.Message}", LogHelper.LogType.Error);
2026-02-04 11:09:49 +08:00
}
}
2026-02-22 20:52:14 +08:00
private static string ParseUriCommand(string uri)
{
if (string.IsNullOrWhiteSpace(uri) || !uri.Trim().StartsWith("icc:", StringComparison.OrdinalIgnoreCase))
return "";
if (Uri.TryCreate(uri, UriKind.Absolute, out Uri uriObj))
{
string host = (uriObj.Host ?? "").Trim().ToLowerInvariant();
string path = (uriObj.AbsolutePath ?? "").Trim('/').ToLowerInvariant();
if (!string.IsNullOrEmpty(host))
return string.IsNullOrEmpty(path) ? host : host + "/" + path;
if (!string.IsNullOrEmpty(path))
return path;
}
string raw = uri.Trim().Substring(4).TrimStart('/').ToLowerInvariant();
return raw;
}
2026-02-23 13:46:13 +08:00
private static string GetUriQueryValue(string uri, string key)
{
if (string.IsNullOrEmpty(uri) || string.IsNullOrEmpty(key)) return "";
try
{
if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri u) || string.IsNullOrEmpty(u.Query))
return "";
string q = u.Query.TrimStart('?');
foreach (var pair in q.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries))
{
var kv = pair.Split(new[] { '=' }, 2, StringSplitOptions.None);
if (kv.Length == 2 && kv[0].Trim().Equals(key, StringComparison.OrdinalIgnoreCase))
return Uri.UnescapeDataString(kv[1].Trim());
}
}
catch (Exception ex) { LogHelper.WriteLogToFile($"解析 URI 参数失败: {ex.Message}", LogHelper.LogType.Warning); }
return "";
}
private const string ConfigProfileListTempFile = "InkCanvasConfigProfileList.json";
private const string ConfigProfileSwitchResultTempFile = "InkCanvasConfigProfileSwitchResult.txt";
private void WriteConfigProfileListToTemp()
{
try
{
var names = ConfigProfileManager.ListProfileNames();
var current = _lastAppliedProfileName ?? "";
var payload = new { list = names, current = current };
string path = Path.Combine(Path.GetTempPath(), ConfigProfileListTempFile);
File.WriteAllText(path, JsonConvert.SerializeObject(payload, Formatting.Indented), System.Text.Encoding.UTF8);
LogHelper.WriteLogToFile($"URI 已输出配置方案列表到: {path}", LogHelper.LogType.Event);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"URI 输出配置方案列表失败: {ex.Message}", LogHelper.LogType.Error);
}
}
private void HandleUriConfigProfileSwitch(string profileName)
{
string resultPath = Path.Combine(Path.GetTempPath(), ConfigProfileSwitchResultTempFile);
try
{
if (string.IsNullOrWhiteSpace(profileName))
{
File.WriteAllText(resultPath, "error: 缺少参数 name", System.Text.Encoding.UTF8);
LogHelper.WriteLogToFile("URI 切换配置方案: 未指定方案名", LogHelper.LogType.Warning);
return;
}
if (!ConfigProfileManager.ApplyProfile(profileName.Trim()))
{
File.WriteAllText(resultPath, "error: 方案不存在或应用失败", System.Text.Encoding.UTF8);
ShowNotification($"切换失败:方案「{profileName}」不存在");
return;
}
_lastAppliedProfileName = profileName.Trim();
ReloadSettingsFromFile();
File.WriteAllText(resultPath, "ok", System.Text.Encoding.UTF8);
ShowNotification($"已通过 URI 切换至方案「{profileName}」");
LogHelper.WriteLogToFile($"URI 已切换配置方案: {profileName}", LogHelper.LogType.Event);
}
catch (Exception ex)
{
try { File.WriteAllText(resultPath, "error: " + ex.Message, System.Text.Encoding.UTF8); } catch { }
LogHelper.WriteLogToFile($"URI 切换配置方案失败: {ex.Message}", LogHelper.LogType.Error);
}
}
2026-02-04 11:09:49 +08:00
}
}