BlueGit

ict rusty-teams

Files

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

dialog.rs

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