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 => {
223 calendar::render_accounts(&self.app, self.caldav.as_ref(), self.ics.as_ref(), cx)
224 .into_any_element()
225 }
226 SettingsSection::Devices => self
227 .devices
228 .clone()
229 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
230 SettingsSection::Agents => self
231 .agents
232 .clone()
233 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
234 SettingsSection::Mcp => self
235 .mcp
236 .clone()
237 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
238 SettingsSection::Help => self
239 .help
240 .clone()
241 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
242 SettingsSection::About => about::render().into_any_element(),
243 }
244 }
245
246 fn header(&self, metrics: DialogMetrics, cx: &mut Context<Self>) -> Div {
247 h_flex()
248 .debug_selector(|| "settings/main/header".into())
249 .w_full()
250 .flex_none()
251 .items_center()
252 .pt(px(metrics.padding))
253 .pr(px(metrics.padding))
254 .pb(px(metrics.padding / 2.))
255 .pl(px(metrics.padding + ROW_INSET))
256 .child(
257 div()
258 .flex_1()
259 .min_w_0()
260 .truncate()
261 .text_size(px(20.))
262 .line_height(px(28.))
263 .font_weight(FontWeight::SEMIBOLD)
264 .text_color(theme::text())
265 .child(label(self.section)),
266 )
267 .child(
268 div()
269 .id("settings-close")
270 .debug_selector(|| "settings/main/header/close".into())
271 .size(px(CLOSE_SIZE))
272 .flex_none()
273 .flex()
274 .items_center()
275 .justify_center()
276 .rounded(px(6.))
277 .cursor_pointer()
278 .hover(|button| button.bg(theme::popup_hover()))
279 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close)))
280 .child(icon(IconName::X, 16., theme::text_muted())),
281 )
282 }
283}
284
285impl Render for SettingsDialog {
286 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
287 let viewport = window.viewport_size();
288 let metrics = metrics(f32::from(viewport.width), f32::from(viewport.height));
289 let dialog = cx.entity();
290 let query = self.search.read(cx).value().to_string();
291 let nav = render_nav(
292 self.section,
293 crate::agents::runs_handle(cx).is_some(),
294 NavLayout {
295 width: metrics.nav_width,
296 padding_y: metrics.padding,
297 },
298 NavSearch {
299 input: self.search_input(),
300 query: &query,
301 },
302 move |section, window, cx| {
303 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx))
304 },
305 );
306 let body_inset = if self.section == SettingsSection::Agents {
307 metrics.padding + ROW_INSET - PAGE_PADDING
308 } else {
309 metrics.padding
310 };
311 let help_preview = self
312 .help
313 .as_ref()
314 .is_some_and(|help| help.read(cx).is_previewing());
315 let content = div()
316 .debug_selector(|| "settings/main/body/content".into())
317 .w_full()
318 .max_w(px(CONTENT_MAX_WIDTH))
319 .when(help_preview, |content| content.h_full().min_h_0())
320 .child(self.content(cx));
321 let scroll_area = if help_preview {
322 div()
323 .id(SharedString::from(format!(
324 "settings-body-{}",
325 label(self.section)
326 )))
327 .w_full()
328 .flex_1()
329 .min_h_0()
330 .overflow_hidden()
331 .px(px(body_inset.max(0.)))
332 .pb(px(metrics.padding))
333 .child(content)
334 .into_any_element()
335 } else {
336 div()
337 .id(SharedString::from(format!(
338 "settings-body-{}",
339 label(self.section)
340 )))
341 .w_full()
342 .flex_1()
343 .min_h_0()
344 .overflow_y_scrollbar()
345 .track_scroll(&self.body_scroll)
346 .px(px(body_inset.max(0.)))
347 .pb(px(metrics.padding))
348 .child(content)
349 .into_any_element()
350 };
351 let body = div()
352 .debug_selector(|| "settings/main/body".into())
353 .w_full()
354 .flex_1()
355 .min_h_0()
356 .flex()
357 .flex_col()
358 .child(scroll_area);
359 div()
360 .id("settings-layer")
361 .absolute()
362 .top_0()
363 .left_0()
364 .size_full()
365 .child(
366 div()
367 .id("settings-backdrop")
368 .absolute()
369 .top_0()
370 .left_0()
371 .size_full()
372 .bg(black().opacity(BACKDROP_OPACITY))
373 .occlude()
374 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))),
375 )
376 .child(
377 h_flex()
378 .id("settings-dialog")
379 .debug_selector(|| "settings".into())
380 .track_focus(&self.focus_handle)
381 .on_key_down(cx.listener(Self::on_key_down))
382 .absolute()
383 .top(px(metrics.inset))
384 .left(px(metrics.inset))
385 .right(px(metrics.inset))
386 .bottom(px(metrics.inset))
387 .overflow_hidden()
388 .rounded(px(PANEL_RADIUS))
389 .dialog_surface()
390 .text_color(theme::text())
391 .occlude()
392 .child(nav)
393 .child(
394 v_flex()
395 .debug_selector(|| "settings/main".into())
396 .flex_1()
397 .min_w_0()
398 .h_full()
399 .child(self.header(metrics, cx))
400 .child(with_top_edge(body, &self.body_scroll, BODY_SCROLL_DIVIDER)),
401 ),
402 )
403 }
404}
405
406#[cfg(test)]
407mod tests {
408 use super::{available, metrics};
409 use crate::settings_section::SettingsSection;
410
411 #[test]
412 fn small_windows_get_the_compact_inset() {
413 assert_eq!(metrics(1280., 800.).inset, 48.);
414 assert_eq!(metrics(960., 640.).inset, 24.);
415 assert_eq!(metrics(1280., 640.).inset, 24.);
416 }
417
418 #[test]
419 fn nav_keeps_the_teams_list_pane_minimum_width() {
420 for (width, height) in [(640., 480.), (960., 640.), (1280., 800.), (1920., 1080.)] {
421 let metrics = metrics(width, height);
422 assert_eq!(metrics.nav_width, 280.);
423 assert_eq!(metrics.padding, 24.);
424 }
425 }
426
427 #[test]
428 fn agents_falls_back_to_general_without_agent_runs() {
429 assert_eq!(
430 available(SettingsSection::Agents, false),
431 SettingsSection::General
432 );
433 assert_eq!(
434 available(SettingsSection::Agents, true),
435 SettingsSection::Agents
436 );
437 assert_eq!(available(SettingsSection::Mcp, false), SettingsSection::Mcp);
438 }
439}