summaryrefslogtreecommitdiff
path: root/awesome/widgets/classiclayoutbox.lua
blob: 818dc0ffc9dbed9d976749b5c2b0187d10a63a62 (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
---------------------------------------------------------------------------
--- Layoutbox widget.
--
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2009 Julien Danjou
-- @classmod awful.widget.layoutbox
---------------------------------------------------------------------------

local setmetatable = setmetatable
local capi = { screen = screen, tag = tag }
local layout = require("awful.layout")
local tooltip = require("awful.tooltip")
local wibox = require("wibox")
local surface = require("gears.surface")

local layout_icons = {
	cornernw = "",
	cornerne = "",
	cornersw = "",
	cornerse = "",
	fairh = "",
	fairv = "",
	max = "[M]",
	floating  = "><>",
	magnifier = "[M]",
	fullscreen = "",
	spiral = "",
	dwindle = "",
	tile = "[]=",
	tiletop  = "TTT",
	tilebottom  = "TTT",
	tileleft  = "=[]",
}

local function get_screen(s)
	return s and capi.screen[s]
end

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]")

	if name == "max" and #screen.clients > 0 then
		w.textbox.text = " [" .. #screen.clients .. "] " -- TODO manage / unmanage signal
		return
	end

	w.textbox.text = layout_icons[name]
	w.textbox.text = " " .. w.textbox.text .. " "
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.
-- @param screen The screen number that the layout will be represented for.
-- @return An imagebox widget configured as a layoutbox.
function layoutbox.new(screen)
	screen = get_screen(screen or 1)

	-- Do we already have the update callbacks registered?
	if boxes == nil then
		boxes = setmetatable({}, { __mode = "kv" })
		capi.tag.connect_signal("property::selected", update_from_tag)
		capi.tag.connect_signal("property::layout", update_from_tag)
		capi.tag.connect_signal("property::screen", function()
			for s, w in pairs(boxes) do
				if s.valid then
					update(w, s)
				end
			end
		end)
		layoutbox.boxes = boxes
	end

	-- Do we already have a layoutbox for this screen?
	local w = boxes[screen]
	if not w then
		w = wibox.widget {
			{
				id     = "textbox",
				widget = wibox.widget.textbox
			},
			layout = wibox.layout.fixed.horizontal
		}

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

		update(w, screen)
		boxes[screen] = w
	end

	return w
end

function layoutbox.mt:__call(...)
	return layoutbox.new(...)
end

return setmetatable(layoutbox, layoutbox.mt)