From 739291bfbb52b23e430cf1ff92b3a0e2e16d3e6a Mon Sep 17 00:00:00 2001 From: wuruofan Date: Mon, 9 Nov 2026 14:49:56 +0800 Subject: [PATCH] Button: fix crash on unpainted frame in feedback highlight (#16327) --- frontend/ui/widget/button.lua | 16 ++++++++++++++-- spec/unit/button_spec.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/frontend/ui/widget/button.lua b/frontend/ui/widget/button.lua index 68e5c2cc3..bdb53fc94 100644 --- a/frontend/ui/widget/button.lua +++ b/frontend/ui/widget/button.lua @@ -430,10 +430,16 @@ end -- Used by onTapSelectButton to handle visual feedback when flash_ui is enabled function Button:_doFeedbackHighlight() -- NOTE: self[1] -> self.frame, if you're confused about what this does vs. onFocus/onUnfocus ;). + -- The frame's geometry is only known after the first paint. + if not (self[1] and self[1].dimen) then + logger.dbg("Button:", tostring(self), "attempted a feedback highlight in an unpainted frame!") + return + end if self.text then -- We only want the button's *highlight* to have rounded corners (otherwise they're redundant, same color as the bg). -- The nil check is to discriminate the default from callers that explicitly request a specific radius. - if self[1].radius == nil or self.background then + -- The frame may have no background: fall back to the invert flag. + if (self[1].radius == nil or self.background) and self[1].background then self[1].radius = Size.radius.button -- And here, it's easier to just invert the bg/fg colors ourselves, -- so as to preserve the rounded corners in one step. @@ -455,8 +461,14 @@ function Button:_doFeedbackHighlight() end function Button:_undoFeedbackHighlight(is_translucent) + -- Same as in _doFeedbackHighlight(). + if not (self[1] and self[1].dimen) then + logger.dbg("Button:", tostring(self), "attempted a feedback unhighlight in an unpainted frame!") + return + end if self.text then - if self[1].radius == Size.radius.button then + -- Only invert back what was inverted. + if self[1].radius == Size.radius.button and self[1].background then self[1].radius = nil self[1].background = self[1].background:invert() self.label_widget.fgcolor = self.label_widget.fgcolor:invert() diff --git a/spec/unit/button_spec.lua b/spec/unit/button_spec.lua index a0dab5cca..c1c3cd2d3 100644 --- a/spec/unit/button_spec.lua +++ b/spec/unit/button_spec.lua @@ -2,12 +2,14 @@ describe("Button widget", function() local Button local ButtonTable local Dbg + local Screen setup(function() require("commonrequire") Button = require("ui/widget/button") ButtonTable = require("ui/widget/buttontable") Dbg = require("dbg") + Screen = require("device").screen end) it("should register a Shortcut key_event for a string key_bindings", function() @@ -70,4 +72,29 @@ describe("Button widget", function() end) Dbg:turnOff() end) + + it("should not crash on a feedback highlight in an unpainted frame", function() + local btn = Button:new{ text = "Test" } + -- The frame's geometry is only known after the first paint, but an + -- unpainted button can still be tapped (see #16288). + assert.has_no.errors(function() + btn:_doFeedbackHighlight() + btn:_undoFeedbackHighlight() + end) + -- Nothing was painted, so nothing should have been inverted either. + assert.is_falsy(btn[1].invert) + end) + + -- The frame's background can be missing entirely (a hidden button, or a + -- Button widget patched by a plugin): inverting a nil background crashed KOReader. + it("should fall back to the invert flag when the frame has no background", function() + local btn = Button:new{ text = "Test" } + -- Paint the button once, so that the frame knows its geometry. + btn:paintTo(Screen.bb, 0, 0) + btn[1].background = nil + assert.has_no.errors(function() btn:_doFeedbackHighlight() end) + assert.is_true(btn[1].invert) + assert.has_no.errors(function() btn:_undoFeedbackHighlight(false) end) + assert.is_false(btn[1].invert) + end) end)