aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/quarrel/ui/init.lua
blob: 5acaa5112b162e6143d234538459fbf5402e1cf0 (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
local M = require "quarrel.ui.consts"
local awful = require "awful"
local gshape = require "gears.shape"
local gtable = require "gears.table"
local qbind = require "quarrel.bind"
local qcolor = require "quarrel.color"
local wibox = require "wibox"

--- Clip Cairo context
---@param cr cairo_surface Cairo surface
---@param w integer Widget width
---@param h integer Widget height
---@return nil
function M.shape(cr, w, h)
    -- gears.shape.rounded_rect(cr, w, h, dpi(4))
    gshape.rounded_rect(cr, w, h, M.BORDER_RADIUS)
    -- gshape.rectangle(cr, w, h)
end

--- Returns qvars.text_font with size scaled by factor
---@param factor number?
---@return string
function M.font(factor)
    return M.TEXT_FONT .. " " .. M.FONT_SIZE * (factor or 1)
end

--- Injects background widget styling into target
---@param target table
---@return table
function M.styled(target)
    return gtable.crush({
        bg = qcolor.palette.bg(),
        border_color = qcolor.palette.border(),
        border_width = M.BORDER_WIDTH,
        shape = M.shape,
    }, target)
end

--- Generates a styled popup
---@param target table
---@return table
function M.popup(target, debug)
    target.widget = {
        widget = wibox.container.margin,
        margins = M.BIG_PADDING,
        target.widget,
    }

    return awful.popup(M.styled(target))
end

--- Generates svg recolor string
---@param color string
---@return string
function M.recolor(color)
    return "svg{fill:" .. color .. "}"
end

--- Generates icon widget
---@param args table
---@return table
function M.icon(args)
    return wibox.widget(gtable.crush({
        widget = wibox.widget.imagebox,
        image = args.icon,
        forced_width = (not args.unsized) and M.CHAR_HEIGHT,
        forced_height = (not args.unsized) and M.CHAR_HEIGHT,
        stylesheet = M.recolor(args.color or qcolor.palette.fg()),
    }, args.widget or {}))
end

--- Generates button widget
---@param args table
---@return table
function M.button(args)
    args.press = args.press or function(_) end
    local widget = wibox.widget(gtable.crush({
        widget = wibox.widget.imagebox,
        image = args.icon,
        forced_height = M.CHAR_HEIGHT,
        forced_width = M.CHAR_HEIGHT,
        stylesheet = M.recolor(qcolor.palette.fg()),
        press = args.press,
    }, args.widget or {}))

    widget.buttons = {
        qbind {
            triggers = qbind.btns.left,
            press = function()
                widget:press()
            end,
            hidden = true,
        },
    }

    return widget
end

--- Generates toggle widget
---@param args table
---@return table
function M.toggle(args)
    args.press = args.press or function(_) end
    local widget = M.button {
        widget = gtable.crush({
            toggled = false,
            silent_press = function(self, state)
                if state then
                    self.toggled = state
                else
                    self.toggled = not self.toggled
                end

                if self.toggled then
                    self.image = args.on
                else
                    self.image = args.off
                end
            end,
        }, args.widget or {}),
        icon = args.off,
        press = function(self)
            if not args.manual then
                self:silent_press()
            end
            args.press(self)
        end,
    }

    return widget
end

---@param widget wibox.widget.base
---@param cursor string
---@return wibox.widget.base
function M.hoverable(widget, cursor)
    local last_wibox

    widget:connect_signal("mouse::enter", function()
        local w = mouse.current_wibox
        last_wibox = w
        if w then
            w.cursor = cursor
        end

        if type(widget.hover) == "function" then -- on enter bind
            widget.hover()
        elseif type(widget.hover) == "table" and widget.hover.enter then -- { enter: fun(), leave: fun() }
            widget.hover.enter()
        end
    end)

    widget:connect_signal("mouse::leave", function()
        local w = last_wibox or mouse.current_wibox
        if w then
            w.cursor = "left_ptr"
        end

        if type(widget.hover) == "table" and widget.hover.leave then -- { enter: fun(), leave: fun() }
            widget.hover.leave()
        end
    end)

    return widget
end

return M