2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local capi = {
|
|
|
|
drawin = drawin,
|
2012-04-07 21:54:51 +02:00
|
|
|
root = root,
|
2010-10-15 18:57:50 +02:00
|
|
|
awesome = awesome
|
2010-10-06 12:42:56 +02:00
|
|
|
}
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local table = table
|
2012-05-27 19:20:34 +02:00
|
|
|
local string_format = string.format
|
2010-10-06 12:42:56 +02:00
|
|
|
local color = require("gears.color")
|
|
|
|
local object = require("gears.object")
|
|
|
|
local sort = require("gears.sort")
|
|
|
|
local beautiful = require("beautiful")
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
|
|
|
local cairo = require("lgi").cairo
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2010-12-12 21:45:54 +01:00
|
|
|
--- This provides widget box windows. Every wibox can also be used as if it were
|
|
|
|
-- a drawin. All drawin functions and properties are also available on wiboxes!
|
2012-06-12 14:14:57 +02:00
|
|
|
-- wibox
|
|
|
|
local wibox = { mt = {} }
|
|
|
|
wibox.layout = require("wibox.layout")
|
|
|
|
wibox.widget = require("wibox.widget")
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
local function do_redraw(_wibox)
|
|
|
|
if not _wibox.drawin.visible then
|
2010-10-06 12:42:56 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
local geom = _wibox.drawin:geometry()
|
|
|
|
local cr = cairo.Context(surface(_wibox.drawin.surface))
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-04-07 21:54:51 +02:00
|
|
|
-- Draw the background
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:save()
|
2012-04-07 21:54:51 +02:00
|
|
|
-- This is pseudo-transparency: We draw the wallpaper in the background
|
2012-05-27 19:20:34 +02:00
|
|
|
local wallpaper = surface(capi.root.wallpaper())
|
2012-04-07 21:54:51 +02:00
|
|
|
if wallpaper then
|
2012-05-27 19:20:34 +02:00
|
|
|
cr.operator = cairo.Operator.SOURCE
|
2012-06-12 14:14:57 +02:00
|
|
|
cr:set_source_surface(wallpaper, -_wibox.drawin.x, -_wibox.drawin.y)
|
2012-04-07 21:54:51 +02:00
|
|
|
cr:paint()
|
|
|
|
end
|
|
|
|
|
2012-05-27 19:20:34 +02:00
|
|
|
cr.operator = cairo.Operator.OVER
|
2012-06-12 14:14:57 +02:00
|
|
|
cr:set_source(_wibox.background_color)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:paint()
|
|
|
|
cr:restore()
|
|
|
|
|
|
|
|
-- Draw the widget
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox._widget_geometries = {}
|
|
|
|
if _wibox.widget and not _wibox.widget.__fake_widget then
|
|
|
|
cr:set_source(_wibox.foreground_color)
|
|
|
|
_wibox.widget:draw(_wibox, cr, geom.width, geom.height)
|
|
|
|
_wibox:widget_at(_wibox.widget, 0, 0, geom.width, geom.height)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox.drawin:refresh()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Register a widget's position.
|
|
|
|
-- This is internal, don't call it yourself! Only wibox.layout.base.draw_widget
|
|
|
|
-- is allowed to call this.
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.widget_at(_wibox, widget, x, y, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
local t = {
|
|
|
|
widget = widget,
|
|
|
|
x = x, y = y,
|
|
|
|
width = width, height = height
|
|
|
|
}
|
2012-06-12 14:14:57 +02:00
|
|
|
table.insert(_wibox._widget_geometries, t)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Find a widget by a point.
|
|
|
|
-- The wibox must have drawn itself at least once for this to work.
|
|
|
|
-- @param wibox The wibox to look at
|
|
|
|
-- @param x X coordinate of the point
|
|
|
|
-- @param y Y coordinate of the point
|
|
|
|
-- @return A sorted table with all widgets that contain the given point. The
|
|
|
|
-- widgets are sorted by relevance.
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.find_widgets(_wibox, x, y)
|
2010-10-06 12:42:56 +02:00
|
|
|
local matches = {}
|
|
|
|
-- Find all widgets that contain the point
|
2012-06-12 14:14:57 +02:00
|
|
|
for k, v in pairs(_wibox._widget_geometries) do
|
2010-10-06 12:42:56 +02:00
|
|
|
local match = true
|
2010-10-30 19:56:07 +02:00
|
|
|
if v.x > x or v.x + v.width <= x then match = false end
|
|
|
|
if v.y > y or v.y + v.height <= y then match = false end
|
2010-10-06 12:42:56 +02:00
|
|
|
if match then
|
|
|
|
table.insert(matches, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sort the matches by area, the assumption here is that widgets don't
|
|
|
|
-- overlap and so smaller widgets are "more specific".
|
|
|
|
local function cmp(a, b)
|
|
|
|
local area_a = a.width * a.height
|
|
|
|
local area_b = b.width * b.height
|
|
|
|
return area_a < area_b
|
|
|
|
end
|
|
|
|
sort(matches, cmp)
|
|
|
|
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the widget that the wibox displays
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.set_widget(_wibox, widget)
|
|
|
|
if _wibox.widget and not _wibox.widget.__fake_widget then
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Disconnect from the old widget so that we aren't updated due to it
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox.widget:disconnect_signal("widget::updated", _wibox.draw)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not widget then
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox.widget = { __fake_widget = true }
|
2010-10-06 12:42:56 +02:00
|
|
|
else
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox.widget = widget
|
|
|
|
widget:connect_signal("widget::updated", _wibox.draw)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Make sure the wibox is updated
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox.draw()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the background of the wibox
|
|
|
|
-- @param wibox The wibox to use
|
|
|
|
-- @param c The background to use. This must either be a cairo pattern object,
|
|
|
|
-- nil or a string that gears.color() understands.
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.set_bg(_wibox, c)
|
2012-05-06 03:55:10 +02:00
|
|
|
local c = c or "#000000"
|
2012-02-17 17:50:31 +01:00
|
|
|
if type(c) == "string" or type(c) == "table" then
|
2010-10-06 12:42:56 +02:00
|
|
|
c = color(c)
|
|
|
|
end
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox.background_color = c
|
|
|
|
_wibox.draw()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the foreground of the wibox
|
|
|
|
-- @param wibox The wibox to use
|
|
|
|
-- @param c The foreground to use. This must either be a cairo pattern object,
|
|
|
|
-- nil or a string that gears.color() understands.
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.set_fg(_wibox, c)
|
2012-05-06 03:55:10 +02:00
|
|
|
local c = c or "#FFFFFF"
|
2012-02-17 17:50:31 +01:00
|
|
|
if type(c) == "string" or type(c) == "table" then
|
2010-10-06 12:42:56 +02:00
|
|
|
c = color(c)
|
|
|
|
end
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox.foreground_color = c
|
|
|
|
_wibox.draw()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Helper function to make wibox:buttons() work as expected
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.buttons(box, ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
return box.drawin:buttons(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Helper function to make wibox:struts() work as expected
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.struts(box, ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
return box.drawin:struts(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Helper function to make wibox:geometry() work as expected
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.geometry(box, ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
return box.drawin:geometry(...)
|
|
|
|
end
|
|
|
|
|
2010-10-06 21:17:05 +02:00
|
|
|
local function emit_difference(name, list, skip)
|
|
|
|
local function in_table(table, val)
|
|
|
|
for k, v in pairs(table) do
|
|
|
|
if v == val then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
for k, v in pairs(list) do
|
|
|
|
if not in_table(skip, v) then
|
|
|
|
v:emit_signal(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
local function handle_leave(_wibox)
|
|
|
|
emit_difference("mouse::leave", _wibox._widgets_under_mouse, {})
|
|
|
|
_wibox._widgets_under_mouse = {}
|
2010-10-06 21:17:05 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
local function handle_motion(_wibox, x, y)
|
|
|
|
if x < 0 or y < 0 or x > _wibox.drawin.width or y > _wibox.drawin.height then
|
|
|
|
return handle_leave(_wibox)
|
2010-10-06 21:17:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Build a plain list of all widgets on that point
|
2012-06-12 14:14:57 +02:00
|
|
|
local widgets_list = _wibox:find_widgets(x, y)
|
2010-10-06 21:17:05 +02:00
|
|
|
local widgets = {}
|
|
|
|
for k, v in pairs(widgets_list) do
|
|
|
|
widgets[#widgets + 1] = v.widget
|
|
|
|
end
|
|
|
|
|
|
|
|
-- First, "leave" all widgets that were left
|
2012-06-12 14:14:57 +02:00
|
|
|
emit_difference("mouse::leave", _wibox._widgets_under_mouse, widgets)
|
2010-10-06 21:17:05 +02:00
|
|
|
-- Then enter some widgets
|
2012-06-12 14:14:57 +02:00
|
|
|
emit_difference("mouse::enter", widgets, _wibox._widgets_under_mouse)
|
2010-10-06 21:17:05 +02:00
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox._widgets_under_mouse = widgets
|
2010-10-06 21:17:05 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
local function setup_signals(_wibox)
|
|
|
|
local w = _wibox.drawin
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
local function clone_signal(name)
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox:add_signal(name)
|
2010-10-06 12:42:56 +02:00
|
|
|
-- When "name" is emitted on wibox.drawin, also emit it on wibox
|
|
|
|
w:connect_signal(name, function(_, ...)
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox:emit_signal(name, ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
clone_signal("mouse::enter")
|
|
|
|
clone_signal("mouse::leave")
|
2010-10-06 20:43:33 +02:00
|
|
|
clone_signal("mouse::move")
|
2010-10-06 12:42:56 +02:00
|
|
|
clone_signal("property::border_color")
|
|
|
|
clone_signal("property::border_width")
|
|
|
|
clone_signal("property::buttons")
|
|
|
|
clone_signal("property::cursor")
|
|
|
|
clone_signal("property::height")
|
|
|
|
clone_signal("property::ontop")
|
|
|
|
clone_signal("property::opacity")
|
|
|
|
clone_signal("property::struts")
|
|
|
|
clone_signal("property::visible")
|
|
|
|
clone_signal("property::widgets")
|
|
|
|
clone_signal("property::width")
|
|
|
|
clone_signal("property::x")
|
|
|
|
clone_signal("property::y")
|
|
|
|
|
|
|
|
-- Update the wibox when its geometry changes
|
2012-06-12 14:14:57 +02:00
|
|
|
w:connect_signal("property::height", _wibox.draw)
|
|
|
|
w:connect_signal("property::width", _wibox.draw)
|
2012-04-07 21:54:51 +02:00
|
|
|
w:connect_signal("property::visible", function()
|
|
|
|
if w.visible then
|
2012-06-12 14:14:57 +02:00
|
|
|
capi.awesome.connect_signal("wallpaper_changed", _wibox.draw)
|
|
|
|
_wibox.draw()
|
2012-04-07 21:54:51 +02:00
|
|
|
else
|
2012-06-12 14:14:57 +02:00
|
|
|
capi.awesome.disconnect_signal("wallpaper_changed", _wibox.draw)
|
2012-04-07 21:54:51 +02:00
|
|
|
end
|
|
|
|
end)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
local function button_signal(name)
|
|
|
|
w:connect_signal(name, function(w, x, y, ...)
|
2012-06-12 14:14:57 +02:00
|
|
|
local widgets = _wibox:find_widgets(x, y)
|
2010-10-06 12:42:56 +02:00
|
|
|
for k, v in pairs(widgets) do
|
|
|
|
-- Calculate x/y inside of the widget
|
|
|
|
local lx = x - v.x
|
|
|
|
local ly = y - v.y
|
|
|
|
v.widget:emit_signal(name, lx, ly, ...)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
button_signal("button::press")
|
|
|
|
button_signal("button::release")
|
2010-10-06 21:17:05 +02:00
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
_wibox:connect_signal("mouse::move", handle_motion)
|
|
|
|
_wibox:connect_signal("mouse::leave", handle_leave)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function new(args)
|
|
|
|
local ret = object()
|
|
|
|
local w = capi.drawin(args)
|
|
|
|
|
|
|
|
ret.drawin = w
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
for k, v in pairs(wibox) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-15 18:57:50 +02:00
|
|
|
-- This is to make sure that the wibox will only be redrawn once even when
|
|
|
|
-- we receive multiple widget::updated signals.
|
|
|
|
ret._redraw_pending = false
|
|
|
|
ret._do_redraw = function()
|
|
|
|
ret._redraw_pending = false
|
|
|
|
capi.awesome.disconnect_signal("refresh", ret._do_redraw)
|
2010-10-06 12:42:56 +02:00
|
|
|
do_redraw(ret)
|
|
|
|
end
|
|
|
|
|
2010-10-15 18:57:50 +02:00
|
|
|
-- Connect our signal when we need a redraw
|
2010-10-06 12:42:56 +02:00
|
|
|
ret.draw = function()
|
2010-10-15 18:57:50 +02:00
|
|
|
if not ret._redraw_pending then
|
|
|
|
capi.awesome.connect_signal("refresh", ret._do_redraw)
|
|
|
|
ret._redraw_pending = true
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup_signals(ret)
|
|
|
|
|
|
|
|
-- Set the default background
|
|
|
|
ret:set_bg(args.bg or beautiful.bg_normal)
|
|
|
|
ret:set_fg(args.fg or beautiful.fg_normal)
|
|
|
|
|
|
|
|
-- Make sure the wibox is drawn at least once
|
|
|
|
ret.draw()
|
|
|
|
|
|
|
|
-- Due to the metatable below, we need this trick
|
|
|
|
ret.widget = { __fake_widget = true }
|
|
|
|
ret._widget_geometries = {}
|
2010-10-06 21:17:05 +02:00
|
|
|
ret._widgets_under_mouse = {}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
-- Redirect all non-existing indexes to the "real" drawin
|
|
|
|
setmetatable(ret, {
|
|
|
|
__index = w,
|
|
|
|
__newindex = w
|
|
|
|
})
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2010-12-12 21:45:54 +01:00
|
|
|
--- Redraw a wibox. You should never have to call this explicitely because it is
|
|
|
|
-- automatically called when needed.
|
|
|
|
-- @param wibox
|
|
|
|
-- @name draw
|
|
|
|
-- @class function
|
|
|
|
|
2010-12-14 21:07:04 +01:00
|
|
|
--- Widget box object.
|
|
|
|
-- Every wibox "inherits" from a drawin and you can use all of drawin's
|
|
|
|
-- functions directly on this as well. When creating a wibox, you can specify a
|
|
|
|
-- "fg" and a "bg" color as keys in the table that is passed to the constructor.
|
|
|
|
-- All other arguments will be passed to drawin's constructor.
|
|
|
|
-- @class table
|
|
|
|
-- @name drawin
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(wibox, wibox.mt)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|