layout/fixed: Remove code duplication

The function has several expressions of the form
    if self._private.dir == "y" then
This patch stores the result of
    self._private.dir == "y"
to avoid code duplication.

Also remove the 'used_in_dir' and 'in_dir' variables since their values
can be calculated using other variables in the function and updating
them individually is error prone.

This patch doesn't do any functional changes.

Signed-off-by: Shay Agroskin <agrosshay@gmail.com>
This commit is contained in:
Shay Agroskin 2021-01-01 21:05:39 +02:00 committed by Shay Agroskin
parent aeb2d85dad
commit 749422100e
1 changed files with 28 additions and 18 deletions

View File

@ -261,40 +261,50 @@ end
-- @param orig_width The available width. -- @param orig_width The available width.
-- @param orig_height The available height. -- @param orig_height The available height.
function fixed:fit(context, orig_width, orig_height) function fixed:fit(context, orig_width, orig_height)
local width, height = orig_width, orig_height local width_left, height_left = orig_width, orig_height
local used_in_dir, used_max = 0, 0 local spacing = self._private.spacing or 0
local is_y = self._private.dir == "y"
local used_max = 0
-- when no widgets exist the function can be called with orig_width or
-- orig_height equal to nil. Exit early in this case.
if #self._private.widgets == 0 then
return 0, 0
end
for _, v in pairs(self._private.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_left, height_left)
local in_dir, max local max
if self._private.dir == "y" then
max, in_dir = w, h if is_y then
height = height - in_dir max = w
height_left = height_left - h
else else
in_dir, max = w, h max = h
width = width - in_dir width_left = width_left - w
end end
if max > used_max then if max > used_max then
used_max = max used_max = max
end end
used_in_dir = used_in_dir + in_dir
if width <= 0 or height <= 0 then if width_left <= 0 or height_left <= 0 then
if self._private.dir == "y" then if is_y then
used_in_dir = orig_height height_left = 0
else else
used_in_dir = orig_width width_left = 0
end end
break break
end end
end end
local spacing = self._private.spacing * (#self._private.widgets-1) local total_spacing = spacing * (#self._private.widgets-1)
if self._private.dir == "y" then if is_y then
return used_max, used_in_dir + spacing return used_max, orig_height - height_left + total_spacing
end end
return used_in_dir + spacing, used_max
return orig_width - width_left + total_spacing, used_max
end end
function fixed:reset() function fixed:reset()