BlueGit

ict rusty-teams

Files

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

dialog.rs

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