BlueGit

ict rusty-teams

Files

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

dialog.rs

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