2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
2019-06-06 08:44:00 +02:00
|
|
|
-- @popupmod wibox
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local capi = {
|
|
|
|
drawin = drawin,
|
2012-04-07 21:54:51 +02:00
|
|
|
root = root,
|
2016-05-10 06:01:35 +02:00
|
|
|
awesome = awesome,
|
|
|
|
screen = screen
|
2010-10-06 12:42:56 +02:00
|
|
|
}
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local object = require("gears.object")
|
2016-05-16 05:46:11 +02:00
|
|
|
local grect = require("gears.geometry").rectangle
|
2010-10-06 12:42:56 +02:00
|
|
|
local beautiful = require("beautiful")
|
2015-09-27 17:00:01 +02:00
|
|
|
local base = require("wibox.widget.base")
|
2016-10-29 13:57:04 +02:00
|
|
|
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
|
2016-05-03 05:57:33 +02:00
|
|
|
local wibox = { mt = {}, object = {} }
|
2012-06-12 14:14:57 +02:00
|
|
|
wibox.layout = require("wibox.layout")
|
2016-05-23 05:56:45 +02:00
|
|
|
wibox.container = require("wibox.container")
|
2012-06-12 14:14:57 +02:00
|
|
|
wibox.widget = require("wibox.widget")
|
2012-10-14 17:20:24 +02:00
|
|
|
wibox.drawable = require("wibox.drawable")
|
2015-08-12 11:53:01 +02:00
|
|
|
wibox.hierarchy = require("wibox.hierarchy")
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2016-05-08 05:46:57 +02:00
|
|
|
local force_forward = {
|
|
|
|
shape_bounding = true,
|
|
|
|
shape_clip = true,
|
2017-01-26 11:15:28 +01:00
|
|
|
shape_input = true,
|
2016-05-08 05:46:57 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 03:35:02 +02:00
|
|
|
--@DOC_wibox_COMMON@
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function wibox:set_widget(widget)
|
|
|
|
self._drawable:set_widget(widget)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-05-10 03:35:02 +02:00
|
|
|
function wibox:get_widget()
|
|
|
|
return self._drawable.widget
|
|
|
|
end
|
2016-05-03 05:27:00 +02:00
|
|
|
|
2015-09-27 17:00:01 +02:00
|
|
|
wibox.setup = base.widget.setup
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function wibox:set_bg(c)
|
|
|
|
self._drawable:set_bg(c)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-02-08 09:37:37 +01:00
|
|
|
function wibox:set_bgimage(image, ...)
|
|
|
|
self._drawable:set_bgimage(image, ...)
|
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function wibox:set_fg(c)
|
|
|
|
self._drawable:set_fg(c)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2014-08-20 11:26:09 +02:00
|
|
|
function wibox:find_widgets(x, y)
|
|
|
|
return self._drawable:find_widgets(x, y)
|
|
|
|
end
|
|
|
|
|
2017-07-02 04:43:01 +02:00
|
|
|
--- Create a widget that reflects the current state of this wibox.
|
|
|
|
-- @treturn widget A new widget.
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method to_widget
|
2017-07-02 04:43:01 +02:00
|
|
|
function wibox:to_widget()
|
|
|
|
local bw = self.border_width or beautiful.border_width or 0
|
|
|
|
return wibox.widget {
|
|
|
|
{
|
|
|
|
self:get_widget(),
|
|
|
|
margins = bw,
|
|
|
|
widget = wibox.container.margin
|
|
|
|
},
|
|
|
|
bg = self.bg or beautiful.bg_normal or "#ffffff",
|
|
|
|
fg = self.fg or beautiful.fg_normal or "#000000",
|
|
|
|
shape_border_color = self.border_color or beautiful.border_color or "#000000",
|
|
|
|
shape_border_width = bw*2,
|
|
|
|
shape_clip = true,
|
|
|
|
shape = self._shape,
|
|
|
|
forced_width = self:geometry().width + 2*bw,
|
|
|
|
forced_height = self:geometry().height + 2*bw,
|
|
|
|
widget = wibox.container.background
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Save a screenshot of the wibox to `path`.
|
|
|
|
-- @tparam string path The path.
|
|
|
|
-- @tparam[opt=nil] table context A widget context.
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method save_to_svg
|
2017-07-02 04:43:01 +02:00
|
|
|
function wibox:save_to_svg(path, context)
|
|
|
|
wibox.widget.draw_to_svg_file(
|
|
|
|
self:to_widget(), path, self:geometry().width, self:geometry().height, context
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-10-29 13:57:04 +02:00
|
|
|
function wibox:_apply_shape()
|
|
|
|
local shape = self._shape
|
|
|
|
|
|
|
|
if not shape then
|
|
|
|
self.shape_bounding = nil
|
|
|
|
self.shape_clip = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local geo = self:geometry()
|
|
|
|
local bw = self.border_width
|
|
|
|
|
|
|
|
-- First handle the bounding shape (things including the border)
|
|
|
|
local img = cairo.ImageSurface(cairo.Format.A1, geo.width + 2*bw, geo.height + 2*bw)
|
|
|
|
local cr = cairo.Context(img)
|
|
|
|
|
2016-12-26 13:55:33 +01:00
|
|
|
-- We just draw the shape in its full size
|
2016-10-29 13:57:04 +02:00
|
|
|
shape(cr, geo.width + 2*bw, geo.height + 2*bw)
|
|
|
|
cr:set_operator(cairo.Operator.SOURCE)
|
|
|
|
cr:fill()
|
|
|
|
self.shape_bounding = img._native
|
|
|
|
img:finish()
|
|
|
|
|
|
|
|
-- Now handle the clip shape (things excluding the border)
|
|
|
|
img = cairo.ImageSurface(cairo.Format.A1, geo.width, geo.height)
|
|
|
|
cr = cairo.Context(img)
|
|
|
|
|
2016-12-26 13:55:33 +01:00
|
|
|
-- We give the shape the same arguments as for the bounding shape and draw
|
|
|
|
-- it in its full size (the translate is to compensate for the smaller
|
|
|
|
-- surface)
|
|
|
|
cr:translate(-bw, -bw)
|
|
|
|
shape(cr, geo.width + 2*bw, geo.height + 2*bw)
|
2016-10-29 13:57:04 +02:00
|
|
|
cr:set_operator(cairo.Operator.SOURCE)
|
2016-12-26 13:55:33 +01:00
|
|
|
cr:fill_preserve()
|
|
|
|
-- Now we remove an area of width 'bw' again around the shape (We use 2*bw
|
|
|
|
-- since half of that is on the outside and only half on the inside)
|
|
|
|
cr:set_source_rgba(0, 0, 0, 0)
|
|
|
|
cr:set_line_width(2*bw)
|
|
|
|
cr:stroke()
|
2016-10-29 13:57:04 +02:00
|
|
|
self.shape_clip = img._native
|
|
|
|
img:finish()
|
|
|
|
end
|
|
|
|
|
|
|
|
function wibox:set_shape(shape)
|
|
|
|
self._shape = shape
|
|
|
|
self:_apply_shape()
|
|
|
|
end
|
|
|
|
|
|
|
|
function wibox:get_shape()
|
|
|
|
return self._shape
|
|
|
|
end
|
|
|
|
|
2017-11-24 04:54:13 +01:00
|
|
|
function wibox:set_input_passthrough(value)
|
|
|
|
rawset(self, "_input_passthrough", value)
|
|
|
|
|
|
|
|
if not value then
|
|
|
|
self.shape_input = nil
|
|
|
|
else
|
|
|
|
local img = cairo.ImageSurface(cairo.Format.A1, 0, 0)
|
|
|
|
self.shape_input = img._native
|
|
|
|
img:finish()
|
|
|
|
end
|
|
|
|
|
|
|
|
self:emit_signal("property::input_passthrough", value)
|
|
|
|
end
|
|
|
|
|
|
|
|
function wibox:get_input_passthrough()
|
|
|
|
return self._input_passthrough
|
|
|
|
end
|
|
|
|
|
2016-05-10 06:01:35 +02:00
|
|
|
function wibox:get_screen()
|
2016-06-04 16:10:54 +02:00
|
|
|
if self.screen_assigned and self.screen_assigned.valid then
|
|
|
|
return self.screen_assigned
|
|
|
|
else
|
|
|
|
self.screen_assigned = nil
|
|
|
|
end
|
2016-05-16 05:46:11 +02:00
|
|
|
local sgeos = {}
|
|
|
|
|
|
|
|
for s in capi.screen do
|
|
|
|
sgeos[s] = s.geometry
|
|
|
|
end
|
|
|
|
|
|
|
|
return grect.get_closest_by_coord(sgeos, self.x, self.y)
|
2016-05-10 06:01:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function wibox:set_screen(s)
|
|
|
|
s = capi.screen[s or 1]
|
|
|
|
if s ~= self:get_screen() then
|
|
|
|
self.x = s.geometry.x
|
|
|
|
self.y = s.geometry.y
|
|
|
|
end
|
2016-06-04 16:10:54 +02:00
|
|
|
|
|
|
|
-- Remember this screen so things work correctly if screens overlap and
|
|
|
|
-- (x,y) is not enough to figure out the correct screen.
|
|
|
|
self.screen_assigned = s
|
2016-11-27 18:09:10 +01:00
|
|
|
self._drawable:_force_screen(s)
|
2016-05-10 06:01:35 +02:00
|
|
|
end
|
|
|
|
|
2019-01-01 21:04:47 +01:00
|
|
|
function wibox:get_children_by_id(name)
|
|
|
|
--TODO v5: Move the ID management to the hierarchy.
|
|
|
|
if rawget(self, "_by_id") then
|
|
|
|
return rawget(self, "_by_id")[name]
|
|
|
|
elseif self._drawable.widget
|
|
|
|
and self._drawable.widget._private
|
|
|
|
and self._drawable.widget._private.by_id then
|
|
|
|
return self._drawable.widget._private.by_id[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2014-03-07 13:49:24 +01:00
|
|
|
for _, k in pairs{ "buttons", "struts", "geometry", "get_xproperty", "set_xproperty" } do
|
|
|
|
wibox[k] = function(self, ...)
|
|
|
|
return self.drawin[k](self.drawin, ...)
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
local function setup_signals(_wibox)
|
2016-02-07 14:13:43 +01:00
|
|
|
local obj
|
2010-10-06 12:42:56 +02:00
|
|
|
local function clone_signal(name)
|
|
|
|
-- When "name" is emitted on wibox.drawin, also emit it on wibox
|
2016-02-07 14:13:43 +01:00
|
|
|
obj: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
|
2016-02-07 14:13:43 +01:00
|
|
|
|
|
|
|
obj = _wibox.drawin
|
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::width")
|
|
|
|
clone_signal("property::x")
|
|
|
|
clone_signal("property::y")
|
2016-03-13 03:14:09 +01:00
|
|
|
clone_signal("property::geometry")
|
|
|
|
clone_signal("property::shape_bounding")
|
|
|
|
clone_signal("property::shape_clip")
|
2017-01-26 11:15:28 +01:00
|
|
|
clone_signal("property::shape_input")
|
2012-10-31 22:12:30 +01:00
|
|
|
|
2016-02-07 14:13:43 +01:00
|
|
|
obj = _wibox._drawable
|
2012-10-31 22:12:30 +01:00
|
|
|
clone_signal("button::press")
|
|
|
|
clone_signal("button::release")
|
|
|
|
clone_signal("mouse::enter")
|
|
|
|
clone_signal("mouse::leave")
|
|
|
|
clone_signal("mouse::move")
|
|
|
|
clone_signal("property::surface")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-05-19 06:17:34 +02:00
|
|
|
--- Create a wibox.
|
|
|
|
-- @tparam[opt=nil] table args
|
2017-11-24 05:19:41 +01:00
|
|
|
--@DOC_wibox_constructor_COMMON@
|
2016-05-19 06:17:34 +02:00
|
|
|
-- @treturn wibox The new wibox
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct wibox
|
2016-05-19 06:17:34 +02:00
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
local function new(args)
|
2016-05-15 07:10:11 +02:00
|
|
|
args = args or {}
|
2010-10-06 12:42:56 +02:00
|
|
|
local ret = object()
|
|
|
|
local w = capi.drawin(args)
|
2016-05-03 05:57:33 +02:00
|
|
|
|
2016-05-03 05:57:33 +02:00
|
|
|
function w.get_wibox()
|
2016-10-05 20:35:04 +02:00
|
|
|
return ret
|
2016-05-03 05:57:33 +02:00
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
ret.drawin = w
|
2015-08-08 13:13:47 +02:00
|
|
|
ret._drawable = wibox.drawable(w.drawable, { wibox = ret },
|
2015-07-29 21:12:41 +02:00
|
|
|
"wibox drawable (" .. object.modulename(3) .. ")")
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2018-01-10 13:49:35 +01:00
|
|
|
function ret._drawable.get_wibox()
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2016-10-04 21:18:18 +02:00
|
|
|
ret._drawable:_inform_visible(w.visible)
|
|
|
|
w:connect_signal("property::visible", function()
|
|
|
|
ret._drawable:_inform_visible(w.visible)
|
|
|
|
end)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
setup_signals(ret)
|
2012-10-14 17:20:24 +02:00
|
|
|
ret.draw = ret._drawable.draw
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
-- Set the default background
|
|
|
|
ret:set_bg(args.bg or beautiful.bg_normal)
|
|
|
|
ret:set_fg(args.fg or beautiful.fg_normal)
|
|
|
|
|
2015-07-29 21:14:40 +02:00
|
|
|
-- Add __tostring method to metatable.
|
|
|
|
local mt = {}
|
|
|
|
local orig_string = tostring(ret)
|
2016-02-07 14:13:43 +01:00
|
|
|
mt.__tostring = function()
|
2015-07-29 21:14:40 +02:00
|
|
|
return string.format("wibox: %s (%s)",
|
|
|
|
tostring(ret._drawable), orig_string)
|
|
|
|
end
|
|
|
|
ret = setmetatable(ret, mt)
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Make sure the wibox is drawn at least once
|
|
|
|
ret.draw()
|
|
|
|
|
2016-10-29 13:57:04 +02:00
|
|
|
ret:connect_signal("property::geometry", ret._apply_shape)
|
|
|
|
ret:connect_signal("property::border_width", ret._apply_shape)
|
|
|
|
|
2016-05-08 05:46:57 +02:00
|
|
|
-- If a value is not found, look in the drawin
|
2010-10-06 12:42:56 +02:00
|
|
|
setmetatable(ret, {
|
2016-05-08 07:41:19 +02:00
|
|
|
__index = function(self, k)
|
|
|
|
if rawget(self, "get_"..k) then
|
|
|
|
return self["get_"..k](self)
|
|
|
|
else
|
|
|
|
return w[k]
|
|
|
|
end
|
|
|
|
end,
|
2016-05-08 05:46:57 +02:00
|
|
|
__newindex = function(self, k,v)
|
2016-05-08 07:41:19 +02:00
|
|
|
if rawget(self, "set_"..k) then
|
|
|
|
self["set_"..k](self, v)
|
2017-08-04 13:10:14 +02:00
|
|
|
elseif force_forward[k] or w[k] ~= nil then
|
2016-05-08 05:46:57 +02:00
|
|
|
w[k] = v
|
|
|
|
else
|
|
|
|
rawset(self, k, v)
|
|
|
|
end
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
})
|
|
|
|
|
2016-05-19 06:05:47 +02:00
|
|
|
-- Set other wibox specific arguments
|
|
|
|
if args.bgimage then
|
|
|
|
ret:set_bgimage( args.bgimage )
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.widget then
|
|
|
|
ret:set_widget ( args.widget )
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.screen then
|
|
|
|
ret:set_screen ( args.screen )
|
|
|
|
end
|
|
|
|
|
2017-11-24 04:54:13 +01:00
|
|
|
if args.shape then
|
|
|
|
ret.shape = args.shape
|
|
|
|
end
|
|
|
|
|
2019-03-12 17:23:53 +01:00
|
|
|
if args.input_passthrough then
|
2017-11-24 04:54:13 +01:00
|
|
|
ret.input_passthrough = args.input_passthrough
|
|
|
|
end
|
2016-10-29 13:57:04 +02:00
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method draw
|
2010-12-12 21:45:54 +01:00
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
function wibox.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
2016-05-03 05:57:33 +02:00
|
|
|
-- Extend the luaobject
|
|
|
|
object.properties(capi.drawin, {
|
|
|
|
getter_class = wibox.object,
|
|
|
|
setter_class = wibox.object,
|
|
|
|
auto_emit = true,
|
|
|
|
})
|
|
|
|
|
2012-06-12 14:14:57 +02:00
|
|
|
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
|