-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
116 lines (97 loc) · 4.32 KB
/
Main.cs
File metadata and controls
116 lines (97 loc) · 4.32 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
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
using BrowserTabs;
using Flow.Launcher.Plugin.BrowserTabs.Views;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace Flow.Launcher.Plugin.BrowserTabs
{
public class Main : IAsyncPlugin, IPluginI18n, IContextMenu, ISettingProvider
{
public const string IconPath = "Images/icon.png";
public const string SwitchTabPath = "Images/switch_tab.png";
public const string CloseTabPath = "Images/close_tab.png";
private SettingWindow? SettingWindow;
public static PluginInitContext Context { get; private set; } = null!;
public static Settings Settings { get; private set; } = null!;
public Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
var results = new List<Result>();
foreach (var tab in BrowserTabManager.GetAllTabs(token))
{
var titleMatch = Context.API.FuzzySearch(query.Search, tab.Title);
var browserNameMatch = Context.API.FuzzySearch(query.Search, tab.BrowserName);
if (string.IsNullOrWhiteSpace(query.Search) ||
titleMatch.IsSearchPrecisionScoreMet() ||
browserNameMatch.IsSearchPrecisionScoreMet())
{
results.Add(new Result()
{
Title = tab.Title,
IcoPath = GetBrowserIcoPath(tab.BrowserName),
Score = titleMatch.Score + browserNameMatch.Score,
TitleHighlightData = titleMatch.MatchData,
SubTitle = tab.BrowserName,
ContextData = tab,
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
tab.CloseTab();
// Re-query to remove closed tab from the results.
// Add a slight delay so re-query can pick up the tab elements again, 200ms was the right amount after testing
Task.Delay(200).ContinueWith(_ => Context.API.ChangeQuery(query.RawQuery, requery: true));
}
else
tab.ActivateTab();
return true;
},
ShowBadge = true,
BadgeIcoPath = IconPath
});
}
}
return Task.FromResult(results.OrderBy(x => x.Title).ToList());
}
private string GetBrowserIcoPath(string browserName)
{
// Defined at
// https://github.com/jjw24/BrowserTabs/blob/6d95bc467c58eb89c8e2f707d72d4cf180c48976/BrowserTabs/BrowserTabManager.cs#L18
return browserName.ToLower() switch
{
"chrome" => "Images/chrome.png",
"msedge" => "Images/msedge.png",
"firefox" => "Images/firefox.png",
_ => "Images/chromium.png",
};
}
public Task InitAsync(PluginInitContext context)
{
Context = context;
Settings = Context.API.LoadSettingJsonStorage<Settings>();
return Task.CompletedTask;
}
public string GetTranslatedPluginTitle()
{
return "Browser Tabs";
}
public string GetTranslatedPluginDescription()
{
return "Search, activate, or close browser tabs. A centralized plugin for managing all open tabs in your browser";
}
public List<Result> LoadContextMenus(Result selectedResult)
{
if (selectedResult == null)
return new List<Result>();
if (selectedResult.ContextData is BrowserTab tab)
return ContextMenu.GetBrowserTabContextMenu(tab);
return new List<Result>();
}
public Control CreateSettingPanel()
{
return SettingWindow;
//return SettingWindow ??= new SettingWindow(Settings);
}
}
}