Files
cursor-lang/CursorLang/Services/LayoutPopupService.cs
T
2026-08-09 20:24:21 +05:00

64 lines
1.7 KiB
C#

using System.Windows.Threading;
using CursorLang.Models;
using CursorLang.ViewModels;
namespace CursorLang.Services;
/// <summary>
/// Manages the lifetime of the popup: the window is only responsible for showing it,
/// while the decision of when to show and when to take it down is made here.
/// </summary>
public sealed class LayoutPopupService : ILayoutPopupService, IDisposable
{
private readonly ILayoutPopupWindow _window;
private readonly LayoutPopupViewModel _viewModel;
private readonly AppSettings _settings;
private readonly DispatcherTimer _hideTimer = new();
public LayoutPopupService(ILayoutPopupWindow window, LayoutPopupViewModel viewModel, AppSettings settings)
{
_window = window;
_viewModel = viewModel;
_settings = settings;
_hideTimer.Tick += OnHideTimerTick;
}
public void Show(KeyboardLayout layout)
{
ShowUntilHidden(layout);
// The duration is read on every show: it is changed in the settings on the fly.
// Restarting the timer also prolongs the show on quick switches
_hideTimer.Interval = _settings.Duration;
_hideTimer.Start();
}
public void ShowUntilHidden(KeyboardLayout layout)
{
_hideTimer.Stop();
_viewModel.ShortName = layout.ShortName;
_window.ShowPopup();
}
public void Hide()
{
_hideTimer.Stop();
_window.Hide();
}
public void Dispose()
{
_hideTimer.Stop();
_hideTimer.Tick -= OnHideTimerTick;
_window.Close();
}
private void OnHideTimerTick(object? sender, EventArgs e)
{
_hideTimer.Stop();
_window.Hide();
}
}