BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / chat_pop_out.rs

chat_pop_out.rs

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