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_height The available height.
function fixed:fit(context, orig_width, orig_height)
local width, height = orig_width, orig_height
local used_in_dir, used_max = 0, 0
local width_left, height_left = orig_width, orig_height
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
local w, h = base.fit_widget(self, context, v, width, height)
local in_dir, max
if self._private.dir == "y" then
max, in_dir = w, h
height = height - in_dir
local w, h = base.fit_widget(self, context, v, width_left, height_left)
local max
if is_y then
max = w
height_left = height_left - h
else
in_dir, max = w, h
width = width - in_dir
max = h
width_left = width_left - w
end
if max > used_max then
used_max = max
end
used_in_dir = used_in_dir + in_dir
if width <= 0 or height <= 0 then
if self._private.dir == "y" then
used_in_dir = orig_height
if width_left <= 0 or height_left <= 0 then
if is_y then
height_left = 0
else
used_in_dir = orig_width
width_left = 0
end
break
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
return used_max, used_in_dir + spacing
if is_y then
return used_max, orig_height - height_left + total_spacing
end
return used_in_dir + spacing, used_max
return orig_width - width_left + total_spacing, used_max
end
function fixed:reset()