Skip to content

Commit

Permalink
always show rect
Browse files Browse the repository at this point in the history
  • Loading branch information
maxded committed Sep 18, 2024
1 parent 014327e commit 074638b
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions crates/egui/src/widgets/selected_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,36 @@ use crate::*;
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub struct SelectableLabel {
selected: bool,
always_show_rect: bool,
text: WidgetText,
}

impl SelectableLabel {
pub fn new(selected: bool, text: impl Into<WidgetText>) -> Self {
Self {
selected,
always_show_rect: false,
text: text.into(),
}
}

/// Always show the rect surrouding the label.
///
/// Default behavior is to only show the rect when the widget is
/// either selected, hovered, highlighted, or in focus.
pub fn always_show_rect(mut self) -> Self {
self.always_show_rect = true;
self
}
}

impl Widget for SelectableLabel {
fn ui(self, ui: &mut Ui) -> Response {
let Self { selected, text } = self;
let Self {
selected,
text,
always_show_rect,
} = self;

let button_padding = ui.spacing().button_padding;
let total_extra = button_padding + button_padding;
Expand All @@ -61,7 +76,12 @@ impl Widget for SelectableLabel {

let visuals = ui.style().interact_selectable(&response, selected);

if selected || response.hovered() || response.highlighted() || response.has_focus() {
if selected
|| response.hovered()
|| response.highlighted()
|| response.has_focus()
|| always_show_rect
{
let rect = rect.expand(visuals.expansion);

ui.painter().rect(
Expand Down

0 comments on commit 074638b

Please sign in to comment.