fixed: Enable the property system

And add the missing documentation
This commit is contained in:
Emmanuel Lepage Vallee 2016-05-26 19:41:58 -04:00
parent 18500838d9
commit b89fbcf940
2 changed files with 47 additions and 41 deletions

View File

@ -29,11 +29,9 @@
-- @name swap_widgets -- @name swap_widgets
-- @class function -- @class function
--- Get all children of this layout. --- Get all direct children of this layout.
-- @param layout The layout you are modifying. -- @param layout The layout you are modifying.
-- @return a list of all widgets -- @property children
-- @name get_children
-- @class function
--- Reset a ratio layout. This removes all widgets from the layout. --- Reset a ratio layout. This removes all widgets from the layout.
-- @param layout The layout you are modifying. -- @param layout The layout you are modifying.

View File

@ -23,28 +23,28 @@ local fixed = {}
-- @param height The available height. -- @param height The available height.
function fixed:layout(context, width, height) function fixed:layout(context, width, height)
local result = {} local result = {}
local pos,spacing = 0, self._spacing local pos,spacing = 0, self._private.spacing
for k, v in pairs(self.widgets) do for k, v in pairs(self._private.widgets) do
local x, y, w, h, _ local x, y, w, h, _
if self.dir == "y" then if self._private.dir == "y" then
x, y = 0, pos x, y = 0, pos
w, h = width, height - pos w, h = width, height - pos
if k ~= #self.widgets or not self._fill_space then if k ~= #self._private.widgets or not self._private.fill_space then
_, h = base.fit_widget(self, context, v, w, h); _, h = base.fit_widget(self, context, v, w, h);
end end
pos = pos + h + spacing pos = pos + h + spacing
else else
x, y = pos, 0 x, y = pos, 0
w, h = width - pos, height w, h = width - pos, height
if k ~= #self.widgets or not self._fill_space then if k ~= #self._private.widgets or not self._private.fill_space then
w, _ = base.fit_widget(self, context, v, w, h); w, _ = base.fit_widget(self, context, v, w, h);
end end
pos = pos + w + spacing pos = pos + w + spacing
end end
if (self.dir == "y" and pos-spacing > height) or if (self._private.dir == "y" and pos-spacing > height) or
(self.dir ~= "y" and pos-spacing > width) then (self._private.dir ~= "y" and pos-spacing > width) then
break break
end end
table.insert(result, base.place_widget_at(v, x, y, w, h)) table.insert(result, base.place_widget_at(v, x, y, w, h))
@ -60,7 +60,7 @@ function fixed:add(...)
assert(args.n > 0, "need at least one widget to add") assert(args.n > 0, "need at least one widget to add")
for i=1, args.n do for i=1, args.n do
base.check_widget(args[i]) base.check_widget(args[i])
table.insert(self.widgets, args[i]) table.insert(self._private.widgets, args[i])
end end
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
@ -70,9 +70,9 @@ end
-- @tparam number index The widget index to remove -- @tparam number index The widget index to remove
-- @treturn boolean index If the operation is successful -- @treturn boolean index If the operation is successful
function fixed:remove(index) function fixed:remove(index)
if not index or index < 1 or index > #self.widgets then return false end if not index or index < 1 or index > #self._private.widgets then return false end
table.remove(self.widgets, index) table.remove(self._private.widgets, index)
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
@ -106,14 +106,10 @@ function fixed:remove_widgets(...)
return #args > (recursive and 1 or 0) and ret return #args > (recursive and 1 or 0) and ret
end end
--- Get all children of this layout
-- @treturn table a list of all widgets
function fixed:get_children() function fixed:get_children()
return self.widgets return self._private.widgets
end end
--- Replace the layout children
-- @tparam table children A table composed of valid widgets
function fixed:set_children(children) function fixed:set_children(children)
self:reset() self:reset()
if #children > 0 then if #children > 0 then
@ -138,12 +134,12 @@ function fixed:replace_widget(widget, widget2, recursive)
end end
function fixed:swap(index1, index2) function fixed:swap(index1, index2)
if not index1 or not index2 or index1 > #self.widgets if not index1 or not index2 or index1 > #self._private.widgets
or index2 > #self.widgets then or index2 > #self._private.widgets then
return false return false
end end
local widget1, widget2 = self.widgets[index1], self.widgets[index2] local widget1, widget2 = self._private.widgets[index1], self._private.widgets[index2]
self:set(index1, widget2) self:set(index1, widget2)
self:set(index2, widget1) self:set(index2, widget1)
@ -177,11 +173,11 @@ function fixed:swap_widgets(widget1, widget2, recursive)
end end
function fixed:set(index, widget2) function fixed:set(index, widget2)
if (not widget2) or (not self.widgets[index]) then return false end if (not widget2) or (not self._private.widgets[index]) then return false end
base.check_widget(widget2) base.check_widget(widget2)
self.widgets[index] = widget2 self._private.widgets[index] = widget2
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
@ -193,10 +189,10 @@ end
-- @param widget The widget -- @param widget The widget
-- @treturn boolean If the operation is successful -- @treturn boolean If the operation is successful
function fixed:insert(index, widget) function fixed:insert(index, widget)
if not index or index < 1 or index > #self.widgets + 1 then return false end if not index or index < 1 or index > #self._private.widgets + 1 then return false end
base.check_widget(widget) base.check_widget(widget)
table.insert(self.widgets, index, widget) table.insert(self._private.widgets, index, widget)
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
return true return true
@ -210,10 +206,10 @@ function fixed:fit(context, orig_width, orig_height)
local width, height = orig_width, orig_height local width, height = orig_width, orig_height
local used_in_dir, used_max = 0, 0 local used_in_dir, used_max = 0, 0
for _, v in pairs(self.widgets) do for _, v in pairs(self._private.widgets) do
local w, h = base.fit_widget(self, context, v, width, height) local w, h = base.fit_widget(self, context, v, width, height)
local in_dir, max local in_dir, max
if self.dir == "y" then if self._private.dir == "y" then
max, in_dir = w, h max, in_dir = w, h
height = height - in_dir height = height - in_dir
else else
@ -226,7 +222,7 @@ function fixed:fit(context, orig_width, orig_height)
used_in_dir = used_in_dir + in_dir used_in_dir = used_in_dir + in_dir
if width <= 0 or height <= 0 then if width <= 0 or height <= 0 then
if self.dir == "y" then if self._private.dir == "y" then
used_in_dir = orig_height used_in_dir = orig_height
else else
used_in_dir = orig_width used_in_dir = orig_width
@ -235,36 +231,38 @@ function fixed:fit(context, orig_width, orig_height)
end end
end end
local spacing = self._spacing * (#self.widgets-1) local spacing = self._private.spacing * (#self._private.widgets-1)
if self.dir == "y" then if self._private.dir == "y" then
return used_max, used_in_dir + spacing return used_max, used_in_dir + spacing
end end
return used_in_dir + spacing, used_max return used_in_dir + spacing, used_max
end end
function fixed:reset() function fixed:reset()
self.widgets = {} self._private.widgets = {}
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
--- Set the layout's fill_space property. If this property is true, the last --- 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 -- 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. -- won't be handled specially and there can be space left unused.
-- @property fill_space
function fixed:fill_space(val) function fixed:fill_space(val)
if self._fill_space ~= val then if self._private.fill_space ~= val then
self._fill_space = not not val self._private.fill_space = not not val
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
end end
local function get_layout(dir, widget1, ...) local function get_layout(dir, widget1, ...)
local ret = base.make_widget() local ret = base.make_widget(nil, nil, {enable_properties = true})
util.table.crush(ret, fixed) util.table.crush(ret, fixed, true)
ret.dir = dir ret._private.dir = dir
ret.widgets = {} ret._private.widgets = {}
ret:set_spacing(0) ret:set_spacing(0)
ret:fill_space(false) ret:fill_space(false)
@ -294,14 +292,24 @@ function fixed.vertical(...)
end end
--- Add spacing between each layout widgets --- Add spacing between each layout widgets
-- @param spacing Spacing between widgets. -- @property spacing
-- @tparam number spacing Spacing between widgets.
function fixed:set_spacing(spacing) function fixed:set_spacing(spacing)
if self._spacing ~= spacing then if self._private.spacing ~= spacing then
self._spacing = spacing self._private.spacing = spacing
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
end end
function fixed:get_spacing()
return self._private.spacing or 0
end
--@DOC_widget_COMMON@
--@DOC_object_COMMON@
return fixed return fixed
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80