BlueGit

ict rusty-teams

Files

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

dialog.rs

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