Browse Source

widget: Add an `:add_button()` method.

This is done now because a lot of code in `lib/` add buttons by manually
extracting buttons from awful.button. Instead of adding ugly code to
prevent using the legacy API, do this.
Emmanuel Lepage Vallee 10 months ago
parent
commit
cf0385af80
2 changed files with 27 additions and 0 deletions
  1. 4
    0
      docs/common/widget.ldoc
  2. 23
    0
      lib/wibox/widget/base.lua

+ 4
- 0
docs/common/widget.ldoc View File

@@ -48,6 +48,10 @@
48 48
 -- @param table
49 49
 -- @see awful.button
50 50
 
51
+--- Add a new `awful.button` to this widget.
52
+-- @tparam awful.button button The button to add.
53
+-- @function add_button
54
+
51 55
 --- Emit a signal and ensure all parent widgets in the hierarchies also
52 56
 -- forward the signal. This is useful to track signals when there is a dynamic
53 57
 -- set of containers and layouts wrapping the widget.

+ 23
- 0
lib/wibox/widget/base.lua View File

@@ -40,6 +40,29 @@ function base.widget:set_visible(b)
40 40
     end
41 41
 end
42 42
 
43
+--- Add a new `awful.button` to this widget.
44
+-- @tparam awful.button button The button to add.
45
+function base.widget:add_button(button)
46
+    if not button then return end
47
+
48
+    -- Simple case
49
+    if not self._private.buttons then
50
+        self:set_buttons({button})
51
+        return
52
+    end
53
+
54
+    -- This could happen if something accidentally uses rawset
55
+    assert(self._private.buttons_formatted)
56
+
57
+    -- an `awful.button` is a tupple of `capi.button`
58
+    self._private.buttons_formatted = gtable.join(
59
+        self._private.buttons_formatted,
60
+        button
61
+    )
62
+
63
+    table.insert(self._private.buttons, button)
64
+end
65
+
43 66
 --- Is the widget visible?
44 67
 -- @treturn boolean
45 68
 -- @method get_visible

Loading…
Cancel
Save