2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @classmod wibox.widget.base
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local object = require("gears.object")
|
2015-09-06 10:16:52 +02:00
|
|
|
local cache = require("gears.cache")
|
2015-07-25 15:59:41 +02:00
|
|
|
local matrix = require("gears.matrix")
|
2016-02-28 12:11:36 +01:00
|
|
|
local protected_call = require("gears.protected_call")
|
2017-03-08 21:18:33 +01:00
|
|
|
local gtable = require("gears.table")
|
2013-08-21 12:26:47 +02:00
|
|
|
local setmetatable = setmetatable
|
2010-10-06 12:42:56 +02:00
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local table = table
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
local base = {}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-12-05 12:25:25 +01:00
|
|
|
-- {{{ Functions on widgets
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Functions available on all widgets.
|
2015-12-05 12:25:25 +01:00
|
|
|
base.widget = {}
|
|
|
|
|
|
|
|
--- Set/get a widget's buttons.
|
2016-10-05 00:10:59 +02:00
|
|
|
-- @tab _buttons The table of buttons that is bound to the widget.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function buttons
|
2015-12-05 12:25:25 +01:00
|
|
|
function base.widget:buttons(_buttons)
|
|
|
|
if _buttons then
|
2016-05-26 20:05:49 +02:00
|
|
|
self._private.widget_buttons = _buttons
|
2015-12-05 12:25:25 +01:00
|
|
|
end
|
2016-05-26 20:05:49 +02:00
|
|
|
return self._private.widget_buttons
|
2015-12-05 12:25:25 +01:00
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Set a widget's visibility.
|
2016-11-22 02:36:16 +01:00
|
|
|
-- @tparam boolean b Whether the widget is visible.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function set_visible
|
2015-12-05 12:25:25 +01:00
|
|
|
function base.widget:set_visible(b)
|
2016-05-26 20:25:07 +02:00
|
|
|
if b ~= self._private.visible then
|
|
|
|
self._private.visible = b
|
2015-12-05 12:25:25 +01:00
|
|
|
self:emit_signal("widget::layout_changed")
|
2016-10-05 00:10:59 +02:00
|
|
|
-- In case something ignored fit and drew the widget anyway.
|
2015-12-05 12:25:25 +01:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Is the widget visible?
|
|
|
|
-- @treturn boolean
|
2016-05-26 20:25:07 +02:00
|
|
|
-- @function get_visible
|
|
|
|
function base.widget:get_visible()
|
|
|
|
return self._private.visible or false
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Set a widget's opacity.
|
|
|
|
-- @tparam number o The opacity to use (a number from 0 (transparent) to 1
|
|
|
|
-- (opaque)).
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function set_opacity
|
2015-12-05 12:25:25 +01:00
|
|
|
function base.widget:set_opacity(o)
|
2016-05-26 19:57:34 +02:00
|
|
|
if o ~= self._private.opacity then
|
|
|
|
self._private.opacity = o
|
2015-12-05 12:25:25 +01:00
|
|
|
self:emit_signal("widget::redraw")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Get the widget's opacity.
|
|
|
|
-- @treturn number The opacity (between 0 (transparent) and 1 (opaque)).
|
2016-05-26 19:57:34 +02:00
|
|
|
-- @function get_opacity
|
|
|
|
function base.widget:get_opacity()
|
|
|
|
return self._private.opacity
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Set the widget's forced width.
|
|
|
|
-- @tparam[opt] number width With `nil` the default mechanism of calling the
|
|
|
|
-- `:fit` method is used.
|
|
|
|
-- @see fit_widget
|
|
|
|
-- @function set_forced_width
|
|
|
|
function base.widget:set_forced_width(width)
|
|
|
|
if width ~= self._private.forced_width then
|
|
|
|
self._private.forced_width = width
|
2015-12-05 12:31:33 +01:00
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Get the widget's forced width.
|
|
|
|
--
|
|
|
|
-- Note that widget instances can be used in different places simultaneously,
|
|
|
|
-- and therefore can have multiple dimensions.
|
|
|
|
-- If there is no forced width/height, then the only way to get the widget's
|
|
|
|
-- actual size is during a `mouse::enter`, `mouse::leave` or button event.
|
|
|
|
-- @treturn[opt] number The forced width (nil if automatic).
|
|
|
|
-- @see fit_widget
|
|
|
|
-- @function get_forced_width
|
2016-05-26 20:32:12 +02:00
|
|
|
function base.widget:get_forced_width()
|
|
|
|
return self._private.forced_width
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Set the widget's forced height.
|
|
|
|
-- @tparam[opt] number height With `nil` the default mechanism of calling the
|
|
|
|
-- `:fit` method is used.
|
|
|
|
-- @see fit_widget
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function set_height
|
2016-10-05 00:10:59 +02:00
|
|
|
function base.widget:set_forced_height(height)
|
|
|
|
if height ~= self._private.forced_height then
|
|
|
|
self._private.forced_height = height
|
2015-12-05 12:31:33 +01:00
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Get the widget's forced height.
|
|
|
|
--
|
|
|
|
-- Note that widget instances can be used in different places simultaneously,
|
|
|
|
-- and therefore can have multiple dimensions.
|
|
|
|
-- If there is no forced width/height, then the only way to get the widget's
|
|
|
|
-- actual size is during a `mouse::enter`, `mouse::leave` or button event.
|
|
|
|
-- @treturn[opt] number The forced height (nil if automatic).
|
2016-05-26 20:32:12 +02:00
|
|
|
-- @function get_forced_height
|
|
|
|
function base.widget:get_forced_height()
|
|
|
|
return self._private.forced_height
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Get the widget's direct children widgets.
|
|
|
|
--
|
|
|
|
-- This method should be re-implemented by the relevant widgets.
|
2016-01-16 07:13:07 +01:00
|
|
|
-- @treturn table The children
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function get_children
|
2016-01-16 07:13:07 +01:00
|
|
|
function base.widget:get_children()
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Replace the layout children.
|
|
|
|
--
|
2016-02-04 07:11:10 +01:00
|
|
|
-- The default implementation does nothing, this must be re-implemented by
|
|
|
|
-- all layout and container widgets.
|
2016-10-05 00:10:59 +02:00
|
|
|
-- @tab children A table composed of valid widgets.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function set_children
|
2016-02-07 14:13:43 +01:00
|
|
|
function base.widget:set_children(children) -- luacheck: no unused
|
2016-02-04 07:11:10 +01:00
|
|
|
-- Nothing on purpose
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- It could have been merged into `get_all_children`, but it's not necessary.
|
2016-01-16 07:13:07 +01:00
|
|
|
local function digg_children(ret, tlw)
|
2016-02-07 14:13:43 +01:00
|
|
|
for _, w in ipairs(tlw:get_children()) do
|
2016-01-16 07:13:07 +01:00
|
|
|
table.insert(ret, w)
|
|
|
|
digg_children(ret, w)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-26 19:59:41 +02:00
|
|
|
--- Get all direct and indirect children widgets.
|
2016-10-05 00:10:59 +02:00
|
|
|
--
|
|
|
|
-- This will scan all containers recursively to find widgets.
|
|
|
|
--
|
|
|
|
-- *Warning*: This method it prone to stack overflow if the widget, or any of
|
|
|
|
-- its children, contains (directly or indirectly) itself.
|
2016-01-16 07:13:07 +01:00
|
|
|
-- @treturn table The children
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function get_all_children
|
2016-01-16 07:13:07 +01:00
|
|
|
function base.widget:get_all_children()
|
|
|
|
local ret = {}
|
|
|
|
digg_children(ret, self)
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2016-08-08 08:40:05 +02:00
|
|
|
--- Emit a signal and ensure all parent widgets in the hierarchies also
|
|
|
|
-- forward the signal. This is useful to track signals when there is a dynamic
|
|
|
|
-- set of containers and layouts wrapping the widget.
|
|
|
|
--
|
2017-08-16 13:37:14 +02:00
|
|
|
-- Note that this function has some flaws:
|
2016-10-05 00:10:59 +02:00
|
|
|
--
|
|
|
|
-- 1. The signal is only forwarded once the widget tree has been built. This
|
|
|
|
-- happens after all currently scheduled functions have been executed.
|
|
|
|
-- Therefore, it will not start to work right away.
|
|
|
|
-- 2. In case the widget is present multiple times in a single widget tree,
|
2017-08-16 13:37:14 +02:00
|
|
|
-- this function will also forward the signal multiple times (once per upward
|
2016-10-05 00:10:59 +02:00
|
|
|
-- tree path).
|
2017-08-16 13:37:14 +02:00
|
|
|
-- 3. If the widget is removed from the widget tree, the signal is still
|
|
|
|
-- forwarded for some time, similar to the first case.
|
2016-08-08 08:40:05 +02:00
|
|
|
--
|
|
|
|
-- @tparam string signal_name
|
|
|
|
-- @param ... Other arguments
|
|
|
|
-- @function emit_signal_recursive
|
|
|
|
function base.widget:emit_signal_recursive(signal_name, ...)
|
|
|
|
-- This is a convenience wrapper, the real implementation is in the
|
|
|
|
-- hierarchy.
|
|
|
|
|
|
|
|
self:emit_signal("widget::emit_recursive", signal_name, ...)
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Get the index of a widget.
|
|
|
|
-- @tparam widget widget The widget to look for.
|
|
|
|
-- @tparam[opt] boolean recursive Also check sub-widgets?
|
|
|
|
-- @tparam[opt] widget ... Additional widgets to add at the end of the "path"
|
|
|
|
-- @treturn number The index.
|
|
|
|
-- @treturn widget The parent widget.
|
|
|
|
-- @treturn table The path between "self" and "widget".
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function index
|
2016-01-18 08:35:28 +01:00
|
|
|
function base.widget:index(widget, recursive, ...)
|
|
|
|
local widgets = self:get_children()
|
|
|
|
for idx, w in ipairs(widgets) do
|
|
|
|
if w == widget then
|
|
|
|
return idx, self, {...}
|
|
|
|
elseif recursive then
|
2016-02-07 14:13:43 +01:00
|
|
|
local child_idx, l, path = w:index(widget, true, self, ...)
|
|
|
|
if child_idx and l then
|
|
|
|
return child_idx, l, path
|
2016-01-18 08:35:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil, self, {}
|
|
|
|
end
|
2015-12-05 12:25:25 +01:00
|
|
|
-- }}}
|
|
|
|
|
2015-09-06 10:16:52 +02:00
|
|
|
-- {{{ Caches
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Indexes are widgets, allow them to be garbage-collected.
|
2015-09-27 14:42:39 +02:00
|
|
|
local widget_dependencies = setmetatable({}, { __mode = "kv" })
|
2015-09-06 10:16:52 +02:00
|
|
|
|
2015-09-17 15:01:50 +02:00
|
|
|
-- Get the cache of the given kind for this widget. This returns a gears.cache
|
2015-09-06 10:16:52 +02:00
|
|
|
-- that calls the callback of kind `kind` on the widget.
|
2015-09-17 15:01:50 +02:00
|
|
|
local function get_cache(widget, kind)
|
2016-05-26 06:52:11 +02:00
|
|
|
if not widget._private.widget_caches[kind] then
|
|
|
|
widget._private.widget_caches[kind] = cache.new(function(...)
|
2016-02-28 12:11:36 +01:00
|
|
|
return protected_call(widget[kind], widget, ...)
|
2015-09-06 10:16:52 +02:00
|
|
|
end)
|
|
|
|
end
|
2016-05-26 06:52:11 +02:00
|
|
|
return widget._private.widget_caches[kind]
|
2015-09-06 10:16:52 +02:00
|
|
|
end
|
|
|
|
|
2015-09-17 15:01:50 +02:00
|
|
|
-- Special value to skip the dependency recording that is normally done by
|
|
|
|
-- base.fit_widget() and base.layout_widget(). The caller must ensure that no
|
2016-10-05 00:10:59 +02:00
|
|
|
-- caches depend on the result of the call and/or must handle the children's
|
2015-09-17 15:01:50 +02:00
|
|
|
-- widget::layout_changed signal correctly when using this.
|
|
|
|
base.no_parent_I_know_what_I_am_doing = {}
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Record a dependency from parent to child: The layout of `parent` depends on
|
|
|
|
-- the layout of `child`.
|
2015-09-17 15:01:50 +02:00
|
|
|
local function record_dependency(parent, child)
|
|
|
|
if parent == base.no_parent_I_know_what_I_am_doing then
|
|
|
|
return
|
2015-09-06 10:16:52 +02:00
|
|
|
end
|
2015-09-17 15:01:50 +02:00
|
|
|
|
|
|
|
base.check_widget(parent)
|
|
|
|
base.check_widget(child)
|
|
|
|
|
|
|
|
local deps = widget_dependencies[child] or {}
|
|
|
|
deps[parent] = true
|
|
|
|
widget_dependencies[child] = deps
|
2015-09-06 10:16:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Clear the caches for `widget` and all widgets that depend on it.
|
2015-09-17 15:01:50 +02:00
|
|
|
local clear_caches
|
|
|
|
function clear_caches(widget)
|
|
|
|
local deps = widget_dependencies[widget] or {}
|
2015-09-06 10:16:52 +02:00
|
|
|
widget_dependencies[widget] = {}
|
2016-05-26 06:52:11 +02:00
|
|
|
widget._private.widget_caches = {}
|
2015-09-17 15:01:50 +02:00
|
|
|
for w in pairs(deps) do
|
|
|
|
clear_caches(w)
|
|
|
|
end
|
2015-09-06 10:16:52 +02:00
|
|
|
end
|
2016-03-19 09:59:06 +01:00
|
|
|
|
2015-09-06 10:16:52 +02:00
|
|
|
-- }}}
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Figure out the geometry in the device coordinate space.
|
|
|
|
--
|
|
|
|
-- This gives only tight bounds if no rotations by non-multiples of 90° are
|
|
|
|
-- used.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.rect_to_device_geometry
|
2015-07-25 15:52:53 +02:00
|
|
|
function base.rect_to_device_geometry(cr, x, y, width, height)
|
2015-07-25 15:59:41 +02:00
|
|
|
return matrix.transform_rectangle(cr.matrix, x, y, width, height)
|
2015-07-25 15:52:53 +02:00
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Fit a widget for the given available width and height.
|
|
|
|
--
|
|
|
|
-- This calls the widget's `:fit` callback and caches the result for later use.
|
|
|
|
-- Never call `:fit` directly, but always through this function!
|
|
|
|
-- @tparam widget parent The parent widget which requests this information.
|
|
|
|
-- @tab context The context in which we are fit.
|
|
|
|
-- @tparam widget widget The widget to fit (this uses
|
|
|
|
-- `widget:fit(context, width, height)`).
|
|
|
|
-- @tparam number width The available width for the widget.
|
|
|
|
-- @tparam number height The available height for the widget.
|
|
|
|
-- @treturn number The width that the widget wants to use.
|
|
|
|
-- @treturn number The height that the widget wants to use.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.fit_widget
|
2015-09-17 15:01:50 +02:00
|
|
|
function base.fit_widget(parent, context, widget, width, height)
|
|
|
|
record_dependency(parent, widget)
|
|
|
|
|
2016-05-26 20:25:07 +02:00
|
|
|
if not widget._private.visible then
|
2015-06-14 16:01:56 +02:00
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sanitize the input. This also filters out e.g. NaN.
|
2016-02-07 14:13:43 +01:00
|
|
|
width = math.max(0, width)
|
|
|
|
height = math.max(0, height)
|
2015-06-14 16:01:56 +02:00
|
|
|
|
|
|
|
local w, h = 0, 0
|
|
|
|
if widget.fit then
|
2015-09-17 15:01:50 +02:00
|
|
|
w, h = get_cache(widget, "fit"):get(context, width, height)
|
2015-06-14 16:01:56 +02:00
|
|
|
else
|
|
|
|
-- If it has no fit method, calculate based on the size of children
|
2015-09-17 15:01:50 +02:00
|
|
|
local children = base.layout_widget(parent, context, widget, width, height)
|
2015-06-14 16:01:56 +02:00
|
|
|
for _, info in ipairs(children or {}) do
|
|
|
|
local x, y, w2, h2 = matrix.transform_rectangle(info._matrix,
|
|
|
|
0, 0, info._width, info._height)
|
|
|
|
w, h = math.max(w, x + w2), math.max(h, y + h2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-28 12:11:36 +01:00
|
|
|
-- Apply forced size and handle nil's
|
2016-05-26 20:32:12 +02:00
|
|
|
w = widget._private.forced_width or w or 0
|
|
|
|
h = widget._private.forced_height or h or 0
|
2015-12-05 12:31:33 +01:00
|
|
|
|
2015-06-14 16:01:56 +02:00
|
|
|
-- Also sanitize the output.
|
|
|
|
w = math.max(0, math.min(w, width))
|
|
|
|
h = math.max(0, math.min(h, height))
|
|
|
|
return w, h
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Lay out a widget for the given available width and height.
|
|
|
|
--
|
|
|
|
-- This calls the widget's `:layout` callback and caches the result for later
|
|
|
|
-- use. Never call `:layout` directly, but always through this function!
|
|
|
|
-- However, normally there shouldn't be any reason why you need to use this
|
|
|
|
-- function.
|
|
|
|
-- @tparam widget parent The parent widget which requests this information.
|
|
|
|
-- @tab context The context in which we are laid out.
|
|
|
|
-- @tparam widget widget The widget to layout (this uses
|
|
|
|
-- `widget:layout(context, width, height)`).
|
|
|
|
-- @tparam number width The available width for the widget.
|
|
|
|
-- @tparam number height The available height for the widget.
|
|
|
|
-- @treturn[opt] table The result from the widget's `:layout` callback.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.layout_widget
|
2015-09-17 15:01:50 +02:00
|
|
|
function base.layout_widget(parent, context, widget, width, height)
|
|
|
|
record_dependency(parent, widget)
|
|
|
|
|
2016-05-26 20:25:07 +02:00
|
|
|
if not widget._private.visible then
|
2015-06-14 16:01:56 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sanitize the input. This also filters out e.g. NaN.
|
2016-02-07 14:13:43 +01:00
|
|
|
width = math.max(0, width)
|
|
|
|
height = math.max(0, height)
|
2015-06-14 16:01:56 +02:00
|
|
|
|
|
|
|
if widget.layout then
|
2015-09-17 15:01:50 +02:00
|
|
|
return get_cache(widget, "layout"):get(context, width, height)
|
2015-06-14 16:01:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Handle a button event on a widget.
|
|
|
|
--
|
|
|
|
-- This is used internally and should not be called directly.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.handle_button
|
2013-03-22 05:52:02 +01:00
|
|
|
function base.handle_button(event, widget, x, y, button, modifiers, geometry)
|
2016-02-07 14:13:43 +01:00
|
|
|
x = x or y -- luacheck: no unused
|
2010-10-06 12:42:56 +02:00
|
|
|
local function is_any(mod)
|
2012-05-13 00:23:23 +02:00
|
|
|
return #mod == 1 and mod[1] == "Any"
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function tables_equal(a, b)
|
|
|
|
if #a ~= #b then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
for k, v in pairs(b) do
|
|
|
|
if a[k] ~= v then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Find all matching button objects.
|
2010-10-06 12:42:56 +02:00
|
|
|
local matches = {}
|
2016-05-26 20:05:49 +02:00
|
|
|
for _, v in pairs(widget._private.widget_buttons) do
|
2010-10-06 12:42:56 +02:00
|
|
|
local match = true
|
|
|
|
-- Is it the right button?
|
|
|
|
if v.button ~= 0 and v.button ~= button then match = false end
|
|
|
|
-- Are the correct modifiers pressed?
|
|
|
|
if (not is_any(v.modifiers)) and (not tables_equal(v.modifiers, modifiers)) then match = false end
|
|
|
|
if match then
|
|
|
|
table.insert(matches, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Emit the signals.
|
2016-02-07 14:13:43 +01:00
|
|
|
for _, v in pairs(matches) do
|
2013-03-22 05:52:02 +01:00
|
|
|
v:emit_signal(event,geometry)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Create widget placement information. This should be used in a widget's
|
2015-06-21 14:27:44 +02:00
|
|
|
-- `:layout()` callback.
|
2016-10-05 00:10:59 +02:00
|
|
|
-- @tparam widget widget The widget that should be placed.
|
2015-09-16 13:57:38 +02:00
|
|
|
-- @param mat A matrix transforming from the parent widget's coordinate
|
2015-10-14 00:21:29 +02:00
|
|
|
-- system. For example, use matrix.create_translate(1, 2) to draw a
|
|
|
|
-- widget at position (1, 2) relative to the parent widget.
|
2016-10-05 00:10:59 +02:00
|
|
|
-- @tparam number width The width of the widget in its own coordinate system.
|
|
|
|
-- That is, after applying the transformation matrix.
|
|
|
|
-- @tparam number height The height of the widget in its own coordinate system.
|
|
|
|
-- That is, after applying the transformation matrix.
|
|
|
|
-- @treturn table An opaque object that can be returned from `:layout()`.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.place_widget_via_matrix
|
2015-06-14 16:01:56 +02:00
|
|
|
function base.place_widget_via_matrix(widget, mat, width, height)
|
|
|
|
return {
|
|
|
|
_widget = widget,
|
|
|
|
_width = width,
|
|
|
|
_height = height,
|
2015-09-16 13:57:38 +02:00
|
|
|
_matrix = mat
|
2015-06-14 16:01:56 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create widget placement information. This should be used for a widget's
|
2015-06-21 14:27:44 +02:00
|
|
|
-- `:layout()` callback.
|
2016-10-05 00:10:59 +02:00
|
|
|
-- @tparam widget widget The widget that should be placed.
|
|
|
|
-- @tparam number x The x coordinate for the widget.
|
|
|
|
-- @tparam number y The y coordinate for the widget.
|
|
|
|
-- @tparam number width The width of the widget in its own coordinate system.
|
|
|
|
-- That is, after applying the transformation matrix.
|
|
|
|
-- @tparam number height The height of the widget in its own coordinate system.
|
|
|
|
-- That is, after applying the transformation matrix.
|
|
|
|
-- @treturn table An opaque object that can be returned from `:layout()`.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.place_widget_at
|
2015-06-14 16:01:56 +02:00
|
|
|
function base.place_widget_at(widget, x, y, width, height)
|
2015-09-16 13:57:38 +02:00
|
|
|
return base.place_widget_via_matrix(widget, matrix.create_translate(x, y), width, height)
|
2015-06-14 16:01:56 +02:00
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Read the table, separate attributes from widgets.
|
2015-09-27 17:00:01 +02:00
|
|
|
local function parse_table(t, leave_empty)
|
|
|
|
local max = 0
|
|
|
|
local attributes, widgets = {}, {}
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if type(k) == "number" then
|
2016-02-28 11:10:46 +01:00
|
|
|
if v then
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Since `ipairs` doesn't always work on sparse tables, update
|
|
|
|
-- the maximum.
|
2016-02-28 11:10:46 +01:00
|
|
|
if k > max then
|
|
|
|
max = k
|
|
|
|
end
|
|
|
|
|
|
|
|
widgets[k] = v
|
2015-09-27 17:00:01 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
attributes[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Pack the sparse table, if the container doesn't support sparse tables.
|
2015-09-27 17:00:01 +02:00
|
|
|
if not leave_empty then
|
2017-03-08 21:18:33 +01:00
|
|
|
widgets = gtable.from_sparse(widgets)
|
2015-09-27 17:00:01 +02:00
|
|
|
max = #widgets
|
|
|
|
end
|
|
|
|
|
|
|
|
return max, attributes, widgets
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Recursively build a container from a declarative table.
|
2015-09-27 17:00:01 +02:00
|
|
|
local function drill(ids, content)
|
|
|
|
if not content then return end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Alias `widget` to `layout` as they are handled the same way.
|
2015-09-27 17:00:01 +02:00
|
|
|
content.layout = content.layout or content.widget
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Make sure the layout is not indexed on a function.
|
2015-09-27 17:00:01 +02:00
|
|
|
local layout = type(content.layout) == "function" and content.layout() or content.layout
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Create layouts based on metatable's __call.
|
2015-09-27 17:00:01 +02:00
|
|
|
local l = layout.is_widget and layout or layout()
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Get the number of children widgets (including nil widgets).
|
2015-09-27 17:00:01 +02:00
|
|
|
local max, attributes, widgets = parse_table(content, l.allow_empty_widget)
|
|
|
|
|
|
|
|
-- Get the optional identifier to create a virtual widget tree to place
|
2016-10-05 00:10:59 +02:00
|
|
|
-- in an "access table" to be able to retrieve the widget.
|
2015-09-27 17:00:01 +02:00
|
|
|
local id = attributes.id
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Clear the internal attributes.
|
2015-09-27 17:00:01 +02:00
|
|
|
attributes.id, attributes.layout, attributes.widget = nil, nil, nil
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Set layout attributes.
|
|
|
|
-- This has to be done before the widgets are added because it might affect
|
|
|
|
-- the output.
|
2016-07-23 23:24:11 +02:00
|
|
|
for attr, val in pairs(attributes) do
|
|
|
|
if l["set_"..attr] then
|
|
|
|
l["set_"..attr](l, val)
|
|
|
|
elseif type(l[attr]) == "function" then
|
|
|
|
l[attr](l, val)
|
|
|
|
else
|
|
|
|
l[attr] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Add all widgets.
|
2015-09-27 17:00:01 +02:00
|
|
|
for k = 1, max do
|
2016-10-05 00:10:59 +02:00
|
|
|
-- ipairs cannot be used on sparse tables.
|
2015-09-27 17:00:01 +02:00
|
|
|
local v, id2, e = widgets[k], id, nil
|
|
|
|
if v then
|
2016-10-05 00:10:59 +02:00
|
|
|
-- It is another declarative container, parse it.
|
2015-09-27 17:00:01 +02:00
|
|
|
if not v.is_widget then
|
|
|
|
e, id2 = drill(ids, v)
|
|
|
|
widgets[k] = e
|
|
|
|
end
|
2016-02-27 13:58:48 +01:00
|
|
|
base.check_widget(widgets[k])
|
2015-09-27 17:00:01 +02:00
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Place the widget in the access table.
|
2015-09-27 17:00:01 +02:00
|
|
|
if id2 then
|
|
|
|
l [id2] = e
|
|
|
|
ids[id2] = ids[id2] or {}
|
|
|
|
table.insert(ids[id2], e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Replace all children (if any) with the new ones.
|
2017-04-01 00:56:20 +02:00
|
|
|
if widgets then
|
|
|
|
l:set_children(widgets)
|
|
|
|
end
|
2015-09-27 17:00:01 +02:00
|
|
|
return l, id
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Only available when the declarative system is used.
|
2015-09-27 17:00:01 +02:00
|
|
|
local function get_children_by_id(self, name)
|
2016-05-26 06:52:11 +02:00
|
|
|
if rawget(self, "_private") then
|
|
|
|
return self._private.by_id[name] or {}
|
|
|
|
else
|
|
|
|
return rawget(self, "_by_id")[name] or {}
|
|
|
|
end
|
2015-09-27 17:00:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set a declarative widget hierarchy description.
|
2016-10-05 00:10:59 +02:00
|
|
|
--
|
|
|
|
-- See [The declarative layout system](../documentation/03-declarative-layout.md.html).
|
|
|
|
-- @tab args A table containing the widget's disposition.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function setup
|
2015-09-27 17:00:01 +02:00
|
|
|
function base.widget:setup(args)
|
|
|
|
local f,ids = self.set_widget or self.add or self.set_first,{}
|
|
|
|
local w, id = drill(ids, args)
|
|
|
|
f(self,w)
|
|
|
|
if id then
|
|
|
|
-- Avoid being dropped by wibox metatable -> drawin
|
|
|
|
rawset(self, id, w)
|
2016-07-30 23:12:20 +02:00
|
|
|
ids[id] = ids[id] or {}
|
|
|
|
table.insert(ids[id], 1, w)
|
2015-09-27 17:00:01 +02:00
|
|
|
end
|
2016-05-26 20:37:58 +02:00
|
|
|
|
|
|
|
if rawget(self, "_private") then
|
|
|
|
self._private.by_id = ids
|
|
|
|
else
|
|
|
|
rawset(self, "_by_id", ids)
|
|
|
|
end
|
|
|
|
|
2019-01-01 21:04:47 +01:00
|
|
|
if not rawget(self, "get_children_by_id") then
|
|
|
|
rawset(self, "get_children_by_id", get_children_by_id)
|
|
|
|
end
|
2015-09-27 17:00:01 +02:00
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Create a widget from a declarative description.
|
|
|
|
--
|
|
|
|
-- See [The declarative layout system](../documentation/03-declarative-layout.md.html).
|
|
|
|
-- @tab args A table containing the widgets disposition.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.make_widget_declarative
|
2016-02-11 09:51:40 +01:00
|
|
|
function base.make_widget_declarative(args)
|
|
|
|
local ids = {}
|
|
|
|
|
|
|
|
if (not args.layout) and (not args.widget) then
|
|
|
|
args.widget = base.make_widget(nil, args.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
local w, id = drill(ids, args)
|
|
|
|
|
2016-05-26 06:42:07 +02:00
|
|
|
local mt = getmetatable(w) or {}
|
2016-02-07 14:13:43 +01:00
|
|
|
local orig_string = tostring(w)
|
2016-02-11 09:51:40 +01:00
|
|
|
|
2016-07-30 23:12:20 +02:00
|
|
|
-- Add the main id (if any)
|
|
|
|
if id then
|
|
|
|
ids[id] = ids[id] or {}
|
|
|
|
table.insert(ids[id], 1, w)
|
|
|
|
end
|
2016-05-26 20:37:58 +02:00
|
|
|
|
|
|
|
if rawget(w, "_private") then
|
|
|
|
w._private.by_id = ids
|
|
|
|
else
|
|
|
|
rawset(w, "_by_id", ids)
|
|
|
|
end
|
|
|
|
|
2016-02-11 09:51:40 +01:00
|
|
|
rawset(w, "get_children_by_id", get_children_by_id)
|
|
|
|
|
2016-02-07 14:13:43 +01:00
|
|
|
mt.__tostring = function()
|
2016-03-04 06:24:38 +01:00
|
|
|
return string.format("%s (%s)", id or w.widget_name or "N/A", orig_string)
|
2016-02-11 09:51:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(w, mt)
|
|
|
|
end
|
|
|
|
|
2017-08-07 23:04:36 +02:00
|
|
|
--- Create a widget from an undetermined value.
|
|
|
|
--
|
|
|
|
-- The value can be:
|
|
|
|
--
|
|
|
|
-- * A widget (in which case nothing new is created)
|
|
|
|
-- * A declarative construct
|
|
|
|
-- * A constructor function
|
|
|
|
-- * A metaobject
|
|
|
|
--
|
|
|
|
-- @param wdg The value.
|
|
|
|
-- @param[opt=nil] ... Arguments passed to the contructor (if any).
|
|
|
|
-- @treturn The new widget.
|
|
|
|
function base.make_widget_from_value(wdg, ...)
|
|
|
|
local is_table = type(wdg) == "table"
|
|
|
|
local is_function = ((not is_table) and type(wdg) == "function")
|
|
|
|
or (is_table and getmetatable(wdg) and getmetatable(wdg).__call)
|
|
|
|
|
|
|
|
if is_function then
|
|
|
|
wdg = wdg(...)
|
|
|
|
elseif is_table and not wdg.is_widget then
|
|
|
|
wdg = base.make_widget_declarative(wdg)
|
|
|
|
else
|
|
|
|
assert(wdg.is_widget, "The argument is not a function, table, or widget.")
|
|
|
|
end
|
|
|
|
|
|
|
|
return wdg
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Create an empty widget skeleton.
|
|
|
|
--
|
2018-01-17 03:15:49 +01:00
|
|
|
-- See [Creating new widgets](../documentation/04-new-widgets.md.html).
|
2016-10-05 00:10:59 +02:00
|
|
|
-- @tparam[opt] widget proxy If this is set, the returned widget will be a
|
|
|
|
-- proxy for this widget. It will be equivalent to this widget.
|
|
|
|
-- This means it looks the same on the screen.
|
2016-02-10 07:21:29 +01:00
|
|
|
-- @tparam[opt] string widget_name Name of the widget. If not set, it will be
|
2016-10-05 00:10:59 +02:00
|
|
|
-- set automatically via @{gears.object.modulename}.
|
2016-05-26 06:42:07 +02:00
|
|
|
-- @tparam[opt={}] table args Widget settings
|
2016-10-05 00:10:59 +02:00
|
|
|
-- @tparam[opt=false] boolean args.enable_properties Enable automatic getter
|
|
|
|
-- and setter methods.
|
2016-05-26 06:42:07 +02:00
|
|
|
-- @tparam[opt=nil] table args.class The widget class
|
2016-02-10 07:21:29 +01:00
|
|
|
-- @see fit_widget
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.make_widget
|
2016-05-26 06:42:07 +02:00
|
|
|
function base.make_widget(proxy, widget_name, args)
|
|
|
|
args = args or {}
|
|
|
|
local ret = object {
|
|
|
|
enable_properties = args.enable_properties,
|
|
|
|
class = args.class,
|
|
|
|
}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Backwards compatibility.
|
2015-06-14 16:01:56 +02:00
|
|
|
-- TODO: Remove this
|
|
|
|
ret:connect_signal("widget::updated", function()
|
|
|
|
ret:emit_signal("widget::layout_changed")
|
|
|
|
ret:emit_signal("widget::redraw_needed")
|
|
|
|
end)
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Create a table used to store the widgets internal data.
|
2016-05-26 06:52:11 +02:00
|
|
|
rawset(ret, "_private", {})
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- No buttons yet.
|
2016-05-26 20:05:49 +02:00
|
|
|
ret._private.widget_buttons = {}
|
2015-12-05 12:25:25 +01:00
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Widget is visible.
|
2016-05-26 20:25:07 +02:00
|
|
|
ret._private.visible = true
|
2015-12-05 12:25:25 +01:00
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Widget is fully opaque.
|
2016-05-26 19:57:34 +02:00
|
|
|
ret._private.opacity = 1
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Differentiate tables from widgets.
|
2016-05-26 06:52:11 +02:00
|
|
|
rawset(ret, "is_widget", true)
|
2015-09-27 17:00:01 +02:00
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Size is not restricted/forced.
|
2016-05-26 20:32:12 +02:00
|
|
|
ret._private.forced_width = nil
|
|
|
|
ret._private.forced_height = nil
|
2015-12-05 12:31:33 +01:00
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Make buttons work.
|
2010-10-06 12:42:56 +02:00
|
|
|
ret:connect_signal("button::press", function(...)
|
2012-06-12 15:55:10 +02:00
|
|
|
return base.handle_button("press", ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
end)
|
|
|
|
ret:connect_signal("button::release", function(...)
|
2012-06-12 15:55:10 +02:00
|
|
|
return base.handle_button("release", ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
if proxy then
|
2016-05-26 06:52:11 +02:00
|
|
|
rawset(ret, "fit", function(_, context, width, height)
|
2015-09-17 15:01:50 +02:00
|
|
|
return base.fit_widget(ret, context, proxy, width, height)
|
2016-05-26 06:52:11 +02:00
|
|
|
end)
|
|
|
|
rawset(ret, "layout", function(_, _, width, height)
|
2015-06-14 16:01:56 +02:00
|
|
|
return { base.place_widget_at(proxy, 0, 0, width, height) }
|
2016-05-26 06:52:11 +02:00
|
|
|
end)
|
2015-06-14 16:01:56 +02:00
|
|
|
proxy:connect_signal("widget::layout_changed", function()
|
|
|
|
ret:emit_signal("widget::layout_changed")
|
|
|
|
end)
|
|
|
|
proxy:connect_signal("widget::redraw_needed", function()
|
|
|
|
ret:emit_signal("widget::redraw_needed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Set up caches.
|
2015-09-06 10:16:52 +02:00
|
|
|
clear_caches(ret)
|
|
|
|
ret:connect_signal("widget::layout_changed", function()
|
|
|
|
clear_caches(ret)
|
|
|
|
end)
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
-- Add functions.
|
2015-12-05 12:25:25 +01:00
|
|
|
for k, v in pairs(base.widget) do
|
2016-05-26 06:52:11 +02:00
|
|
|
rawset(ret, k, v)
|
2015-08-13 00:31:06 +02:00
|
|
|
end
|
|
|
|
|
2015-07-23 21:42:54 +02:00
|
|
|
-- Add __tostring method to metatable.
|
2016-05-26 06:52:11 +02:00
|
|
|
rawset(ret, "widget_name", widget_name or object.modulename(3))
|
2016-05-26 06:42:07 +02:00
|
|
|
local mt = getmetatable(ret) or {}
|
2015-07-29 21:09:08 +02:00
|
|
|
local orig_string = tostring(ret)
|
2016-02-07 14:13:43 +01:00
|
|
|
mt.__tostring = function()
|
2015-07-29 21:09:08 +02:00
|
|
|
return string.format("%s (%s)", ret.widget_name, orig_string)
|
2015-07-23 21:42:54 +02:00
|
|
|
end
|
|
|
|
return setmetatable(ret, mt)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Generate an empty widget which takes no space and displays nothing.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.empty_widget
|
2012-11-27 22:55:42 +01:00
|
|
|
function base.empty_widget()
|
2015-06-14 16:01:56 +02:00
|
|
|
return base.make_widget()
|
2012-11-27 22:55:42 +01:00
|
|
|
end
|
|
|
|
|
2016-10-05 00:10:59 +02:00
|
|
|
--- Do some sanity checking on a widget.
|
|
|
|
--
|
|
|
|
-- This function raises an error if the widget is not valid.
|
2016-05-26 07:49:16 +02:00
|
|
|
-- @function wibox.widget.base.check_widget
|
2012-06-12 15:55:10 +02:00
|
|
|
function base.check_widget(widget)
|
2016-02-27 14:01:31 +01:00
|
|
|
assert(type(widget) == "table", "Type should be table, but is " .. tostring(type(widget)))
|
|
|
|
assert(widget.is_widget, "Argument is not a widget!")
|
2016-06-04 18:23:48 +02:00
|
|
|
for _, func in pairs({ "connect_signal", "disconnect_signal" }) do
|
2015-09-06 13:23:05 +02:00
|
|
|
assert(type(widget[func]) == "function", func .. " is not a function")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
return base
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|