diff --git a/VdLabel/Config.cs b/VdLabel/Config.cs index e628d8b..9d525ed 100644 --- a/VdLabel/Config.cs +++ b/VdLabel/Config.cs @@ -33,12 +33,7 @@ record DesktopConfig public IReadOnlyList TargetWindows { get; init; } = []; } -record WindowConfig -{ - public WindowMatchType MatchType { get; set; } - public WindowPatternType PatternType { get; set; } - public string Pattern { get; set; } = string.Empty; -} +record WindowConfig(WindowMatchType MatchType, WindowPatternType PatternType, string Pattern); enum WindowMatchType { diff --git a/VdLabel/IPresentationService.cs b/VdLabel/IPresentationService.cs new file mode 100644 index 0000000..7c85e40 --- /dev/null +++ b/VdLabel/IPresentationService.cs @@ -0,0 +1,37 @@ +using System.Windows; +using Kamishibai; + +namespace VdLabel; + +public partial interface IPresentationService : IPresentationServiceBase +{ + Task OpenTargetWindowDialogAsync(); +} + +public class PresentationService(IServiceProvider serviceProvider, INavigationFrameProvider navigationFrameProvider, IWindowService windowService) + : PresentationServiceBase(serviceProvider, navigationFrameProvider, windowService), IPresentationService +{ + private readonly IServiceProvider _serviceProvider = serviceProvider; + + public async Task OpenTargetWindowDialogAsync() + { + var current = Application.Current.Windows.OfType().SingleOrDefault(x => x.IsActive); + current?.Hide(); + try + { + var vm = new TargetWindowViewModel(); + if (await OpenDialogAsync(vm) == true) + { + return vm.SelectedWindow; + } + else + { + return null; + } + } + finally + { + current?.Show(); + } + } +} \ No newline at end of file diff --git a/VdLabel/MainViewModel.cs b/VdLabel/MainViewModel.cs index 07d3a59..7a2e3da 100644 --- a/VdLabel/MainViewModel.cs +++ b/VdLabel/MainViewModel.cs @@ -16,6 +16,7 @@ partial class MainViewModel : ObservableObject private readonly IContentDialogService dialogService; private readonly IVirualDesktopService virualDesktopService; private readonly ICommandLabelService commandLabelService; + private readonly IPresentationService presentationService; private readonly IUpdateChecker updateChecker; [ObservableProperty] @@ -48,6 +49,7 @@ partial class MainViewModel : ObservableObject public MainViewModel( IConfigStore configStore, + IPresentationService presentationService, IContentDialogService dialogService, IVirualDesktopService virualDesktopService, ICommandLabelService commandLabelService, @@ -55,6 +57,7 @@ public MainViewModel( { BindingOperations.EnableCollectionSynchronization(this.DesktopConfigs, new()); this.configStore = configStore; + this.presentationService = presentationService; this.dialogService = dialogService; this.virualDesktopService = virualDesktopService; this.commandLabelService = commandLabelService; @@ -93,7 +96,7 @@ private async void Load() this.DesktopConfigs.Clear(); foreach (var desktopConfig in this.Config.DesktopConfigs) { - this.DesktopConfigs.Add(new(desktopConfig, this.dialogService, this.virualDesktopService, this.commandLabelService)); + this.DesktopConfigs.Add(new(desktopConfig, this.presentationService, this.dialogService, this.virualDesktopService, this.commandLabelService)); } this.SelectedDesktopConfig = this.DesktopConfigs.FirstOrDefault(); } @@ -181,8 +184,15 @@ private static bool GetIsStartup() } } -partial class DesktopConfigViewModel(DesktopConfig desktopConfig, IContentDialogService dialogService, IVirualDesktopService virualDesktopService, ICommandLabelService commandLabelService) : ObservableObject +partial class DesktopConfigViewModel( + DesktopConfig desktopConfig, + IPresentationService presentationService, + IContentDialogService dialogService, + IVirualDesktopService virualDesktopService, + ICommandLabelService commandLabelService) + : ObservableObject { + private readonly IPresentationService presentationService = presentationService; private readonly IContentDialogService dialogService = dialogService; private readonly IVirualDesktopService virualDesktopService = virualDesktopService; private readonly ICommandLabelService commandLabelService = commandLabelService; @@ -217,7 +227,7 @@ partial class DesktopConfigViewModel(DesktopConfig desktopConfig, IContentDialog public bool IsVisibleImage => this.ImagePath is not null; - public ObservableCollection TargetWindows { get; } = new(desktopConfig.TargetWindows); + public ObservableCollection TargetWindows { get; } = new(desktopConfig.TargetWindows.Select(c => new WindowConfigViewModel(c))); public IReadOnlyList MatchTypes { get; } = Enum.GetValues(); @@ -273,9 +283,26 @@ public void AddTargetWindow() => this.TargetWindows.Add(new()); [RelayCommand] - public void RemoveTargetWindow(WindowConfig target) + public void RemoveTargetWindow(WindowConfigViewModel target) => this.TargetWindows.Remove(target); + [RelayCommand] + public async Task FindWindow(WindowConfigViewModel config) + { + var info = await this.presentationService.OpenTargetWindowDialogAsync(); + if (info is null) + { + return; + } + config.Pattern = config.MatchType switch + { + WindowMatchType.CommandLine => info.CommandLine, + WindowMatchType.Title => info.Title, + WindowMatchType.Path => info.Path, + _ => throw new NotSupportedException(), + }; + } + public DesktopConfig GetSaveConfig() => new() { @@ -285,6 +312,16 @@ public DesktopConfig GetSaveConfig() Utf8Command = this.Utf8Command, Command = this.Command, ImagePath = this.ImagePath, - TargetWindows = this.TargetWindows.ToArray(), + TargetWindows = this.TargetWindows.Select(c => new WindowConfig(c.MatchType, c.PatternType, c.Pattern)).ToArray(), }; } + +partial class WindowConfigViewModel(WindowConfig? config = null) : ObservableObject +{ + [ObservableProperty] + private WindowMatchType matchType = config?.MatchType ?? default; + [ObservableProperty] + private WindowPatternType patternType = config?.PatternType ?? default; + [ObservableProperty] + private string pattern = config?.Pattern ?? string.Empty; +} diff --git a/VdLabel/MainWindow.xaml b/VdLabel/MainWindow.xaml index 6cdbd11..4faf72a 100644 --- a/VdLabel/MainWindow.xaml +++ b/VdLabel/MainWindow.xaml @@ -339,11 +339,11 @@ DockPanel.Dock="Right" Icon="{ui:SymbolIcon Delete24}" />