| 1 | use app_ui_kit::settings_controls::PAGE_PADDING; |
| 2 | use gpui_kit::assets::IconName; |
| 3 | use gpui_kit::component::input::{Input, InputEvent, InputState}; |
| 4 | use gpui_kit::component::scroll::ScrollableElement as _; |
| 5 | use gpui_kit::component::{h_flex, v_flex}; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | |
| 9 | use super::caldav_form::{CalDavForm, IcsForm}; |
| 10 | use super::devices::DevicesView; |
| 11 | use super::google_client_form::GoogleClientForm; |
| 12 | use super::help::HelpView; |
| 13 | use super::mcp::McpView; |
| 14 | use super::nav::{NavLayout, NavSearch, first_match, label, matches, render_nav}; |
| 15 | use super::rows::ROW_INSET; |
| 16 | use super::{about, appearance, calendar, calls, chats, general, notifications}; |
| 17 | use crate::agents::AgentsSettings; |
| 18 | use crate::app_state::AppState; |
| 19 | use crate::notify::NotificationCenter; |
| 20 | use crate::settings_section::SettingsSection; |
| 21 | use crate::theme::{self, ElevatedSurface as _}; |
| 22 | use crate::views::scroll_edge::with_top_edge; |
| 23 | use crate::views::widgets::icon; |
| 24 | |
| 25 | pub const BODY_SCROLL_DIVIDER: &str = "settings/main/body/divider-scroll"; |
| 26 | const BACKDROP_OPACITY: f32 = 0.55; |
| 27 | pub(super) const PANEL_RADIUS: f32 = 10.; |
| 28 | const CONTENT_MAX_WIDTH: f32 = 744.; |
| 29 | const CLOSE_SIZE: f32 = 32.; |
| 30 | const COMPACT_BELOW_WIDTH: f32 = 1100.; |
| 31 | const COMPACT_BELOW_HEIGHT: f32 = 720.; |
| 32 | const NAV_WIDTH: f32 = 280.; |
| 33 | const PADDING: f32 = 24.; |
| 34 | const SEARCH_HEIGHT: f32 = 32.; |
| 35 | const SEARCH_PLACEHOLDER: &str = "Find in Settings"; |
| 36 | |
| 37 | pub enum SettingsDialogEvent { |
| 38 | Close, |
| 39 | } |
| 40 | |
| 41 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 42 | pub struct DialogMetrics { |
| 43 | pub inset: f32, |
| 44 | pub nav_width: f32, |
| 45 | pub padding: f32, |
| 46 | } |
| 47 | |
| 48 | pub fn metrics(window_width: f32, window_height: f32) -> DialogMetrics { |
| 49 | let compact = window_width < COMPACT_BELOW_WIDTH || window_height < COMPACT_BELOW_HEIGHT; |
| 50 | DialogMetrics { |
| 51 | inset: if compact { 24. } else { 48. }, |
| 52 | nav_width: NAV_WIDTH, |
| 53 | padding: PADDING, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub fn available(section: SettingsSection, agents_available: bool) -> SettingsSection { |
| 58 | if section == SettingsSection::Agents && !agents_available { |
| 59 | SettingsSection::General |
| 60 | } else { |
| 61 | section |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub struct SettingsDialog { |
| 66 | app: Entity<AppState>, |
| 67 | center: Entity<NotificationCenter>, |
| 68 | section: SettingsSection, |
| 69 | devices: Option<Entity<DevicesView>>, |
| 70 | help: Option<Entity<HelpView>>, |
| 71 | mcp: Option<Entity<McpView>>, |
| 72 | agents: Option<Entity<AgentsSettings>>, |
| 73 | google_client: Option<Entity<GoogleClientForm>>, |
| 74 | caldav: Option<Entity<CalDavForm>>, |
| 75 | ics: Option<Entity<IcsForm>>, |
| 76 | body_scroll: ScrollHandle, |
| 77 | focus_handle: FocusHandle, |
| 78 | search: Entity<InputState>, |
| 79 | _subscriptions: [Subscription; 3], |
| 80 | } |
| 81 | |
| 82 | impl EventEmitter<SettingsDialogEvent> for SettingsDialog {} |
| 83 | |
| 84 | impl SettingsDialog { |
| 85 | pub fn new( |
| 86 | app: Entity<AppState>, |
| 87 | center: Entity<NotificationCenter>, |
| 88 | section: SettingsSection, |
| 89 | window: &mut Window, |
| 90 | cx: &mut Context<Self>, |
| 91 | ) -> Self { |
| 92 | let search = cx.new(|cx| InputState::new(window, cx).placeholder(SEARCH_PLACEHOLDER)); |
| 93 | let subscriptions = [ |
| 94 | cx.observe(¢er, |_, _, cx| cx.notify()), |
| 95 | cx.observe(&app, |_, _, cx| cx.notify()), |
| 96 | cx.subscribe_in( |
| 97 | &search, |
| 98 | window, |
| 99 | |this, _, event: &InputEvent, window, cx| { |
| 100 | if matches!(event, InputEvent::Change) { |
| 101 | this.follow_search(window, cx); |
| 102 | } |
| 103 | }, |
| 104 | ), |
| 105 | ]; |
| 106 | let focus_handle = cx.focus_handle(); |
| 107 | window.focus(&focus_handle, cx); |
| 108 | let mut dialog = SettingsDialog { |
| 109 | app, |
| 110 | center, |
| 111 | section, |
| 112 | devices: None, |
| 113 | help: None, |
| 114 | mcp: None, |
| 115 | agents: None, |
| 116 | google_client: None, |
| 117 | caldav: None, |
| 118 | ics: None, |
| 119 | body_scroll: ScrollHandle::new(), |
| 120 | focus_handle, |
| 121 | search, |
| 122 | _subscriptions: subscriptions, |
| 123 | }; |
| 124 | dialog.show(section, window, cx); |
| 125 | dialog |
| 126 | } |
| 127 | |
| 128 | pub fn show(&mut self, section: SettingsSection, window: &mut Window, cx: &mut Context<Self>) { |
| 129 | let section = available(section, crate::agents::runs_handle(cx).is_some()); |
| 130 | if section != self.section { |
| 131 | self.body_scroll = ScrollHandle::new(); |
| 132 | } |
| 133 | self.section = section; |
| 134 | if section == SettingsSection::Devices { |
| 135 | if self.devices.is_none() { |
| 136 | let app = self.app.clone(); |
| 137 | self.devices = Some(cx.new(|cx| DevicesView::new(app, cx))); |
| 138 | } |
| 139 | } else { |
| 140 | self.devices = None; |
| 141 | } |
| 142 | if section == SettingsSection::Help { |
| 143 | if self.help.is_none() { |
| 144 | let app = self.app.clone(); |
| 145 | self.help = Some(cx.new(|cx| HelpView::new(app, cx))); |
| 146 | } else if let Some(help) = self.help.as_ref() { |
| 147 | help.update(cx, |view, cx| { |
| 148 | view.reload_bundles(); |
| 149 | cx.notify(); |
| 150 | }); |
| 151 | } |
| 152 | } else { |
| 153 | self.help = None; |
| 154 | } |
| 155 | if section == SettingsSection::Mcp && self.mcp.is_none() { |
| 156 | let app = self.app.clone(); |
| 157 | self.mcp = Some(cx.new(|cx| McpView::new(app, window, cx))); |
| 158 | } |
| 159 | if section == SettingsSection::Agents && self.agents.is_none() { |
| 160 | self.agents = crate::agents::runs_handle(cx) |
| 161 | .map(|runs| cx.new(|cx| AgentsSettings::new(runs, cx))); |
| 162 | } |
| 163 | if section == SettingsSection::CalendarAccounts { |
| 164 | if self.google_client.is_none() { |
| 165 | let app = self.app.clone(); |
| 166 | self.google_client = Some(cx.new(|cx| GoogleClientForm::new(app, window, cx))); |
| 167 | } |
| 168 | if self.caldav.is_none() { |
| 169 | let app = self.app.clone(); |
| 170 | self.caldav = Some(cx.new(|cx| CalDavForm::new(app, window, cx))); |
| 171 | } |
| 172 | if self.ics.is_none() { |
| 173 | let app = self.app.clone(); |
| 174 | self.ics = Some(cx.new(|cx| IcsForm::new(app, window, cx))); |
| 175 | } |
| 176 | } |
| 177 | cx.notify(); |
| 178 | } |
| 179 | |
| 180 | fn follow_search(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 181 | let query = self.search.read(cx).value().to_string(); |
| 182 | if !matches(self.section, &query) { |
| 183 | let agents_available = crate::agents::runs_handle(cx).is_some(); |
| 184 | if let Some(section) = first_match(&query, agents_available) { |
| 185 | self.show(section, window, cx); |
| 186 | } |
| 187 | } |
| 188 | cx.notify(); |
| 189 | } |
| 190 | |
| 191 | fn search_input(&self) -> AnyElement { |
| 192 | h_flex() |
| 193 | .h(px(SEARCH_HEIGHT)) |
| 194 | .w_full() |
| 195 | .px(px(10.)) |
| 196 | .gap(px(8.)) |
| 197 | .items_center() |
| 198 | .rounded(px(6.)) |
| 199 | .bg(theme::popup()) |
| 200 | .child(icon(IconName::Search, 14., theme::text_muted())) |
| 201 | .child( |
| 202 | div() |
| 203 | .flex_1() |
| 204 | .min_w_0() |
| 205 | .child(Input::new(&self.search).appearance(false).bordered(false)), |
| 206 | ) |
| 207 | .into_any_element() |
| 208 | } |
| 209 | |
| 210 | fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) { |
| 211 | if event.keystroke.key == "escape" { |
| 212 | cx.emit(SettingsDialogEvent::Close); |
| 213 | cx.stop_propagation(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | fn content(&self, cx: &mut Context<Self>) -> AnyElement { |
| 218 | match self.section { |
| 219 | SettingsSection::General => { |
| 220 | general::render(&self.center, &self.app, cx).into_any_element() |
| 221 | } |
| 222 | SettingsSection::Notifications => { |
| 223 | notifications::render(&self.center, &self.app, cx).into_any_element() |
| 224 | } |
| 225 | SettingsSection::Appearance => appearance::render(&self.app, cx).into_any_element(), |
| 226 | SettingsSection::Chats => chats::render(&self.app, cx).into_any_element(), |
| 227 | SettingsSection::Calls => calls::render(&self.center, cx).into_any_element(), |
| 228 | SettingsSection::Calendar => calendar::render_view(&self.app, cx).into_any_element(), |
| 229 | SettingsSection::CalendarAccounts => calendar::render_accounts( |
| 230 | &self.app, |
| 231 | self.google_client.as_ref(), |
| 232 | self.caldav.as_ref(), |
| 233 | self.ics.as_ref(), |
| 234 | cx, |
| 235 | ) |
| 236 | .into_any_element(), |
| 237 | SettingsSection::Devices => self |
| 238 | .devices |
| 239 | .clone() |
| 240 | .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), |
| 241 | SettingsSection::Agents => self |
| 242 | .agents |
| 243 | .clone() |
| 244 | .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), |
| 245 | SettingsSection::Mcp => self |
| 246 | .mcp |
| 247 | .clone() |
| 248 | .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), |
| 249 | SettingsSection::Help => self |
| 250 | .help |
| 251 | .clone() |
| 252 | .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), |
| 253 | SettingsSection::About => about::render().into_any_element(), |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | fn header(&self, metrics: DialogMetrics, cx: &mut Context<Self>) -> Div { |
| 258 | h_flex() |
| 259 | .debug_selector(|| "settings/main/header".into()) |
| 260 | .w_full() |
| 261 | .flex_none() |
| 262 | .items_center() |
| 263 | .pt(px(metrics.padding)) |
| 264 | .pr(px(metrics.padding)) |
| 265 | .pb(px(metrics.padding / 2.)) |
| 266 | .pl(px(metrics.padding + ROW_INSET)) |
| 267 | .child( |
| 268 | div() |
| 269 | .flex_1() |
| 270 | .min_w_0() |
| 271 | .truncate() |
| 272 | .text_size(px(20.)) |
| 273 | .line_height(px(28.)) |
| 274 | .font_weight(FontWeight::SEMIBOLD) |
| 275 | .text_color(theme::text()) |
| 276 | .child(label(self.section)), |
| 277 | ) |
| 278 | .child( |
| 279 | div() |
| 280 | .id("settings-close") |
| 281 | .debug_selector(|| "settings/main/header/close".into()) |
| 282 | .size(px(CLOSE_SIZE)) |
| 283 | .flex_none() |
| 284 | .flex() |
| 285 | .items_center() |
| 286 | .justify_center() |
| 287 | .rounded(px(6.)) |
| 288 | .cursor_pointer() |
| 289 | .hover(|button| button.bg(theme::popup_hover())) |
| 290 | .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))) |
| 291 | .child(icon(IconName::X, 16., theme::text_muted())), |
| 292 | ) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | impl Render for SettingsDialog { |
| 297 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 298 | let viewport = window.viewport_size(); |
| 299 | let metrics = metrics(f32::from(viewport.width), f32::from(viewport.height)); |
| 300 | let dialog = cx.entity(); |
| 301 | let query = self.search.read(cx).value().to_string(); |
| 302 | let nav = render_nav( |
| 303 | self.section, |
| 304 | crate::agents::runs_handle(cx).is_some(), |
| 305 | NavLayout { |
| 306 | width: metrics.nav_width, |
| 307 | padding_y: metrics.padding, |
| 308 | }, |
| 309 | NavSearch { |
| 310 | input: self.search_input(), |
| 311 | query: &query, |
| 312 | }, |
| 313 | move |section, window, cx| { |
| 314 | dialog.update(cx, |dialog, cx| dialog.show(section, window, cx)) |
| 315 | }, |
| 316 | ); |
| 317 | let body_inset = if self.section == SettingsSection::Agents { |
| 318 | metrics.padding + ROW_INSET - PAGE_PADDING |
| 319 | } else { |
| 320 | metrics.padding |
| 321 | }; |
| 322 | let help_preview = self |
| 323 | .help |
| 324 | .as_ref() |
| 325 | .is_some_and(|help| help.read(cx).is_previewing()); |
| 326 | let content = div() |
| 327 | .debug_selector(|| "settings/main/body/content".into()) |
| 328 | .w_full() |
| 329 | .max_w(px(CONTENT_MAX_WIDTH)) |
| 330 | .when(help_preview, |content| content.h_full().min_h_0()) |
| 331 | .child(self.content(cx)); |
| 332 | let scroll_area = if help_preview { |
| 333 | div() |
| 334 | .id(SharedString::from(format!( |
| 335 | "settings-body-{}", |
| 336 | label(self.section) |
| 337 | ))) |
| 338 | .w_full() |
| 339 | .flex_1() |
| 340 | .min_h_0() |
| 341 | .overflow_hidden() |
| 342 | .px(px(body_inset.max(0.))) |
| 343 | .pb(px(metrics.padding)) |
| 344 | .child(content) |
| 345 | .into_any_element() |
| 346 | } else { |
| 347 | div() |
| 348 | .id(SharedString::from(format!( |
| 349 | "settings-body-{}", |
| 350 | label(self.section) |
| 351 | ))) |
| 352 | .w_full() |
| 353 | .flex_1() |
| 354 | .min_h_0() |
| 355 | .overflow_y_scrollbar() |
| 356 | .track_scroll(&self.body_scroll) |
| 357 | .px(px(body_inset.max(0.))) |
| 358 | .pb(px(metrics.padding)) |
| 359 | .child(content) |
| 360 | .into_any_element() |
| 361 | }; |
| 362 | let body = div() |
| 363 | .debug_selector(|| "settings/main/body".into()) |
| 364 | .w_full() |
| 365 | .flex_1() |
| 366 | .min_h_0() |
| 367 | .flex() |
| 368 | .flex_col() |
| 369 | .child(scroll_area); |
| 370 | div() |
| 371 | .id("settings-layer") |
| 372 | .absolute() |
| 373 | .top_0() |
| 374 | .left_0() |
| 375 | .size_full() |
| 376 | .child( |
| 377 | div() |
| 378 | .id("settings-backdrop") |
| 379 | .absolute() |
| 380 | .top_0() |
| 381 | .left_0() |
| 382 | .size_full() |
| 383 | .bg(black().opacity(BACKDROP_OPACITY)) |
| 384 | .occlude() |
| 385 | .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))), |
| 386 | ) |
| 387 | .child( |
| 388 | h_flex() |
| 389 | .id("settings-dialog") |
| 390 | .debug_selector(|| "settings".into()) |
| 391 | .track_focus(&self.focus_handle) |
| 392 | .on_key_down(cx.listener(Self::on_key_down)) |
| 393 | .absolute() |
| 394 | .top(px(metrics.inset)) |
| 395 | .left(px(metrics.inset)) |
| 396 | .right(px(metrics.inset)) |
| 397 | .bottom(px(metrics.inset)) |
| 398 | .overflow_hidden() |
| 399 | .rounded(px(PANEL_RADIUS)) |
| 400 | .dialog_surface() |
| 401 | .text_color(theme::text()) |
| 402 | .occlude() |
| 403 | .child(nav) |
| 404 | .child( |
| 405 | v_flex() |
| 406 | .debug_selector(|| "settings/main".into()) |
| 407 | .flex_1() |
| 408 | .min_w_0() |
| 409 | .h_full() |
| 410 | .child(self.header(metrics, cx)) |
| 411 | .child(with_top_edge(body, &self.body_scroll, BODY_SCROLL_DIVIDER)), |
| 412 | ), |
| 413 | ) |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | #[cfg(test)] |
| 418 | mod tests { |
| 419 | use super::{available, metrics}; |
| 420 | use crate::settings_section::SettingsSection; |
| 421 | |
| 422 | #[test] |
| 423 | fn small_windows_get_the_compact_inset() { |
| 424 | assert_eq!(metrics(1280., 800.).inset, 48.); |
| 425 | assert_eq!(metrics(960., 640.).inset, 24.); |
| 426 | assert_eq!(metrics(1280., 640.).inset, 24.); |
| 427 | } |
| 428 | |
| 429 | #[test] |
| 430 | fn nav_keeps_the_teams_list_pane_minimum_width() { |
| 431 | for (width, height) in [(640., 480.), (960., 640.), (1280., 800.), (1920., 1080.)] { |
| 432 | let metrics = metrics(width, height); |
| 433 | assert_eq!(metrics.nav_width, 280.); |
| 434 | assert_eq!(metrics.padding, 24.); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | #[test] |
| 439 | fn agents_falls_back_to_general_without_agent_runs() { |
| 440 | assert_eq!( |
| 441 | available(SettingsSection::Agents, false), |
| 442 | SettingsSection::General |
| 443 | ); |
| 444 | assert_eq!( |
| 445 | available(SettingsSection::Agents, true), |
| 446 | SettingsSection::Agents |
| 447 | ); |
| 448 | assert_eq!(available(SettingsSection::Mcp, false), SettingsSection::Mcp); |
| 449 | } |
| 450 | } |