aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/ui/fresnel/init.lua
blob: 613bc9ca0850d9d0b676e722d78395e7e648b3e4 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
local text_input = require "ui.fresnel.text_input"
local awful = require "awful"
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_VISIBLE_ENTRIES = 10
local DEFAULT_SCROLL_AMOUNT = 0
local DEFAULT_SELECTED_IDX = 1

---@class (exact) Fresnel
---@field private _toggled boolean Whether fresnel is open
---@field private _entries (Entry|string)[]
---@field private _scroll_amount integer How many entries down are we
---@field private _prev_scroll_amount integer The _scroll_amount of the previous cycle
---@field private _visible_entries Entry[] The currently visible entries
---@field private _selected_idx integer The index of the currently selected entry. Is in the range 1..
---@field private _prev_selected_idx integer The _selected_idx of the previous cycle
---@field private _active_lenses integer The number of lense titles in the _entries table
---@field private _lense_header_positions integer[] The positions of all the lense titles
---@field private _prev_text string
---@field private _first_query boolean
---@field private _make_header fun(string): wibox.container.background
---@field private _w_popup wibox.widget.base
---@field private _w_prompt wibox.widget.base
---@field private _w_status wibox.widget.base
---@field private _l_entries wibox.widget.base
---@field private _t_opacity table
---@field private _t_height table
local fresnel = {
    _toggled = false,
    _entries = {},
    _scroll_amount = DEFAULT_SCROLL_AMOUNT,
    _prev_scroll_amount = DEFAULT_SCROLL_AMOUNT,
    _visible_entries = {},
    _selected_idx = DEFAULT_SELECTED_IDX,
    _prev_selected_idx = DEFAULT_SELECTED_IDX,
    _lense_header_positions = {}
}

---@private
---@param lense_name string
function fresnel._make_header(lense_name)
    return wibox.widget {
        widget = wibox.container.background,
        fg = qcolor.palette.fg.low,
        {
            widget = wibox.container.margin,
            margins = qui.PADDING,
            {
                {
                    widget = wibox.container.place,
                    {
                        widget = wibox.container.background,
                        bg = qcolor.palette.border(),
                        forced_height = qui.BORDER_WIDTH,
                        forced_width = qui.BIG_PADDING * 2,
                    },
                },
                {
                    widget = wibox.container.constraint,
                    strategy = "max",
                    height = qui.CHAR_HEIGHT,
                    {
                        widget = wibox.widget.textbox,
                        text = lense_name,
                    },
                },
                {
                    widget = wibox.container.place,
                    fill_horizontal = true,
                    content_fill_horizontal = true,
                    {
                        widget = wibox.container.background,
                        bg = qcolor.palette.border(),
                        forced_height = qui.BORDER_WIDTH,
                    },
                },
                layout = wibox.layout.fixed.horizontal,
                spacing = qui.BIG_PADDING,
            },
        },
    }
end

---@private
---@param entry Entry
function fresnel:_make_entry(entry)
    ---@class FresnelEntry : wibox.widget.base
    ---@field _selected boolean
    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.widget.textbox,
                    text = entry.message,
                },
            },
        },
        buttons = {
            awful.button {
                modifiers = {},
                button = btn.LEFT,
                on_press = function()
                    fresnel:_exec_entry(entry)
                end,
            },
        },
        _selected = false,
    }

    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)

    return entry_widget
end

---@private
---@param entry integer|Entry
function fresnel:_exec_entry(entry)
    if type(entry) == "number" then
        entry = self._visible_entries[entry]
    end
    local exec = entry.exec
    if type(exec) ~= "userdata" and type(exec) ~= "nil" then
        if exec[2] then
            awful.spawn.with_shell("xdg-terminal-exec /bin/sh -c '" .. exec[1]:gsub("'", [[\']]) .. "' 1>/dev/null 2>&1")
        else
            awful.spawn.with_shell(exec[1] .. " 1>/dev/null 2>&1")
        end
    end
end

---@private
---@param query string?
function fresnel:_update(query)
    query = query or ""
    self._entries = {}
    self._lense_header_positions = {}
    self._active_lenses = 0

    for _, lense in ipairs(qnative.lenses) do
        local entries = lense:query(query)
        if type(entries) == "table" then
            table.insert(self._entries, lense.name)
            table.insert(self._lense_header_positions, #self._entries)
            if #entries > 0 then -- Entry[]
                self._entries = gtable.join(self._entries, entries)
            elseif entries.message then -- Entry
                table.insert(self._entries, entries)
            end
            self._active_lenses = self._active_lenses + 1
        end -- either empty Entry[] or nil, in which case we shouldn't display them
    end

    -- reset the scroll
    if type(self._entries[1]) == "string" then
        self._selected_idx = 2 -- to avoid selecting the header first
    else
        self._selected_idx = DEFAULT_SELECTED_IDX
    end
    self._scroll_amount = DEFAULT_SCROLL_AMOUNT
    fresnel:_render(true)
    self._first_query = false
end

---@private
---@param update boolean
function fresnel:_render(update)
    local visible_entries_end = math.min(self._scroll_amount + MAX_VISIBLE_ENTRIES, #self._entries)
    self._visible_entries = qtable.slice(self._entries, self._scroll_amount + 1, visible_entries_end)

    local layout = fresnel._l_entries

    if self._scroll_amount ~= self._prev_scroll_amount or update or self._first_query then
        layout:reset()
        for i, entry in ipairs(self._visible_entries) do
            if type(entry) == "string" then
                layout:add(self._make_header(entry))
            else
                local entry_widget = self:_make_entry(entry)
                if self._selected_idx == i then
                    entry_widget._selected = true
                    entry_widget.bg = qcolor.palette.bg.high
                end
                layout:add(entry_widget)
            end
        end
    elseif self._selected_idx ~= self._prev_selected_idx then
        local entry_widget = layout.children[self._selected_idx] --[[@as FresnelEntry ]]
        entry_widget._selected = true
        entry_widget.bg = qcolor.palette.bg.high

        local prev_entry_widget = layout.children[self._prev_selected_idx] --[[@as FresnelEntry ]]
        prev_entry_widget._selected = false
        prev_entry_widget.bg = qcolor.palette.bg()
    end

    local headers_passed = 0
    local current_position = self._scroll_amount + self._selected_idx
    for _, position in ipairs(self._lense_header_positions) do
        if current_position > position then
            headers_passed = headers_passed + 1
        end
    end
    self._w_status.text = (self._scroll_amount + self._selected_idx - headers_passed) .. "/" .. (#self._entries - self._active_lenses)
    self._prev_scroll_amount = self._scroll_amount
    self._prev_selected_idx = self._selected_idx
end

---@param up boolean Whether to scroll up or down
function fresnel:scroll(up)
    local direction = up and -1 or 1
    local offset = direction


    if type(self._entries[self._scroll_amount + self._selected_idx + offset]) == "string" then
        offset = offset + direction
    end

    local new_selected_idx = self._selected_idx + offset
    local new_position = self._scroll_amount + new_selected_idx
    local new_scroll_amount = self._scroll_amount + offset

    if new_position < 1 then
        if self._scroll_amount > 0 then
            self._scroll_amount = self._scroll_amount + direction
            self._selected_idx = self._selected_idx - direction
            self:_render(false)
        end
        return
    elseif new_position > #self._entries then
        return
    end

    if up and new_selected_idx <= 0 then
        self._scroll_amount = new_scroll_amount
    elseif not up and new_selected_idx > #self._visible_entries then
        self._scroll_amount = new_scroll_amount
    else
        self._selected_idx = new_selected_idx
    end

    self:_render(false)
end

fresnel._prev_text = ""
fresnel._w_prompt = wibox.widget {
    widget = text_input,
    reset_on_unfocus = true,
    unfocus_keys = {},
}

fresnel._w_prompt:connect_signal("unfocus", function ()
    fresnel:_hide()
end)

fresnel._w_prompt:connect_signal("property::text", function(_, text)
    fresnel:_update(text)
end)

fresnel._w_prompt:connect_signal("key::press", function(_, _mods, key)
    -- Convert index array to hash table
    local mods = {}
    for _, v in ipairs(_mods) do
        mods[v] = true
    end

    if key == "Escape" or key == " " and mods.Mod4 then
        fresnel:hide()
    elseif key == "Return" then
        fresnel:_exec_entry(fresnel._selected_idx)
        fresnel:hide()
    elseif key == "k" and mods.Mod1 then
        fresnel:scroll(true)
    elseif key == "j" and mods.Mod1 then
        fresnel:scroll(false)
    end
end)

local max_height = qui.BIG_PADDING * 2 + (qui.BIG_PADDING * 2 + qui.CHAR_HEIGHT) * 10

local width = awful.screen.focused().geometry.width / 2

fresnel._l_entries = wibox.widget {
    spacing = qui.PADDING,
    layout = wibox.layout.fixed.vertical,
}
fresnel._w_status = wibox.widget {
    widget = wibox.widget.textbox,
    text = "0/0",
}
fresnel._w_popup = qui.animateable_shape(qui.popup {
    -- visible = false,
    ontop = true,
    placement = false,
    shape = function(cr, w, h)
        gshape.partially_rounded_rect(cr, w, h, false, false, true, true, qui.BORDER_RADIUS)
    end,
    x = width / 2,
    y = -qui.BORDER_WIDTH,
    minimum_width = width,
    maximum_width = width,
    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,
                        markup = [[<span foreground="]] .. qcolor.palette.yellow() .. [[">></span>]],
                    },
                    {
                        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._w_prompt,
                            },
                        },
                    },
                    fresnel._w_status,
                    layout = wibox.layout.align.horizontal,
                },
            },
        },
        {
            widget = wibox.container.margin,
            margins = {
                top = qui.PADDING,
            },
            fresnel._l_entries,
        },
        layout = wibox.layout.align.vertical,
    },
})

function fresnel:show()
    self._prev_scroll_amount = DEFAULT_SCROLL_AMOUNT
    self._prev_selected_idx = DEFAULT_SELECTED_IDX
    self._first_query = true
    self._toggled = true
    self._t_opacity.target = 1
    self._t_height:set(max_height)
    self:_update()
    self._w_prompt:focus()
end

---@private
function fresnel:_hide()
    self._toggled = false
    self._t_opacity.target = 0
    self._t_height:set(0)
    for _, lense in ipairs(qnative.lenses) do
        lense:interrupt()
    end
end

function fresnel:hide()
    self._w_prompt:unfocus()
    self:_hide()
end

fresnel._t_height = qanim:new {
    duration = qvars.anim_duration,
    pos = 0,
    easing = qvars.easing,
    subscribed = function(pos)
        fresnel._w_popup.shape_height = pos
    end,
}

fresnel._t_opacity = rubato.timed {
    duration = qvars.anim_duration,
    pos = 0,
    subscribed = function(pos)
        fresnel._w_popup.opacity = pos

        if pos == 0 then
            fresnel._w_popup.visible = false
        else
            fresnel._w_popup.visible = true
        end
    end,
}

return fresnel