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
|
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 qdelegate = require "quarrel.delegate"
local wibox = require "wibox"
local cairo = require("lgi").cairo
--- 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
--- Wraps a widget inside of a margin widget
---@param target table
---@return table
function M.padded(target)
return {
widget = wibox.container.margin,
margins = M.PADDING,
target,
}
end
--- Wraps a widget inside of a margin widget
---@param target table
---@return table
function M.padded_big(target)
return {
widget = wibox.container.margin,
margins = M.BIG_PADDING,
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.separator(args)
args = args or {}
return wibox.widget(gtable.crush({
widget = wibox.container.background,
bg = qcolor.palette.border(),
forced_height = not args.vertical and args.size,
forced_width = args.vertical and args.size,
}, args.widget or {}))
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
---@param wibox wibox
function M.animateable_shape(wibox)
local old_apply_shape = wibox._apply_shape
wibox:disconnect_signal("property::geometry", old_apply_shape)
wibox:disconnect_signal("property::border_width", old_apply_shape)
function wibox:set_shape_width(value)
self._shape_width = value
self:emit_signal("property::shape_width")
end
function wibox:get_shape_width(value)
return self._shape_width
end
function wibox:set_shape_height(value)
self._shape_height = value
self:emit_signal("property::shape_height")
end
function wibox:get_shape_height(value)
return self._shape_height
end
-- override to allow us to override shape
wibox._apply_shape = qdelegate(function(env, self)
local shape = self._shape
if not shape then
self.shape_bounding = nil
self.shape_clip = nil
return
end
local geo = self:geometry()
local bw = self.border_width
if self.shape_width then
geo.width = self.shape_width
end
if self.shape_height then
geo.height = self.shape_height
end
-- First handle the bounding shape (things including the border)
local img = cairo.ImageSurface(cairo.Format.A1, geo.width + 2*bw, geo.height + 2*bw)
local cr = cairo.Context(img)
-- We just draw the shape in its full size
shape(cr, geo.width + 2*bw, geo.height + 2*bw)
cr:set_operator(cairo.Operator.SOURCE)
cr:fill()
self.shape_bounding = img._native
img:finish()
-- Now handle the clip shape (things excluding the border)
img = cairo.ImageSurface(cairo.Format.A1, geo.width, geo.height)
cr = cairo.Context(img)
-- We give the shape the same arguments as for the bounding shape and draw
-- it in its full size (the translate is to compensate for the smaller
-- surface)
cr:translate(-bw, -bw)
shape(cr, geo.width + 2*bw, geo.height + 2*bw)
cr:set_operator(cairo.Operator.SOURCE)
cr:fill_preserve()
-- Now we remove an area of width 'bw' again around the shape (We use 2*bw
-- since half of that is on the outside and only half on the inside)
cr:set_source_rgba(0, 0, 0, 0)
cr:set_line_width(2*bw)
cr:stroke()
self.shape_clip = img._native
img:finish()
end, old_apply_shape)
wibox:connect_signal("property::geometry", wibox._apply_shape)
wibox:connect_signal("property::border_width", wibox._apply_shape)
wibox:connect_signal("property::shape_width", wibox._apply_shape)
wibox:connect_signal("property::shape_height", wibox._apply_shape)
return wibox
end
return M
|