-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorWindow.xaml.cs
140 lines (115 loc) · 3.99 KB
/
EditorWindow.xaml.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
using Microsoft.Win32;
using System.Windows;
namespace Flow.Launcher.Plugin.OneTimePassword
{
public class EditorWindowViewModel : BaseModel
{
public readonly PluginInitContext Context;
private string uri = "";
public string Uri
{
get => uri;
set
{
var trimmed = value.Trim();
if (uri == trimmed || string.IsNullOrEmpty(trimmed)) return;
if (!Otp.ValidateUri(trimmed))
{
var warning = Context.API.GetTranslation("flowlauncher_plugin_otp_editor_form_uri_invalid_warning");
MessageBox.Show(warning);
return;
}
if (string.IsNullOrEmpty(Label))
Label = Otp.GetOtpLabel(trimmed);
uri = trimmed;
OnPropertyChanged(nameof(uri));
}
}
private string label = "";
public string Label
{
get => label;
set
{
var trimmed = value.Trim();
if (label == trimmed || string.IsNullOrEmpty(trimmed)) return;
label = trimmed;
OnPropertyChanged(nameof(label));
}
}
private string icon = "";
public string? Icon
{
get => icon;
set
{
if (string.IsNullOrEmpty(value)) return;
icon = value;
OnPropertyChanged(nameof(icon));
}
}
public EditorWindowViewModel(PluginInitContext context) => Context = context;
public EditorWindowViewModel(PluginInitContext context, Otp otp) : this(context)
{
Uri = otp.Uri;
Label = otp.Label;
Icon = otp.Icon;
}
public Otp ToOtp() => new(Uri) { Label = Label, Icon = Icon };
}
public partial class EditorWindow : Window
{
private readonly PluginInitContext Context;
private readonly EditorWindowViewModel ViewModel;
public event EventHandler<Otp>? OnConfirmed;
public EditorWindow(PluginInitContext context)
{
InitializeComponent();
Context = context;
DataContext = ViewModel = new EditorWindowViewModel(context);
}
public EditorWindow(PluginInitContext context, Otp otp) : this(context)
{
DataContext = ViewModel = new EditorWindowViewModel(context, otp);
LoadImage(otp.Icon);
}
private void LoadImage(string? path)
{
if (string.IsNullOrEmpty(path)) return;
try
{
OtpIcon.Source = Main.LoadImage(path);
}
catch { }
}
private void OnSelectIcon(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog
{
Filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.ico)|*.jpg; *.jpeg; *.gif; *.png; *.ico|All files (*.*)|*.*"
};
if (dialog.ShowDialog() != true) return;
ViewModel.Icon = dialog.FileName;
LoadImage(dialog.FileName);
}
private void OnCancel(object sender, RoutedEventArgs e) => Close();
private void OnConfirm(object sender, RoutedEventArgs e)
{
var GetTranslation = Context.API.GetTranslation;
if (string.IsNullOrEmpty(ViewModel.Uri))
{
var warning = GetTranslation("flowlauncher_plugin_otp_editor_form_uri_empty_warning");
MessageBox.Show(warning);
return;
}
if (string.IsNullOrEmpty(ViewModel.Label))
{
var warning = GetTranslation("flowlauncher_plugin_otp_editor_form_label_empty_warning");
MessageBox.Show(warning);
return;
}
OnConfirmed?.Invoke(this, ViewModel.ToOtp());
Close();
}
}
}