-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinkSettings.cs
179 lines (153 loc) · 6.06 KB
/
LinkSettings.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.Json;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Win32;
using System.ComponentModel;
using System.Linq;
namespace Flow.Launcher.Plugin.LinkOpener
{
public partial class LinkSettings : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public PluginInitContext Context { get; private set; }
string SettingsPath => Path.Combine(
Path.GetDirectoryName(Path.GetDirectoryName(Context.CurrentPluginMetadata.PluginDirectory)),
"Settings",
"Plugins",
"Flow.Launcher.Plugin.LinkOpener",
"Settings.json"
);
protected virtual void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private bool addToBulkOpenUrls;
public bool AddToBulkOpenUrls
{
get => addToBulkOpenUrls;
set
{
if (addToBulkOpenUrls != value)
{
addToBulkOpenUrls = value;
OnPropertyChanged(nameof(AddToBulkOpenUrls));
}
}
}
public ObservableCollection<SettingItem> SettingsItems { get; set; }
public ICommand SelectIconCommand { get; private set; }
public ICommand RemoveIconCommand { get; private set; }
public LinkSettings(ObservableCollection<SettingItem> settingsItems, PluginInitContext context)
{
InitializeComponent();
Context = context;
SettingsItems = settingsItems;
SettingsItems.ToList().ForEach(x => x.PropertyChanged += (s, e) => SaveSettings());
SettingsItems.CollectionChanged += (s, e) =>
{
if (e.NewItems != null)
{
foreach (SettingItem newItem in e.NewItems)
{
newItem.PropertyChanged += Item_PropertyChanged;
newItem.AddToBulkOpenUrls = AddToBulkOpenUrls;
}
}
if (e.OldItems != null)
{
foreach (SettingItem oldItem in e.OldItems)
{
oldItem.PropertyChanged -= Item_PropertyChanged;
}
}
SaveSettings();
};
SelectIconCommand = new RelayCommand(OnSelectIcon);
RemoveIconCommand = new RelayCommand(OnRemoveIcon);
this.DataContext = this;
AddToBulkOpenUrls = SettingsItems.Any(x => x.AddToBulkOpenUrls);
}
private void OnRemoveIcon(object parameter)
{
var selectedItem = parameter as SettingItem;
if (selectedItem != null)
{
Dispatcher.InvokeAsync(() =>
{
selectedItem.IconPath = string.Empty;
int index = SettingsItems.IndexOf(selectedItem);
if (index >= 0)
{
SettingsItems[index] = selectedItem;
}
});
}
}
private void OnCheckBoxClicked(object sender, System.Windows.RoutedEventArgs e)
{
bool isChecked = (sender as CheckBox)?.IsChecked ?? false;
foreach (var item in SettingsItems)
{
item.AddToBulkOpenUrls = isChecked;
}
SaveSettings();
}
private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) => SaveSettings();
private void SaveSettings()
{
var filteredSettingsItems = SettingsItems
.Where(item => !string.IsNullOrWhiteSpace(item.Keyword) ||
!string.IsNullOrWhiteSpace(item.Title) ||
!string.IsNullOrWhiteSpace(item.Url) ||
!string.IsNullOrWhiteSpace(item.IconPath))
.ToList();
string jsonData = JsonSerializer.Serialize(filteredSettingsItems, new JsonSerializerOptions { WriteIndented = true });
Dispatcher.InvokeAsync(() =>
{
File.WriteAllText(SettingsPath, jsonData);
});
}
private void OnSelectIcon(object parameter)
{
var openFileDialog = new OpenFileDialog
{
Filter = "Image Files|*.png;*.jpg|All Files|*.*",
Title = "Select an Icon"
};
if (openFileDialog.ShowDialog() == true)
{
var selectedItem = parameter as SettingItem;
if (selectedItem != null)
{
Dispatcher.InvokeAsync(() =>
{
selectedItem.IconPath = openFileDialog.FileName;
int index = SettingsItems.IndexOf(selectedItem);
if (index >= 0)
{
SettingsItems[index] = selectedItem;
}
});
}
}
}
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Func<object, bool> _canExecute;
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true;
public void Execute(object parameter) => _execute(parameter);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
}