From 07f54deb0bf609eeba54a1ae43856626f4d8bf9d Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Sat, 28 May 2022 00:25:17 +0200 Subject: [PATCH] fix wrong `Label.frame_body.x2` value (#2134) * fix wrong `Label.frame_body.x2` value `update_scroll_inset` might change `frame_inset`, i.e. we need to `computeFrame` with the new values. * add tests for Label * add missing `local`, remove code in comments * move `TestFramedScreen` outside test functions --- library/lua/gui/widgets.lua | 12 +++- test/library/gui/widgets.Label.lua | 94 ++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 test/library/gui/widgets.Label.lua diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 21e03370b..c6e84d7a1 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -478,8 +478,16 @@ function Label:render_scroll_icons(dc, x, y1, y2) end end -function Label:postComputeFrame() - self:update_scroll_inset() +function Label:computeFrame(parent_rect) + local frame_rect,body_rect = Label.super.computeFrame(self, parent_rect) + + self.frame_rect = frame_rect + self.frame_body = parent_rect:viewport(body_rect or frame_rect) + + self:update_scroll_inset() -- frame_body is now set + + -- recalc with updated frame_inset + return Label.super.computeFrame(self, parent_rect) end function Label:preUpdateLayout() diff --git a/test/library/gui/widgets.Label.lua b/test/library/gui/widgets.Label.lua new file mode 100644 index 000000000..49a75a235 --- /dev/null +++ b/test/library/gui/widgets.Label.lua @@ -0,0 +1,94 @@ +-- test -dhack/scripts/devel/tests -twidgets.Label + +local gui = require('gui') +local widgets = require('gui.widgets') + +local xtest = {} -- use to temporarily disable tests (change `function xtest.somename` to `function xxtest.somename`) +local wait = function(n) + delay(n or 30) -- enable for debugging the tests +end + +local fs = defclass(fs, gui.FramedScreen) +fs.ATTRS = { + frame_style = gui.GREY_LINE_FRAME, + frame_title = 'TestFramedScreen', + frame_width = 10, + frame_height = 10, + frame_inset = 0, + focus_path = 'test-framed-screen', +} + +function test.Label_correct_frame_body_with_scroll_icons() + local t = {} + for i = 1, 12 do + t[#t+1] = tostring(i) + t[#t+1] = NEWLINE + end + + function fs:init(args) + self:addviews{ + widgets.Label{ + view_id = 'text', + frame_inset = 0, + text = t, + --show_scroll_icons = 'right', + }, + } + end + + local o = fs{} + --o:show() + --wait() + expect.eq(o.subviews.text.frame_body.width, 9, "Label's frame_body.x2 and .width should be one smaller because of show_scroll_icons.") + --o:dismiss() +end + +function test.Label_correct_frame_body_with_few_text_lines() + local t = {} + for i = 1, 10 do + t[#t+1] = tostring(i) + t[#t+1] = NEWLINE + end + + function fs:init(args) + self:addviews{ + widgets.Label{ + view_id = 'text', + frame_inset = 0, + text = t, + --show_scroll_icons = 'right', + }, + } + end + + local o = fs{} + --o:show() + --wait() + expect.eq(o.subviews.text.frame_body.width, 10, "Label's frame_body.x2 and .width should not change with show_scroll_icons = false.") + --o:dismiss() +end + +function test.Label_correct_frame_body_without_show_scroll_icons() + local t = {} + for i = 1, 12 do + t[#t+1] = tostring(i) + t[#t+1] = NEWLINE + end + + function fs:init(args) + self:addviews{ + widgets.Label{ + view_id = 'text', + frame_inset = 0, + text = t, + show_scroll_icons = false, + }, + } + end + + local o = fs{} + --o:show() + --wait() + expect.eq(o.subviews.text.frame_body.width, 10, "Label's frame_body.x2 and .width should not change with show_scroll_icons = false.") + --o:dismiss() +end