BlueGit

ict rusty-teams

Files

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

dialog.rs

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