From 697f15224c8b3f421bcbf5a667a00bff47d88363 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Sat, 18 Feb 2023 16:06:03 -0800 Subject: [PATCH] Address PR comments, and remove BG fill BG fill eats up a lot of cycles anyway, and there's not a real tangible benefit in all cases, as it relies on the text label being sized appropriately (width-wise) to the container, or would otherwise require padding. --- docs/dev/Lua API.rst | 16 +++++++++++++++- library/lua/gui/widgets.lua | 17 ++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 02260513f..43c9d0cb0 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4662,7 +4662,9 @@ It has the following attributes: :text_pen: Specifies the pen for active text. :text_dpen: Specifies the pen for disabled text. -:text_hpen: Specifies the pen for text hovered over by the mouse, if a click handler is registered. +:text_hpen: Specifies the pen for text hovered over by the mouse, if a click + handler is registered. By default, this will invert the foreground + and background colors. :disabled: Boolean or a callback; if true, the label is disabled. :enabled: Boolean or a callback; if false, the label is disabled. :auto_height: Sets self.frame.h from the text height. @@ -4769,6 +4771,18 @@ The Label widget implements the following methods: ``+halfpage``, ``-halfpage``, ``home``, or ``end``. It returns the number of lines that were actually scrolled (negative for scrolling up). +* ``label:shouldHover()`` + + This method returns whether or not this widget should show a hover effect, + generally you want to return ``true`` if there is some type of mouse handler + present. For example, for a ``HotKeyLabel``:: + + function HotkeyLabel:shouldHover() + -- When on_activate is set, text should also hover on mouseover + return HotkeyLabel.super.shouldHover(self) or self.on_activate + end + + WrappedLabel class ------------------ diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 142e928d3..6a1726115 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1232,7 +1232,7 @@ Label = defclass(Label, Widget) Label.ATTRS{ text_pen = COLOR_WHITE, text_dpen = COLOR_DARKGREY, -- disabled - text_hpen = DEFAULT_NIL, -- highlight - default is text_pen with reversed brightness + text_hpen = DEFAULT_NIL, -- hover - default is to invert the fg/bg colors disabled = DEFAULT_NIL, enabled = DEFAULT_NIL, auto_height = true, @@ -1250,8 +1250,6 @@ function Label:init(args) self:addviews{self.scrollbar} self:setText(args.text or self.text) - - -- self.text_hpen = make_hpen(self.text_pen, self.text_hpen) end local function update_label_scrollbar(label) @@ -1305,15 +1303,6 @@ function Label:shouldHover() return self.on_click or self.on_rclick end -function Label:onRenderFrame(dc, rect) - Label.super.onRenderFrame(self, dc, rect) - -- Fill the background with text_hpen on hover - if self:getMousePos() and self:shouldHover() then - local hpen = make_hpen(self.text_pen, self.text_hpen) - dc:fill(rect, hpen) - end -end - function Label:onRenderBody(dc) local text_pen = self.text_pen local hovered = self:getMousePos() and self:shouldHover() @@ -1614,7 +1603,7 @@ List = defclass(List, Widget) List.ATTRS{ text_pen = COLOR_CYAN, - text_hpen = DEFAULT_NIL, -- pen to render list item when mouse is hovered over; defaults to text_pen with inverted brightness + text_hpen = DEFAULT_NIL, -- hover color, defaults to inverting the FG/BG pens for each text object cursor_pen = COLOR_LIGHTCYAN, inactive_pen = DEFAULT_NIL, on_select = DEFAULT_NIL, @@ -1644,8 +1633,6 @@ function List:init(info) end self.last_select_click_ms = 0 -- used to track double-clicking on an item - - -- self.text_hpen = make_hpen(self.text_pen, self.text_hpen) end function List:setChoices(choices, selected)