BlueGit

ict rusty-teams

Files

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

dialog.rs

dialog.rs 10.1 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, 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::Chats => chats::render(&self.app, cx).into_any_element(),
139 SettingsSection::Calls => calls::render(&self.center, cx).into_any_element(),
140 SettingsSection::Devices => self
141 .devices
142 .clone()
143 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
144 SettingsSection::Agents => self
145 .agents
146 .clone()
147 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
148 SettingsSection::Mcp => self
149 .mcp
150 .clone()
151 .map_or_else(|| div().into_any_element(), |view| view.into_any_element()),
152 SettingsSection::About => about::render().into_any_element(),
153 }
154 }
155
156 fn header(&self, metrics: DialogMetrics, cx: &mut Context<Self>) -> Div {
157 h_flex()
158 .debug_selector(|| "settings/main/header".into())
159 .w_full()
160 .flex_none()
161 .items_center()
162 .pt(px(metrics.padding))
163 .pr(px(metrics.padding))
164 .pb(px(metrics.padding / 2.))
165 .pl(px(metrics.padding + ROW_INSET))
166 .child(
167 div()
168 .flex_1()
169 .min_w_0()
170 .truncate()
171 .text_size(px(20.))
172 .line_height(px(28.))
173 .font_weight(FontWeight::SEMIBOLD)
174 .text_color(theme::text())
175 .child(label(self.section)),
176 )
177 .child(
178 div()
179 .id("settings-close")
180 .size(px(CLOSE_SIZE))
181 .flex_none()
182 .flex()
183 .items_center()
184 .justify_center()
185 .rounded(px(6.))
186 .cursor_pointer()
187 .hover(|button| button.bg(theme::popup_hover()))
188 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close)))
189 .child(icon(IconName::X, 16., theme::text_muted())),
190 )
191 }
192}
193
194impl Render for SettingsDialog {
195 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
196 let viewport = window.viewport_size();
197 let metrics = metrics(f32::from(viewport.width), f32::from(viewport.height));
198 let dialog = cx.entity();
199 let nav = render_nav(
200 self.section,
201 crate::agents::runs_handle(cx).is_some(),
202 NavLayout {
203 width: metrics.nav_width,
204 padding_y: metrics.padding,
205 },
206 move |section, window, cx| {
207 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx))
208 },
209 );
210 let body_inset = if self.section == SettingsSection::Agents {
211 metrics.padding + ROW_INSET - PAGE_PADDING
212 } else {
213 metrics.padding
214 };
215 let body = div()
216 .id(SharedString::from(format!(
217 "settings-body-{}",
218 label(self.section)
219 )))
220 .debug_selector(|| "settings/main/body".into())
221 .w_full()
222 .flex_1()
223 .min_h_0()
224 .overflow_y_scroll()
225 .px(px(body_inset.max(0.)))
226 .pb(px(metrics.padding))
227 .child(
228 div()
229 .w_full()
230 .max_w(px(CONTENT_MAX_WIDTH))
231 .child(self.content(cx)),
232 );
233 div()
234 .id("settings-layer")
235 .absolute()
236 .top_0()
237 .left_0()
238 .size_full()
239 .child(
240 div()
241 .id("settings-backdrop")
242 .absolute()
243 .top_0()
244 .left_0()
245 .size_full()
246 .bg(black().opacity(BACKDROP_OPACITY))
247 .occlude()
248 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))),
249 )
250 .child(
251 h_flex()
252 .id("settings-dialog")
253 .debug_selector(|| "settings".into())
254 .track_focus(&self.focus_handle)
255 .on_key_down(cx.listener(Self::on_key_down))
256 .absolute()
257 .top(px(metrics.inset))
258 .left(px(metrics.inset))
259 .right(px(metrics.inset))
260 .bottom(px(metrics.inset))
261 .overflow_hidden()
262 .rounded(px(PANEL_RADIUS))
263 .dialog_surface()
264 .text_color(theme::text())
265 .occlude()
266 .child(nav)
267 .child(
268 v_flex()
269 .debug_selector(|| "settings/main".into())
270 .flex_1()
271 .min_w_0()
272 .h_full()
273 .child(self.header(metrics, cx))
274 .child(body),
275 ),
276 )
277 }
278}
279
280#[cfg(test)]
281mod tests {
282 use super::{available, metrics};
283 use crate::settings_section::SettingsSection;
284
285 #[test]
286 fn small_windows_get_the_compact_inset() {
287 assert_eq!(metrics(1280., 800.).inset, 48.);
288 assert_eq!(metrics(1920., 1080.).nav_width, 264.);
289 assert_eq!(metrics(960., 640.).inset, 24.);
290 assert_eq!(metrics(1280., 640.).nav_width, 216.);
291 }
292
293 #[test]
294 fn agents_falls_back_to_general_without_agent_runs() {
295 assert_eq!(
296 available(SettingsSection::Agents, false),
297 SettingsSection::General
298 );
299 assert_eq!(
300 available(SettingsSection::Agents, true),
301 SettingsSection::Agents
302 );
303 assert_eq!(available(SettingsSection::Mcp, false), SettingsSection::Mcp);
304 }
305}