42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CursorLang.Models;
|
|
using CursorLang.Services;
|
|
|
|
namespace CursorLang.ViewModels;
|
|
|
|
/// <summary>
|
|
/// Показывает текущую раскладку в главном окне и просит показать подсказку
|
|
/// у курсора, когда пользователь переключил раскладку сам.
|
|
/// </summary>
|
|
public sealed partial class MainViewModel : ObservableObject, IDisposable
|
|
{
|
|
private readonly IKeyboardLayoutService _layoutService;
|
|
private readonly ILayoutPopupService _popupService;
|
|
|
|
[ObservableProperty]
|
|
private string _currentLayout;
|
|
|
|
public MainViewModel(IKeyboardLayoutService layoutService, ILayoutPopupService popupService)
|
|
{
|
|
_layoutService = layoutService;
|
|
_popupService = popupService;
|
|
|
|
_currentLayout = layoutService.Current.DisplayName;
|
|
_layoutService.LayoutChanged += OnLayoutChanged;
|
|
}
|
|
|
|
public void Dispose() => _layoutService.LayoutChanged -= OnLayoutChanged;
|
|
|
|
private void OnLayoutChanged(object? sender, LayoutChangedEventArgs e)
|
|
{
|
|
CurrentLayout = e.Layout.DisplayName;
|
|
|
|
// При переходе в другое приложение раскладка меняется без участия
|
|
// пользователя, и всплывающая подсказка была бы навязчивой
|
|
if (e.Reason == LayoutChangeReason.UserSwitched)
|
|
{
|
|
_popupService.Show(e.Layout);
|
|
}
|
|
}
|
|
}
|