Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

widget: Added widget_template argument to layoutbox #3208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions lib/awful/widget/layoutbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,17 @@ local layoutbox = { mt = {} }

local boxes = nil

local function update(w, screen)
screen = get_screen(screen)
local name = layout.getname(layout.get(screen))
w._layoutbox_tooltip:set_text(name or "[no name]")

local img = surface.load_silently(beautiful["layout_" .. name], false)
w.imagebox.image = img
w.textbox.text = img and "" or name
end

local function update_from_tag(t)
local screen = get_screen(t.screen)
local w = boxes[screen]
if w then
update(w, screen)
end
end

--- Create a layoutbox widget. It draws a picture with the current layout
-- symbol of the current tag.
-- symbol of the current tag. The last two arguments (widget_template and
-- update_function) serve to customize the layout of the layoutbox.
-- By default your widget_template should contain textbox with id 'textbox'
-- and imagebox with id 'imagebox'. If that is not enough you can pass your
-- own update_function to manipulate your custom template the way you want.
-- @tparam table args The arguments.
-- @tparam screen args.screen The screen number that the layout will be represented for.
-- @tparam table args.buttons The `awful.button`s for this layoutbox.
-- @tparam table args.widget_template A custom widget to be used for each layout
-- @tparam function args.update_function Function to create a layout widget on each update.
-- @return The layoutbox.
function layoutbox.new(args)
args = args or {}
Expand All @@ -67,6 +55,24 @@ function layoutbox.new(args)

screen = get_screen(screen or 1)

local update = args.update_function or function(w, screen)
screen = get_screen(screen)
local name = layout.getname(layout.get(screen))
w._layoutbox_tooltip:set_text(name or "[no name]")

local img = surface.load_silently(beautiful["layout_" .. name], false)
w:get_children_by_id("imagebox")[1].image = img
w:get_children_by_id("textbox")[1].text = img and "" or name
end

local function update_from_tag(t)
local screen = get_screen(t.screen)
local w = boxes[screen]
if w then
update(w, screen)
end
end

-- Do we already have the update callbacks registered?
if boxes == nil then
boxes = setmetatable({}, { __mode = "kv" })
Expand All @@ -85,7 +91,7 @@ function layoutbox.new(args)
-- Do we already have a layoutbox for this screen?
local w = boxes[screen]
if not w then
w = wibox.widget {
local template = args.widget_template or {
{
id = "imagebox",
widget = wibox.widget.imagebox
Expand All @@ -97,6 +103,8 @@ function layoutbox.new(args)
layout = wibox.layout.fixed.horizontal
}

w = wibox.widget(template)

w._layoutbox_tooltip = tooltip {objects = {w}, delay_show = 1}

-- Apply the buttons, visible, forced_width and so on
Expand Down