-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
76 lines (66 loc) · 2.84 KB
/
Main.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
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Flow.Launcher.Plugin.OneTimePassword
{
public class Main : IPlugin, IPluginI18n, ISettingProvider
{
public static ImageSource LoadImage(string path) => new BitmapImage(new Uri(path));
private PluginInitContext Context = new();
private Setting Settings = new();
public void Init(PluginInitContext context)
{
Context = context;
Settings = context.API.LoadSettingJsonStorage<Setting>();
}
public List<Result> Query(Query query)
{
var API = Context.API;
var GetTranslation = Context.API.GetTranslation;
var otps = Settings.Otps;
if (otps == null || otps.Count == 0)
return new List<Result> {
new() {
Title = GetTranslation("flowlauncher_plugin_otp_main_empty_title"),
SubTitle = GetTranslation("flowlauncher_plugin_otp_main_empty_sub_title"),
Action = (_) =>
{
API.OpenSettingDialog();
return true;
},
}
};
Result compute(Otp otp)
{
var (password, remaining) = otp.Compute();
return new Result
{
Title = $"{password} - {otp.Label}",
SubTitle = $"{GetTranslation("flowlauncher_plugin_otp_main_expired")}: {remaining}s ({GetTranslation("flowlauncher_plugin_otp_main_copy")})",
Icon = string.IsNullOrEmpty(otp.Icon) ? null : () => LoadImage(otp.Icon),
Action = (_) =>
{
API.CopyToClipboard(password, showDefaultNotification: false);
return true;
}
};
}
if (string.IsNullOrEmpty(query.Search))
return new List<Result>(otps.Select(compute));
else
return new List<Result>(otps.Where(otp => API.FuzzySearch(query.Search, otp.Label).Success).Select(compute));
}
public string? GetTranslatedPluginTitle() => Context.API.GetTranslation("flowlauncher_plugin_otp_plugin_title");
public string? GetTranslatedPluginDescription() => Context.API.GetTranslation("flowlauncher_plugin_otp_plugin_description");
public Control CreateSettingPanel()
{
var control = new SettingControl(Context, Settings);
control.OnUpdate += (sender, settings) =>
{
Settings = settings;
Context.API.SaveSettingJsonStorage<Setting>();
};
return control;
}
}
}