using System;
namespace Hua.Todo.Avalonia.Services.Platforms;
///
/// Linux 平台全局热键服务实现类
/// 提供基于 Linux 平台的全局热键支持
///
public class LinuxGlobalHotKeyService : IGlobalHotKeyService
{
private Action? _callback;
private string? _modifiers;
private string? _key;
///
/// Linux 平台支持全局热键
///
public bool IsSupported => true;
///
/// 注册全局热键
///
/// 修饰键(如 Alt、Control;多个键用逗号分隔)。
/// 主键(如 X、C)。
/// 热键触发时回调(不得阻塞,应自行切回 UI 线程)。
public void RegisterHotKey(string modifiers, string key, Action callback)
{
try
{
_callback = callback;
_modifiers = modifiers;
_key = key;
// Linux 平台热键注册逻辑
// 这里可以使用如 libX11 或其他 Linux 热键库
// 由于 Avalonia 本身对 Linux 热键支持有限,这里提供基本实现
Console.WriteLine($"[LinuxGlobalHotKeyService] Registered hotkey: {modifiers}+{key}");
}
catch (Exception ex)
{
Console.WriteLine($"[LinuxGlobalHotKeyService] Failed to register hotkey: {ex.Message}");
}
}
///
/// 注销已注册的热键
///
public void UnregisterHotKey()
{
try
{
// Linux 平台热键注销逻辑
Console.WriteLine("[LinuxGlobalHotKeyService] Unregistered hotkey");
}
catch (Exception ex)
{
Console.WriteLine($"[LinuxGlobalHotKeyService] Failed to unregister hotkey: {ex.Message}");
}
}
///
/// 更新热键配置
///
/// 新的修饰键。
/// 新的主键。
public void UpdateHotKey(string modifiers, string key)
{
try
{
UnregisterHotKey();
if (_callback != null)
{
RegisterHotKey(modifiers, key, _callback);
}
}
catch (Exception ex)
{
Console.WriteLine($"[LinuxGlobalHotKeyService] Failed to update hotkey: {ex.Message}");
}
}
}