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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
local al_prompt = require "lib.bling.widget.app_launcher.prompt"
local awful = require "awful"
local cfg = require "misc.cfg"
local gshape = require "gears.shape"
local gtable = require "gears.table"
local qanim = require "quarrel.animation"
local qcolor = require "quarrel.color"
local qnative = require "quarrel.native"
local qtable = require "quarrel.table"
local qui = require "quarrel.ui"
local qvars = require "quarrel.vars"
local rubato = require "lib.rubato"
local wibox = require "wibox"
local btn = awful.button.names
local max_entries = 10
local fresnel = {}
fresnel._toggled = false
fresnel._entries_exec = {}
fresnel._entries_offset = 0
fresnel._entries_count = 0
fresnel._visible_entries = 0
fresnel._selected_index = 1
function fresnel:_exec_entry(entry_index)
local exec = self._entries_exec[entry_index]
if type(exec) ~= "userdata" and type(exec) ~= "nil" then
if exec[2] then
awful.spawn(cfg.terminal .. " -e /bin/sh -c " .. exec[1] .. " 1>/dev/null 2>&1")
else
awful.spawn.with_shell(exec[1] .. " 1>/dev/null 2>&1")
end
end
end
function fresnel:_update(query, scrolled)
query = query or ""
scrolled = scrolled or false
if not scrolled then
self._selected_index = 1
self._entries_offset = 0
end
local layout = self._popup.widget:get_children_by_id("entry_layout")[1]
local status = self._popup.widget:get_children_by_id("status")[1]
local all_providers = {}
local entries_count = 0
self._entries_exec = {}
layout:reset()
for _, provider in qtable.opairs(qnative.lenses) do
local entries = provider(query)
table.sort(entries, function(a, b)
return a.message < b.message
end)
all_providers = gtable.join(all_providers, entries)
end
self._entries_count = #all_providers
for i, entry in ipairs(all_providers) do
if i > self._entries_offset then
if entries_count == max_entries then
break
end
table.insert(self._entries_exec, entry.exec)
local entry_widget = wibox.widget {
widget = wibox.container.background,
shape = qui.shape,
{
widget = wibox.container.margin,
margins = qui.PADDING,
{
widget = wibox.container.constraint,
strategy = "max",
height = qui.CHAR_HEIGHT,
{
{
widget = wibox.container.background,
fg = qcolor.palette.low,
{
widget = wibox.widget.textbox,
text = entry.provider .. " | ",
},
},
{
widget = wibox.widget.textbox,
text = entry.message,
},
spacing = qui.PADDING,
layout = wibox.layout.fixed.horizontal,
},
},
},
buttons = {
awful.button {
modifiers = {},
button = btn.LEFT,
on_press = function()
self:_exec_entry(i)
end,
},
},
_selected = false,
}
if self._selected_index + self._entries_offset == i then
entry_widget._selected = true
entry_widget.bg = qcolor.palette.bg.high
end
entry_widget:connect_signal("mouse::enter", function()
if entry_widget._selected == true then
return
end
entry_widget.bg = qcolor.palette.bg.high
end)
entry_widget:connect_signal("mouse::leave", function()
if entry_widget._selected == true then
return
end
entry_widget.bg = qcolor.palette.bg()
end)
layout:add(entry_widget)
entries_count = entries_count + 1
self._visible_entries = entries_count
end
end
status.text = self._entries_offset + self._selected_index .. "/" .. self._entries_count
end
fresnel._text = ""
fresnel._prompt = al_prompt {
prompt = "",
reset_on_stop = true,
ul_cursor = "low",
bg_cursor = qcolor.palette.bg.high,
changed_callback = function(text)
if fresnel._text == text then
return
end
if fresnel._toggled == false then
return
end
fresnel:_update(text)
fresnel._text = text
end,
keypressed_callback = function(mod, key)
if key == "Escape" or key == " " and mod.Mod4 then
fresnel:hide()
elseif key == "Return" then
fresnel:_exec_entry(fresnel._selected_index)
fresnel:hide()
elseif key == "Up" then
local next_index = fresnel._selected_index - 1
if next_index < 1 and fresnel._entries_offset == 0 then
return
elseif next_index < 1 and fresnel._entries_offset > 0 then
fresnel._entries_offset = fresnel._entries_offset - 1
else
fresnel._selected_index = next_index
end
fresnel:_update(fresnel._text, true)
elseif key == "Down" then
local next_index = fresnel._selected_index + 1
if
next_index > fresnel._visible_entries
and fresnel._entries_offset + fresnel._visible_entries == fresnel._entries_count
then
return
elseif
next_index > fresnel._visible_entries
and fresnel._entries_offset + fresnel._visible_entries < fresnel._entries_count
then
fresnel._entries_offset = fresnel._entries_offset + 1
else
fresnel._selected_index = next_index
end
fresnel:_update(fresnel._text, true)
end
end,
}
local max_height = qui.BIG_PADDING * 2 + (qui.BIG_PADDING * 2 + qui.CHAR_HEIGHT) * 10
-- + qui.PADDING
-- + qui.PADDING * 9
local width = awful.screen.focused().geometry.width / 2
fresnel._popup = qui.popup {
-- visible = false,
ontop = true,
placement = false,
shape = function(cr, w)
gshape.partially_rounded_rect(cr, w, 0, false, false, true, true, qui.BORDER_RADIUS)
end,
x = width / 2,
minimum_width = width,
maximum_width = width,
-- maximum_height = max_height,
widget = {
qui.styled {
widget = wibox.container.background,
fg = qcolor.palette.fg.low,
bg = qcolor.palette.bg.high,
{
widget = wibox.container.margin,
margins = qui.BIG_PADDING,
{
{
widget = wibox.widget.textbox,
text = ">",
},
{
widget = wibox.container.margin,
margins = {
left = qui.PADDING,
right = qui.PADDING,
},
{
widget = wibox.container.constraint,
strategy = "max",
height = qui.CHAR_HEIGHT,
{
widget = wibox.container.background,
fg = qcolor.palette.fg(),
fresnel._prompt.textbox,
},
},
},
{
widget = wibox.widget.textbox,
text = "0/0",
id = "status",
},
layout = wibox.layout.align.horizontal,
},
},
},
{
widget = wibox.container.margin,
margins = {
top = qui.PADDING,
},
{
spacing = qui.PADDING,
layout = wibox.layout.fixed.vertical,
id = "entry_layout",
},
},
layout = wibox.layout.align.vertical,
},
}
function fresnel:show()
self._toggled = true
self._opacity_timed.target = 1
self._height_timed:set(max_height)
self:_update()
self._prompt:start()
end
function fresnel:hide()
self._toggled = false
self._opacity_timed.target = 0
self._height_timed:set(0)
self._prompt:stop()
end
fresnel._height_timed = qanim:new {
duration = qvars.anim_duration,
pos = 0,
easing = qvars.easing,
subscribed = function(pos)
fresnel._popup.shape = function(cr, w)
gshape.partially_rounded_rect(cr, w, pos, false, false, true, true, qui.BORDER_RADIUS)
end
end,
}
-- TODO: optimize the search algo to be more efficient and not require making fresnel invisible
fresnel._opacity_timed = rubato.timed {
duration = qvars.anim_duration,
pos = 0,
subscribed = function(pos)
fresnel._popup.opacity = pos
if pos == 0 then
fresnel._popup.visible = false
else
fresnel._popup.visible = true
end
end,
}
return fresnel
|