aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/ui/insightful/init.lua
blob: 4ed6099bdc2996d15a66ea568e455d9b0fd7919e (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
local awful = require "awful"
local gtable = require "gears.table"
local h = require "misc.helpers"
local vars = require "misc.vars"
local wibox = require "wibox"
local rubato = require "lib.rubato"
local btn = awful.button.names

local first_time = true
local mouse = "󰍽 "

local insightful = {}

insightful._toggled = false
insightful._bindings = {}
insightful._selected_group = ""

insightful._keymap = {
    Control          = "Control",
    Mod1             = "Alt",
    ISO_Level3_Shift = "Alt Gr",
    Mod4             = "Super",
    Insert           = "Insert",
    Delete           = "Delete",
    Next             = "Page Up",
    Prior            = "Page Down",
    Left             = "",
    Up               = "",
    Right            = "",
    Down             = "",
    KP_End           = "1",
    KP_Down          = "#2",
    KP_Next          = "#3",
    KP_Left          = "#4",
    KP_Begin         = "#5",
    KP_Right         = "#6",
    KP_Home          = "#7",
    KP_Up            = "#8",
    KP_Prior         = "#9",
    KP_Insert        = "#0",
    KP_Delete        = "#.",
    KP_Divide        = "#/",
    KP_Multiply      = "#*",
    KP_Subtract      = "#-",
    KP_Add           = "#+",
    KP_Enter         = "#Enter",
    Escape           = "Esc",
    Tab              = "Tab",
    space            = "Space",
    Return           = "Enter",
    dead_acute       = "´",
    dead_circumflex  = "^",
    dead_grave       = "`",
    XF86MonBrightnessUp   = "🔆+",
    XF86MonBrightnessDown = "🔅-",
    XF86AudioRaiseVolume = "ﱛ",
    XF86AudioLowerVolume = "ﱜ",
    XF86AudioMute = "ﱝ",
    XF86AudioPlay = "⏯",
    XF86AudioPrev = "⏮",
    XF86AudioNext = "⏭",
    XF86AudioStop = "⏹",
    [tostring(btn.LEFT)] = mouse .. "Left",
    [tostring(btn.MIDDLE)] = mouse .. "Middle",
    [tostring(btn.RIGHT)] = mouse .. "Right"
}

insightful._widget = h.popup {
    visible = false,
    ontop = true,
    placement = awful.placement.centered,
    minimum_height = screen[1].geometry.height / 2,
    minimum_width = screen[1].geometry.width / 2,
    widget = {
        layout = wibox.layout.fixed.vertical,
        spacing = vars.big_padding,
        id = "layout_container"
    }
}

function insightful:_generate()
    local grouped_binds = {}
    local layout_container = insightful._widget.widget.layout_container

    for _, keybind in ipairs(self._bindings) do
        local group = keybind.group or "general"
        local group_exists = grouped_binds[group] ~= nil

        if not group_exists then
            grouped_binds[group] = {}
        end

        table.insert(grouped_binds[group], { 
            mods = keybind.mods,
            triggers = keybind.triggers,
            desc = keybind.desc
        })
    end

    for group, keybinds in h.opairs(grouped_binds) do
        local group_layout = {
            spacing = vars.padding,
            layout = wibox.layout.fixed.vertical
        }

        for _, keybind in ipairs(keybinds) do
            local key_layout = {
                layout = wibox.layout.fixed.horizontal
            }

            local key_and_desc_layout = {
                nil, -- key
                nil,
                nil, -- description?
                layout = wibox.layout.align.horizontal
            }
        
            for _, mod in ipairs(keybind.mods) do
                table.insert(key_layout, 1, h.styled {
                    widget = wibox.container.background,
                    bg = vars.colors.bright.black,
                    border_width = 0,
                    {
                        widget = wibox.container.margin,
                        margins = vars.padding,
                        {
                            widget = wibox.widget.textbox,
                            text = insightful._keymap[mod] or mod
                        }
                    }
                })
                table.insert(key_layout, 2, {
                    widget = wibox.widget.textbox,
                    text = " + "
                })
            end

            if type(keybind.triggers) == "string" or type(keybind.triggers) == "number" then
                table.insert(key_layout, {
                    widget = wibox.widget.textbox,
                    text = insightful._keymap[tostring(keybind.triggers)] or keybind.triggers
                })
            elseif type(keybind.triggers) == "table" then
                local display_trigger = {}
                for _, trigger in ipairs(keybind.triggers) do
                    table.insert(display_trigger, insightful._keymap[trigger[1]] or trigger[1])
                end
                table.insert(key_layout, {
                    widget = wibox.widget.textbox,
                    text = table.concat(display_trigger, "/")
                })
            end

            if keybind.desc then
                key_and_desc_layout[3] = {
                    widget = wibox.container.background,
                    fg = vars.colors.dim.fg,
                    {
                        widget = wibox.widget.textbox,
                        text = keybind.desc
                    }
                }
            end

            key_and_desc_layout[1] = key_layout

            table.insert(group_layout, key_and_desc_layout)
        end 
    
        layout_container:add {
            {
                {
                    widget = wibox.container.background,
                    bg = vars.colors.yellow,
                    fg = vars.colors.bg,
                    shape = vars.shape,
                    {
                        widget = wibox.container.margin,
                        margins = vars.padding,
                        {
                            widget = wibox.widget.textbox,
                            text = group
                        }
                    }
                },
                nil,
                layout = wibox.layout.align.horizontal
            },
            group_layout,
            spacing = vars.padding,
            layout = wibox.layout.fixed.vertical
        }
    end
end

local timed = rubato.timed {
    duration = vars.anim_duration,
    intro = vars.anim_intro,
    pos = 0,
    subscribed = function(pos)
        insightful._widget.opacity = pos

        if pos == 0 then
            insightful._widget.visible = false
        else
            insightful._widget.visible = true
        end
    end
}

function insightful:toggle()
    if first_time then 
        insightful:_generate_widget()
        first_time = false
    end

    timed.target = insightful._toggled and 0 or 1
    insightful._toggled = not insightful._toggled
end

local function get_binding_function(trigger)
    if type(trigger) == "number" and trigger <= 5 and trigger > 0 then
        return "button"
    elseif type(trigger) == "string" then
        return "key"
    end
    error("trigger can only be a mouse button or a key", 2)
end

local function translate_binding(binding, trigger, multiple)
    local value = nil
    if multiple then
        value = trigger[2]
        trigger = trigger[1]
    end

    local awful_binding = {
        modifiers = binding.mods,
        [get_binding_function(trigger)] = trigger,
        on_press = multiple and function(...) binding.press(value, ...) end or binding.press
    }

    if binding.desc then
        awful_binding.description = binding.desc
    end
    if binding.group then
        awful_binding.group = binding.group
    end

    return awful[get_binding_function(trigger)](awful_binding)
end

function insightful:bind(binding)
    local awful_bindings = {}
    table.insert(self._bindings, binding)

    if type(binding.triggers) == "table" then
        for _, trigger in ipairs(binding.triggers) do
            table.insert(awful_bindings, translate_binding(binding, trigger, true))
        end
    elseif type(binding.triggers) == "string" or type(binding.triggers) == "number" then
        return translate_binding(binding, binding.triggers, false)
    else
        error("binding.triggers can only be a string or a table")
    end

    -- for some reason multi-trigger bindings only work if i do this
    -- i spent a day debugging this
    -- thanks awesome
    return gtable.join(table.unpack(awful_bindings))
end

return insightful