forked from Flow-Launcher/Flow.Launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectBrowserViewModel.cs
More file actions
68 lines (58 loc) · 2.01 KB
/
SelectBrowserViewModel.cs
File metadata and controls
68 lines (58 loc) · 2.01 KB
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
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.ViewModel;
public partial class SelectBrowserViewModel : BaseModel
{
private readonly Settings _settings;
private int selectedCustomBrowserIndex;
public int SelectedCustomBrowserIndex
{
get => selectedCustomBrowserIndex;
set
{
// When one custom browser is selected and removed, the index will become -1, so we need to ignore this change
if (value < 0) return;
if (selectedCustomBrowserIndex != value)
{
selectedCustomBrowserIndex = value;
OnPropertyChanged(nameof(CustomBrowser));
}
}
}
public ObservableCollection<CustomBrowserViewModel> CustomBrowsers { get; }
public CustomBrowserViewModel CustomBrowser => CustomBrowsers[SelectedCustomBrowserIndex];
public SelectBrowserViewModel(Settings settings)
{
_settings = settings;
CustomBrowsers = new ObservableCollection<CustomBrowserViewModel>(_settings.CustomBrowserList.Select(x => x.Copy()));
SelectedCustomBrowserIndex = _settings.CustomBrowserIndex;
}
public bool SaveSettings()
{
_settings.CustomBrowserList = CustomBrowsers.ToList();
_settings.CustomBrowserIndex = SelectedCustomBrowserIndex;
return true;
}
[RelayCommand]
private void Add()
{
CustomBrowsers.Add(new()
{
Name = Localize.defaultBrowser_new_profile()
});
SelectedCustomBrowserIndex = CustomBrowsers.Count - 1;
}
[RelayCommand]
private void Delete()
{
var currentIndex = SelectedCustomBrowserIndex;
if (currentIndex >= 0 && currentIndex < CustomBrowsers.Count)
{
CustomBrowsers.RemoveAt(currentIndex);
SelectedCustomBrowserIndex = currentIndex > 0 ? currentIndex - 1 : 0;
}
}
}