Module: wibox.container.conditional

A container displaying a child widget when a condition is met.

This widget accept multiple widget templates (or instances) and will display the first one matching the condition. If templates are provided (instead of widget instances), the widget will be created only when the condition is met.

If multiple candidates meet, the condition, the first one is used.

Please note that the widget and children properties are read-only due to the nature of this widget.

Each template (or widget instance) encapsulated by this container has the following properties:

  • when (function, mandatory): The function called to determine if the condition is met. It has to return true or false. It will be called when eval is called or when one of the attached signal is emitted.
  • selected_callback (function): When the template is selected.
  • deselected_callback (function): When the template is no longer selected.
  • updated_callback (function): When eval returns the same condition has before.

This widget can be tailored to specific use case. For example, to track the focused client:

local function extended_conditional()
    return wibox.container.conditional {
        argument_callback = function()
            return client.focus, awful.screen.focused().selected_tag
        end,
        connected_global_signals = {
            {client , "focus"               },
            {tag    , "property::selected"  },
            {tag    , "property::activated" },
        }
    }
end
local widget = wibox.widget {
    {
        text   = "There is no tag!",
        when   = function(focused_client, current_tag) -- luacheck: no unused args
            return not current_tag
        end,
        widget = wibox.widget.textbox
    },
    {
        text   = "There is no client!",
        when   = function(focused_client, current_tag) -- luacheck: no unused args
            return not focused_client
        end,
        widget = wibox.widget.textbox
    },
    {
        text   = "There is a client!",
        when   = function(focused_client, current_tag) -- luacheck: no unused args
            return true
        end,
        widget = wibox.widget.textbox
    },
    layout = extended_conditional
}

Info:

  • Copyright: 2016-2018 Emmanuel Lepage Vallee
  • Originally authored by: Emmanuel Lepage Vallee <elv1313@gmail.com>
    (Full contributors list available on our github project)

Functions

wibox.container.conditional ([widget[, args={}]]) Returns a new conditional container.

Object properties

widget widget The current child widget instance.
templates table The list of conditional templates.
default_when function Set the fallback conditional handler.
selected_template table The list of conditional templates.
attached_signals table Return all attached signals.
connected_global_signals table The list of global signals used to automatically call eval().
argument_callback function Controls the arguments passed to the when callback.
children table Get or set the children elements. Inherited from wibox.widget
all_children table Get all direct and indirect children widgets. Inherited from wibox.widget
forced_height number or nil Force a widget height. Inherited from wibox.widget
forced_width number or nil Force a widget width. Inherited from wibox.widget
opacity number The widget opacity (transparency). Inherited from wibox.widget
visible boolean The widget visibility. Inherited from wibox.widget
buttons table The widget buttons. Inherited from wibox.widget

Object methods

:setup {[args]} Set a declarative widget hierarchy description. Inherited from wibox.widget
:add_button (button) Add a new awful.button to this widget. Inherited from wibox.widget
:emit_signal_recursive (signal_name, ...) Emit a signal and ensure all parent widgets in the hierarchies also forward the signal. Inherited from wibox.widget
:emit_signal (name, ...) Emit a signal. Inherited from gears.object
:connect_signal (name, func) Connect to a signal. Inherited from gears.object
:weak_connect_signal (name, func) Connect to a signal weakly. Inherited from gears.object

Signals

widget::layout_changed When the layout (size) change. Inherited from wibox.widget
widget::redraw_needed When the widget content changed. Inherited from wibox.widget
button::press When a mouse button is pressed over the widget. Inherited from wibox.widget
button::release When a mouse button is released over the widget. Inherited from wibox.widget
mouse::enter When the mouse enter a widget. Inherited from wibox.widget
mouse::leave When the mouse leave a widget. Inherited from wibox.widget

Methods

:get_children () Get the number of children element
:reset () Reset this layout.
:eval () Evaluate all the when clauses and update the current widget(s).
:attach_signal (object, signal) Add a signal source to automatically call eval().
:detach_signal (object, signal) Remove a signal source to automatically call eval().


Functions

Methods
wibox.container.conditional ([widget[, args={}]])
Returns a new conditional container.

Parameters:

  • widget The widget to display. (optional)
  • args table Options for this widget. (default {})

Object properties

widget widget
The current child widget instance.

The widget that has been produced be the current_template.

This property is read only.

templates table
The list of conditional templates.
default_when function
Set the fallback conditional handler.

When no when function is set on the template, this one is called. This is useful if an extended version of this widget adds additional properties to templates.

selected_template table
The list of conditional templates.

Type constraints:

  • selected_template table The selected template or a widget instance
attached_signals table
Return all attached signals.

The format is a list of tables with the object as the first value and the signal name has the second.

connected_global_signals table
The list of global signals used to automatically call eval().

The format is a table of table with objects as keys and signal name as values.

Usage:

    local widget = wibox.widget {
        {
            text   = "Useless example!",
            when   = function() return true end,
            widget = wibox.widget.textbox
        },
        connected_global_signals = {
            {client, "focused"  },
            {tag   , "selected" },
            {tag   , "activated"},
        },
        layout  = wibox.container.conditional
    }
argument_callback function
Controls the arguments passed to the when callback.

This is useful when extending this container to fit specific purposes.

All values returned by this function will be passed to the when callback. Note that it will be called only once for all templates.

The default value returns the conditional widget instance.

The callback has a single parameter (the widget instance).

children table · Inherited from wibox.widget
Get or set the children elements.

Type constraints:

  • children table The children.
all_children table · Inherited from wibox.widget
Get all direct and indirect children widgets. This will scan all containers recursively to find widgets Warning: This method it prone to stack overflow id the widget, or any of its children, contain (directly or indirectly) itself.

Type constraints:

  • children table The children.
forced_height number or nil · Inherited from wibox.widget
Force a widget height.

Type constraints:

  • height number or nil The height (nil for automatic)
forced_width number or nil · Inherited from wibox.widget
Force a widget width.

Type constraints:

  • width number or nil The width (nil for automatic)
opacity number · Inherited from wibox.widget
The widget opacity (transparency).

Type constraints:

  • opacity number The opacity (between 0 and 1) (default 1)
visible boolean · Inherited from wibox.widget
The widget visibility.
buttons table · Inherited from wibox.widget
The widget buttons.

The table contains a list of awful.button objects.

See also:

Object methods

:setup {[args]} · Inherited from wibox.widget
Set a declarative widget hierarchy description. See The declarative layout system

Parameters:

  • args An array containing the widgets disposition
:add_button (button) · Inherited from wibox.widget
Add a new awful.button to this widget.

Parameters:

  • button awful.button The button to add.
:emit_signal_recursive (signal_name, ...) · Inherited from wibox.widget
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.

Parameters:

  • signal_name string
  • ... Other arguments
:emit_signal (name, ...) · Inherited from gears.object
Emit a signal.

Parameters:

  • name string The name of the signal.
  • ... Extra arguments for the callback functions. Each connected function receives the object as first argument and then any extra arguments that are given to emit_signal().
:connect_signal (name, func) · Inherited from gears.object
Connect to a signal.

Parameters:

  • name string The name of the signal.
  • func function The callback to call when the signal is emitted.
:weak_connect_signal (name, func) · Inherited from gears.object
Connect to a signal weakly.

This allows the callback function to be garbage collected and automatically disconnects the signal when that happens.

Warning: Only use this function if you really, really, really know what you are doing.

Parameters:

  • name string The name of the signal.
  • func function The callback to call when the signal is emitted.

Signals

widget::layout_changed · Inherited from wibox.widget
When the layout (size) change. This signal is emitted when the previous results of :layout() and :fit() are no longer valid. Unless this signal is emitted, :layout() and :fit() must return the same result when called with the same arguments.

See also:

widget::redraw_needed · Inherited from wibox.widget
When the widget content changed. This signal is emitted when the content of the widget changes. The widget will be redrawn, it is not re-layouted. Put differently, it is assumed that :layout() and :fit() would still return the same results as before.

See also:

button::press · Inherited from wibox.widget
When a mouse button is pressed over the widget.

Arguments:

  • self table The current object instance itself.
  • lx number The horizontal position relative to the (0,0) position in the widget.
  • ly number The vertical position relative to the (0,0) position in the widget.
  • button number The button number.
  • mods table The modifiers (mod4, mod1 (alt), Control, Shift)
  • find_widgets_result The entry from the result of wibox.drawable:find_widgets for the position that the mouse hit.
    • drawable wibox.drawable The drawable containing the widget.
    • widget widget The widget being displayed.
    • hierarchy wibox.hierarchy The hierarchy managing the widget's geometry.
    • x number An approximation of the X position that the widget is visible at on the surface.
    • y number An approximation of the Y position that the widget is visible at on the surface.
    • width number An approximation of the width that the widget is visible at on the surface.
    • height number An approximation of the height that the widget is visible at on the surface.
    • widget_width number The exact width of the widget in its local coordinate system.
    • widget_height number The exact height of the widget in its local coordinate system.

See also:

button::release · Inherited from wibox.widget
When a mouse button is released over the widget.

Arguments:

  • self table The current object instance itself.
  • lx number The horizontal position relative to the (0,0) position in the widget.
  • ly number The vertical position relative to the (0,0) position in the widget.
  • button number The button number.
  • mods table The modifiers (mod4, mod1 (alt), Control, Shift)
  • find_widgets_result The entry from the result of wibox.drawable:find_widgets for the position that the mouse hit.
    • drawable wibox.drawable The drawable containing the widget.
    • widget widget The widget being displayed.
    • hierarchy wibox.hierarchy The hierarchy managing the widget's geometry.
    • x number An approximation of the X position that the widget is visible at on the surface.
    • y number An approximation of the Y position that the widget is visible at on the surface.
    • width number An approximation of the width that the widget is visible at on the surface.
    • height number An approximation of the height that the widget is visible at on the surface.
    • widget_width number The exact width of the widget in its local coordinate system.
    • widget_height number The exact height of the widget in its local coordinate system.

See also:

mouse::enter · Inherited from wibox.widget
When the mouse enter a widget.

Arguments:

  • self table The current object instance itself.
  • find_widgets_result The entry from the result of wibox.drawable:find_widgets for the position that the mouse hit.
    • drawable wibox.drawable The drawable containing the widget.
    • widget widget The widget being displayed.
    • hierarchy wibox.hierarchy The hierarchy managing the widget's geometry.
    • x number An approximation of the X position that the widget is visible at on the surface.
    • y number An approximation of the Y position that the widget is visible at on the surface.
    • width number An approximation of the width that the widget is visible at on the surface.
    • height number An approximation of the height that the widget is visible at on the surface.
    • widget_width number The exact width of the widget in its local coordinate system.
    • widget_height number The exact height of the widget in its local coordinate system.

See also:

mouse::leave · Inherited from wibox.widget
When the mouse leave a widget.

Arguments:

  • self table The current object instance itself.
  • find_widgets_result The entry from the result of wibox.drawable:find_widgets for the position that the mouse hit.
    • drawable wibox.drawable The drawable containing the widget.
    • widget widget The widget being displayed.
    • hierarchy wibox.hierarchy The hierarchy managing the widget's geometry.
    • x number An approximation of the X position that the widget is visible at on the surface.
    • y number An approximation of the Y position that the widget is visible at on the surface.
    • width number An approximation of the width that the widget is visible at on the surface.
    • height number An approximation of the height that the widget is visible at on the surface.
    • widget_width number The exact width of the widget in its local coordinate system.
    • widget_height number The exact height of the widget in its local coordinate system.

See also:

Methods

:get_children ()
Get the number of children element

Returns:

    table The children
:reset ()
Reset this layout. The widget will be removed and the rotation reset.
:eval ()
Evaluate all the when clauses and update the current widget(s).

Usage:

    local widget = wibox.widget {
        {
            text   = "There is no client",
            when   = function() return client.focus == nil end,
            widget = wibox.widget.textbox
        },
        {
            text   = "There is a client",
            when   = function() return client.focus ~= nil end,
            widget = wibox.widget.textbox
        },
        layout  = wibox.container.conditional
    }
    client.connect_signal("focus", function(c) -- luacheck: no unused args
        widget:eval()
    end)
    
    The signal is property::selected_template and has the following argument
       * The wibox.container.conditional widget instance
       * The current template
       * The old template
       * The current template widget instance
       * The old template widget instance
:attach_signal (object, signal)
Add a signal source to automatically call eval().

Parameters:

  • object gears.object The source object.
  • signal string The signal name.

Usage:

    local obj1 = gears.object {
        enable_properties   = true,
        enable_auto_signals = true,
    }
    local widget = wibox.widget {
        {
            text   = "Foo is not set",
            when   = function() return obj1.foo ~= "bar" end,
            widget = wibox.widget.textbox
        },
        {
            text   = "Foo is set",
            when   = function() return obj1.foo == "bar" end,
            widget = wibox.widget.textbox
        },
        layout  = wibox.container.conditional
    }
    widget:attach_signal(obj1, "property::foo")
    obj1.foo = "bar"
:detach_signal (object, signal)
Remove a signal source to automatically call eval().

Parameters:

  • object
  • signal
generated by LDoc 1.4.6 Last updated 2021-11-13 00:35:50