-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
LaunchPreviewPopupAction.cs
70 lines (57 loc) · 1.83 KB
/
LaunchPreviewPopupAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.
namespace Files.App.Actions
{
internal sealed class LaunchPreviewPopupAction : ObservableObject, IAction
{
private readonly IContentPageContext context;
private readonly IPreviewPopupService previewPopupService;
public string Label
=> "LaunchPreviewPopup".GetLocalizedResource();
public string Description
=> "LaunchPreviewPopupDescription".GetLocalizedResource();
public HotKey HotKey
=> new(Keys.Space);
public bool IsExecutable =>
context.SelectedItems.Count == 1 &&
(!context.ShellPage?.ToolbarViewModel?.IsEditModeEnabled ?? false) &&
(!context.ShellPage?.SlimContentPage?.IsRenamingItem ?? false);
public LaunchPreviewPopupAction()
{
context = Ioc.Default.GetRequiredService<IContentPageContext>();
previewPopupService = Ioc.Default.GetRequiredService<IPreviewPopupService>();
context.PropertyChanged += Context_PropertyChanged;
}
public async Task ExecuteAsync()
{
var provider = await previewPopupService.GetProviderAsync();
if (provider is null)
return;
var itemPath = context.SelectedItem?.ItemPath;
if (itemPath is not null)
await provider.TogglePreviewPopupAsync(itemPath);
}
private async Task SwitchPopupPreviewAsync()
{
if (IsExecutable)
{
var provider = await previewPopupService.GetProviderAsync();
if (provider is null)
return;
var itemPath = context.SelectedItem?.ItemPath;
if (itemPath is not null)
await provider.SwitchPreviewAsync(itemPath);
}
}
public async void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.SelectedItems):
OnPropertyChanged(nameof(IsExecutable));
await SwitchPopupPreviewAsync();
break;
}
}
}
}