wibox: Make the functions a little more object-y

This replaces lots of function foo.bar(this, ...) with function foo:bar(...).
There should be no other changes in this commit.

The point is to make it easier for api documentation tools to figure out that
these are methods on objects.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2012-11-18 20:44:03 +01:00
parent ee46c9e5ae
commit 90f7f55348
11 changed files with 193 additions and 199 deletions

View File

@ -30,39 +30,37 @@ wibox.widget = require("wibox.widget")
wibox.drawable = require("wibox.drawable")
--- Set the widget that the wibox displays
function wibox.set_widget(_wibox, widget)
_wibox._drawable:set_widget(widget)
function wibox:set_widget(widget)
self._drawable:set_widget(widget)
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.
function wibox.set_bg(_wibox, c)
_wibox._drawable:set_bg(c)
function wibox:set_bg(c)
self._drawable:set_bg(c)
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.
function wibox.set_fg(_wibox, c)
_wibox._drawable:set_fg(c)
function wibox:set_fg(c)
self._drawable:set_fg(c)
end
--- Helper function to make wibox:buttons() work as expected
function wibox.buttons(box, ...)
return box.drawin:buttons(...)
function wibox:buttons(...)
return self.drawin:buttons(...)
end
--- Helper function to make wibox:struts() work as expected
function wibox.struts(box, ...)
return box.drawin:struts(...)
function wibox:struts(...)
return self.drawin:struts(...)
end
--- Helper function to make wibox:geometry() work as expected
function wibox.geometry(box, ...)
return box.drawin:geometry(...)
function wibox:geometry(...)
return self.drawin:geometry(...)
end
local function setup_signals(_wibox)

View File

@ -74,28 +74,28 @@ local function widget_changed(layout, old_w, new_w)
end
--- Set the layout's first widget. This is the widget that is at the left/top
function align.set_first(layout, widget)
widget_changed(layout, layout.first, widget)
layout.first = widget
function align:set_first(widget)
widget_changed(self, self.first, widget)
self.first = widget
end
--- Set the layout's second widget. This is the centered one.
function align.set_second(layout, widget)
widget_changed(layout, layout.second, widget)
layout.second = widget
function align:set_second(widget)
widget_changed(self, self.second, widget)
self.second = widget
end
--- Set the layout's third widget. This is the widget that is at the right/bottom
function align.set_third(layout, widget)
widget_changed(layout, layout.third, widget)
layout.third = widget
function align:set_third(widget)
widget_changed(self, self.third, widget)
self.third = widget
end
function align.reset(layout)
function align:reset()
for k, v in pairs({ "first", "second", "third" }) do
layout[v] = nil
self[v] = nil
end
layout:emit_signal("widget::updated")
self:emit_signal("widget::updated")
end
local function get_layout(dir)

View File

@ -54,11 +54,11 @@ function fixed.draw_fixed(dir, widgets, fill_space, wibox, cr, width, height)
end
--- Add a widget to the given fixed layout
function fixed.add(layout, widget)
function fixed:add(widget)
widget_base.check_widget(widget)
table.insert(layout.widgets, widget)
widget:connect_signal("widget::updated", layout._emit_updated)
layout._emit_updated()
table.insert(self.widgets, widget)
widget:connect_signal("widget::updated", self._emit_updated)
self._emit_updated()
end
--- Fit the fixed layout into the given space
@ -102,20 +102,20 @@ function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
end
--- Reset a fixed layout. This removes all widgets from the layout.
function fixed.reset(layout)
for k, v in pairs(layout.widgets) do
v:disconnect_signal("widget::updated", layout._emit_updated)
function fixed:reset()
for k, v in pairs(self.widgets) do
v:disconnect_signal("widget::updated", self._emit_updated)
end
layout.widgets = {}
layout:emit_signal("widget::updated")
self.widgets = {}
self:emit_signal("widget::updated")
end
--- Set the layout's fill_space property. If this property is true, the last
-- widget will get all the space that is left. If this is false, the last widget
-- won't be handled specially and there can be space left unused.
function fixed.fill_space(layout, val)
layout._fill_space = val
layout:emit_signal("widget::updated")
function fixed:fill_space(val)
self._fill_space = val
self:emit_signal("widget::updated")
end
local function get_layout(dir)

View File

@ -14,56 +14,56 @@ local widget_base = require("wibox.widget.base")
local margin = { mt = {} }
--- Draw a margin layout
function margin.draw(layout, wibox, cr, width, height)
local x = layout.left
local y = layout.top
local w = layout.right
local h = layout.bottom
function margin:draw(wibox, cr, width, height)
local x = self.left
local y = self.top
local w = self.right
local h = self.bottom
if not layout.widget or width <= x + w or height <= y + h then
if not self.widget or width <= x + w or height <= y + h then
return
end
base.draw_widget(wibox, cr, layout.widget, x, y, width - x - w, height - y - h)
base.draw_widget(wibox, cr, self.widget, x, y, width - x - w, height - y - h)
end
--- Fit a margin layout into the given space
function margin.fit(layout, width, height)
local extra_w = layout.left + layout.right
local extra_h = layout.top + layout.bottom
function margin:fit(width, height)
local extra_w = self.left + self.right
local extra_h = self.top + self.bottom
local w, h = 0, 0
if layout.widget then
w, h = layout.widget:fit(width - extra_w, height - extra_h)
if self.widget then
w, h = self.widget:fit(width - extra_w, height - extra_h)
end
return w + extra_w, h + extra_h
end
--- Set the widget that this layout adds a margin on.
function margin.set_widget(layout, widget)
if layout.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated)
function margin:set_widget(widget)
if self.widget then
self.widget:disconnect_signal("widget::updated", self._emit_updated)
end
if widget then
widget_base.check_widget(widget)
widget:connect_signal("widget::updated", layout._emit_updated)
widget:connect_signal("widget::updated", self._emit_updated)
end
layout.widget = widget
layout._emit_updated()
self.widget = widget
self._emit_updated()
end
--- Set all the margins to val.
function margin.set_margins(layout, val)
layout.left = val
layout.right = val
layout.top = val
layout.bottom = val
layout:emit_signal("widget::updated")
function margin:set_margins(val)
self.left = val
self.right = val
self.top = val
self.bottom = val
self:emit_signal("widget::updated")
end
--- Reset this layout. The widget will be unreferenced and the margins set to 0.
function margin.reset(layout)
layout:set_widget(nil)
layout:set_margins(0)
function margin:reset()
self:set_widget(nil)
self:set_margins(0)
end
--- Set the left margin that this layout adds to its widget.

View File

@ -16,10 +16,10 @@ local widget_base = require("wibox.widget.base")
local mirror = { mt = {} }
--- Draw this layout
function mirror.draw(layout, wibox, cr, width, height)
if not layout.widget then return { width = 0, height = 0 } end
if not layout.horizontal and not layout.vertical then
layout.widget:draw(wibox, cr, width, height)
function mirror:draw(wibox, cr, width, height)
if not self.widget then return { width = 0, height = 0 } end
if not self.horizontal and not self.vertical then
self.widget:draw(wibox, cr, width, height)
return -- nothing changed
end
@ -27,75 +27,71 @@ function mirror.draw(layout, wibox, cr, width, height)
local t = { x = 0, y = 0 } -- translation
local s = { x = 1, y = 1 } -- scale
if layout.horizontal then
if self.horizontal then
t.y = height
s.y = -1
end
if layout.vertical then
if self.vertical then
t.x = width
s.x = -1
end
cr:translate(t.x, t.y)
cr:scale(s.x, s.y)
layout.widget:draw(wibox, cr, width, height)
self.widget:draw(wibox, cr, width, height)
-- Undo the scale and translation from above.
cr:restore()
end
--- Fit this layout into the given area
function mirror.fit(layout, ...)
if not layout.widget then
function mirror:fit(...)
if not self.widget then
return 0, 0
end
return layout.widget:fit(...)
return self.widget:fit(...)
end
--- Set the widget that this layout mirrors.
-- @param layout The layout
-- @param widget The widget to mirror
function mirror.set_widget(layout, widget)
if layout.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated)
function mirror:set_widget(widget)
if self.widget then
self.widget:disconnect_signal("widget::updated", self._emit_updated)
end
if widget then
widget_base.check_widget(widget)
widget:connect_signal("widget::updated", layout._emit_updated)
widget:connect_signal("widget::updated", self._emit_updated)
end
layout.widget = widget
layout._emit_updated()
self.widget = widget
self._emit_updated()
end
--- Reset this layout. The widget will be removed and the axes reset.
-- @param layout The layout
function mirror.reset(layout)
layout.horizontal = false
layout.vertical = false
layout:set_widget(nil)
function mirror:reset()
self.horizontal = false
self.vertical = false
self:set_widget(nil)
end
--- Set the reflection of this mirror layout.
-- @param layout The layout
-- @param reflection a table which contains new values for horizontal and/or vertical (booleans)
function mirror.set_reflection(layout, reflection)
function mirror:set_reflection(reflection)
if type(reflection) ~= 'table' then
error("Invalid type of reflection for mirror layout: " ..
type(reflection) .. " (should be a table)")
end
for _, ref in ipairs({"horizontal", "vertical"}) do
if reflection[ref] ~= nil then
layout[ref] = reflection[ref]
self[ref] = reflection[ref]
end
end
layout._emit_updated()
self._emit_updated()
end
--- Get the reflection of this mirror layout.
-- @param layout The layout
-- @return a table of booleans with the keys "horizontal", "vertical".
function mirror.get_reflection(layout)
return { horizontal = layout.horizontal, vertical = layout.vertical }
function mirror:get_reflection()
return { horizontal = self.horizontal, vertical = self.vertical }
end
--- Returns a new mirror layout. A mirror layout mirrors a given widget. Use

View File

@ -25,10 +25,10 @@ local function transform(layout, width, height)
end
--- Draw this layout
function rotate.draw(layout, wibox, cr, width, height)
if not layout.widget then return { width = 0, height = 0 } end
function rotate:draw(wibox, cr, width, height)
if not self.widget then return { width = 0, height = 0 } end
local dir = layout:get_direction()
local dir = self:get_direction()
if dir == "west" then
cr:rotate(pi / 2)
@ -43,39 +43,39 @@ function rotate.draw(layout, wibox, cr, width, height)
-- Since we rotated, we might have to swap width and height.
-- transform() does that for us.
base.draw_widget(wibox, cr, layout.widget, 0, 0, transform(layout, width, height))
base.draw_widget(wibox, cr, self.widget, 0, 0, transform(self, width, height))
end
--- Fit this layout into the given area
function rotate.fit(layout, width, height)
if not layout.widget then
function rotate:fit(self, width, height)
if not self.widget then
return 0, 0
end
return transform(layout, layout.widget:fit(transform(layout, width, height)))
return transform(self, self.widget:fit(transform(self, width, height)))
end
--- Set the widget that this layout rotates.
function rotate.set_widget(layout, widget)
if layout.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated)
function rotate:set_widget(widget)
if self.widget then
self.widget:disconnect_signal("widget::updated", self._emit_updated)
end
if widget then
widget_base.check_widget(widget)
widget:connect_signal("widget::updated", layout._emit_updated)
widget:connect_signal("widget::updated", self._emit_updated)
end
layout.widget = widget
layout._emit_updated()
self.widget = widget
self._emit_updated()
end
--- Reset this layout. The widget will be removed and the rotation reset.
function rotate.reset(layout)
layout.direction = nil
layout:set_widget(nil)
function rotate:reset()
self.direction = nil
self:set_widget(nil)
end
--- Set the direction of this rotating layout. Valid values are "north", "east",
-- "south" and "west". On an invalid value, this function will throw an error.
function rotate.set_direction(layout, dir)
function rotate:set_direction(dir)
local allowed = {
north = true,
east = true,
@ -87,13 +87,13 @@ function rotate.set_direction(layout, dir)
error("Invalid direction for rotate layout: " .. tostring(dir))
end
layout.direction = dir
layout._emit_updated()
self.direction = dir
self._emit_updated()
end
--- Get the direction of this rotating layout
function rotate.get_direction(layout)
return layout.direction or "north"
function rotate:get_direction()
return self.direction or "north"
end
--- Returns a new rotate layout. A rotate layout rotates a given widget. Use

View File

@ -17,81 +17,81 @@ local type = type
local background = { mt = {} }
--- Draw this widget
function background.draw(box, wibox, cr, width, height)
if not box.widget then
function background:draw(wibox, cr, width, height)
if not self.widget then
return
end
cr:save()
if box.background then
cr:set_source(box.background)
if self.background then
cr:set_source(self.background)
cr:paint()
end
if box.bgimage then
local pattern = cairo.Pattern.create_for_surface(box.bgimage)
if self.bgimage then
local pattern = cairo.Pattern.create_for_surface(self.bgimage)
cr:set_source(pattern)
cr:paint()
end
cr:restore()
if box.foreground then
if self.foreground then
cr:save()
cr:set_source(box.foreground)
cr:set_source(self.foreground)
end
layout_base.draw_widget(wibox, cr, box.widget, 0, 0, width, height)
if box.foreground then
layout_base.draw_widget(wibox, cr, self.widget, 0, 0, width, height)
if self.foreground then
cr:restore()
end
end
--- Fit this widget into the given area
function background.fit(box, width, height)
if not box.widget then
function background:fit(width, height)
if not self.widget then
return 0, 0
end
return box.widget:fit(width, height)
return self.widget:fit(width, height)
end
--- Set the widget that is drawn on top of the background
function background.set_widget(box, widget)
if box.widget then
box.widget:disconnect_signal("widget::updated", box._emit_updated)
function background:set_widget(widget)
if self.widget then
self.widget:disconnect_signal("widget::updated", self._emit_updated)
end
if widget then
base.check_widget(widget)
widget:connect_signal("widget::updated", box._emit_updated)
widget:connect_signal("widget::updated", self._emit_updated)
end
box.widget = widget
box._emit_updated()
self.widget = widget
self._emit_updated()
end
--- Set the background to use
function background.set_bg(box, bg)
function background:set_bg(bg)
if bg then
box.background = color(bg)
self.background = color(bg)
else
box.background = nil
self.background = nil
end
box._emit_updated()
self._emit_updated()
end
--- Set the foreground to use
function background.set_fg(box, fg)
function background:set_fg(fg)
if fg then
box.foreground = color(fg)
self.foreground = color(fg)
else
box.foreground = nil
self.foreground = nil
end
box._emit_updated()
self._emit_updated()
end
--- Set the background image to use
function background.set_bgimage(box, image)
box.bgimage = surface.load(image)
box._emit_updated()
function background:set_bgimage(image)
self.bgimage = surface.load(image)
self._emit_updated()
end
local function new()

View File

@ -14,12 +14,12 @@ local table = table
local base = {}
--- Set/get a widget's buttons
function base.buttons(widget, _buttons)
function base:buttons(_buttons)
if _buttons then
widget.widget_buttons = _buttons
self.widget_buttons = _buttons
end
return widget.widget_buttons
return self.widget_buttons
end
--- Handle a button event on a widget. This is used internally.

View File

@ -16,35 +16,35 @@ local print = print
local imagebox = { mt = {} }
--- Draw an imagebox with the given cairo context in the given geometry.
function imagebox.draw(box, wibox, cr, width, height)
if not box.image then return end
function imagebox:draw(wibox, cr, width, height)
if not self.image then return end
cr:save()
if not box.resize_forbidden then
if not self.resize_forbidden then
-- Let's scale the image so that it fits into (width, height)
local w = box.image:get_width()
local h = box.image:get_height()
local w = self.image:get_width()
local h = self.image:get_height()
local aspect = width / w
local aspect_h = height / h
if aspect > aspect_h then aspect = aspect_h end
cr:scale(aspect, aspect)
end
cr:set_source_surface(box.image, 0, 0)
cr:set_source_surface(self.image, 0, 0)
cr:paint()
cr:restore()
end
--- Fit the imagebox into the given geometry
function imagebox.fit(box, width, height)
if not box.image then
function imagebox:fit(width, height)
if not self.image then
return 0, 0
end
local w = box.image:get_width()
local h = box.image:get_height()
local w = self.image:get_width()
local h = self.image:get_height()
if w > width then
h = h * width / w
@ -55,7 +55,7 @@ function imagebox.fit(box, width, height)
h = height
end
if not box.resize_forbidden then
if not self.resize_forbidden then
local aspect = width / w
local aspect_h = height / h
@ -71,7 +71,7 @@ end
--- Set an imagebox' image
-- @param image Either a string or a cairo image surface. A string is
-- interpreted as the path to a png image file.
function imagebox.set_image(box, image)
function imagebox:set_image(image)
local image = image
if type(image) == "string" then
@ -93,18 +93,18 @@ function imagebox.set_image(box, image)
end
end
box.image = image
self.image = image
box:emit_signal("widget::updated")
self:emit_signal("widget::updated")
return true
end
--- Should the image be resized to fit into the available space?
-- @param allowed If false, the image will be clipped, else it will be resized
-- to fit into the available space.
function imagebox.set_resize(box, allowed)
box.resize_forbidden = not allowed
box:emit_signal("widget::updated")
function imagebox:set_resize(allowed)
self.resize_forbidden = not allowed
self:emit_signal("widget::updated")
end
-- Returns a new imagebox

View File

@ -18,7 +18,7 @@ local created_systray = false
local horizontal = true
local base_size = nil
function systray.draw(box, wibox, cr, width, height)
function systray:draw(wibox, cr, width, height)
local x, y, width, height = lbase.rect_to_device_geometry(cr, 0, 0, width, height)
local num_entries = capi.awesome.systray()
local bg = beautiful.bg_systray or beautiful.bg_normal
@ -37,7 +37,7 @@ function systray.draw(box, wibox, cr, width, height)
capi.awesome.systray(wibox.drawin, x, y, base, horizontal, bg)
end
function systray.fit(box, width, height)
function systray:fit(width, height)
local num_entries = capi.awesome.systray()
local base = base_size
if base == nil then

View File

@ -27,91 +27,91 @@ local function setup_layout(box, width, height)
end
--- Draw the given textbox on the given cairo context in the given geometry
function textbox.draw(box, wibox, cr, width, height)
cr:update_layout(box._layout)
setup_layout(box, width, height)
local ink, logical = box._layout:get_pixel_extents()
function textbox:draw(wibox, cr, width, height)
cr:update_layout(self._layout)
setup_layout(self, width, height)
local ink, logical = self._layout:get_pixel_extents()
local offset = 0
if box._valign == "center" then
if self._valign == "center" then
offset = (height - logical.height) / 2
elseif box._valign == "bottom" then
elseif self._valign == "bottom" then
offset = height - logical.height
end
cr:move_to(0, offset)
cr:show_layout(box._layout)
cr:show_layout(self._layout)
end
--- Fit the given textbox
function textbox.fit(box, width, height)
setup_layout(box, width, height)
local ink, logical = box._layout:get_pixel_extents()
function textbox:fit(width, height)
setup_layout(self, width, height)
local ink, logical = self._layout:get_pixel_extents()
return logical.width, logical.height
end
--- Set a textbox' text.
-- @param text The text to set. This can contain pango markup (e.g. <b>bold</b>)
function textbox.set_markup(box, text)
function textbox:set_markup(text)
local attr, parsed = Pango.parse_markup(text, -1, 0)
-- In case of error, attr is false and parsed is an error message
if not attr then error(parsed) end
box._layout.text = parsed
box._layout.attributes = attr
box:emit_signal("widget::updated")
self._layout.text = parsed
self._layout.attributes = attr
self:emit_signal("widget::updated")
end
--- Set a textbox' text.
-- @param text The text to display. Pango markup is ignored and shown as-is.
function textbox.set_text(box, text)
box._layout.text = text
box._layout.attributes = nil
box:emit_signal("widget::updated")
function textbox:set_text(text)
self._layout.text = text
self._layout.attributes = nil
self:emit_signal("widget::updated")
end
--- Set a textbox' ellipsize mode.
-- @param mode Where should long lines be shortened? "start", "middle" or "end"
function textbox.set_ellipsize(box, mode)
function textbox:set_ellipsize(mode)
local allowed = { none = "NONE", start = "START", middle = "MIDDLE", ["end"] = "END" }
if allowed[mode] then
box._layout:set_ellipsize(allowed[mode])
box:emit_signal("widget::updated")
self._layout:set_ellipsize(allowed[mode])
self:emit_signal("widget::updated")
end
end
--- Set a textbox' wrap mode.
-- @param mode Where to wrap? After "word", "char" or "word_char"
function textbox.set_wrap(box, mode)
function textbox:set_wrap(mode)
local allowed = { word = "WORD", char = "CHAR", word_char = "WORD_CHAR" }
if allowed[mode] then
box._layout:set_wrap(allowed[mode])
box:emit_signal("widget::updated")
self._layout:set_wrap(allowed[mode])
self:emit_signal("widget::updated")
end
end
--- Set a textbox' vertical alignment
-- @param mode Where should the textbox be drawn? "top", "center" or "bottom"
function textbox.set_valign(box, mode)
function textbox:set_valign(mode)
local allowed = { top = true, center = true, bottom = true }
if allowed[mode] then
box._valign = mode
box:emit_signal("widget::updated")
self._valign = mode
self:emit_signal("widget::updated")
end
end
--- Set a textbox' horizontal alignment
-- @param mode Where should the textbox be drawn? "left", "center" or "right"
function textbox.set_align(box, mode)
function textbox:set_align(mode)
local allowed = { left = "LEFT", center = "CENTER", right = "RIGHT" }
if allowed[mode] then
box._layout:set_alignment(allowed[mode])
box:emit_signal("widget::updated")
self._layout:set_alignment(allowed[mode])
self:emit_signal("widget::updated")
end
end
--- Set a textbox' font
-- @param font The font description as string
function textbox.set_font(box, font)
box._layout:set_font_description(beautiful.get_font(font))
function textbox:set_font(font)
self._layout:set_font_description(beautiful.get_font(font))
end
-- Returns a new textbox