BlueGit

ict rusty-teams

Files

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

dialog.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
dialog.rs 13.0 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, 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::Devices => self
190 .devices
191 .clone()
192 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
193 SettingsSection::Agents => self
194 .agents
195 .clone()
196 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
197 SettingsSection::Mcp => self
198 .mcp
199 .clone()
200 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
201 SettingsSection::About => about::render().into_any_element(),
202 }
203 }
204
205 fn header(&self, metrics: DialogMetrics, cx: &mut Context<Self>) -> Div {
206 h_flex()
207 .debug_selector(|| "settings/main/header".into())
208 .w_full()
209 .flex_none()
210 .items_center()
211 .pt(px(metrics.padding))
212 .pr(px(metrics.padding))
213 .pb(px(metrics.padding / 2.))
214 .pl(px(metrics.padding + ROW_INSET))
215 .child(
216 div()
217 .flex_1()
218 .min_w_0()
219 .truncate()
220 .text_size(px(20.))
221 .line_height(px(28.))
222 .font_weight(FontWeight::SEMIBOLD)
223 .text_color(theme::text())
224 .child(label(self.section)),
225 )
226 .child(
227 div()
228 .id("settings-close")
229 .debug_selector(|| "settings/main/header/close".into())
230 .size(px(CLOSE_SIZE))
231 .flex_none()
232 .flex()
233 .items_center()
234 .justify_center()
235 .rounded(px(6.))
236 .cursor_pointer()
237 .hover(|button| button.bg(theme::popup_hover()))
238 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close)))
239 .child(icon(IconName::X, 16., theme::text_muted())),
240 )
241 }
242}
243
244impl Render for SettingsDialog {
245 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
246 let viewport = window.viewport_size();
247 let metrics = metrics(f32::from(viewport.width), f32::from(viewport.height));
248 let dialog = cx.entity();
249 let query = self.search.read(cx).value().to_string();
250 let nav = render_nav(
251 self.section,
252 crate::agents::runs_handle(cx).is_some(),
253 NavLayout {
254 width: metrics.nav_width,
255 padding_y: metrics.padding,
256 },
257 NavSearch {
258 input: self.search_input(),
259 query: &query,
260 },
261 move |section, window, cx| {
262 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx))
263 },
264 );
265 let body_inset = if self.section == SettingsSection::Agents {
266 metrics.padding + ROW_INSET - PAGE_PADDING
267 } else {
268 metrics.padding
269 };
270 let scroll_area = div()
271 .id(SharedString::from(format!(
272 "settings-body-{}",
273 label(self.section)
274 )))
275 .w_full()
276 .flex_1()
277 .min_h_0()
278 .overflow_y_scrollbar()
279 .track_scroll(&self.body_scroll)
280 .px(px(body_inset.max(0.)))
281 .pb(px(metrics.padding))
282 .child(
283 div()
284 .debug_selector(|| "settings/main/body/content".into())
285 .w_full()
286 .max_w(px(CONTENT_MAX_WIDTH))
287 .child(self.content(cx)),
288 );
289 let body = div()
290 .debug_selector(|| "settings/main/body".into())
291 .w_full()
292 .flex_1()
293 .min_h_0()
294 .flex()
295 .flex_col()
296 .child(scroll_area);
297 div()
298 .id("settings-layer")
299 .absolute()
300 .top_0()
301 .left_0()
302 .size_full()
303 .child(
304 div()
305 .id("settings-backdrop")
306 .absolute()
307 .top_0()
308 .left_0()
309 .size_full()
310 .bg(black().opacity(BACKDROP_OPACITY))
311 .occlude()
312 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))),
313 )
314 .child(
315 h_flex()
316 .id("settings-dialog")
317 .debug_selector(|| "settings".into())
318 .track_focus(&self.focus_handle)
319 .on_key_down(cx.listener(Self::on_key_down))
320 .absolute()
321 .top(px(metrics.inset))
322 .left(px(metrics.inset))
323 .right(px(metrics.inset))
324 .bottom(px(metrics.inset))
325 .overflow_hidden()
326 .rounded(px(PANEL_RADIUS))
327 .dialog_surface()
328 .text_color(theme::text())
329 .occlude()
330 .child(nav)
331 .child(
332 v_flex()
333 .debug_selector(|| "settings/main".into())
334 .flex_1()
335 .min_w_0()
336 .h_full()
337 .child(self.header(metrics, cx))
338 .child(with_top_edge(body, &self.body_scroll, BODY_SCROLL_DIVIDER)),
339 ),
340 )
341 }
342}
343
344#[cfg(test)]
345mod tests {
346 use super::{available, metrics};
347 use crate::settings_section::SettingsSection;
348
349 #[test]
350 fn small_windows_get_the_compact_inset() {
351 assert_eq!(metrics(1280., 800.).inset, 48.);
352 assert_eq!(metrics(960., 640.).inset, 24.);
353 assert_eq!(metrics(1280., 640.).inset, 24.);
354 }
355
356 #[test]
357 fn nav_keeps_the_teams_list_pane_minimum_width() {
358 for (width, height) in [(640., 480.), (960., 640.), (1280., 800.), (1920., 1080.)] {
359 let metrics = metrics(width, height);
360 assert_eq!(metrics.nav_width, 280.);
361 assert_eq!(metrics.padding, 24.);
362 }
363 }
364
365 #[test]
366 fn agents_falls_back_to_general_without_agent_runs() {
367 assert_eq!(
368 available(SettingsSection::Agents, false),
369 SettingsSection::General
370 );
371 assert_eq!(
372 available(SettingsSection::Agents, true),
373 SettingsSection::Agents
374 );
375 assert_eq!(available(SettingsSection::Mcp, false), SettingsSection::Mcp);
376 }
377}