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
|
|
|
wibox = wibox,
|
|
|
|
client = client
|
2009-05-05 17:32:53 +02:00
|
|
|
}
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local ipairs = ipairs
|
|
|
|
local table = table
|
|
|
|
local type = type
|
2009-06-23 12:42:36 +02:00
|
|
|
local image = image
|
2009-05-05 17:32:53 +02:00
|
|
|
|
|
|
|
--- Wibox module for awful.
|
|
|
|
module("awful.wibox")
|
|
|
|
|
|
|
|
-- Array of table with wiboxes inside.
|
|
|
|
-- It's an array so it is ordered.
|
|
|
|
local wiboxes = {}
|
|
|
|
|
|
|
|
--- Get the workarea space without wiboxes geometry.
|
|
|
|
-- @param s The screen number.
|
|
|
|
-- @return The screen workarea.
|
|
|
|
function get_workarea(s)
|
|
|
|
local area = capi.screen[s].workarea
|
|
|
|
for _, wprop in ipairs(wiboxes) do
|
|
|
|
if wprop.wibox.visible and wprop.wibox.screen == s then
|
|
|
|
if wprop.position == "top" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.y = area.y + wprop.wibox.height + (2 * wprop.wibox.border_width)
|
|
|
|
area.height = area.height - (wprop.wibox.height + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif wprop.position == "bottom" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.height = area.height - (wprop.wibox.height + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif wprop.position == "left" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.x = area.x + wprop.wibox.width + (2 * wprop.wibox.border_width)
|
|
|
|
area.width = area.width - (wprop.wibox.width + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif wprop.position == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.width = area.width - (wprop.wibox.width + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return area
|
|
|
|
end
|
|
|
|
|
|
|
|
local function compute_area(wibox, position, s)
|
|
|
|
local s = wibox.screen or s or 1
|
|
|
|
local area = capi.screen[s].workarea
|
|
|
|
local ignore = false
|
|
|
|
for _, wprop in ipairs(wiboxes) do
|
|
|
|
if wprop.wibox == wibox then
|
|
|
|
ignore = true
|
|
|
|
elseif wprop.wibox.visible and wprop.wibox.screen == s
|
|
|
|
and not (ignore and wprop.position == position) then
|
|
|
|
if (wprop.position == "right" or wprop.position == "left")
|
|
|
|
and wprop.position == position then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.x = area.x + (wibox.width + (2 * wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif wprop.position == "top" then
|
|
|
|
if position == "top" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.y = area.y + (wprop.wibox.height + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif position == "left" or position == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.height = area.height - (wprop.wibox.height + (2 * wprop.wibox.border_width))
|
|
|
|
area.y = area.y + (wprop.wibox.height + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
elseif wprop.position == "bottom" then
|
|
|
|
if position == "bottom" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.y = area.y - (wprop.wibox.height + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
elseif position == "left" or position == "right" then
|
2009-08-17 16:56:03 +02:00
|
|
|
area.height = area.height - (wprop.wibox.height + (2 * wprop.wibox.border_width))
|
2009-05-05 17:32:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return area
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get a wibox position if it has been set, or return top.
|
|
|
|
-- @param wibox The wibox
|
|
|
|
-- @return The wibox position.
|
|
|
|
function get_position(wibox)
|
|
|
|
for _, wprop in ipairs(wiboxes) do
|
|
|
|
if wprop.wibox == wibox then
|
|
|
|
return wprop.position
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return "top"
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Attach a wibox to a screen at the position.
|
|
|
|
-- @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.
|
|
|
|
function set_position(wibox, position, screen)
|
|
|
|
local area = compute_area(wibox, position, screen)
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
set_position(wprop.wibox, wprop.position)
|
2009-08-11 15:27:31 +02:00
|
|
|
capi.awesome.emit_signal("arrange", wprop.wibox.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)
|
|
|
|
capi.awesome.emit_signal("arrange", w.screen)
|
|
|
|
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
|
|
|
|
if wprop.position == "top" then
|
|
|
|
wibox:struts { left = 0, right = 0, bottom = 0, top = wibox.height }
|
|
|
|
elseif wprop.position == "bottom" then
|
|
|
|
wibox:struts { left = 0, right = 0, bottom = wibox.height, top = 0 }
|
|
|
|
elseif wprop.position == "left" then
|
|
|
|
wibox:struts { left = wibox.width, right = 0, bottom = 0, top = 0 }
|
|
|
|
elseif wprop.position == "right" then
|
|
|
|
wibox:struts { left = 0, right = wibox.width, bottom = 0, top = 0 }
|
|
|
|
end
|
|
|
|
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.
|
|
|
|
function attach(wibox, position)
|
|
|
|
-- 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
|
|
|
|
|
2009-06-05 19:49:03 +02:00
|
|
|
if position ~= "top" and position ~= "bottom" and position ~= "right" and position ~= "left" then
|
|
|
|
position = "floating"
|
|
|
|
end
|
|
|
|
|
2009-05-05 17:32:53 +02:00
|
|
|
if not wibox_prop_table then
|
|
|
|
table.insert(wiboxes,
|
|
|
|
setmetatable({ wibox = wibox, position = position }, { __mode = 'v' }))
|
|
|
|
else
|
|
|
|
wibox_prop_table.position = position
|
|
|
|
end
|
|
|
|
|
2009-08-18 14:50:00 +02:00
|
|
|
wibox:add_signal("property::width", wibox_update_strut)
|
|
|
|
wibox:add_signal("property::height", wibox_update_strut)
|
|
|
|
|
2009-05-05 17:32:53 +02:00
|
|
|
if wibox.screen and wibox.visible then
|
|
|
|
update_all_wiboxes_position()
|
|
|
|
end
|
2009-08-11 15:27:31 +02:00
|
|
|
|
|
|
|
wibox:add_signal("property::screen", call_wibox_position_hook_on_prop_update)
|
|
|
|
wibox:add_signal("property::visible", call_wibox_position_hook_on_prop_update)
|
2009-08-18 11:45:16 +02:00
|
|
|
wibox:add_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.
|
|
|
|
function align(wibox, align, screen)
|
|
|
|
local position = get_position(wibox)
|
|
|
|
local area = compute_area(wibox, position, screen)
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Stretch a wibox so it takes all screen width or height.
|
|
|
|
-- @param wibox The wibox.
|
|
|
|
function stretch(wibox)
|
|
|
|
local position = get_position(wibox)
|
|
|
|
local area = compute_area(wibox, position)
|
|
|
|
if position == "right" or position == "left" then
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.height = area.height - (2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
align(wibox, "center")
|
|
|
|
else
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.width = area.width - (2 * wibox.border_width)
|
2009-05-05 17:32:53 +02:00
|
|
|
align(wibox, "left")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a new wibox and attach it to a screen edge.
|
|
|
|
-- @see capi.wibox
|
|
|
|
-- @param args A table with standard arguments to wibox() creator.
|
|
|
|
-- 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.
|
|
|
|
function new(arg)
|
|
|
|
local arg = arg or {}
|
|
|
|
local position = arg.position or "top"
|
2009-07-31 11:33:43 +02:00
|
|
|
local has_to_stretch = true
|
2009-05-05 17:32:53 +02:00
|
|
|
-- Empty position and align in arg so we are passing deprecation warning
|
|
|
|
arg.position = nil
|
|
|
|
|
2009-06-05 23:28:54 +02:00
|
|
|
-- Set default size
|
|
|
|
if position == "left" or position == "right" then
|
|
|
|
arg.width = arg.width or capi.awesome.font_height * 1.5
|
2009-07-31 11:33:43 +02:00
|
|
|
if arg.height then
|
|
|
|
has_to_stretch = false
|
|
|
|
if arg.screen then
|
|
|
|
local hp = arg.height:match("(%d+)%%")
|
|
|
|
if hp then
|
|
|
|
arg.height = capi.screen[arg.screen].workarea.height * hp / 100
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-05 23:28:54 +02:00
|
|
|
else
|
|
|
|
arg.height = arg.height or capi.awesome.font_height * 1.5
|
2009-07-31 11:33:43 +02:00
|
|
|
if arg.width then
|
|
|
|
has_to_stretch = false
|
|
|
|
if arg.screen then
|
|
|
|
local wp = arg.width:match("(%d+)%%")
|
|
|
|
if wp then
|
|
|
|
arg.width = capi.screen[arg.screen].workarea.width * wp / 100
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-05 23:28:54 +02:00
|
|
|
end
|
|
|
|
|
2009-05-05 17:32:53 +02:00
|
|
|
local w = capi.wibox(arg)
|
|
|
|
|
|
|
|
if position == "left" then
|
|
|
|
w.orientation = "north"
|
|
|
|
elseif position == "right" then
|
|
|
|
w.orientation = "south"
|
|
|
|
end
|
|
|
|
|
|
|
|
w.screen = arg.screen or 1
|
|
|
|
|
|
|
|
attach(w, position)
|
2009-07-31 11:33:43 +02:00
|
|
|
if has_to_stretch then
|
|
|
|
stretch(w)
|
|
|
|
else
|
|
|
|
align(w, arg.align)
|
|
|
|
end
|
2009-05-05 17:32:53 +02:00
|
|
|
|
|
|
|
return w
|
|
|
|
end
|
|
|
|
|
2009-06-23 12:42:36 +02:00
|
|
|
local function do_rounded_corners(width, height, corner)
|
|
|
|
local img = image.argb32(width, height, nil)
|
|
|
|
|
|
|
|
-- The image starts completely black which is fully opaque for our use
|
|
|
|
|
|
|
|
local function transp_rect(x, y)
|
|
|
|
img:draw_rectangle(x, y, corner, corner, true, "#ffffff")
|
|
|
|
end
|
|
|
|
local function opaque_circle(x, y)
|
|
|
|
-- x, y are the center of the circle
|
|
|
|
img:draw_circle(x, y, corner, corner, true, "#000000")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Upper left corner
|
|
|
|
-- First make a 'corner times corner' rectangle transparent
|
|
|
|
transp_rect(0, 0)
|
|
|
|
-- Then add the rounded corner
|
|
|
|
opaque_circle(corner, corner)
|
|
|
|
|
|
|
|
-- Upper right corner
|
|
|
|
transp_rect(width - corner, 0)
|
|
|
|
opaque_circle(width - corner - 1, corner)
|
|
|
|
|
|
|
|
-- Bottom left corner
|
|
|
|
transp_rect(0, height - corner)
|
|
|
|
opaque_circle(corner, height - corner - 1)
|
|
|
|
|
|
|
|
-- Bottom right corner
|
|
|
|
transp_rect(width - corner, height - corner)
|
|
|
|
opaque_circle(width - corner - 1, height - corner - 1)
|
|
|
|
|
|
|
|
return img
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Add rounded corners to a wibox
|
|
|
|
-- @param wibox The wibox.
|
|
|
|
-- @param corner_size The size in pixel of the rounded corners.
|
|
|
|
function rounded_corners(wibox, corner_size)
|
|
|
|
local border = wibox.border_width
|
|
|
|
|
|
|
|
-- Corners can't be larger than half the wibox' space
|
2009-08-17 16:56:03 +02:00
|
|
|
if wibox.width / 2 < corner_size then
|
|
|
|
corner_size = wibox.width / 2
|
2009-06-23 12:42:36 +02:00
|
|
|
end
|
2009-08-17 16:56:03 +02:00
|
|
|
if wibox.height / 2 < corner_size then
|
|
|
|
corner_size = wibox.height / 2
|
2009-06-23 12:42:36 +02:00
|
|
|
end
|
|
|
|
|
2009-08-17 16:56:03 +02:00
|
|
|
wibox.shape_clip = do_rounded_corners(wibox.width, wibox.height, corner_size)
|
|
|
|
wibox.shape_bounding = do_rounded_corners(wibox.width + border * 2, wibox.height + border * 2, corner_size + border)
|
2009-06-23 12:42:36 +02:00
|
|
|
end
|
|
|
|
|
2009-05-05 17:32:53 +02:00
|
|
|
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.
|
2009-08-11 15:27:31 +02:00
|
|
|
capi.client.add_signal("manage", function(c)
|
|
|
|
update_wiboxes_on_struts(c)
|
|
|
|
c:add_signal("property::struts", update_wiboxes_on_struts)
|
|
|
|
end)
|
2009-07-10 14:38:37 +02:00
|
|
|
capi.client.add_signal("unmanage", update_wiboxes_on_struts)
|
2009-05-05 17:32:53 +02:00
|
|
|
|
|
|
|
setmetatable(_M, { __call = function(_, ...) return new(...) end })
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|