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