Files
cursor-lang/CursorLang/Services/CapsLockSwitchCoordinator.cs
T
2026-08-09 20:02:23 +05:00

78 lines
2.3 KiB
C#

using System.ComponentModel;
using CursorLang.Models;
namespace CursorLang.Services;
/// <summary>
/// Turns Caps Lock presses into actions: a short one switches the layout, a long one
/// only shows the popup. It also turns the hook on and off following the checkbox in
/// the settings.
/// </summary>
public sealed class CapsLockSwitchCoordinator : IDisposable
{
private readonly ICapsLockHotkeyService _hotkeyService;
private readonly IKeyboardLayoutService _layoutService;
private readonly ILayoutPopupService _popupService;
private readonly AppSettings _settings;
public CapsLockSwitchCoordinator(
ICapsLockHotkeyService hotkeyService,
IKeyboardLayoutService layoutService,
ILayoutPopupService popupService,
AppSettings settings)
{
_hotkeyService = hotkeyService;
_layoutService = layoutService;
_popupService = popupService;
_settings = settings;
}
public void Start()
{
_hotkeyService.Tapped += OnTapped;
_hotkeyService.HoldStarted += OnHoldStarted;
_hotkeyService.HoldEnded += OnHoldEnded;
_settings.PropertyChanged += OnSettingsChanged;
ApplySetting();
}
public void Dispose()
{
_settings.PropertyChanged -= OnSettingsChanged;
_hotkeyService.Tapped -= OnTapped;
_hotkeyService.HoldStarted -= OnHoldStarted;
_hotkeyService.HoldEnded -= OnHoldEnded;
_hotkeyService.Stop();
}
private void OnSettingsChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(AppSettings.UseCapsLockHotkey))
{
ApplySetting();
}
}
private void ApplySetting()
{
if (_settings.UseCapsLockHotkey)
{
_hotkeyService.Start();
}
else
{
_hotkeyService.Stop();
}
}
private void OnTapped(object? sender, EventArgs e) => _layoutService.SwitchToNext();
// We do not change the layout, but staying silent will not do either: without the
// popup a long press looks as if the key simply did not work
private void OnHoldStarted(object? sender, EventArgs e) =>
_popupService.ShowUntilHidden(_layoutService.Current);
private void OnHoldEnded(object? sender, EventArgs e) => _popupService.Hide();
}