| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use app_ui_kit::layout::ISLAND_GAP; |
| 4 | use gpui_kit::component::v_flex; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use super::conversation::ConversationView; |
| 8 | use super::shell::AppShell; |
| 9 | use crate::app_state::{AppState, Selection, selection_title}; |
| 10 | use crate::theme; |
| 11 | |
| 12 | pub const POP_OUT_CHAT_LABEL: &str = "Pop out chat"; |
| 13 | pub const POP_IN_TOOLTIP: &str = "Pop in back to the main window"; |
| 14 | const CHAT_WINDOW_SIZE: (f32, f32) = (760., 820.); |
| 15 | const CHAT_WINDOW_MIN_SIZE: (f32, f32) = (420., 480.); |
| 16 | |
| 17 | #[derive(Default)] |
| 18 | struct ChatPopOuts { |
| 19 | windows: HashMap<String, AnyWindowHandle>, |
| 20 | opening: Vec<String>, |
| 21 | } |
| 22 | |
| 23 | impl Global for ChatPopOuts {} |
| 24 | |
| 25 | pub struct ChatPopOutView { |
| 26 | conversation: Entity<ConversationView>, |
| 27 | } |
| 28 | |
| 29 | impl ChatPopOutView { |
| 30 | fn new( |
| 31 | app: Entity<AppState>, |
| 32 | chat_id: String, |
| 33 | window: &mut Window, |
| 34 | cx: &mut Context<Self>, |
| 35 | ) -> Self { |
| 36 | let closing_chat_id = chat_id.clone(); |
| 37 | window.on_window_should_close(cx, move |_, cx| { |
| 38 | forget(&closing_chat_id, cx); |
| 39 | true |
| 40 | }); |
| 41 | let selection = Selection::Chat(chat_id); |
| 42 | let conversation = cx.new(|cx| ConversationView::popped_out(app, selection, window, cx)); |
| 43 | ChatPopOutView { conversation } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Render for ChatPopOutView { |
| 48 | fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement { |
| 49 | v_flex() |
| 50 | .id("chat-pop-out") |
| 51 | .size_full() |
| 52 | .p(px(ISLAND_GAP)) |
| 53 | .bg(theme::background()) |
| 54 | .child(self.conversation.clone()) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn forget(chat_id: &str, cx: &mut App) { |
| 59 | if cx.has_global::<ChatPopOuts>() { |
| 60 | cx.global_mut::<ChatPopOuts>().windows.remove(chat_id); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | pub fn pop_out_chat(app: &Entity<AppState>, chat_id: &str, cx: &mut App) { |
| 65 | let pop_outs = cx.default_global::<ChatPopOuts>(); |
| 66 | if pop_outs.opening.iter().any(|opening| opening == chat_id) { |
| 67 | return; |
| 68 | } |
| 69 | if let Some(handle) = pop_outs.windows.get(chat_id).copied() { |
| 70 | if handle |
| 71 | .update(cx, |_, window, _| window.activate_window()) |
| 72 | .is_ok() |
| 73 | { |
| 74 | return; |
| 75 | } |
| 76 | cx.global_mut::<ChatPopOuts>().windows.remove(chat_id); |
| 77 | } |
| 78 | cx.global_mut::<ChatPopOuts>() |
| 79 | .opening |
| 80 | .push(chat_id.to_owned()); |
| 81 | let title = selection_title(&app.read(cx).sidebar, &Selection::Chat(chat_id.to_owned())); |
| 82 | let options = WindowOptions { |
| 83 | window_bounds: Some(WindowBounds::Windowed(Bounds::centered( |
| 84 | None, |
| 85 | size(px(CHAT_WINDOW_SIZE.0), px(CHAT_WINDOW_SIZE.1)), |
| 86 | cx, |
| 87 | ))), |
| 88 | window_min_size: Some(size(px(CHAT_WINDOW_MIN_SIZE.0), px(CHAT_WINDOW_MIN_SIZE.1))), |
| 89 | titlebar: Some(TitlebarOptions { |
| 90 | title: Some(title.into()), |
| 91 | ..Default::default() |
| 92 | }), |
| 93 | ..Default::default() |
| 94 | }; |
| 95 | let (app, chat_id) = (app.clone(), chat_id.to_owned()); |
| 96 | // open_window draws right away, and the conversation view reads the app entity while rendering. |
| 97 | cx.defer(move |cx| { |
| 98 | let window_chat_id = chat_id.clone(); |
| 99 | let opened = gpui_kit::open_window(options, cx, move |window, cx| { |
| 100 | cx.new(|cx| ChatPopOutView::new(app, window_chat_id, window, cx)) |
| 101 | }); |
| 102 | let pop_outs = cx.global_mut::<ChatPopOuts>(); |
| 103 | pop_outs.opening.retain(|opening| opening != &chat_id); |
| 104 | if let Ok((handle, _)) = opened { |
| 105 | pop_outs.windows.insert(chat_id, handle); |
| 106 | } |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | pub fn return_to_main_window( |
| 111 | app: &Entity<AppState>, |
| 112 | chat_id: &str, |
| 113 | window: &mut Window, |
| 114 | cx: &mut App, |
| 115 | ) { |
| 116 | forget(chat_id, cx); |
| 117 | let selection = Selection::Chat(chat_id.to_owned()); |
| 118 | app.update(cx, |state, cx| state.select(selection, cx)); |
| 119 | let main_window = cx |
| 120 | .windows() |
| 121 | .into_iter() |
| 122 | .find(|handle| handle.downcast::<AppShell>().is_some()); |
| 123 | if let Some(main_window) = main_window { |
| 124 | main_window |
| 125 | .update(cx, |_, window, _| window.activate_window()) |
| 126 | .ok(); |
| 127 | } |
| 128 | window.remove_window(); |
| 129 | } |