From 40c293e184a40a054516250045528fd5a3d20c21 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Wed, 26 Jul 2023 15:50:01 -0700 Subject: [PATCH] Add channel_modal file --- crates/collab_ui/src/panel/channel_modal.rs | 95 +++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 crates/collab_ui/src/panel/channel_modal.rs diff --git a/crates/collab_ui/src/panel/channel_modal.rs b/crates/collab_ui/src/panel/channel_modal.rs new file mode 100644 index 0000000000000000000000000000000000000000..562536d58cd3863b1d57fc92ae33a1469fb4938f --- /dev/null +++ b/crates/collab_ui/src/panel/channel_modal.rs @@ -0,0 +1,95 @@ +use editor::Editor; +use gpui::{elements::*, AnyViewHandle, Entity, View, ViewContext, ViewHandle, AppContext}; +use menu::Cancel; +use workspace::{item::ItemHandle, Modal}; + +pub fn init(cx: &mut AppContext) { + cx.add_action(ChannelModal::cancel) +} + +pub struct ChannelModal { + has_focus: bool, + input_editor: ViewHandle, +} + +pub enum Event { + Dismiss, +} + +impl Entity for ChannelModal { + type Event = Event; +} + +impl ChannelModal { + pub fn new(cx: &mut ViewContext) -> Self { + let input_editor = cx.add_view(|cx| { + let mut editor = Editor::single_line(None, cx); + editor.set_placeholder_text("Create or add a channel", cx); + editor + }); + + ChannelModal { + has_focus: false, + input_editor, + } + } + + pub fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext) { + self.dismiss(cx); + } + + fn dismiss(&mut self, cx: &mut ViewContext) { + cx.emit(Event::Dismiss) + } +} + +impl View for ChannelModal { + fn ui_name() -> &'static str { + "Channel Modal" + } + + fn render(&mut self, cx: &mut gpui::ViewContext<'_, '_, Self>) -> gpui::AnyElement { + let style = theme::current(cx).editor.hint_diagnostic.message.clone(); + let modal_container = theme::current(cx).picker.container.clone(); + + enum ChannelModal {} + MouseEventHandler::::new(0, cx, |_, cx| { + Flex::column() + .with_child(ChildView::new(self.input_editor.as_any(), cx)) + .with_child(Label::new("ADD OR BROWSE CHANNELS HERE", style)) + .contained() + .with_style(modal_container) + .constrained() + .with_max_width(540.) + .with_max_height(420.) + + }) + .on_click(gpui::platform::MouseButton::Left, |_, _, _| {}) // Capture click and down events + .on_down_out(gpui::platform::MouseButton::Left, |_, v, cx| { + v.dismiss(cx) + }).into_any_named("channel modal") + } + + fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext) { + self.has_focus = true; + if cx.is_self_focused() { + cx.focus(&self.input_editor); + } + } + + fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext) { + self.has_focus = false; + } +} + +impl Modal for ChannelModal { + fn has_focus(&self) -> bool { + self.has_focus + } + + fn dismiss_on_event(event: &Self::Event) -> bool { + match event { + Event::Dismiss => true, + } + } +}