forked from hyprwm/hyprlauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryProcessor.cpp
More file actions
128 lines (103 loc) · 3.94 KB
/
Copy pathQueryProcessor.cpp
File metadata and controls
128 lines (103 loc) · 3.94 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
117
118
119
120
121
122
123
124
125
126
127
128
#include "QueryProcessor.hpp"
#include "../ui/UI.hpp"
#include "../config/ConfigManager.hpp"
#include "../finders/desktop/DesktopFinder.hpp"
#include "../finders/unicode/UnicodeFinder.hpp"
#include "../finders/math/MathFinder.hpp"
#include "../finders/font/FontFinder.hpp"
#include <hyprutils/utils/ScopeGuard.hpp>
using namespace Hyprutils::Utils;
static WP<IFinder> finderForName(const std::string& x) {
if (x == "desktop")
return g_desktopFinder;
if (x == "unicode")
return g_unicodeFinder;
if (x == "math")
return g_mathFinder;
return WP<IFinder>{};
}
static std::pair<WP<IFinder>, bool> finderForPrefix(const char x) {
static auto PDEFAULTFINDER = Hyprlang::CSimpleConfigValue<Hyprlang::STRING>(g_configManager->m_config.get(), "finders:default_finder");
static auto PDESKTOPPREFIX = Hyprlang::CSimpleConfigValue<Hyprlang::STRING>(g_configManager->m_config.get(), "finders:desktop_prefix");
static auto PUNICODEPREFIX = Hyprlang::CSimpleConfigValue<Hyprlang::STRING>(g_configManager->m_config.get(), "finders:unicode_prefix");
static auto PMATHPREFIX = Hyprlang::CSimpleConfigValue<Hyprlang::STRING>(g_configManager->m_config.get(), "finders:math_prefix");
static auto PFONTPREFIX = Hyprlang::CSimpleConfigValue<Hyprlang::STRING>(g_configManager->m_config.get(), "finders:font_prefix");
if (x == (*PDESKTOPPREFIX)[0])
return {g_desktopFinder, true};
if (x == (*PUNICODEPREFIX)[0])
return {g_unicodeFinder, true};
if (x == (*PMATHPREFIX)[0])
return {g_mathFinder, true};
if (x == (*PFONTPREFIX)[0])
return {g_fontFinder, true};
return {finderForName(*PDEFAULTFINDER), false};
}
CQueryProcessor::CQueryProcessor() {
m_queryThread = std::thread([this] {
while (!m_quit) {
std::unique_lock lk(m_threadMutex);
m_threadCV.wait(lk, [this] { return m_event; });
m_event = false;
if (m_quit)
break;
while (!m_quit && m_newQuery) {
process();
}
}
});
}
CQueryProcessor::~CQueryProcessor() {
m_quit = true;
m_pendingQuery = "exit";
m_event = true;
m_threadCV.notify_all();
m_queryThread.join();
}
void CQueryProcessor::scheduleQueryUpdate(const std::string& str) {
m_queryStrMutex.lock();
m_pendingQuery = str;
m_newQuery = true;
m_event = true;
m_queryStrMutex.unlock();
m_threadCV.notify_all();
}
void CQueryProcessor::overrideQueryProvider(WP<IFinder> finder) {
std::lock_guard<std::mutex> lg(m_processingMutex);
m_overrideFinder = finder;
}
// Only ran on process thread
void CQueryProcessor::process() {
CScopeGuard x([this] { m_newQuery = false; });
if (m_quit)
return;
m_queryStrMutex.lock();
std::string query = m_pendingQuery;
m_pendingQuery = "";
m_queryStrMutex.unlock();
WP<IFinder> FINDER;
bool eat = false;
if (!m_overrideFinder) {
const auto [F, e] = finderForPrefix(query[0]);
if (e && query.size() == 1) {
// Prefix typed alone: show all results from that finder
FINDER = F;
if (FINDER) {
auto RESULTS = FINDER->getResultsForQuery("");
if (g_ui && g_ui->m_backend)
g_ui->m_backend->addIdle([r = std::move(RESULTS)] mutable { g_ui->updateResults(std::move(r)); });
}
return;
}
FINDER = F;
eat = e;
} else
FINDER = m_overrideFinder;
if (query.empty() && !m_overrideFinder) {
if (g_ui)
g_ui->m_backend->addIdle([] mutable { g_ui->updateResults({}); });
return;
}
auto RESULTS = FINDER ? FINDER->getResultsForQuery(eat ? query.substr(1) : query) : std::vector<SFinderResult>{};
if (g_ui && g_ui->m_backend)
g_ui->m_backend->addIdle([r = std::move(RESULTS)] mutable { g_ui->updateResults(std::move(r)); });
}