WIP: Add disclosable channels

This commit is contained in:
Mikayla 2023-08-18 14:54:05 -07:00
parent 29c339e3b4
commit 3178adefde
No known key found for this signature in database
5 changed files with 136 additions and 36 deletions

View File

@ -8,6 +8,7 @@ use client::{
proto::PeerId, Channel, ChannelEvent, ChannelId, ChannelStore, Client, Contact, User, UserStore, proto::PeerId, Channel, ChannelEvent, ChannelId, ChannelStore, Client, Contact, User, UserStore,
}; };
use components::DisclosureExt;
use context_menu::{ContextMenu, ContextMenuItem}; use context_menu::{ContextMenu, ContextMenuItem};
use db::kvp::KEY_VALUE_STORE; use db::kvp::KEY_VALUE_STORE;
use editor::{Cancel, Editor}; use editor::{Cancel, Editor};
@ -16,7 +17,7 @@ use fuzzy::{match_strings, StringMatchCandidate};
use gpui::{ use gpui::{
actions, actions,
elements::{ elements::{
Canvas, ChildView, Empty, Flex, Image, Label, List, ListOffset, ListState, Canvas, ChildView, Component, Empty, Flex, Image, Label, List, ListOffset, ListState,
MouseEventHandler, Orientation, OverlayPositionMode, Padding, ParentElement, Stack, Svg, MouseEventHandler, Orientation, OverlayPositionMode, Padding, ParentElement, Stack, Svg,
}, },
geometry::{ geometry::{
@ -1615,6 +1616,10 @@ impl CollabPanel {
this.deploy_channel_context_menu(Some(e.position), channel_id, cx); this.deploy_channel_context_menu(Some(e.position), channel_id, cx);
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.component()
.styleable()
.disclosable()
.into_element()
.into_any() .into_any()
} }
@ -2522,3 +2527,87 @@ fn render_icon_button(style: &IconButton, svg_path: &'static str) -> impl Elemen
.contained() .contained()
.with_style(style.container) .with_style(style.container)
} }
mod components {
use gpui::{
elements::{Empty, Flex, GeneralComponent, ParentElement, StyleableComponent},
Action, Element,
};
use theme::components::{
action_button::ActionButton, svg::Svg, ComponentExt, ToggleIconButtonStyle,
};
#[derive(Clone)]
struct DisclosureStyle<S> {
disclosure: ToggleIconButtonStyle,
spacing: f32,
content: S,
}
struct Disclosable<C, S> {
disclosed: bool,
action: Box<dyn Action>,
content: C,
style: S,
}
impl Disclosable<(), ()> {
fn new<C>(disclosed: bool, content: C, action: Box<dyn Action>) -> Disclosable<C, ()> {
Disclosable {
disclosed,
content,
action,
style: (),
}
}
}
impl<C: StyleableComponent> StyleableComponent for Disclosable<C, ()> {
type Style = DisclosureStyle<C::Style>;
type Output = Disclosable<C, Self::Style>;
fn with_style(self, style: Self::Style) -> Self::Output {
Disclosable {
disclosed: self.disclosed,
action: self.action,
content: self.content,
style,
}
}
}
impl<C: StyleableComponent> GeneralComponent for Disclosable<C, DisclosureStyle<C::Style>> {
fn render<V: gpui::View>(
self,
v: &mut V,
cx: &mut gpui::ViewContext<V>,
) -> gpui::AnyElement<V> {
Flex::row()
.with_child(
ActionButton::new_dynamic(self.action)
.with_contents(Svg::new("path"))
.toggleable(self.disclosed)
.with_style(self.style.disclosure)
.element(),
)
.with_child(Empty::new().constrained().with_width(self.style.spacing))
.with_child(self.content.with_style(self.style.content).render(v, cx))
.align_children_center()
.into_any()
}
}
pub trait DisclosureExt {
fn disclosable(self, disclosed: bool, action: Box<dyn Action>) -> Disclosable<Self, ()>
where
Self: Sized;
}
impl<C: StyleableComponent> DisclosureExt for C {
fn disclosable(self, disclosed: bool, action: Box<dyn Action>) -> Disclosable<Self, ()> {
Disclosable::new(disclosed, self, action)
}
}
}

View File

@ -229,6 +229,13 @@ pub trait Element<V: View>: 'static {
{ {
MouseEventHandler::for_child::<Tag>(self.into_any(), region_id) MouseEventHandler::for_child::<Tag>(self.into_any(), region_id)
} }
fn component(self) -> ElementAdapter<V>
where
Self: Sized,
{
ElementAdapter::new(self.into_any())
}
} }
pub trait RenderElement { pub trait RenderElement {

View File

@ -50,6 +50,13 @@ pub trait Component<V: View> {
{ {
ComponentAdapter::new(self) ComponentAdapter::new(self)
} }
fn styleable(self) -> StylableComponentAdapter<Self, V>
where
Self: Sized,
{
StylableComponentAdapter::new(self)
}
} }
impl<V: View, C: GeneralComponent> Component<V> for C { impl<V: View, C: GeneralComponent> Component<V> for C {

View File

@ -8,7 +8,9 @@ use gpui::{
pub use mode::SearchMode; pub use mode::SearchMode;
use project::search::SearchQuery; use project::search::SearchQuery;
pub use project_search::{ProjectSearchBar, ProjectSearchView}; pub use project_search::{ProjectSearchBar, ProjectSearchView};
use theme::components::{action_button::ActionButton, ComponentExt, ToggleIconButtonStyle}; use theme::components::{
action_button::ActionButton, svg::Svg, ComponentExt, ToggleIconButtonStyle,
};
pub mod buffer_search; pub mod buffer_search;
mod history; mod history;
@ -89,15 +91,12 @@ impl SearchOptions {
tooltip_style: TooltipStyle, tooltip_style: TooltipStyle,
button_style: ToggleIconButtonStyle, button_style: ToggleIconButtonStyle,
) -> AnyElement<V> { ) -> AnyElement<V> {
ActionButton::new_dynamic( ActionButton::new_dynamic(self.to_toggle_action())
self.to_toggle_action(), .with_tooltip(format!("Toggle {}", self.label()), tooltip_style)
format!("Toggle {}", self.label()), .with_contents(Svg::new(self.icon()))
tooltip_style, .toggleable(active)
) .with_style(button_style)
.with_contents(theme::components::svg::Svg::new(self.icon())) .element()
.toggleable(active) .into_any()
.with_style(button_style)
.element()
.into_any()
} }
} }

View File

@ -81,8 +81,7 @@ pub mod action_button {
pub struct ActionButton<C, S> { pub struct ActionButton<C, S> {
action: Box<dyn Action>, action: Box<dyn Action>,
tooltip: Cow<'static, str>, tooltip: Option<(Cow<'static, str>, TooltipStyle)>,
tooltip_style: TooltipStyle,
tag: TypeTag, tag: TypeTag,
contents: C, contents: C,
style: Interactive<S>, style: Interactive<S>,
@ -99,27 +98,27 @@ pub mod action_button {
} }
impl ActionButton<(), ()> { impl ActionButton<(), ()> {
pub fn new_dynamic( pub fn new_dynamic(action: Box<dyn Action>) -> Self {
action: Box<dyn Action>,
tooltip: impl Into<Cow<'static, str>>,
tooltip_style: TooltipStyle,
) -> Self {
Self { Self {
contents: (), contents: (),
tag: action.type_tag(), tag: action.type_tag(),
style: Interactive::new_blank(), style: Interactive::new_blank(),
tooltip: tooltip.into(), tooltip: None,
tooltip_style,
action, action,
} }
} }
pub fn new<A: Action + Clone>( pub fn new<A: Action + Clone>(action: A) -> Self {
action: A, Self::new_dynamic(Box::new(action))
}
pub fn with_tooltip(
mut self,
tooltip: impl Into<Cow<'static, str>>, tooltip: impl Into<Cow<'static, str>>,
tooltip_style: TooltipStyle, tooltip_style: TooltipStyle,
) -> Self { ) -> Self {
Self::new_dynamic(Box::new(action), tooltip, tooltip_style) self.tooltip = Some((tooltip.into(), tooltip_style));
self
} }
pub fn with_contents<C: StyleableComponent>(self, contents: C) -> ActionButton<C, ()> { pub fn with_contents<C: StyleableComponent>(self, contents: C) -> ActionButton<C, ()> {
@ -128,7 +127,6 @@ pub mod action_button {
tag: self.tag, tag: self.tag,
style: self.style, style: self.style,
tooltip: self.tooltip, tooltip: self.tooltip,
tooltip_style: self.tooltip_style,
contents, contents,
} }
} }
@ -144,7 +142,7 @@ pub mod action_button {
tag: self.tag, tag: self.tag,
contents: self.contents, contents: self.contents,
tooltip: self.tooltip, tooltip: self.tooltip,
tooltip_style: self.tooltip_style,
style, style,
} }
} }
@ -152,7 +150,7 @@ pub mod action_button {
impl<C: StyleableComponent> GeneralComponent for ActionButton<C, ButtonStyle<C::Style>> { impl<C: StyleableComponent> GeneralComponent for ActionButton<C, ButtonStyle<C::Style>> {
fn render<V: View>(self, v: &mut V, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> { fn render<V: View>(self, v: &mut V, cx: &mut gpui::ViewContext<V>) -> gpui::AnyElement<V> {
MouseEventHandler::new_dynamic(self.tag, 0, cx, |state, cx| { let mut button = MouseEventHandler::new_dynamic(self.tag, 0, cx, |state, cx| {
let style = self.style.style_for(state); let style = self.style.style_for(state);
let mut contents = self let mut contents = self
.contents .contents
@ -180,15 +178,15 @@ pub mod action_button {
} }
}) })
.with_cursor_style(CursorStyle::PointingHand) .with_cursor_style(CursorStyle::PointingHand)
.with_dynamic_tooltip( .into_any();
self.tag,
0, if let Some((tooltip, style)) = self.tooltip {
self.tooltip, button = button
Some(self.action), .with_dynamic_tooltip(self.tag, 0, tooltip, Some(self.action), style, cx)
self.tooltip_style, .into_any()
cx, }
)
.into_any() button
} }
} }
} }