moved to MVVM architecture
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user