BlueGit

ict rusty-teams

Files

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

dialog.rs

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