| | |
| | | using Moli.Desktop.Configuration; |
| | | using Moli.Desktop.Diagnostics; |
| | | using Moli.Desktop.Host; |
| | | using Moli.Desktop.Hotkeys; |
| | | using Moli.Desktop.Layout; |
| | | using Moli.Desktop.Plugins; |
| | | using Moli.Desktop.Runtime; |
| | |
| | | using MessageBox = System.Windows.MessageBox; |
| | | using WpfButton = System.Windows.Controls.Button; |
| | | using WpfTextBox = System.Windows.Controls.TextBox; |
| | | using WpfComboBox = System.Windows.Controls.ComboBox; |
| | | using WpfCheckBox = System.Windows.Controls.CheckBox; |
| | | |
| | | namespace Moli.Desktop.Management; |
| | | |
| | |
| | | private readonly DesktopPaths _paths; |
| | | private readonly DispatcherTimer _timer; |
| | | private AppRuntimeState? _state; |
| | | private IReadOnlyList<HotkeyBindingStatus> _hotkeys = []; |
| | | private string? _selectedWidgetForDisplayLayer; |
| | | private bool _isRefreshing; |
| | | |
| | |
| | | try |
| | | { |
| | | _state = await _runtime.GetStateAsync(); |
| | | _hotkeys = await _runtime.GetHotkeyStatusesAsync(); |
| | | var api = _apiStatus.GetStatus(); |
| | | var diagnostics = _diagnostics.Snapshot().OrderByDescending(entry => entry.Timestamp).ToArray(); |
| | | var webViewStatus = diagnostics.FirstOrDefault(entry => entry.Code == DiagnosticCodes.WebViewHostInitialized) is null |
| | |
| | | WidgetSelector.ItemsSource = widgets; |
| | | WidgetSelector.SelectedItem = widgets.FirstOrDefault(widget => widget.InstanceId == selected) ?? widgets.FirstOrDefault(); |
| | | WidgetsGrid.ItemsSource = widgets; |
| | | HotkeysGrid.ItemsSource = _hotkeys.Select(HotkeyRow.From).ToArray(); |
| | | SyncDisplayLayerSelector(force: _selectedWidgetForDisplayLayer != (WidgetSelector.SelectedItem as WidgetRow)?.InstanceId); |
| | | PluginsGrid.ItemsSource = _state.Plugins.Select(PluginRow.From).ToArray(); |
| | | DiagnosticsGrid.ItemsSource = diagnostics.Select(DiagnosticRow.From).ToArray(); |
| | |
| | | return; |
| | | } |
| | | |
| | | await ExecuteWidgetAsync(widget.InstanceId, new WidgetLayoutPatch { IsVisible = !widget.IsVisible }); |
| | | await ExecuteWidgetAsync(widget.InstanceId, new WidgetLayoutPatch { IsVisible = !widget.LayoutIsVisible }); |
| | | } |
| | | |
| | | private async void ToggleWidgetLocked_Click(object sender, RoutedEventArgs e) |
| | |
| | | DisplayLayerSelector.SelectedItem = DisplayLayer.Desktop; |
| | | _selectedWidgetForDisplayLayer = null; |
| | | ClickThroughButton.Content = "点击穿透"; |
| | | WidgetSessionText.Text = "选择一个组件后可查看本次会话可交互和有效穿透。"; |
| | | return; |
| | | } |
| | | |
| | | ClickThroughButton.Content = widget.ClickThrough ? "关闭穿透" : "开启穿透"; |
| | | WidgetSessionText.Text = $"本次会话可交互:{(widget.IsInteractable ? "是" : "否")};有效穿透:{(widget.ClickThrough ? "是" : "否")}。点击穿透会写入 layout.json,并同步本次会话覆盖,使窗口立刻可点或穿透。"; |
| | | |
| | | if (!force) |
| | | { |
| | |
| | | |
| | | private static string Html(string value) => System.Net.WebUtility.HtmlEncode(value); |
| | | |
| | | private sealed record WidgetRow(string InstanceId, string PluginId, string Bounds, string MonitorId, bool IsVisible, bool IsLocked, double Opacity, bool ClickThrough, DisplayLayer DisplayLayer) |
| | | private sealed record WidgetRow( |
| | | string InstanceId, |
| | | string PluginId, |
| | | string Bounds, |
| | | string MonitorId, |
| | | bool IsVisible, |
| | | bool LayoutIsVisible, |
| | | bool IsLocked, |
| | | double Opacity, |
| | | bool ClickThrough, |
| | | bool LayoutClickThrough, |
| | | bool IsInteractable, |
| | | DisplayLayer DisplayLayer) |
| | | { |
| | | public static WidgetRow From(WidgetRuntimeState state) |
| | | => new( |
| | |
| | | $"{state.Bounds.X:0},{state.Bounds.Y:0} {state.Bounds.Width:0}x{state.Bounds.Height:0}", |
| | | state.MonitorId.Value, |
| | | state.IsVisible, |
| | | state.LayoutIsVisible, |
| | | state.IsLocked, |
| | | state.Opacity, |
| | | state.ClickThrough, |
| | | state.LayoutClickThrough, |
| | | state.IsInteractable, |
| | | state.DisplayLayer); |
| | | } |
| | | |
| | | private async void AddHotkey_Click(object sender, RoutedEventArgs e) |
| | | => await EditHotkeyAsync(null); |
| | | |
| | | private async void EditHotkey_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (SelectedHotkey() is not { } row) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | await EditHotkeyAsync(row); |
| | | } |
| | | |
| | | private async void DeleteHotkey_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (SelectedHotkey() is not { } row) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | if (string.Equals(row.Source, "builtin", StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | MessageBox.Show(this, "内置快捷键不能删除,只能改键、禁用或恢复默认。", "快捷键", MessageBoxButton.OK, MessageBoxImage.Information); |
| | | return; |
| | | } |
| | | |
| | | var document = CurrentHotkeysDocument(); |
| | | document.Bindings.RemoveAll(binding => string.Equals(binding.Id, row.Id, StringComparison.OrdinalIgnoreCase)); |
| | | await SaveHotkeysDocumentAsync(document); |
| | | } |
| | | |
| | | private async void ToggleHotkeyEnabled_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (SelectedHotkey() is not { } row) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | var document = CurrentHotkeysDocument(); |
| | | var index = document.Bindings.FindIndex(binding => string.Equals(binding.Id, row.Id, StringComparison.OrdinalIgnoreCase)); |
| | | if (index < 0) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | document.Bindings[index] = document.Bindings[index] with { Enabled = !document.Bindings[index].Enabled }; |
| | | await SaveHotkeysDocumentAsync(document); |
| | | } |
| | | |
| | | private async void RestoreDefaultHotkeys_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | var result = await _runtime.RestoreDefaultHotkeysAsync(); |
| | | if (!result.Succeeded) |
| | | { |
| | | MessageBox.Show(this, result.Message ?? "恢复默认快捷键失败。", result.ErrorCode ?? "HotkeyRestoreFailed", MessageBoxButton.OK, MessageBoxImage.Warning); |
| | | } |
| | | |
| | | await RefreshAsync(); |
| | | } |
| | | |
| | | private HotkeyRow? SelectedHotkey() => HotkeysGrid.SelectedItem as HotkeyRow; |
| | | |
| | | private HotkeysDocument CurrentHotkeysDocument() |
| | | => new() |
| | | { |
| | | SchemaVersion = 1, |
| | | Bindings = [.. _hotkeys.Select(item => item.Binding)] |
| | | }; |
| | | |
| | | private async Task EditHotkeyAsync(HotkeyRow? existing) |
| | | { |
| | | var plugins = _state?.Plugins.Select(plugin => plugin.Id.Value).Distinct().ToArray() ?? []; |
| | | var instances = _state?.Widgets.Select(widget => (widget.PluginId.Value, widget.InstanceId.Value)).ToArray() ?? []; |
| | | var dialog = new HotkeyEditorDialog(existing?.ToBinding(), plugins, instances, PauseRecordingAsync, ResumeRecordingAsync) |
| | | { |
| | | Owner = this |
| | | }; |
| | | if (dialog.ShowDialog() != true || dialog.Result is null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | var document = CurrentHotkeysDocument(); |
| | | var index = document.Bindings.FindIndex(binding => string.Equals(binding.Id, dialog.Result.Id, StringComparison.OrdinalIgnoreCase)); |
| | | if (index >= 0) |
| | | { |
| | | document.Bindings[index] = dialog.Result; |
| | | } |
| | | else |
| | | { |
| | | document.Bindings.Add(dialog.Result); |
| | | } |
| | | |
| | | await SaveHotkeysDocumentAsync(document); |
| | | } |
| | | |
| | | private async Task SaveHotkeysDocumentAsync(HotkeysDocument document) |
| | | { |
| | | var result = await _runtime.SaveHotkeysAsync(document); |
| | | if (!result.Succeeded) |
| | | { |
| | | MessageBox.Show(this, result.Message ?? "保存快捷键失败。", result.ErrorCode ?? "HotkeySaveFailed", MessageBoxButton.OK, MessageBoxImage.Warning); |
| | | } |
| | | |
| | | await RefreshAsync(); |
| | | } |
| | | |
| | | private Task PauseRecordingAsync() => _runtime.PauseHotkeyRegistrationAsync(); |
| | | |
| | | private Task ResumeRecordingAsync() => _runtime.ResumeHotkeyRegistrationAsync(); |
| | | |
| | | private sealed record HotkeyRow(string Id, string Keys, string Action, string Target, bool Enabled, string Source, string Status) |
| | | { |
| | | public static HotkeyRow From(HotkeyBindingStatus status) |
| | | => new( |
| | | status.Binding.Id, |
| | | status.Binding.Keys, |
| | | status.Binding.Action, |
| | | FormatTarget(status.Binding.Target), |
| | | status.Binding.Enabled, |
| | | status.Binding.Source.ToString().ToLowerInvariant(), |
| | | status.Status); |
| | | |
| | | public HotkeyBinding ToBinding() |
| | | { |
| | | var parts = Target.Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| | | string? pluginId = null; |
| | | string? instanceId = null; |
| | | foreach (var part in parts) |
| | | { |
| | | if (part.StartsWith("plugin:", StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | pluginId = part["plugin:".Length..]; |
| | | } |
| | | else if (part.StartsWith("instance:", StringComparison.OrdinalIgnoreCase)) |
| | | { |
| | | instanceId = part["instance:".Length..]; |
| | | } |
| | | } |
| | | |
| | | return new HotkeyBinding |
| | | { |
| | | Id = Id, |
| | | Keys = Keys, |
| | | Action = Action, |
| | | Target = string.IsNullOrWhiteSpace(pluginId) && string.IsNullOrWhiteSpace(instanceId) |
| | | ? null |
| | | : new HotkeyTarget { PluginId = pluginId, InstanceId = instanceId }, |
| | | Enabled = Enabled, |
| | | Source = string.Equals(Source, "builtin", StringComparison.OrdinalIgnoreCase) ? HotkeySource.Builtin : HotkeySource.User |
| | | }; |
| | | } |
| | | |
| | | private static string FormatTarget(HotkeyTarget? target) |
| | | { |
| | | if (target is null || target.IsEmpty) |
| | | { |
| | | return ""; |
| | | } |
| | | |
| | | var plugin = string.IsNullOrWhiteSpace(target.PluginId) ? "" : $"plugin:{target.PluginId}"; |
| | | var instance = string.IsNullOrWhiteSpace(target.InstanceId) ? "" : $"instance:{target.InstanceId}"; |
| | | return string.Join(" ", new[] { plugin, instance }.Where(item => item.Length > 0)); |
| | | } |
| | | } |
| | | |
| | | private sealed record PluginRow(string Id, string Name, string Type, string State, string? ErrorCode, string Entry) |
| | |
| | | => new(entry.Timestamp, entry.Severity.ToString(), entry.Code, entry.Message); |
| | | } |
| | | |
| | | private sealed class HotkeyEditorDialog : Window |
| | | { |
| | | private readonly WpfTextBox _keys; |
| | | private readonly WpfComboBox _action; |
| | | private readonly WpfComboBox _plugin; |
| | | private readonly WpfComboBox _instance; |
| | | private readonly WpfCheckBox _enabled; |
| | | private readonly HotkeyBinding? _existing; |
| | | private readonly Func<Task> _pauseAsync; |
| | | private readonly Func<Task> _resumeAsync; |
| | | private readonly (string PluginId, string InstanceId)[] _instances; |
| | | private bool _recording; |
| | | |
| | | public HotkeyEditorDialog( |
| | | HotkeyBinding? existing, |
| | | IReadOnlyList<string> plugins, |
| | | IReadOnlyList<(string PluginId, string InstanceId)> instances, |
| | | Func<Task> pauseAsync, |
| | | Func<Task> resumeAsync) |
| | | { |
| | | _existing = existing; |
| | | _pauseAsync = pauseAsync; |
| | | _resumeAsync = resumeAsync; |
| | | _instances = instances.ToArray(); |
| | | Title = existing is null ? "新增快捷键" : "编辑快捷键"; |
| | | Width = 420; |
| | | Height = 360; |
| | | WindowStartupLocation = WindowStartupLocation.CenterOwner; |
| | | ResizeMode = ResizeMode.NoResize; |
| | | |
| | | _keys = new WpfTextBox { Text = existing?.Keys ?? "Ctrl+Shift+K", Margin = new Thickness(0, 0, 0, 8) }; |
| | | _action = new WpfComboBox |
| | | { |
| | | ItemsSource = HotkeyActions.All.ToArray(), |
| | | SelectedItem = existing?.Action ?? HotkeyActions.WidgetToggleInteractable, |
| | | Margin = new Thickness(0, 0, 0, 8) |
| | | }; |
| | | _plugin = new WpfComboBox |
| | | { |
| | | ItemsSource = plugins.ToArray(), |
| | | SelectedItem = existing?.Target?.PluginId ?? plugins.FirstOrDefault(), |
| | | Margin = new Thickness(0, 0, 0, 8) |
| | | }; |
| | | _instance = new WpfComboBox { Margin = new Thickness(0, 0, 0, 8) }; |
| | | _enabled = new WpfCheckBox { Content = "启用", IsChecked = existing?.Enabled ?? true, Margin = new Thickness(0, 0, 0, 8) }; |
| | | _plugin.SelectionChanged += (_, _) => RefreshInstances(); |
| | | RefreshInstances(); |
| | | if (!string.IsNullOrWhiteSpace(existing?.Target?.InstanceId)) |
| | | { |
| | | _instance.SelectedItem = existing!.Target!.InstanceId; |
| | | } |
| | | |
| | | var grid = new Grid { Margin = new Thickness(16) }; |
| | | grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(88) }); |
| | | grid.ColumnDefinitions.Add(new ColumnDefinition()); |
| | | for (var i = 0; i < 7; i++) |
| | | { |
| | | grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); |
| | | } |
| | | |
| | | AddRow(grid, 0, "快捷键", _keys); |
| | | var record = new WpfButton { Content = "录制", Width = 72, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(0, 0, 0, 8) }; |
| | | record.Click += async (_, _) => await ToggleRecordingAsync(record); |
| | | Grid.SetRow(record, 1); |
| | | Grid.SetColumn(record, 1); |
| | | grid.Children.Add(record); |
| | | AddRow(grid, 2, "动作", _action); |
| | | AddRow(grid, 3, "插件", _plugin); |
| | | AddRow(grid, 4, "实例", _instance); |
| | | Grid.SetRow(_enabled, 5); |
| | | Grid.SetColumn(_enabled, 1); |
| | | grid.Children.Add(_enabled); |
| | | |
| | | var buttons = new StackPanel { Orientation = System.Windows.Controls.Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Right, Margin = new Thickness(0, 12, 0, 0) }; |
| | | var ok = new WpfButton { Content = "确定", Width = 72, Margin = new Thickness(0, 0, 8, 0) }; |
| | | var cancel = new WpfButton { Content = "取消", Width = 72 }; |
| | | ok.Click += async (_, _) => |
| | | { |
| | | await StopRecordingAsync(record); |
| | | if (TryBuild()) |
| | | { |
| | | DialogResult = true; |
| | | } |
| | | }; |
| | | cancel.Click += async (_, _) => |
| | | { |
| | | await StopRecordingAsync(record); |
| | | DialogResult = false; |
| | | }; |
| | | buttons.Children.Add(ok); |
| | | buttons.Children.Add(cancel); |
| | | Grid.SetRow(buttons, 6); |
| | | Grid.SetColumnSpan(buttons, 2); |
| | | grid.Children.Add(buttons); |
| | | Content = grid; |
| | | PreviewKeyDown += OnPreviewKeyDown; |
| | | Closed += async (_, _) => await StopRecordingAsync(record); |
| | | } |
| | | |
| | | public HotkeyBinding? Result { get; private set; } |
| | | |
| | | private void RefreshInstances() |
| | | { |
| | | var pluginId = _plugin.SelectedItem as string; |
| | | var items = new List<string> { "" }; |
| | | items.AddRange(_instances.Where(item => string.Equals(item.PluginId, pluginId, StringComparison.OrdinalIgnoreCase)).Select(item => item.InstanceId)); |
| | | _instance.ItemsSource = items; |
| | | _instance.SelectedIndex = 0; |
| | | } |
| | | |
| | | private async Task ToggleRecordingAsync(WpfButton button) |
| | | { |
| | | if (_recording) |
| | | { |
| | | await StopRecordingAsync(button); |
| | | return; |
| | | } |
| | | |
| | | _recording = true; |
| | | button.Content = "停止"; |
| | | _keys.Text = "请按下组合键…"; |
| | | await _pauseAsync(); |
| | | } |
| | | |
| | | private async Task StopRecordingAsync(WpfButton button) |
| | | { |
| | | if (!_recording) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | _recording = false; |
| | | button.Content = "录制"; |
| | | await _resumeAsync(); |
| | | } |
| | | |
| | | private void OnPreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) |
| | | { |
| | | if (!_recording) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | e.Handled = true; |
| | | var key = e.Key == System.Windows.Input.Key.System ? e.SystemKey : e.Key; |
| | | if (key is System.Windows.Input.Key.LeftCtrl or System.Windows.Input.Key.RightCtrl |
| | | or System.Windows.Input.Key.LeftShift or System.Windows.Input.Key.RightShift |
| | | or System.Windows.Input.Key.LeftAlt or System.Windows.Input.Key.RightAlt |
| | | or System.Windows.Input.Key.LWin or System.Windows.Input.Key.RWin) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | var modifiers = new List<string>(); |
| | | if ((System.Windows.Input.Keyboard.Modifiers & System.Windows.Input.ModifierKeys.Control) != 0) |
| | | { |
| | | modifiers.Add("Ctrl"); |
| | | } |
| | | |
| | | if ((System.Windows.Input.Keyboard.Modifiers & System.Windows.Input.ModifierKeys.Alt) != 0) |
| | | { |
| | | modifiers.Add("Alt"); |
| | | } |
| | | |
| | | if ((System.Windows.Input.Keyboard.Modifiers & System.Windows.Input.ModifierKeys.Shift) != 0) |
| | | { |
| | | modifiers.Add("Shift"); |
| | | } |
| | | |
| | | if ((System.Windows.Input.Keyboard.Modifiers & System.Windows.Input.ModifierKeys.Windows) != 0) |
| | | { |
| | | modifiers.Add("Win"); |
| | | } |
| | | |
| | | if (modifiers.Count == 0) |
| | | { |
| | | _keys.Text = "需要包含 Ctrl/Shift/Alt"; |
| | | return; |
| | | } |
| | | |
| | | modifiers.Add(key.ToString()); |
| | | var chord = string.Join("+", modifiers); |
| | | if (!HotkeyChord.TryParse(chord, out var parsed, out _)) |
| | | { |
| | | _keys.Text = "组合键无效"; |
| | | return; |
| | | } |
| | | |
| | | _keys.Text = parsed.Canonical; |
| | | } |
| | | |
| | | private bool TryBuild() |
| | | { |
| | | if (!HotkeyChord.TryParse(_keys.Text, out var chord, out var errorCode)) |
| | | { |
| | | MessageBox.Show(this, "请输入有效组合键,不能只按修饰键。", errorCode ?? "HotkeyInvalidKeys", MessageBoxButton.OK, MessageBoxImage.Warning); |
| | | return false; |
| | | } |
| | | |
| | | var action = _action.SelectedItem as string ?? HotkeyActions.WidgetToggleInteractable; |
| | | var pluginId = _plugin.SelectedItem as string; |
| | | var instanceId = _instance.SelectedItem as string; |
| | | Result = new HotkeyBinding |
| | | { |
| | | Id = _existing?.Id ?? $"user-{Guid.NewGuid():N}"[..16], |
| | | Keys = chord.Canonical, |
| | | Action = action, |
| | | Target = HotkeyActions.All.Contains(action) && (action.StartsWith("widget.", StringComparison.OrdinalIgnoreCase)) |
| | | ? new HotkeyTarget { PluginId = pluginId, InstanceId = string.IsNullOrWhiteSpace(instanceId) ? null : instanceId } |
| | | : null, |
| | | Enabled = _enabled.IsChecked == true, |
| | | Source = _existing?.Source ?? HotkeySource.User |
| | | }; |
| | | return true; |
| | | } |
| | | |
| | | private static void AddRow(Grid grid, int row, string label, UIElement field) |
| | | { |
| | | var text = new TextBlock { Text = label, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 8, 8) }; |
| | | Grid.SetRow(text, row); |
| | | Grid.SetColumn(text, 0); |
| | | Grid.SetRow(field, row); |
| | | Grid.SetColumn(field, 1); |
| | | grid.Children.Add(text); |
| | | grid.Children.Add(field); |
| | | } |
| | | } |
| | | |
| | | private sealed class WidgetLayoutDialog : Window |
| | | { |
| | | private readonly WpfTextBox _x; |