2009-05-05 17:32:53 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
awesome = awesome,
|
|
|
|
screen = screen,
|
2009-07-10 14:36:04 +02:00
|
|
|
client = client
|
2009-05-05 17:32:53 +02:00
|
|
|
}
|
|
|
|
local setmetatable = setmetatable
|
2010-01-04 23:37:16 +01:00
|
|
|
local tostring = tostring
|
2009-05-05 17:32:53 +02:00
|
|
|
local ipairs = ipairs
|
|
|
|
local table = table
|
2009-09-09 10:39:08 +02:00
|
|
|
local error = error
|
2010-10-06 14:20:30 +02:00
|
|
|
local wibox = require("wibox")
|
|
|
|
local beautiful = require("beautiful")
|
2009-05-05 17:32:53 +02:00
|
|
|
|
|
|
|
--- Wibox module for awful.
|
2009-09-08 17:02:56 +02:00
|
|
|
-- This module allows you to easily create wibox and attach them to the edge of
|
|
|
|
-- a screen.
|
2012-06-12 20:13:09 +02:00
|
|
|
-- awful.wibox
|
|
|
|
local awfulwibox = { mt = {} }
|
2009-05-05 17:32:53 +02:00
|
|
|
|
|
|
|
-- Array of table with wiboxes inside.
|
|
|
|
-- It's an array so it is ordered.
|
|
|
|
local wiboxes = {}
|
|
|
|
|
|
|
|
--- Get a wibox position if it has been set, or return top.
|
|
|
|
-- @param wibox The wibox
|
|
|
|
-- @return The wibox position.
|
2012-06-12 20:13:09 +02:00
|
|
|
function awfulwibox.get_position(wibox)
|
2009-05-05 17:32:53 +02:00
|
|
|
for _, wprop in ipairs(wiboxes) do
|
|
|
|
if wprop.wibox == wibox then
|
|
|
|
return wprop.position
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return "top"
|
|
|
|
end
|
|
|
|
|
2009-08-18 15:45:17 +02:00
|
|
|
--- Put a wibox on a screen at this position.
|
2009-05-05 17:32:53 +02:00
|
|
|
-- @param wibox The wibox to attach.
|
|
|
|
-- @param position The position: top, bottom left or right.
|
|
|
|
-- @param screen If the wibox it not attached to a screen, specified on which
|
|
|
|
-- screen the position should be set.
|
2012-06-12 20:13:09 +02:00
|
|
|
function awfulwibox.set_position(wibox, position, screen)
|
2009-08-18 15:45:17 +02:00
|
|
|
local area = capi.screen[screen].geometry
|
2009-05-05 17:32:53 +02:00
|
|
|
|
|
|
|
-- The "length" of a wibox is always chosen to be the optimal size
|
|
|
|
-- (non-floating).
|
|
|
|
-- The "width" of a wibox is kept if it exists.
|
|
|
|
if position == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x + area.width - (wibox.width + 2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif position == "left" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif position == "bottom" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = (area.y + area.height) - (wibox.height + 2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif position == "top" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = area.y
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
|
2009-06-05 19:49:03 +02:00
|
|
|
for _, wprop in ipairs(wiboxes) do
|
|
|
|
if wprop.wibox == wibox then
|
|
|
|
wprop.position = position
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Reset all wiboxes positions.
|
|
|
|
local function update_all_wiboxes_position()
|
|
|
|
for _, wprop in ipairs(wiboxes) do
|
2012-06-12 20:13:09 +02:00
|
|
|
awfulwibox.set_position(wprop.wibox, wprop.position, wprop.screen)
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-11 15:27:31 +02:00
|
|
|
local function call_wibox_position_hook_on_prop_update(w)
|
|
|
|
update_all_wiboxes_position()
|
|
|
|
end
|
|
|
|
|
2009-08-18 14:50:00 +02:00
|
|
|
local function wibox_update_strut(wibox)
|
|
|
|
for _, wprop in ipairs(wiboxes) do
|
|
|
|
if wprop.wibox == wibox then
|
2009-08-21 20:04:29 +02:00
|
|
|
if not wibox.visible then
|
|
|
|
wibox:struts { left = 0, right = 0, bottom = 0, top = 0 }
|
|
|
|
elseif wprop.position == "top" then
|
2010-03-19 03:19:14 +01:00
|
|
|
wibox:struts { left = 0, right = 0, bottom = 0, top = wibox.height + 2 * wibox.border_width }
|
2009-08-18 14:50:00 +02:00
|
|
|
elseif wprop.position == "bottom" then
|
2010-03-19 03:19:14 +01:00
|
|
|
wibox:struts { left = 0, right = 0, bottom = wibox.height + 2 * wibox.border_width, top = 0 }
|
2009-08-18 14:50:00 +02:00
|
|
|
elseif wprop.position == "left" then
|
2010-03-19 03:19:14 +01:00
|
|
|
wibox:struts { left = wibox.width + 2 * wibox.border_width, right = 0, bottom = 0, top = 0 }
|
2009-08-18 14:50:00 +02:00
|
|
|
elseif wprop.position == "right" then
|
2010-03-19 03:19:14 +01:00
|
|
|
wibox:struts { left = 0, right = wibox.width + 2 * wibox.border_width, bottom = 0, top = 0 }
|
2009-08-18 14:50:00 +02:00
|
|
|
end
|
2009-08-21 20:04:29 +02:00
|
|
|
break
|
2009-08-18 14:50:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-05 17:32:53 +02:00
|
|
|
--- Attach a wibox to a screen.
|
|
|
|
-- If a wibox is attached, it will be automatically be moved when other wiboxes
|
|
|
|
-- will be attached.
|
|
|
|
-- @param wibox The wibox to attach.
|
|
|
|
-- @param position The position of the wibox: top, bottom, left or right.
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @param screen TODO, this seems to be unused
|
2012-06-12 20:13:09 +02:00
|
|
|
function awfulwibox.attach(wibox, position, screen)
|
2009-05-05 17:32:53 +02:00
|
|
|
-- Store wibox as attached in a weak-valued table
|
|
|
|
local wibox_prop_table
|
|
|
|
-- Start from end since we sometimes remove items
|
|
|
|
for i = #wiboxes, 1, -1 do
|
|
|
|
-- Since wiboxes are stored as weak value, they can disappear.
|
|
|
|
-- If they did, remove their entries
|
|
|
|
if wiboxes[i].wibox == nil then
|
|
|
|
table.remove(wiboxes, i)
|
|
|
|
elseif wiboxes[i].wibox == wibox then
|
|
|
|
wibox_prop_table = wiboxes[i]
|
|
|
|
-- We could break here, but well, let's check if there is no other
|
|
|
|
-- table with their wiboxes been garbage collected.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not wibox_prop_table then
|
2011-04-30 17:13:56 +02:00
|
|
|
table.insert(wiboxes, setmetatable({ wibox = wibox, position = position, screen = screen }, { __mode = 'v' }))
|
2009-05-05 17:32:53 +02:00
|
|
|
else
|
|
|
|
wibox_prop_table.position = position
|
|
|
|
end
|
|
|
|
|
2009-10-09 20:39:55 +02:00
|
|
|
wibox:connect_signal("property::width", wibox_update_strut)
|
|
|
|
wibox:connect_signal("property::height", wibox_update_strut)
|
|
|
|
wibox:connect_signal("property::visible", wibox_update_strut)
|
|
|
|
|
|
|
|
wibox:connect_signal("property::width", call_wibox_position_hook_on_prop_update)
|
|
|
|
wibox:connect_signal("property::height", call_wibox_position_hook_on_prop_update)
|
|
|
|
wibox:connect_signal("property::visible", call_wibox_position_hook_on_prop_update)
|
|
|
|
wibox:connect_signal("property::border_width", call_wibox_position_hook_on_prop_update)
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Align a wibox.
|
|
|
|
-- @param wibox The wibox.
|
|
|
|
-- @param align The alignment: left, right or center.
|
|
|
|
-- @param screen If the wibox is not attached to any screen, you can specify the
|
|
|
|
-- screen where to align. Otherwise 1 is assumed.
|
2012-06-12 20:13:09 +02:00
|
|
|
function awfulwibox.align(wibox, align, screen)
|
|
|
|
local position = awfulwibox.get_position(wibox)
|
2010-01-05 09:52:43 +01:00
|
|
|
local area = capi.screen[screen].workarea
|
2009-05-05 17:32:53 +02:00
|
|
|
|
|
|
|
if position == "right" then
|
|
|
|
if align == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = area.y
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "left" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = area.y + area.height - (wibox.height + 2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "center" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = area.y + (area.height - wibox.height) / 2
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
elseif position == "left" then
|
|
|
|
if align == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = (area.y + area.height) - (wibox.height + 2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "left" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = area.y
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "center" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.y = area.y + (area.height - wibox.height) / 2
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
elseif position == "bottom" then
|
|
|
|
if align == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x + area.width - (wibox.width + 2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "left" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "center" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x + (area.width - wibox.width) / 2
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
elseif position == "top" then
|
|
|
|
if align == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x + area.width - (wibox.width + 2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "left" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif align == "center" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.x = area.x + (area.width - wibox.width) / 2
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
end
|
2010-03-23 00:51:29 +01:00
|
|
|
|
|
|
|
-- Update struts regardless of changes
|
|
|
|
wibox_update_strut(wibox)
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Stretch a wibox so it takes all screen width or height.
|
|
|
|
-- @param wibox The wibox.
|
2009-08-18 15:45:17 +02:00
|
|
|
-- @param screen The screen to stretch on, or the wibox screen.
|
2012-06-12 20:13:09 +02:00
|
|
|
function awfulwibox.stretch(wibox, screen)
|
2009-08-18 15:45:17 +02:00
|
|
|
if screen then
|
2012-06-12 20:13:09 +02:00
|
|
|
local position = awfulwibox.get_position(wibox)
|
2009-08-18 15:45:17 +02:00
|
|
|
local area = capi.screen[screen].workarea
|
|
|
|
if position == "right" or position == "left" then
|
|
|
|
wibox.height = area.height - (2 * wibox.border_width)
|
2010-03-19 03:17:20 +01:00
|
|
|
wibox.y = area.y
|
2009-08-18 15:45:17 +02:00
|
|
|
else
|
|
|
|
wibox.width = area.width - (2 * wibox.border_width)
|
2010-03-19 03:17:20 +01:00
|
|
|
wibox.x = area.x
|
2009-08-18 15:45:17 +02:00
|
|
|
end
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a new wibox and attach it to a screen edge.
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @see wibox
|
2014-04-13 18:06:49 +02:00
|
|
|
-- @param arg A table with standard arguments to wibox() creator.
|
2009-05-05 17:32:53 +02:00
|
|
|
-- You can add also position key with value top, bottom, left or right.
|
2009-07-31 11:33:43 +02:00
|
|
|
-- You can also use width or height in % and set align to center, right or left.
|
2009-05-05 17:32:53 +02:00
|
|
|
-- You can also set the screen key with a screen number to attach the wibox.
|
|
|
|
-- If not specified, 1 is assumed.
|
|
|
|
-- @return The wibox created.
|
2012-06-12 20:13:09 +02:00
|
|
|
function awfulwibox.new(arg)
|
2009-05-05 17:32:53 +02:00
|
|
|
local arg = arg or {}
|
|
|
|
local position = arg.position or "top"
|
2009-07-31 11:33:43 +02:00
|
|
|
local has_to_stretch = true
|
2011-04-30 17:13:56 +02:00
|
|
|
local screen = arg.screen or 1
|
2009-05-05 17:32:53 +02:00
|
|
|
|
2010-09-28 13:09:19 +02:00
|
|
|
arg.type = arg.type or "dock"
|
|
|
|
|
2009-09-09 10:39:08 +02:00
|
|
|
if position ~= "top" and position ~="bottom"
|
|
|
|
and position ~= "left" and position ~= "right" then
|
|
|
|
error("Invalid position in awful.wibox(), you may only use"
|
|
|
|
.. " 'top', 'bottom', 'left' and 'right'")
|
|
|
|
end
|
|
|
|
|
2009-06-05 23:28:54 +02:00
|
|
|
-- Set default size
|
|
|
|
if position == "left" or position == "right" then
|
2011-04-14 07:04:48 +02:00
|
|
|
arg.width = arg.width or beautiful.get_font_height(arg.font) * 1.5
|
2009-07-31 11:33:43 +02:00
|
|
|
if arg.height then
|
|
|
|
has_to_stretch = false
|
|
|
|
if arg.screen then
|
2010-01-04 23:37:16 +01:00
|
|
|
local hp = tostring(arg.height):match("(%d+)%%")
|
2009-07-31 11:33:43 +02:00
|
|
|
if hp then
|
2009-08-18 15:45:17 +02:00
|
|
|
arg.height = capi.screen[arg.screen].geometry.height * hp / 100
|
2009-07-31 11:33:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-05 23:28:54 +02:00
|
|
|
else
|
2011-04-14 07:04:48 +02:00
|
|
|
arg.height = arg.height or beautiful.get_font_height(arg.font) * 1.5
|
2009-07-31 11:33:43 +02:00
|
|
|
if arg.width then
|
|
|
|
has_to_stretch = false
|
|
|
|
if arg.screen then
|
2010-01-04 23:37:16 +01:00
|
|
|
local wp = tostring(arg.width):match("(%d+)%%")
|
2009-07-31 11:33:43 +02:00
|
|
|
if wp then
|
2009-08-18 15:45:17 +02:00
|
|
|
arg.width = capi.screen[arg.screen].geometry.width * wp / 100
|
2009-07-31 11:33:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-05 23:28:54 +02:00
|
|
|
end
|
|
|
|
|
2010-10-06 14:20:30 +02:00
|
|
|
local w = wibox(arg)
|
2009-05-05 17:32:53 +02:00
|
|
|
|
2011-03-27 16:35:15 +02:00
|
|
|
w.visible = true
|
2009-05-05 17:32:53 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
awfulwibox.attach(w, position, screen)
|
2009-07-31 11:33:43 +02:00
|
|
|
if has_to_stretch then
|
2012-06-12 20:13:09 +02:00
|
|
|
awfulwibox.stretch(w, screen)
|
2009-07-31 11:33:43 +02:00
|
|
|
else
|
2012-06-12 20:13:09 +02:00
|
|
|
awfulwibox.align(w, arg.align, screen)
|
2009-07-31 11:33:43 +02:00
|
|
|
end
|
2009-05-05 17:32:53 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
awfulwibox.set_position(w, position, screen)
|
2009-09-01 16:46:15 +02:00
|
|
|
|
2009-05-05 17:32:53 +02:00
|
|
|
return w
|
|
|
|
end
|
|
|
|
|
|
|
|
local function update_wiboxes_on_struts(c)
|
|
|
|
local struts = c:struts()
|
|
|
|
if struts.left ~= 0 or struts.right ~= 0
|
|
|
|
or struts.top ~= 0 or struts.bottom ~= 0 then
|
|
|
|
update_all_wiboxes_position()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Hook registered to reset all wiboxes position.
|
2010-08-26 18:42:38 +02:00
|
|
|
capi.client.connect_signal("property::struts", update_wiboxes_on_struts)
|
2009-10-09 20:39:55 +02:00
|
|
|
capi.client.connect_signal("unmanage", update_wiboxes_on_struts)
|
2009-05-05 17:32:53 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
function awfulwibox.mt:__call(...)
|
|
|
|
return awfulwibox.new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(awfulwibox, awfulwibox.mt)
|
2009-05-05 17:32:53 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|