BlueGit

ict rusty-teams

Files

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

dialog.rs

dialog.rs 10.3 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 .debug_selector(|| "settings/main/header/close".into())
181 .size(px(CLOSE_SIZE))
182 .flex_none()
183 .flex()
184 .items_center()
185 .justify_center()
186 .rounded(px(6.))
187 .cursor_pointer()
188 .hover(|button| button.bg(theme::popup_hover()))
189 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close)))
190 .child(icon(IconName::X, 16., theme::text_muted())),
191 )
192 }
193}
194
195impl Render for SettingsDialog {
196 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
197 let viewport = window.viewport_size();
198 let metrics = metrics(f32::from(viewport.width), f32::from(viewport.height));
199 let dialog = cx.entity();
200 let nav = render_nav(
201 self.section,
202 crate::agents::runs_handle(cx).is_some(),
203 NavLayout {
204 width: metrics.nav_width,
205 padding_y: metrics.padding,
206 },
207 move |section, window, cx| {
208 dialog.update(cx, |dialog, cx| dialog.show(section, window, cx))
209 },
210 );
211 let body_inset = if self.section == SettingsSection::Agents {
212 metrics.padding + ROW_INSET - PAGE_PADDING
213 } else {
214 metrics.padding
215 };
216 let body = div()
217 .id(SharedString::from(format!(
218 "settings-body-{}",
219 label(self.section)
220 )))
221 .debug_selector(|| "settings/main/body".into())
222 .w_full()
223 .flex_1()
224 .min_h_0()
225 .overflow_y_scroll()
226 .px(px(body_inset.max(0.)))
227 .pb(px(metrics.padding))
228 .child(
229 div()
230 .debug_selector(|| "settings/main/body/content".into())
231 .w_full()
232 .max_w(px(CONTENT_MAX_WIDTH))
233 .child(self.content(cx)),
234 );
235 div()
236 .id("settings-layer")
237 .absolute()
238 .top_0()
239 .left_0()
240 .size_full()
241 .child(
242 div()
243 .id("settings-backdrop")
244 .absolute()
245 .top_0()
246 .left_0()
247 .size_full()
248 .bg(black().opacity(BACKDROP_OPACITY))
249 .occlude()
250 .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))),
251 )
252 .child(
253 h_flex()
254 .id("settings-dialog")
255 .debug_selector(|| "settings".into())
256 .track_focus(&self.focus_handle)
257 .on_key_down(cx.listener(Self::on_key_down))
258 .absolute()
259 .top(px(metrics.inset))
260 .left(px(metrics.inset))
261 .right(px(metrics.inset))
262 .bottom(px(metrics.inset))
263 .overflow_hidden()
264 .rounded(px(PANEL_RADIUS))
265 .dialog_surface()
266 .text_color(theme::text())
267 .occlude()
268 .child(nav)
269 .child(
270 v_flex()
271 .debug_selector(|| "settings/main".into())
272 .flex_1()
273 .min_w_0()
274 .h_full()
275 .child(self.header(metrics, cx))
276 .child(body),
277 ),
278 )
279 }
280}
281
282#[cfg(test)]
283mod tests {
284 use super::{available, metrics};
285 use crate::settings_section::SettingsSection;
286
287 #[test]
288 fn small_windows_get_the_compact_inset() {
289 assert_eq!(metrics(1280., 800.).inset, 48.);
290 assert_eq!(metrics(1920., 1080.).nav_width, 264.);
291 assert_eq!(metrics(960., 640.).inset, 24.);
292 assert_eq!(metrics(1280., 640.).nav_width, 216.);
293 }
294
295 #[test]
296 fn agents_falls_back_to_general_without_agent_runs() {
297 assert_eq!(
298 available(SettingsSection::Agents, false),
299 SettingsSection::General
300 );
301 assert_eq!(
302 available(SettingsSection::Agents, true),
303 SettingsSection::Agents
304 );
305 assert_eq!(available(SettingsSection::Mcp, false), SettingsSection::Mcp);
306 }
307}