Enable panel switching
This commit is contained in:
parent
7f70712dac
commit
0a9fb3978b
15 changed files with 203 additions and 131 deletions
|
@ -61,7 +61,7 @@ impl ButtonVariant {
|
|||
}
|
||||
}
|
||||
|
||||
pub type ClickHandler<V> = Arc<dyn Fn(&mut V, &mut ViewContext<V>) + Send + Sync>;
|
||||
pub type ClickHandler<V> = Arc<dyn Fn(&mut V, &mut ViewContext<V>)>;
|
||||
|
||||
struct ButtonHandlers<V: 'static> {
|
||||
click: Option<ClickHandler<V>>,
|
||||
|
|
|
@ -25,6 +25,7 @@ pub enum Icon {
|
|||
ChevronRight,
|
||||
ChevronUp,
|
||||
Close,
|
||||
Collab,
|
||||
Dash,
|
||||
Exit,
|
||||
ExclamationTriangle,
|
||||
|
@ -83,6 +84,7 @@ impl Icon {
|
|||
Icon::ChevronRight => "icons/chevron_right.svg",
|
||||
Icon::ChevronUp => "icons/chevron_up.svg",
|
||||
Icon::Close => "icons/x.svg",
|
||||
Icon::Collab => "icons/user_group_16.svg",
|
||||
Icon::Dash => "icons/dash.svg",
|
||||
Icon::Exit => "icons/exit.svg",
|
||||
Icon::ExclamationTriangle => "icons/warning.svg",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{h_stack, prelude::*, ClickHandler, Icon, IconElement, TextTooltip};
|
||||
use gpui::{prelude::*, MouseButton, VisualContext};
|
||||
use crate::{h_stack, prelude::*, ClickHandler, Icon, IconElement};
|
||||
use gpui::{prelude::*, AnyView, MouseButton};
|
||||
use std::sync::Arc;
|
||||
|
||||
struct IconButtonHandlers<V: 'static> {
|
||||
|
@ -19,7 +19,7 @@ pub struct IconButton<V: 'static> {
|
|||
color: TextColor,
|
||||
variant: ButtonVariant,
|
||||
state: InteractionState,
|
||||
tooltip: Option<SharedString>,
|
||||
tooltip: Option<Box<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static>>,
|
||||
handlers: IconButtonHandlers<V>,
|
||||
}
|
||||
|
||||
|
@ -56,22 +56,23 @@ impl<V: 'static> IconButton<V> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn tooltip(mut self, tooltip: impl Into<SharedString>) -> Self {
|
||||
self.tooltip = Some(tooltip.into());
|
||||
pub fn tooltip(
|
||||
mut self,
|
||||
tooltip: impl Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static,
|
||||
) -> Self {
|
||||
self.tooltip = Some(Box::new(tooltip));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn on_click(
|
||||
mut self,
|
||||
handler: impl 'static + Fn(&mut V, &mut ViewContext<V>) + Send + Sync,
|
||||
) -> Self {
|
||||
pub fn on_click(mut self, handler: impl 'static + Fn(&mut V, &mut ViewContext<V>)) -> Self {
|
||||
self.handlers.click = Some(Arc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
fn render(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||
fn render(mut self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
|
||||
let icon_color = match (self.state, self.color) {
|
||||
(InteractionState::Disabled, _) => TextColor::Disabled,
|
||||
(InteractionState::Active, _) => TextColor::Error,
|
||||
_ => self.color,
|
||||
};
|
||||
|
||||
|
@ -99,15 +100,16 @@ impl<V: 'static> IconButton<V> {
|
|||
.child(IconElement::new(self.icon).color(icon_color));
|
||||
|
||||
if let Some(click_handler) = self.handlers.click.clone() {
|
||||
button = button.on_mouse_down(MouseButton::Left, move |state, event, cx| {
|
||||
cx.stop_propagation();
|
||||
click_handler(state, cx);
|
||||
});
|
||||
button = button
|
||||
.on_mouse_down(MouseButton::Left, move |state, event, cx| {
|
||||
cx.stop_propagation();
|
||||
click_handler(state, cx);
|
||||
})
|
||||
.cursor_pointer();
|
||||
}
|
||||
|
||||
if let Some(tooltip) = self.tooltip.clone() {
|
||||
button =
|
||||
button.tooltip(move |_, cx| cx.build_view(|cx| TextTooltip::new(tooltip.clone())));
|
||||
if let Some(tooltip) = self.tooltip.take() {
|
||||
button = button.tooltip(move |view: &mut V, cx| (tooltip)(view, cx))
|
||||
}
|
||||
|
||||
button
|
||||
|
|
|
@ -1,17 +1,53 @@
|
|||
use gpui::{Div, Render};
|
||||
use gpui::{Action, AnyView, Div, Render, VisualContext};
|
||||
use settings2::Settings;
|
||||
use theme2::{ActiveTheme, ThemeSettings};
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::{h_stack, v_stack, KeyBinding, Label, LabelSize, StyledExt, TextColor};
|
||||
|
||||
pub struct TextTooltip {
|
||||
pub struct Tooltip {
|
||||
title: SharedString,
|
||||
meta: Option<SharedString>,
|
||||
key_binding: Option<KeyBinding>,
|
||||
}
|
||||
|
||||
impl TextTooltip {
|
||||
impl Tooltip {
|
||||
pub fn text(title: impl Into<SharedString>, cx: &mut WindowContext) -> AnyView {
|
||||
cx.build_view(|cx| Self {
|
||||
title: title.into(),
|
||||
meta: None,
|
||||
key_binding: None,
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn for_action(
|
||||
title: impl Into<SharedString>,
|
||||
action: &dyn Action,
|
||||
cx: &mut WindowContext,
|
||||
) -> AnyView {
|
||||
cx.build_view(|cx| Self {
|
||||
title: title.into(),
|
||||
meta: None,
|
||||
key_binding: KeyBinding::for_action(action, cx),
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn with_meta(
|
||||
title: impl Into<SharedString>,
|
||||
action: Option<&dyn Action>,
|
||||
meta: impl Into<SharedString>,
|
||||
cx: &mut WindowContext,
|
||||
) -> AnyView {
|
||||
cx.build_view(|cx| Self {
|
||||
title: title.into(),
|
||||
meta: Some(meta.into()),
|
||||
key_binding: action.and_then(|action| KeyBinding::for_action(action, cx)),
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn new(title: impl Into<SharedString>) -> Self {
|
||||
Self {
|
||||
title: title.into(),
|
||||
|
@ -31,7 +67,7 @@ impl TextTooltip {
|
|||
}
|
||||
}
|
||||
|
||||
impl Render for TextTooltip {
|
||||
impl Render for Tooltip {
|
||||
type Element = Div<Self>;
|
||||
|
||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue