BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / dialog.rs

dialog.rs

dialog.rs 13.1 KiB Download Raw
1use app_ui_kit::settings_controls::PAGE_PADDING;
2use gpui_kit::assets::IconName;
3use gpui_kit::component::input::{Input, InputEvent, InputState};
4use gpui_kit::component::scroll::ScrollableElement as _;
5use gpui_kit::component::{h_flex, v_flex};
6use gpui_kit::*;
7
8use super::devices::DevicesView;
9use super::mcp::McpView;
10use super::nav::{NavLayout, NavSearch, first_match, label, matches, render_nav};
11use super::rows::ROW_INSET;
12use super::{about, appearance, calendar, calls, chats, general, notifications};
13use crate::agents::AgentsSettings;
14use crate::app_state::AppState;
15use crate::notify::NotificationCenter;
16use crate::settings_section::SettingsSection;
17use crate::theme::{self, ElevatedSurface as _};
18use crate::views::scroll_edge::with_top_edge;
19use crate::views::widgets::icon;
20
21pub const BODY_SCROLL_DIVIDER: &str = "settings/main/body/divider-scroll";
22const BACKDROP_OPACITY: f32 = 0.55;
23pub(super) const PANEL_RADIUS: f32 = 10.;
24const CONTENT_MAX_WIDTH: f32 = 744.;
25const CLOSE_SIZE: f32 = 32.;
26const COMPACT_BELOW_WIDTH: f32 = 1100.;
27const COMPACT_BELOW_HEIGHT: f32 = 720.;
28const NAV_WIDTH: f32 = 280.;
29const PADDING: f32 = 24.;
30const SEARCH_HEIGHT: f32 = 32.;
31const SEARCH_PLACEHOLDER: &str = "Find in Settings";
32
33pub enum SettingsDialogEvent {
34 Close,
35}
36
37#[derive(Debug, Clone, Copy, PartialEq)]
38pub struct DialogMetrics {
39 pub inset: f32,
40 pub nav_width: f32,
41 pub padding: f32,
42}
43
44pub fn metrics(window_width: f32, window_height: f32) -> DialogMetrics {
45 let compact = window_width < COMPACT_BELOW_WIDTH || window_height < COMPACT_BELOW_HEIGHT;
46 DialogMetrics {
47 inset: if compact { 24. } else { 48. },
48 nav_width: NAV_WIDTH,
49 padding: PADDING,
50 }
51}
52
53pub fn available(section: SettingsSection, agents_available: bool) -> SettingsSection {
54 if section == SettingsSection::Agents && !agents_available {
55 SettingsSection::General
56 } else {
57 section
58 }
59}
60
61pub struct SettingsDialog {
62 app: Entity<AppState>,
63 center: Entity<NotificationCenter>,
64 section: SettingsSection,
65 devices: Option<Entity<DevicesView>>,
66 mcp: Option<Entity<McpView>>,
67 agents: Option<Entity<AgentsSettings>>,
68 body_scroll: ScrollHandle,
69 focus_handle: FocusHandle,
70 search: Entity<InputState>,
71 _subscriptions: [Subscription; 3],
72}
73
74impl EventEmitter<SettingsDialogEvent> for SettingsDialog {}
75
76impl SettingsDialog {
77 pub fn new(
78 app: Entity<AppState>,
79 center: Entity<NotificationCenter>,
80 section: SettingsSection,
81 window: &mut Window,
82 cx: &mut Context<Self>,
83 ) -> Self {
84 let search = cx.new(|cx| InputState::new(window, cx).placeholder(SEARCH_PLACEHOLDER));
85 let subscriptions = [
86 cx.observe(&center, |_, _, cx| cx.notify()),
87 cx.observe(&app, |_, _, cx| cx.notify()),
88 cx.subscribe_in(
89 &search,
90 window,
91 |this, _, event: &InputEvent, window, cx| {
92 if matches!(event, InputEvent::Change) {
93 this.follow_search(window, cx);
94 }
95 },
96 ),
97 ];
98 let focus_handle = cx.focus_handle();
99 window.focus(&focus_handle, cx);
100 let mut dialog = SettingsDialog {
101 app,
102 center,
103 section,
104 devices: None,
105 mcp: None,
106 agents: None,
107 body_scroll: ScrollHandle::new(),
108 focus_handle,
109 search,
110 _subscriptions: subscriptions,
111 };
112 dialog.show(section, window, cx);
113 dialog
114 }
115
116 pub fn show(&mut self, section: SettingsSection, window: &mut Window, cx: &mut Context<Self>) {
117 let section = available(section, crate::agents::runs_handle(cx).is_some());
118 if section != self.section {
119 self.body_scroll = ScrollHandle::new();
120 }
121 self.section = section;
122 if section == SettingsSection::Devices {
123 if self.devices.is_none() {
124 let app = self.app.clone();
125 self.devices = Some(cx.new(|cx| DevicesView::new(app, cx)));
126 }
127 } else {
128 self.devices = None;
129 }
130 if section == SettingsSection::Mcp && self.mcp.is_none() {
131 let app = self.app.clone();
132 self.mcp = Some(cx.new(|cx| McpView::new(app, window, cx)));
133 }
134 if section == SettingsSection::Agents && self.agents.is_none() {
135 self.agents = crate::agents::runs_handle(cx)
136 .map(|runs| cx.new(|cx| AgentsSettings::new(runs, cx)));
137 }
138 cx.notify();
139 }
140
141 fn follow_search(&mut self, window: &mut Window, cx: &mut Context<Self>) {
142 let query = self.search.read(cx).value().to_string();
143 if !matches(self.section, &query) {
144 let agents_available = crate::agents::runs_handle(cx).is_some();
145 if let Some(section) = first_match(&query, agents_available) {
146 self.show(section, window, cx);
147 }
148 }
149 cx.notify();
150 }
151
152 fn search_input(&self) -> AnyElement {
153 h_flex()
154 .h(px(SEARCH_HEIGHT))
155 .w_full()
156 .px(px(10.))
157 .gap(px(8.))
158 .items_center()
159 .rounded(px(6.))
160 .bg(theme::popup())
161 .child(icon(IconName::Search, 14., theme::text_muted()))
162 .child(
163 div()
164 .flex_1()
165 .min_w_0()
166 .child(Input::new(&self.search).appearance(false).bordered(false)),
167 )
168 .into_any_element()
169 }
170
171 fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
172 if event.keystroke.key == "escape" {
173 cx.emit(SettingsDialogEvent::Close);
174 cx.stop_propagation();
175 }
176 }
177
178 fn content(&self, cx: &mut Context<Self>) -> AnyElement {
179 match self.section {
180 SettingsSection::General => {
181 general::render(&self.center, &self.app, cx).into_any_element()
182 }
183 SettingsSection::Notifications => {
184 notifications::render(&self.center, &self.app, cx).into_any_element()
185 }
186 SettingsSection::Appearance => appearance::render(&self.app, cx).into_any_element(),
187 SettingsSection::Chats => chats::render(&self.app, cx).into_any_element(),
188 SettingsSection::Calls => calls::render(&self.center, cx).into_any_element(),
189 SettingsSection::Calendar => calendar::render(&self.app, cx).into_any_element(),
190 SettingsSection::Devices => self
191 .devices
192 .clone()
193 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
194 SettingsSection::Agents => self
195 .agents
196 .clone()
197 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
198 SettingsSection::Mcp => self
199 .mcp
200 .clone()
201 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
202 SettingsSection::About => about::render().into_any_element(),
203 }
204 }
205
206 fn header(&self, metrics: DialogMetrics, cx: &mut Context<Self>) -> Div {
207 h_flex()
208 .debug_selector(|| "settings/main/header".into())
209 .w_full()
210 .flex_none()
211 .items_center()
212 .pt(px(metrics.padding))
213 .pr(px(metrics.padding))
214 .pb(px(metrics.padding / 2.))
215 .pl(px(metrics.padding + ROW_INSET))
216 .child(
217 div()
218 .flex_1()
219 .min_w_0()
220 .truncate()
221 .text_size(px(20.))
222 .line_height(px(28.))
223 .font_weight(FontWeight::SEMIBOLD)
224 .text_color(theme::text())
225 .child(label(self.section)),
226 )
227 .child(
228 div()
229 .id("settings-close")
230 .debug_selector(|| "settings/main/header/close".into())
231 .size(px(CLOSE_SIZE))
232 .flex_none()
233 .flex()
234 .items_center()
235 .justify_center()
236 .rounded(px(6.))
237 .cursor_pointer()
238 .hover(|button| button.bg(theme::popup_hover()))
239 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close)))
240 .child(icon(IconName::X, 16., theme::text_muted())),
241 )
242 }
243}
244
245impl Render for SettingsDialog {
246 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
247 let viewport = window.viewport_size();
248 let metrics = metrics(f32::from(viewport.width), f32::from(viewport.height));
249 let dialog = cx.entity();
250 let query = self.search.read(cx).value().to_string();
251 let nav = render_nav(
252 self.section,
253 crate::agents::runs_handle(cx).is_some(),
254 NavLayout {
255 width: metrics.nav_width,
256 padding_y: metrics.padding,
257 },
258 NavSearch {
259 input: self.search_input(),
260 query: &query,
261 },
262 move |section, window, cx| {
263 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx))
264 },
265 );
266 let body_inset = if self.section == SettingsSection::Agents {
267 metrics.padding + ROW_INSET - PAGE_PADDING
268 } else {
269 metrics.padding
270 };
271 let scroll_area = div()
272 .id(SharedString::from(format!(
273 "settings-body-{}",
274 label(self.section)
275 )))
276 .w_full()
277 .flex_1()
278 .min_h_0()
279 .overflow_y_scrollbar()
280 .track_scroll(&self.body_scroll)
281 .px(px(body_inset.max(0.)))
282 .pb(px(metrics.padding))
283 .child(
284 div()
285 .debug_selector(|| "settings/main/body/content".into())
286 .w_full()
287 .max_w(px(CONTENT_MAX_WIDTH))
288 .child(self.content(cx)),
289 );
290 let body = div()
291 .debug_selector(|| "settings/main/body".into())
292 .w_full()
293 .flex_1()
294 .min_h_0()
295 .flex()
296 .flex_col()
297 .child(scroll_area);
298 div()
299 .id("settings-layer")
300 .absolute()
301 .top_0()
302 .left_0()
303 .size_full()
304 .child(
305 div()
306 .id("settings-backdrop")
307 .absolute()
308 .top_0()
309 .left_0()
310 .size_full()
311 .bg(black().opacity(BACKDROP_OPACITY))
312 .occlude()
313 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))),
314 )
315 .child(
316 h_flex()
317 .id("settings-dialog")
318 .debug_selector(|| "settings".into())
319 .track_focus(&self.focus_handle)
320 .on_key_down(cx.listener(Self::on_key_down))
321 .absolute()
322 .top(px(metrics.inset))
323 .left(px(metrics.inset))
324 .right(px(metrics.inset))
325 .bottom(px(metrics.inset))
326 .overflow_hidden()
327 .rounded(px(PANEL_RADIUS))
328 .dialog_surface()
329 .text_color(theme::text())
330 .occlude()
331 .child(nav)
332 .child(
333 v_flex()
334 .debug_selector(|| "settings/main".into())
335 .flex_1()
336 .min_w_0()
337 .h_full()
338 .child(self.header(metrics, cx))
339 .child(with_top_edge(body, &self.body_scroll, BODY_SCROLL_DIVIDER)),
340 ),
341 )
342 }
343}
344
345#[cfg(test)]
346mod tests {
347 use super::{available, metrics};
348 use crate::settings_section::SettingsSection;
349
350 #[test]
351 fn small_windows_get_the_compact_inset() {
352 assert_eq!(metrics(1280., 800.).inset, 48.);
353 assert_eq!(metrics(960., 640.).inset, 24.);
354 assert_eq!(metrics(1280., 640.).inset, 24.);
355 }
356
357 #[test]
358 fn nav_keeps_the_teams_list_pane_minimum_width() {
359 for (width, height) in [(640., 480.), (960., 640.), (1280., 800.), (1920., 1080.)] {
360 let metrics = metrics(width, height);
361 assert_eq!(metrics.nav_width, 280.);
362 assert_eq!(metrics.padding, 24.);
363 }
364 }
365
366 #[test]
367 fn agents_falls_back_to_general_without_agent_runs() {
368 assert_eq!(
369 available(SettingsSection::Agents, false),
370 SettingsSection::General
371 );
372 assert_eq!(
373 available(SettingsSection::Agents, true),
374 SettingsSection::Agents
375 );
376 assert_eq!(available(SettingsSection::Mcp, false), SettingsSection::Mcp);
377 }
378}