wibar: Restore the ability to align a wibar.

This was lost in 3.5->4.0 update, but still had some references in
the code and doc. At the time, the plan was to add it back
"shortly after" based on the `awful.placement` code, but it was
never merged.
This commit is contained in:
Emmanuel Lepage Vallee 2021-07-05 16:40:58 -07:00
parent b62f343409
commit dab767af3e
1 changed files with 68 additions and 5 deletions

View File

@ -47,11 +47,36 @@ local opposite_margin = {
right = "left" right = "left"
} }
local align_map = {
top = "left",
left = "top",
bottom = "right",
right = "bottom",
centered = "centered"
}
--- If the wibar needs to be stretched to fill the screen. --- If the wibar needs to be stretched to fill the screen.
-- @property stretch -- @property stretch
-- @tparam boolean stretch -- @tparam boolean stretch
-- @propbeautiful -- @propbeautiful
-- @propemits true false -- @propemits true false
-- see align
--- How to align non-stretched wibars.
--
-- Values are:
--
-- * top
-- * bottom
-- * left
-- * right
-- * centered
--
-- @property align
-- @tparam string align
-- @propbeautiful
-- @propemits true false
-- @see stretch
--- Margins on each side of the wibar. --- Margins on each side of the wibar.
-- --
@ -119,6 +144,10 @@ local opposite_margin = {
-- @beautiful beautiful.wibar_margins -- @beautiful beautiful.wibar_margins
-- @tparam number|table margins -- @tparam number|table margins
--- The wibar's alignments.
-- @beautiful beautiful.wibar_align
-- @tparam string align
-- Compute the margin on one side -- Compute the margin on one side
local function get_margin(w, position, auto_stop) local function get_margin(w, position, auto_stop)
@ -165,16 +194,30 @@ local function get_margins(w)
end end
-- Create the placement function -- Create the placement function
local function gen_placement(position, stretch) local function gen_placement(position, align, stretch)
local maximize = (position == "right" or position == "left") and local maximize = (position == "right" or position == "left") and
"maximize_vertically" or "maximize_horizontally" "maximize_vertically" or "maximize_horizontally"
return placement[position] + (stretch and placement[maximize] or nil) local corner = nil
if align ~= "centered" then
if position == "right" or position == "left" then
corner = placement[align .. "_" .. position]
or placement[align_map[align] .. "_" .. position]
else
corner = placement[position .. "_" .. align]
or placement[position .. "_" .. align_map[align]]
end
end
corner = corner or placement[position]
return corner + (stretch and placement[maximize] or nil)
end end
-- Attach the placement function. -- Attach the placement function.
local function attach(wb, align) local function attach(wb, position)
gen_placement(align, wb._stretch)(wb, { gen_placement(position, wb._private.align, wb._stretch)(wb, {
attach = true, attach = true,
update_workarea = true, update_workarea = true,
margins = get_margins(wb) margins = get_margins(wb)
@ -311,6 +354,21 @@ function awfulwibar.get_margins(self)
return self._private.meta_margins return self._private.meta_margins
end end
function awfulwibar.get_align(self)
return self._private.align
end
function awfulwibar.set_align(self, value)
assert(align_map[value])
self._private.align = value
attach(self, self.position)
self:emit_signal("property::align", value)
end
--- Remove a wibar. --- Remove a wibar.
-- @method remove -- @method remove
@ -471,7 +529,7 @@ function awfulwibar.new(args)
-- The C code scans the table directly, so metatable magic cannot be used. -- The C code scans the table directly, so metatable magic cannot be used.
for _, prop in ipairs { for _, prop in ipairs {
"border_width", "border_color", "font", "opacity", "ontop", "cursor", "border_width", "border_color", "font", "opacity", "ontop", "cursor",
"bgimage", "bg", "fg", "type", "stretch", "shape", "margins" "bgimage", "bg", "fg", "type", "stretch", "shape", "margins", "align"
} do } do
if (args[prop] == nil) and beautiful["wibar_"..prop] ~= nil then if (args[prop] == nil) and beautiful["wibar_"..prop] ~= nil then
args[prop] = beautiful["wibar_"..prop] args[prop] = beautiful["wibar_"..prop]
@ -480,6 +538,8 @@ function awfulwibar.new(args)
local w = wibox(args) local w = wibox(args)
w._private.align = (args.align and align_map[args.align]) and args.align or "centered"
w._private.margins = { w._private.margins = {
left = 0, left = 0,
right = 0, right = 0,
@ -493,6 +553,9 @@ function awfulwibar.new(args)
w._screen = screen --HACK When a screen is removed, then getbycoords wont work w._screen = screen --HACK When a screen is removed, then getbycoords wont work
w._stretch = args.stretch == nil and has_to_stretch or args.stretch w._stretch = args.stretch == nil and has_to_stretch or args.stretch
w.get_align = awfulwibar.get_align
w.set_align = awfulwibar.set_align
w.get_position = get_position w.get_position = get_position
w.set_position = set_position w.set_position = set_position