BlueGit

ict rusty-teams

Files

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

chat_pop_out.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
chat_pop_out.rs 4.0 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 = cx.new(|cx| ConversationView::popped_out(app, selection, window, cx));
43 ChatPopOutView { conversation }
44 }
45}
46
47impl 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
58fn 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
64pub 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
110pub 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}