Button: fix crash on unpainted frame in feedback highlight (#16327)

This commit is contained in:
wuruofan authored and GitHub committed 2026-11-09 08:49:56 +02:00
1 parent a3ebf281fc
commit 739291bfbb
2 files changed
+41 -2

No files matched your search

+14 -2
View File
@@ -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()
+27
View File
@@ -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)