From 1ebc34b5e9c31b82013ed7ef2c59716d31333267 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 8 Aug 2015 13:43:35 +0200 Subject: [PATCH] Widgets: Also add a context argument to :fit() Together with the context argument to :draw(), this even allows widgets to have DPI-dependant size. Signed-off-by: Uli Schlachter --- lib/awful/tooltip.lua | 2 +- lib/awful/widget/graph.lua | 2 +- lib/awful/widget/progressbar.lua | 2 +- lib/naughty/core.lua | 6 +++--- lib/wibox/layout/align.lua | 19 ++++++++++--------- lib/wibox/layout/base.lua | 5 +++-- lib/wibox/layout/constraint.lua | 4 ++-- lib/wibox/layout/fixed.lua | 9 +++++---- lib/wibox/layout/flex.lua | 5 +++-- lib/wibox/layout/margin.lua | 4 ++-- lib/wibox/layout/mirror.lua | 4 ++-- lib/wibox/layout/rotate.lua | 4 ++-- lib/wibox/widget/background.lua | 4 ++-- lib/wibox/widget/base.lua | 2 +- lib/wibox/widget/imagebox.lua | 2 +- lib/wibox/widget/systray.lua | 2 +- lib/wibox/widget/textbox.lua | 2 +- spec/wibox/test_utils.lua | 2 +- 18 files changed, 42 insertions(+), 38 deletions(-) diff --git a/lib/awful/tooltip.lua b/lib/awful/tooltip.lua index 2f4b12483..a308f0e49 100644 --- a/lib/awful/tooltip.lua +++ b/lib/awful/tooltip.lua @@ -83,7 +83,7 @@ end local function set_geometry(self) local my_geo = self.wibox:geometry() -- calculate width / height - local n_w, n_h = self.textbox:fit(-1, -1) + local n_w, n_h = self.textbox:fit(nil, -1, -1) -- Hack! :( n_w = n_w + self.marginbox.left + self.marginbox.right n_h = n_h + self.marginbox.top + self.marginbox.bottom if my_geo.width ~= n_w or my_geo.height ~= n_h then diff --git a/lib/awful/widget/graph.lua b/lib/awful/widget/graph.lua index a1252fadd..b5b4309c6 100644 --- a/lib/awful/widget/graph.lua +++ b/lib/awful/widget/graph.lua @@ -158,7 +158,7 @@ function graph.draw(_graph, context, cr, width, height) end end -function graph.fit(_graph, width, height) +function graph.fit(_graph) return data[_graph].width, data[_graph].height end diff --git a/lib/awful/widget/progressbar.lua b/lib/awful/widget/progressbar.lua index 1e92326ab..08d679238 100644 --- a/lib/awful/widget/progressbar.lua +++ b/lib/awful/widget/progressbar.lua @@ -154,7 +154,7 @@ function progressbar.draw(pbar, context, cr, width, height) end end -function progressbar.fit(pbar, width, height) +function progressbar.fit(pbar) return data[pbar].width, data[pbar].height end diff --git a/lib/naughty/core.lua b/lib/naughty/core.lua index bc6579f38..14d1a6b1b 100644 --- a/lib/naughty/core.lua +++ b/lib/naughty/core.lua @@ -526,7 +526,7 @@ function naughty.notify(args) actiontextbox:set_font(font) actiontextbox:set_markup(string.format('%s', action)) -- calculate the height and width - local w, h = actiontextbox:fit(-1, -1) + local w, h = actiontextbox:fit(nil, -1, -1) -- Hack! :( local height = h + 2 * margin local width = w + 2 * margin @@ -596,7 +596,7 @@ function naughty.notify(args) -- calculate the height if not height then - local w, h = textbox:fit(-1, -1) + local w, h = textbox:fit(nil, -1, -1) -- Hack! :-( if iconbox and icon_h + 2 * margin > h + 2 * margin then height = icon_h + 2 * margin else @@ -608,7 +608,7 @@ function naughty.notify(args) -- calculate the width if not width then - local w, h = textbox:fit(-1, -1) + local w, h = textbox:fit(nil, -1, -1) -- Hack! :-( width = w + (iconbox and icon_w + 2 * margin or 0) + 2 * margin end diff --git a/lib/wibox/layout/align.lua b/lib/wibox/layout/align.lua index 3b6d4431d..1a048b854 100644 --- a/lib/wibox/layout/align.lua +++ b/lib/wibox/layout/align.lua @@ -37,7 +37,7 @@ function align:draw(context, cr, width, height) -- if the second widget doesn't exist, we will prioritise the first one -- instead if self._expand ~= "inside" and self.second then - local w, h = base.fit_widget(self.second, width, height) + local w, h = base.fit_widget(context, self.second, width, height) local size_second = self.dir == "y" and h or w -- if all the space is taken, skip the rest, and draw just the middle -- widget @@ -57,7 +57,7 @@ function align:draw(context, cr, width, height) -- into the remaining space if self._expand ~= "outside" then if self.dir == "y" then - _, h = base.fit_widget(self.first, width, size_remains) + _, h = base.fit_widget(context, self.first, width, size_remains) size_first = h -- for "inside", the third widget will get a chance to use the -- remaining space, then the middle widget. For "none" we give @@ -68,7 +68,7 @@ function align:draw(context, cr, width, height) size_remains = size_remains - h end else - w, _ = base.fit_widget(self.first, size_remains, height) + w, _ = base.fit_widget(context, self.first, size_remains, height) size_first = w if self._expand == "inside" or not self.second then size_remains = size_remains - w @@ -88,13 +88,13 @@ function align:draw(context, cr, width, height) local w, h, _ = width, height, nil if self._expand ~= "outside" then if self.dir == "y" then - _, h = base.fit_widget(self.third, width, size_remains) + _, h = base.fit_widget(context, self.third, width, size_remains) -- give the middle widget the rest of the space for "inside" mode if self._expand == "inside" then size_remains = size_remains - h end else - w, _ = base.fit_widget(self.third, size_remains, height) + w, _ = base.fit_widget(context, self.third, size_remains, height) if self._expand == "inside" then size_remains = size_remains - w end @@ -123,10 +123,10 @@ function align:draw(context, cr, width, height) end else if self.dir == "y" then - _, h = base.fit_widget(self.second, width, size_remains) + _, h = base.fit_widget(context, self.second, width, size_remains) y = floor( (height - h)/2 ) else - w, _ = base.fit_widget(self.second, width, size_remains) + w, _ = base.fit_widget(context, self.second, width, size_remains) x = floor( (width -w)/2 ) end end @@ -166,14 +166,15 @@ end --- Fit the align layout into the given space. The align layout will -- ask for the sum of the sizes of its sub-widgets in its direction -- and the largest sized sub widget in the other direction. +-- @param context The context in which we are fit. -- @param orig_width The available width. -- @param orig_height The available height. -function align:fit(orig_width, orig_height) +function align:fit(context, orig_width, orig_height) local used_in_dir = 0 local used_in_other = 0 for k, v in pairs{self.first, self.second, self.third} do - local w, h = base.fit_widget(v, orig_width, orig_height) + local w, h = base.fit_widget(context, v, orig_width, orig_height) local max = self.dir == "y" and w or h if max > used_in_other then diff --git a/lib/wibox/layout/base.lua b/lib/wibox/layout/base.lua index 117cafd7f..8af64702e 100644 --- a/lib/wibox/layout/base.lua +++ b/lib/wibox/layout/base.lua @@ -29,11 +29,12 @@ function base.rect_to_device_geometry(cr, x, y, width, height) end --- Fit a widget for the given available width and height +-- @param context The context in which we are fit. -- @param widget The widget to fit (this uses widget:fit(width, height)). -- @param width The available width for the widget -- @param height The available height for the widget -- @return The width and height that the widget wants to use -function base.fit_widget(widget, width, height) +function base.fit_widget(context, widget, width, height) if not widget.visible then return 0, 0 end @@ -41,7 +42,7 @@ function base.fit_widget(widget, width, height) local width = math.max(0, width) local height = math.max(0, height) - return widget._fit_geometry_cache:get(width, height) + return widget._fit_geometry_cache:get(context, width, height) end --- Draw a widget via a cairo context diff --git a/lib/wibox/layout/constraint.lua b/lib/wibox/layout/constraint.lua index b4e0e3770..5a0c975c7 100644 --- a/lib/wibox/layout/constraint.lua +++ b/lib/wibox/layout/constraint.lua @@ -24,13 +24,13 @@ function constraint:draw(context, cr, width, height) end --- Fit a constraint layout into the given space -function constraint:fit(width, height) +function constraint:fit(context, width, height) local w, h if self.widget then w = self._strategy(width, self._width) h = self._strategy(height, self._height) - w, h = base.fit_widget(self.widget, w, h) + w, h = base.fit_widget(context, self.widget, w, h) else w, h = 0, 0 end diff --git a/lib/wibox/layout/fixed.lua b/lib/wibox/layout/fixed.lua index a5cce63be..1a2ea1dbb 100644 --- a/lib/wibox/layout/fixed.lua +++ b/lib/wibox/layout/fixed.lua @@ -28,7 +28,7 @@ function fixed:draw(context, cr, width, height) x, y = 0, pos w, h = width, height - pos if k ~= #self.widgets or not self._fill_space then - _, h = base.fit_widget(v, w, h); + _, h = base.fit_widget(context, v, w, h); end pos = pos + h + spacing in_dir = h @@ -36,7 +36,7 @@ function fixed:draw(context, cr, width, height) x, y = pos, 0 w, h = width - pos, height if k ~= #self.widgets or not self._fill_space then - w, _ = base.fit_widget(v, w, h); + w, _ = base.fit_widget(context, v, w, h); end pos = pos + w + spacing in_dir = w @@ -59,14 +59,15 @@ function fixed:add(widget) end --- Fit the fixed layout into the given space +-- @param context The context in which we are fit. -- @param orig_width The available width. -- @param orig_height The available height. -function fixed:fit(orig_width, orig_height) +function fixed:fit(context, orig_width, orig_height) local width, height = orig_width, orig_height local used_in_dir, used_max = 0, 0 for k, v in pairs(self.widgets) do - local w, h = base.fit_widget(v, width, height) + local w, h = base.fit_widget(context, v, width, height) local in_dir, max if self.dir == "y" then max, in_dir = w, h diff --git a/lib/wibox/layout/flex.lua b/lib/wibox/layout/flex.lua index a24af0be4..0b47ff784 100644 --- a/lib/wibox/layout/flex.lua +++ b/lib/wibox/layout/flex.lua @@ -75,9 +75,10 @@ function flex:set_max_widget_size(val) end --- Fit the flex layout into the given space. +-- @param context The context in which we are fit. -- @param orig_width The available width. -- @param orig_height The available height. -function flex:fit(orig_width, orig_height) +function flex:fit(context, orig_width, orig_height) local used_in_dir = 0 local used_in_other = 0 @@ -86,7 +87,7 @@ function flex:fit(orig_width, orig_height) local sub_width = self.dir == "y" and orig_width or floor(orig_width / #self.widgets) for k, v in pairs(self.widgets) do - local w, h = base.fit_widget(v, sub_width, sub_height) + local w, h = base.fit_widget(context, v, sub_width, sub_height) local max = self.dir == "y" and w or h if max > used_in_other then diff --git a/lib/wibox/layout/margin.lua b/lib/wibox/layout/margin.lua index 7e164b50d..0fc2117e2 100644 --- a/lib/wibox/layout/margin.lua +++ b/lib/wibox/layout/margin.lua @@ -41,12 +41,12 @@ function margin:draw(context, cr, width, height) end --- Fit a margin layout into the given space -function margin:fit(width, height) +function margin:fit(context, width, height) local extra_w = self.left + self.right local extra_h = self.top + self.bottom local w, h = 0, 0 if self.widget then - w, h = base.fit_widget(self.widget, width - extra_w, height - extra_h) + w, h = base.fit_widget(context, self.widget, width - extra_w, height - extra_h) end return w + extra_w, h + extra_h end diff --git a/lib/wibox/layout/mirror.lua b/lib/wibox/layout/mirror.lua index cace8c0bd..b03924095 100644 --- a/lib/wibox/layout/mirror.lua +++ b/lib/wibox/layout/mirror.lua @@ -45,11 +45,11 @@ function mirror:draw(context, cr, width, height) end --- Fit this layout into the given area -function mirror:fit(...) +function mirror:fit(context, ...) if not self.widget then return 0, 0 end - return base.fit_widget(self.widget, ...) + return base.fit_widget(context, self.widget, ...) end --- Set the widget that this layout mirrors. diff --git a/lib/wibox/layout/rotate.lua b/lib/wibox/layout/rotate.lua index 3e687525b..cf929c096 100644 --- a/lib/wibox/layout/rotate.lua +++ b/lib/wibox/layout/rotate.lua @@ -49,11 +49,11 @@ function rotate:draw(context, cr, width, height) end --- Fit this layout into the given area -function rotate:fit(width, height) +function rotate:fit(context, width, height) if not self.widget then return 0, 0 end - return transform(self, base.fit_widget(self.widget, transform(self, width, height))) + return transform(self, base.fit_widget(context, self.widget, transform(self, width, height))) end --- Set the widget that this layout rotates. diff --git a/lib/wibox/widget/background.lua b/lib/wibox/widget/background.lua index 940dff20d..5847cc3eb 100644 --- a/lib/wibox/widget/background.lua +++ b/lib/wibox/widget/background.lua @@ -47,12 +47,12 @@ function background:draw(context, cr, width, height) end --- Fit this widget into the given area -function background:fit(width, height) +function background:fit(context, width, height) if not self.widget then return 0, 0 end - return self.widget:fit(width, height) + return layout_base.fit_widget(context, self.widget, width, height) end --- Set the widget that is drawn on top of the background diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index f57c01f56..ef562d779 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -142,7 +142,7 @@ function base.check_widget(widget) debug.assert(type(widget[func]) == "function", func .. " is not a function") end - local width, height = widget:fit(0, 0) + local width, height = widget:fit({}, 0, 0) debug.assert(type(width) == "number") debug.assert(type(height) == "number") end diff --git a/lib/wibox/widget/imagebox.lua b/lib/wibox/widget/imagebox.lua index 24a03cc94..e623525db 100644 --- a/lib/wibox/widget/imagebox.lua +++ b/lib/wibox/widget/imagebox.lua @@ -39,7 +39,7 @@ function imagebox:draw(context, cr, width, height) end --- Fit the imagebox into the given geometry -function imagebox:fit(width, height) +function imagebox:fit(context, width, height) if not self._image then return 0, 0 end diff --git a/lib/wibox/widget/systray.lua b/lib/wibox/widget/systray.lua index 1c692b05c..25db97d8b 100644 --- a/lib/wibox/widget/systray.lua +++ b/lib/wibox/widget/systray.lua @@ -45,7 +45,7 @@ function systray:draw(context, cr, width, height) base, is_rotated, bg, reverse, spacing) end -function systray:fit(width, height) +function systray:fit(context, width, height) local num_entries = capi.awesome.systray() local base = base_size local spacing = beautiful.systray_icon_spacing or 0 diff --git a/lib/wibox/widget/textbox.lua b/lib/wibox/widget/textbox.lua index 6b99b16e2..a5cb35301 100644 --- a/lib/wibox/widget/textbox.lua +++ b/lib/wibox/widget/textbox.lua @@ -42,7 +42,7 @@ function textbox:draw(context, cr, width, height) end --- Fit the given textbox -function textbox:fit(width, height) +function textbox:fit(context, width, height) setup_layout(self, width, height) local ink, logical = self._layout:get_pixel_extents() diff --git a/spec/wibox/test_utils.lua b/spec/wibox/test_utils.lua index 01879cca4..d993baf75 100644 --- a/spec/wibox/test_utils.lua +++ b/spec/wibox/test_utils.lua @@ -35,7 +35,7 @@ local function widget_fit(state, arguments) local widget = arguments[1] local given = arguments[2] local expected = arguments[3] - local w, h = widget:fit(given[1], given[2]) + local w, h = widget:fit({ "fake context" }, given[1], given[2]) local fits = expected[1] == w and expected[2] == h if state.mod == fits then