scroll: Enable the property system

And add the missing documentation
This commit is contained in:
Emmanuel Lepage Vallee 2016-05-26 16:35:02 -04:00
parent 4ffbff3a5f
commit 03e2310c54
1 changed files with 64 additions and 54 deletions

View File

@ -76,29 +76,29 @@ end)
-- @field context The widget context for drawing the hierarchy -- @field context The widget context for drawing the hierarchy
local function calculate_info(self, context, width, height) local function calculate_info(self, context, width, height)
local result = {} local result = {}
assert(self.widget) assert(self._private.widget)
-- First, get the size of the widget (and the size of extra space) -- First, get the size of the widget (and the size of extra space)
local surface_width, surface_height = width, height local surface_width, surface_height = width, height
local extra_width, extra_height, extra = 0, 0, self.expand and self.extra_space or 0 local extra_width, extra_height, extra = 0, 0, self._private.expand and self._private.extra_space or 0
local w, h local w, h
if self.dir == "h" then if self._private.dir == "h" then
w, h = base.fit_widget(self, context, self.widget, self.space_for_scrolling, height) w, h = base.fit_widget(self, context, self._private.widget, self._private.space_for_scrolling, height)
surface_width = w surface_width = w
extra_width = extra extra_width = extra
else else
w, h = base.fit_widget(self, context, self.widget, width, self.space_for_scrolling) w, h = base.fit_widget(self, context, self._private.widget, width, self._private.space_for_scrolling)
surface_height = h surface_height = h
extra_height = extra extra_height = extra
end end
result.fit_width, result.fit_height = w, h result.fit_width, result.fit_height = w, h
if self.dir == "h" then if self._private.dir == "h" then
if self.max_size then if self._private.max_size then
result.fit_width = math.min(w, self.max_size) result.fit_width = math.min(w, self._private.max_size)
end end
else else
if self.max_size then if self._private.max_size then
result.fit_height = math.min(h, self.max_size) result.fit_height = math.min(h, self._private.max_size)
end end
end end
if w > width or h > height then if w > width or h > height then
@ -109,17 +109,17 @@ local function calculate_info(self, context, width, height)
local x, y = 0, 0 local x, y = 0, 0
local function get_scroll_offset(size, visible_size) local function get_scroll_offset(size, visible_size)
return self.step_function(self.timer:elapsed(), size, visible_size, self.speed, self.extra_space) return self._private.step_function(self._private.timer:elapsed(), size, visible_size, self._private.speed, self._private.extra_space)
end end
if self.dir == "h" then if self._private.dir == "h" then
x = -get_scroll_offset(surface_width - extra, width) x = -get_scroll_offset(surface_width - extra, width)
else else
y = -get_scroll_offset(surface_height - extra, height) y = -get_scroll_offset(surface_height - extra, height)
end end
result.first_x, result.first_y = x, y result.first_x, result.first_y = x, y
-- Was the extra space already included elsewhere? -- Was the extra space already included elsewhere?
local extra_spacer = self.expand and 0 or self.extra_space local extra_spacer = self._private.expand and 0 or self._private.extra_space
if self.dir == "h" then if self._private.dir == "h" then
x = x + surface_width + extra_spacer x = x + surface_width + extra_spacer
else else
y = y + surface_height + extra_spacer y = y + surface_height + extra_spacer
@ -132,7 +132,7 @@ local function calculate_info(self, context, width, height)
-- Get the hierarchy and subscribe ourselves to updates -- Get the hierarchy and subscribe ourselves to updates
local hier, do_pending_updates, ctx = hierarchy_cache:get(context, local hier, do_pending_updates, ctx = hierarchy_cache:get(context,
self.widget, surface_width, surface_height) self._private.widget, surface_width, surface_height)
result.hierarchy = hier result.hierarchy = hier
result.context = ctx result.context = ctx
do_pending_updates(self) do_pending_updates(self)
@ -146,7 +146,7 @@ end
-- @param width The available width. -- @param width The available width.
-- @param height The available height. -- @param height The available height.
function scroll:draw(context, cr, width, height) function scroll:draw(context, cr, width, height)
if not self.widget then if not self._private.widget then
return return
end end
@ -175,7 +175,7 @@ end
-- @param width The available width. -- @param width The available width.
-- @param height The available height. -- @param height The available height.
function scroll:fit(context, width, height) function scroll:fit(context, width, height)
if not self.widget then if not self._private.widget then
return 0, 0 return 0, 0
end end
local info = calculate_info(self, context, width, height) local info = calculate_info(self, context, width, height)
@ -189,9 +189,9 @@ end
-- This function must be idempotent (calling it multiple times right after -- This function must be idempotent (calling it multiple times right after
-- another does not make a difference). -- another does not make a difference).
_need_scroll_redraw = function(self) _need_scroll_redraw = function(self)
if not self.paused and not self.scroll_timer then if not self._private.paused and not self._private.scroll_timer then
self.scroll_timer = timer.start_new(1 / self.fps, function() self._private.scroll_timer = timer.start_new(1 / self._private.fps, function()
self.scroll_timer = nil self._private.scroll_timer = nil
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end) end)
end end
@ -200,21 +200,21 @@ end
--- Pause the scrolling animation. --- Pause the scrolling animation.
-- @see continue -- @see continue
function scroll:pause() function scroll:pause()
if self.paused then if self._private.paused then
return return
end end
self.paused = true self._private.paused = true
self.timer:stop() self._private.timer:stop()
end end
--- Continue the scrolling animation. --- Continue the scrolling animation.
-- @see pause -- @see pause
function scroll:continue() function scroll:continue()
if not self.paused then if not self._private.paused then
return return
end end
self.paused = false self._private.paused = false
self.timer:continue() self._private.timer:continue()
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
@ -223,44 +223,50 @@ end
-- display the widget without any scrolling applied. -- display the widget without any scrolling applied.
-- This function does not undo the effect of @{pause}. -- This function does not undo the effect of @{pause}.
function scroll:reset_scrolling() function scroll:reset_scrolling()
self.timer:start() self._private.timer:start()
if self.paused then if self._private.paused then
self.timer:stop() self._private.timer:stop()
end end
end end
--- Set the direction in which this widget scroll. --- Set the direction in which this widget scroll.
-- @param dir Either "h" for horizontal scrolling or "v" for vertical scrolling -- @param dir Either "h" for horizontal scrolling or "v" for vertical scrolling
function scroll:set_direction(dir) function scroll:set_direction(dir)
if dir == self.dir then if dir == self._private.dir then
return return
end end
if dir ~= "h" and dir ~= "v" then if dir ~= "h" and dir ~= "v" then
error("Invalid direction, can only be 'h' or 'v'") error("Invalid direction, can only be 'h' or 'v'")
end end
self.dir = dir self._private.dir = dir
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
--- Set the widget which we scroll. --- The widget to be scrolled.
-- @tparam widget widget The widget that we should display -- @property widget
-- @tparam widget widget The widget
function scroll:set_widget(widget) function scroll:set_widget(widget)
if widget == self.widget then if widget == self._private.widget then
return return
end end
if widget then if widget then
base.check_widget(widget) base.check_widget(widget)
end end
self.widget = widget self._private.widget = widget
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
function scroll:get_widget()
return self._private.widget
end
--- Get the number of children element --- Get the number of children element
-- @treturn table The children -- @treturn table The children
function scroll:get_children() function scroll:get_children()
return {self.widget} return {self._private.widget}
end end
--- Replace the layout children --- Replace the layout children
@ -275,20 +281,20 @@ end
-- space. If false, the extra space is simply left empty. -- space. If false, the extra space is simply left empty.
-- @see set_extra_space -- @see set_extra_space
function scroll:set_expand(expand) function scroll:set_expand(expand)
if expand == self.expand then if expand == self._private.expand then
return return
end end
self.expand = expand self._private.expand = expand
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
--- Set the number of frames per second that this widget should draw. --- Set the number of frames per second that this widget should draw.
-- @tparam number fps The number of frames per second -- @tparam number fps The number of frames per second
function scroll:set_fps(fps) function scroll:set_fps(fps)
if fps == self.fps then if fps == self._private.fps then
return return
end end
self.fps = fps self._private.fps = fps
-- No signal needed: If we are scrolling, the next redraw will apply the new -- No signal needed: If we are scrolling, the next redraw will apply the new
-- FPS, else it obviously doesn't make a difference. -- FPS, else it obviously doesn't make a difference.
end end
@ -298,10 +304,10 @@ end
-- @tparam number extra_space The amount of extra space -- @tparam number extra_space The amount of extra space
-- @see set_expand -- @see set_expand
function scroll:set_extra_space(extra_space) function scroll:set_extra_space(extra_space)
if extra_space == self.extra_space then if extra_space == self._private.extra_space then
return return
end end
self.extra_space = extra_space self._private.extra_space = extra_space
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
@ -310,10 +316,10 @@ end
-- in pixels per second. -- in pixels per second.
-- @tparam number speed The speed for the animation -- @tparam number speed The speed for the animation
function scroll:set_speed(speed) function scroll:set_speed(speed)
if speed == self.speed then if speed == self._private.speed then
return return
end end
self.speed = speed self._private.speed = speed
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
@ -323,10 +329,10 @@ end
-- and the rest is made visible via scrolling. -- and the rest is made visible via scrolling.
-- @tparam number max_size The maximum size of this widget or nil for unlimited. -- @tparam number max_size The maximum size of this widget or nil for unlimited.
function scroll:set_max_size(max_size) function scroll:set_max_size(max_size)
if max_size == self.max_size then if max_size == self._private.max_size then
return return
end end
self.max_size = max_size self._private.max_size = max_size
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
@ -350,10 +356,10 @@ end
function scroll:set_step_function(step_function) function scroll:set_step_function(step_function)
-- Call the step functions once to see if it works -- Call the step functions once to see if it works
step_function(0, 42, 10, 10, 5) step_function(0, 42, 10, 10, 5)
if step_function == self.step_function then if step_function == self._private.step_function then
return return
end end
self.step_function = step_function self._private.step_function = step_function
self:emit_signal("widget::redraw_needed") self:emit_signal("widget::redraw_needed")
end end
@ -361,19 +367,19 @@ end
-- This restricts the child widget's maximal size. -- This restricts the child widget's maximal size.
-- @tparam number space_for_scrolling The space for scrolling -- @tparam number space_for_scrolling The space for scrolling
function scroll:set_space_for_scrolling(space_for_scrolling) function scroll:set_space_for_scrolling(space_for_scrolling)
if space_for_scrolling == self.space_for_scrolling then if space_for_scrolling == self._private.space_for_scrolling then
return return
end end
self.space_for_scrolling = space_for_scrolling self._private.space_for_scrolling = space_for_scrolling
self:emit_signal("widget::layout_changed") self:emit_signal("widget::layout_changed")
end end
local function get_layout(dir, widget, fps, speed, extra_space, expand, max_size, step_function, space_for_scrolling) local function get_layout(dir, widget, fps, speed, extra_space, expand, max_size, step_function, space_for_scrolling)
local ret = base.make_widget() local ret = base.make_widget(nil, nil, {enable_properties = true})
ret.paused = false ret._priavte.paused = false
ret.timer = GLib.Timer() ret._private.timer = GLib.Timer()
ret.scroll_timer = nil ret._private.scroll_timer = nil
setmetatable(ret, scroll_mt) setmetatable(ret, scroll_mt)
@ -504,6 +510,10 @@ function scroll.step_functions.waiting_nonlinear_back_and_forth(elapsed, size, v
return (size - visible_size) * state return (size - visible_size) * state
end end
--@DOC_widget_COMMON@
--@DOC_object_COMMON@
return scroll return scroll
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80