@@ -1,6 +1,8 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use gpui::{AnyView, ClickEvent};
|
||||
|
||||
use crate::{ButtonLike, ButtonLikeRounding, ElevationIndex, TintColor, prelude::*};
|
||||
use crate::{ButtonLike, ButtonLikeRounding, ElevationIndex, TintColor, Tooltip, prelude::*};
|
||||
|
||||
/// The position of a [`ToggleButton`] within a group of buttons.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
@@ -301,6 +303,7 @@ pub struct ButtonConfiguration {
|
||||
icon: Option<IconName>,
|
||||
on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
|
||||
selected: bool,
|
||||
tooltip: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyView>>,
|
||||
}
|
||||
|
||||
mod private {
|
||||
@@ -315,6 +318,7 @@ pub struct ToggleButtonSimple {
|
||||
label: SharedString,
|
||||
on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
|
||||
selected: bool,
|
||||
tooltip: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyView>>,
|
||||
}
|
||||
|
||||
impl ToggleButtonSimple {
|
||||
@@ -326,6 +330,7 @@ impl ToggleButtonSimple {
|
||||
label: label.into(),
|
||||
on_click: Box::new(on_click),
|
||||
selected: false,
|
||||
tooltip: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,6 +338,11 @@ impl ToggleButtonSimple {
|
||||
self.selected = selected;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
|
||||
self.tooltip = Some(Rc::new(tooltip));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl private::ToggleButtonStyle for ToggleButtonSimple {}
|
||||
@@ -344,6 +354,7 @@ impl ButtonBuilder for ToggleButtonSimple {
|
||||
icon: None,
|
||||
on_click: self.on_click,
|
||||
selected: self.selected,
|
||||
tooltip: self.tooltip,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -353,6 +364,7 @@ pub struct ToggleButtonWithIcon {
|
||||
icon: IconName,
|
||||
on_click: Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>,
|
||||
selected: bool,
|
||||
tooltip: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyView>>,
|
||||
}
|
||||
|
||||
impl ToggleButtonWithIcon {
|
||||
@@ -366,6 +378,7 @@ impl ToggleButtonWithIcon {
|
||||
icon,
|
||||
on_click: Box::new(on_click),
|
||||
selected: false,
|
||||
tooltip: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -373,6 +386,11 @@ impl ToggleButtonWithIcon {
|
||||
self.selected = selected;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn tooltip(mut self, tooltip: impl Fn(&mut Window, &mut App) -> AnyView + 'static) -> Self {
|
||||
self.tooltip = Some(Rc::new(tooltip));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl private::ToggleButtonStyle for ToggleButtonWithIcon {}
|
||||
@@ -384,6 +402,7 @@ impl ButtonBuilder for ToggleButtonWithIcon {
|
||||
icon: Some(self.icon),
|
||||
on_click: self.on_click,
|
||||
selected: self.selected,
|
||||
tooltip: self.tooltip,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -486,11 +505,13 @@ impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> RenderOnce
|
||||
icon,
|
||||
on_click,
|
||||
selected,
|
||||
tooltip,
|
||||
} = button.into_configuration();
|
||||
|
||||
let entry_index = row_index * COLS + col_index;
|
||||
|
||||
ButtonLike::new((self.group_name, entry_index))
|
||||
.rounding(None)
|
||||
.when_some(self.tab_index, |this, tab_index| {
|
||||
this.tab_index(tab_index + entry_index as isize)
|
||||
})
|
||||
@@ -498,7 +519,6 @@ impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> RenderOnce
|
||||
this.toggle_state(true)
|
||||
.selected_style(ButtonStyle::Tinted(TintColor::Accent))
|
||||
})
|
||||
.rounding(None)
|
||||
.when(self.style == ToggleButtonGroupStyle::Filled, |button| {
|
||||
button.style(ButtonStyle::Filled)
|
||||
})
|
||||
@@ -527,6 +547,9 @@ impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> RenderOnce
|
||||
|this| this.color(Color::Accent),
|
||||
)),
|
||||
)
|
||||
.when_some(tooltip, |this, tooltip| {
|
||||
this.tooltip(move |window, cx| tooltip(window, cx))
|
||||
})
|
||||
.on_click(on_click)
|
||||
.into_any_element()
|
||||
})
|
||||
@@ -920,6 +943,23 @@ impl<T: ButtonBuilder, const COLS: usize, const ROWS: usize> Component
|
||||
),
|
||||
],
|
||||
)])
|
||||
.children(vec![single_example(
|
||||
"With Tooltips",
|
||||
ToggleButtonGroup::single_row(
|
||||
"with_tooltips",
|
||||
[
|
||||
ToggleButtonSimple::new("First", |_, _, _| {})
|
||||
.tooltip(Tooltip::text("This is a tooltip. Hello!")),
|
||||
ToggleButtonSimple::new("Second", |_, _, _| {})
|
||||
.tooltip(Tooltip::text("This is a tooltip. Hey?")),
|
||||
ToggleButtonSimple::new("Third", |_, _, _| {})
|
||||
.tooltip(Tooltip::text("This is a tooltip. Get out of here now!")),
|
||||
],
|
||||
)
|
||||
.selected_index(1)
|
||||
.button_width(rems_from_px(100.))
|
||||
.into_any_element(),
|
||||
)])
|
||||
.into_any_element(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user