BlueGit

ict rusty-teams

Files

rusty-teams / spikes / gpui-list / src / main.rs

main.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
main.rs 6.5 KiB Download Raw
1mod data;
2
3use std::{cell::RefCell, rc::Rc};
4
5use data::{ChatMessage, Row};
6use gpui_kit::component::{
7 h_flex,
8 input::{InputEvent, Textarea, TextareaState},
9 message_scroller::{MessageScroller, MessageScrollerState},
10 sidebar::{Sidebar, SidebarGroup, SidebarMenu, SidebarMenuItem},
11 text::TextView,
12 notification::Notification, v_flex, ActiveTheme, IconName, StyledExt as _, WindowExt as _,
13};
14use gpui_kit::*;
15
16const MESSAGE_COUNT: usize = 5_000;
17
18struct ChatApp {
19 rows: Rc<RefCell<Vec<Row>>>,
20 scroller: Entity<MessageScrollerState>,
21 composer: Entity<TextareaState>,
22 selected_chat: SharedString,
23 _subscriptions: Vec<Subscription>,
24}
25
26impl ChatApp {
27 fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
28 let rows = synthetic_rows();
29 let scroller = cx.new(|cx| MessageScrollerState::new(rows.len(), cx));
30 let composer = cx.new(|cx| {
31 TextareaState::new(window, cx)
32 .auto_grow(1, 6)
33 .submit_on_enter(true)
34 .placeholder("Message - Enter sends, Shift+Enter newline")
35 });
36 let subscription = cx.subscribe_in(&composer, window, Self::on_composer_event);
37 scroller.update(cx, |state, cx| state.scroll_to_end(cx));
38 Self {
39 rows: Rc::new(RefCell::new(rows)),
40 scroller,
41 composer,
42 selected_chat: "Chat 1".into(),
43 _subscriptions: vec![subscription],
44 }
45 }
46
47 fn on_composer_event(
48 &mut self,
49 composer: &Entity<TextareaState>,
50 event: &InputEvent,
51 window: &mut Window,
52 cx: &mut Context<Self>,
53 ) {
54 let InputEvent::PressEnter { shift: false, .. } = event else {
55 return;
56 };
57 let text = composer.read(cx).value().trim().to_string();
58 if text.is_empty() {
59 return;
60 }
61 self.rows.borrow_mut().push(Row::Message(ChatMessage {
62 author: "Me".into(),
63 time: "now".into(),
64 markdown: text,
65 }));
66 composer.update(cx, |state, cx| state.set_value("", window, cx));
67 self.scroller.update(cx, |state, cx| {
68 state.append(1, cx);
69 state.scroll_to_end(cx);
70 });
71 window.push_notification(Notification::success("Sent"), cx);
72 }
73
74 fn sidebar(&self, cx: &mut Context<Self>) -> Sidebar<SidebarGroup<SidebarMenu>> {
75 let chat_item = |label: &'static str, cx: &mut Context<Self>| {
76 SidebarMenuItem::new(label)
77 .icon(IconName::Inbox)
78 .active(self.selected_chat == label)
79 .on_click(cx.listener(move |this, _, _, cx| {
80 this.selected_chat = label.into();
81 cx.notify();
82 }))
83 };
84 let favorites = SidebarMenu::new().children([chat_item("Chat 1", cx), chat_item("Channel A / General", cx)]);
85 let chats = SidebarMenu::new().children([chat_item("Chat 2", cx), chat_item("Chat 3", cx), chat_item("Chat 4", cx)]);
86 let teams = SidebarMenu::new().children([
87 SidebarMenuItem::new("Team Red")
88 .icon(IconName::Folder)
89 .default_open(true)
90 .click_to_toggle(true)
91 .children([chat_item("Red / General", cx), chat_item("Red / Dev", cx)]),
92 SidebarMenuItem::new("Team Blue")
93 .icon(IconName::Folder)
94 .click_to_toggle(true)
95 .children([chat_item("Blue / General", cx), chat_item("Blue / Ops", cx)]),
96 ]);
97 Sidebar::new("sidebar")
98 .w(px(260.))
99 .child(SidebarGroup::new("Favorites").child(favorites))
100 .child(SidebarGroup::new("Chats").child(chats))
101 .child(SidebarGroup::new("Teams").child(teams))
102 }
103}
104
105fn synthetic_rows() -> Vec<Row> {
106 data::synthetic_rows(MESSAGE_COUNT)
107}
108
109fn render_row(row: &Row, row_index: usize, cx: &App) -> AnyElement {
110 match row {
111 Row::DaySeparator(label) => div()
112 .w_full()
113 .py_2()
114 .text_center()
115 .text_xs()
116 .text_color(cx.theme().muted_foreground)
117 .child(label.clone())
118 .into_any_element(),
119 Row::Message(message) => v_flex()
120 .w_full()
121 .gap_1()
122 .child(
123 h_flex()
124 .gap_2()
125 .child(div().font_semibold().child(message.author.clone()))
126 .child(
127 div()
128 .text_xs()
129 .text_color(cx.theme().muted_foreground)
130 .child(message.time.clone()),
131 ),
132 )
133 .child(TextView::markdown(("message", row_index), message.markdown.clone()))
134 .into_any_element(),
135 }
136}
137
138impl Render for ChatApp {
139 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
140 let rows = self.rows.clone();
141 let theme_background = cx.theme().background;
142 h_flex()
143 .size_full()
144 .bg(theme_background)
145 .child(self.sidebar(cx))
146 .child(
147 v_flex()
148 .flex_1()
149 .min_w_0()
150 .h_full()
151 .child(
152 div().flex_1().min_h_0().child(
153 MessageScroller::new("messages", self.scroller.clone(), move |row_index, _, cx| {
154 match rows.borrow().get(row_index) {
155 Some(row) => render_row(row, row_index, cx),
156 None => div().into_any_element(),
157 }
158 })
159 .with_bottom_fade(theme_background),
160 ),
161 )
162 .child(
163 div()
164 .p_3()
165 .border_t_1()
166 .border_color(cx.theme().border)
167 .child(Textarea::new(&self.composer)),
168 ),
169 )
170 }
171}
172
173fn main() {
174 gpui_kit::application()
175 .with_assets(gpui_kit::assets::Assets)
176 .run(|cx| {
177 gpui_kit::init(cx);
178 gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| {
179 cx.new(|cx| ChatApp::new(window, cx))
180 })
181 .expect("failed to open window");
182 cx.activate(true);
183 });
184}