From d8762eb245d3969713d269c037848b349af65ff3 Mon Sep 17 00:00:00 2001 From: Alex Belykh Date: Fri, 7 May 2021 02:59:36 +0700 Subject: [PATCH] Merge the draw() context into graph's draw_callback_options The context fields aren't mentioned in documentation, because their documentation should be linked instead of copied, but there's no documented table for it, no matter where I looked. --- lib/wibox/widget/graph.lua | 10 +++++----- spec/wibox/widget/graph_spec.lua | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/wibox/widget/graph.lua b/lib/wibox/widget/graph.lua index 02b49fa5e..92e036a4e 100644 --- a/lib/wibox/widget/graph.lua +++ b/lib/wibox/widget/graph.lua @@ -650,7 +650,7 @@ local function graph_choose_coordinate_system(self, scaling_values, drawn_values return min_value, max_value, baseline_y end -local function graph_draw_values(self, cr, width, height, drawn_values_num) +local function graph_draw_values(self, context, cr, width, height, drawn_values_num) local values = self._private.values local step_spacing = self._private.step_spacing or prop_fallbacks.step_spacing @@ -688,14 +688,14 @@ local function graph_draw_values(self, cr, width, height, drawn_values_num) -- @tfield number _height The height of the values draw area. -- @tfield number _group_idx Index of the currently drawn data group. -- @tfield wibox.widget.graph _graph The graph widget itself, explicitly named. - local options = { + local options = setmetatable({ _step_width = step_width, _step_spacing = step_spacing, _width = width, _height = height, _group_idx = nil, -- will be set later _graph = self, - } + }, {__index = context}) -- The user callback to call before drawing each data group local group_start = self._private.group_start @@ -799,7 +799,7 @@ local function graph_draw_values(self, cr, width, height, drawn_values_num) end end -function graph:draw(_, cr, width, height) +function graph:draw(context, cr, width, height) local border_width = self._private.border_width or prop_fallbacks.border_width local drawn_values_num = self:compute_drawn_values_num(width-2*border_width) @@ -822,7 +822,7 @@ function graph:draw(_, cr, width, height) local values_width = width - 2*border_width local values_height = height - 2*border_width - graph_draw_values(self, cr, values_width, values_height, drawn_values_num) + graph_draw_values(self, context, cr, values_width, values_height, drawn_values_num) -- Undo the cr:translate() for the border and step shapes cr:restore() diff --git a/spec/wibox/widget/graph_spec.lua b/spec/wibox/widget/graph_spec.lua index 3d1cb4e63..f85df2e5c 100644 --- a/spec/wibox/widget/graph_spec.lua +++ b/spec/wibox/widget/graph_spec.lua @@ -585,6 +585,8 @@ describe("wibox.widget.graph", function() -- Ensure it's integer. assert.is.equal(math.floor(options._group_idx), options._group_idx) assert.is.equal(widget, options._graph) + -- It also contains the draw context + assert.is.equal("fake context", options.fake_context) end describe("property step_hook()", function()