use app_ui_kit::settings_controls::PAGE_PADDING; use gpui_kit::assets::IconName; use gpui_kit::component::input::{Input, InputEvent, InputState}; use gpui_kit::component::scroll::ScrollableElement as _; use gpui_kit::component::{h_flex, v_flex}; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use super::caldav_form::{CalDavForm, IcsForm}; use super::devices::DevicesView; use super::google_client_form::GoogleClientForm; use super::help::HelpView; use super::mcp::McpView; use super::nav::{NavLayout, NavSearch, first_match, label, matches, render_nav}; use super::rows::ROW_INSET; use super::{about, appearance, calendar, calls, chats, general, notifications}; use crate::agents::AgentsSettings; use crate::app_state::AppState; use crate::notify::NotificationCenter; use crate::settings_section::SettingsSection; use crate::theme::{self, ElevatedSurface as _}; use crate::views::scroll_edge::with_top_edge; use crate::views::widgets::icon; pub const BODY_SCROLL_DIVIDER: &str = "settings/main/body/divider-scroll"; const BACKDROP_OPACITY: f32 = 0.55; pub(super) const PANEL_RADIUS: f32 = 10.; const CONTENT_MAX_WIDTH: f32 = 744.; const CLOSE_SIZE: f32 = 32.; const COMPACT_BELOW_WIDTH: f32 = 1100.; const COMPACT_BELOW_HEIGHT: f32 = 720.; const NAV_WIDTH: f32 = 280.; const PADDING: f32 = 24.; const SEARCH_HEIGHT: f32 = 32.; const SEARCH_PLACEHOLDER: &str = "Find in Settings"; pub enum SettingsDialogEvent { Close, } #[derive(Debug, Clone, Copy, PartialEq)] pub struct DialogMetrics { pub inset: f32, pub nav_width: f32, pub padding: f32, } pub fn metrics(window_width: f32, window_height: f32) -> DialogMetrics { let compact = window_width < COMPACT_BELOW_WIDTH || window_height < COMPACT_BELOW_HEIGHT; DialogMetrics { inset: if compact { 24. } else { 48. }, nav_width: NAV_WIDTH, padding: PADDING, } } pub fn available(section: SettingsSection, agents_available: bool) -> SettingsSection { if section == SettingsSection::Agents && !agents_available { SettingsSection::General } else { section } } pub struct SettingsDialog { app: Entity, center: Entity, section: SettingsSection, devices: Option>, help: Option>, mcp: Option>, agents: Option>, google_client: Option>, caldav: Option>, ics: Option>, body_scroll: ScrollHandle, focus_handle: FocusHandle, search: Entity, _subscriptions: [Subscription; 3], } impl EventEmitter for SettingsDialog {} impl SettingsDialog { pub fn new( app: Entity, center: Entity, section: SettingsSection, window: &mut Window, cx: &mut Context, ) -> Self { let search = cx.new(|cx| InputState::new(window, cx).placeholder(SEARCH_PLACEHOLDER)); let subscriptions = [ cx.observe(¢er, |_, _, cx| cx.notify()), cx.observe(&app, |_, _, cx| cx.notify()), cx.subscribe_in( &search, window, |this, _, event: &InputEvent, window, cx| { if matches!(event, InputEvent::Change) { this.follow_search(window, cx); } }, ), ]; let focus_handle = cx.focus_handle(); window.focus(&focus_handle, cx); let mut dialog = SettingsDialog { app, center, section, devices: None, help: None, mcp: None, agents: None, google_client: None, caldav: None, ics: None, body_scroll: ScrollHandle::new(), focus_handle, search, _subscriptions: subscriptions, }; dialog.show(section, window, cx); dialog } pub fn show(&mut self, section: SettingsSection, window: &mut Window, cx: &mut Context) { let section = available(section, crate::agents::runs_handle(cx).is_some()); if section != self.section { self.body_scroll = ScrollHandle::new(); } self.section = section; if section == SettingsSection::Devices { if self.devices.is_none() { let app = self.app.clone(); self.devices = Some(cx.new(|cx| DevicesView::new(app, cx))); } } else { self.devices = None; } if section == SettingsSection::Help { if self.help.is_none() { let app = self.app.clone(); self.help = Some(cx.new(|cx| HelpView::new(app, cx))); } else if let Some(help) = self.help.as_ref() { help.update(cx, |view, cx| { view.reload_bundles(); cx.notify(); }); } } else { self.help = None; } if section == SettingsSection::Mcp && self.mcp.is_none() { let app = self.app.clone(); self.mcp = Some(cx.new(|cx| McpView::new(app, window, cx))); } if section == SettingsSection::Agents && self.agents.is_none() { self.agents = crate::agents::runs_handle(cx) .map(|runs| cx.new(|cx| AgentsSettings::new(runs, cx))); } if section == SettingsSection::CalendarAccounts { if self.google_client.is_none() { let app = self.app.clone(); self.google_client = Some(cx.new(|cx| GoogleClientForm::new(app, window, cx))); } if self.caldav.is_none() { let app = self.app.clone(); self.caldav = Some(cx.new(|cx| CalDavForm::new(app, window, cx))); } if self.ics.is_none() { let app = self.app.clone(); self.ics = Some(cx.new(|cx| IcsForm::new(app, window, cx))); } } cx.notify(); } fn follow_search(&mut self, window: &mut Window, cx: &mut Context) { let query = self.search.read(cx).value().to_string(); if !matches(self.section, &query) { let agents_available = crate::agents::runs_handle(cx).is_some(); if let Some(section) = first_match(&query, agents_available) { self.show(section, window, cx); } } cx.notify(); } fn search_input(&self) -> AnyElement { h_flex() .h(px(SEARCH_HEIGHT)) .w_full() .px(px(10.)) .gap(px(8.)) .items_center() .rounded(px(6.)) .bg(theme::popup()) .child(icon(IconName::Search, 14., theme::text_muted())) .child( div() .flex_1() .min_w_0() .child(Input::new(&self.search).appearance(false).bordered(false)), ) .into_any_element() } fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context) { if event.keystroke.key == "escape" { cx.emit(SettingsDialogEvent::Close); cx.stop_propagation(); } } fn content(&self, cx: &mut Context) -> AnyElement { match self.section { SettingsSection::General => { general::render(&self.center, &self.app, cx).into_any_element() } SettingsSection::Notifications => { notifications::render(&self.center, &self.app, cx).into_any_element() } SettingsSection::Appearance => appearance::render(&self.app, cx).into_any_element(), SettingsSection::Chats => chats::render(&self.app, cx).into_any_element(), SettingsSection::Calls => calls::render(&self.center, cx).into_any_element(), SettingsSection::Calendar => calendar::render_view(&self.app, cx).into_any_element(), SettingsSection::CalendarAccounts => calendar::render_accounts( &self.app, self.google_client.as_ref(), self.caldav.as_ref(), self.ics.as_ref(), cx, ) .into_any_element(), SettingsSection::Devices => self .devices .clone() .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), SettingsSection::Agents => self .agents .clone() .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), SettingsSection::Mcp => self .mcp .clone() .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), SettingsSection::Help => self .help .clone() .map_or_else(|| div().into_any_element(), |view| view.into_any_element()), SettingsSection::About => about::render().into_any_element(), } } fn header(&self, metrics: DialogMetrics, cx: &mut Context) -> Div { h_flex() .debug_selector(|| "settings/main/header".into()) .w_full() .flex_none() .items_center() .pt(px(metrics.padding)) .pr(px(metrics.padding)) .pb(px(metrics.padding / 2.)) .pl(px(metrics.padding + ROW_INSET)) .child( div() .flex_1() .min_w_0() .truncate() .text_size(px(20.)) .line_height(px(28.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::text()) .child(label(self.section)), ) .child( div() .id("settings-close") .debug_selector(|| "settings/main/header/close".into()) .size(px(CLOSE_SIZE)) .flex_none() .flex() .items_center() .justify_center() .rounded(px(6.)) .cursor_pointer() .hover(|button| button.bg(theme::popup_hover())) .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))) .child(icon(IconName::X, 16., theme::text_muted())), ) } } impl Render for SettingsDialog { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { let viewport = window.viewport_size(); let metrics = metrics(f32::from(viewport.width), f32::from(viewport.height)); let dialog = cx.entity(); let query = self.search.read(cx).value().to_string(); let nav = render_nav( self.section, crate::agents::runs_handle(cx).is_some(), NavLayout { width: metrics.nav_width, padding_y: metrics.padding, }, NavSearch { input: self.search_input(), query: &query, }, move |section, window, cx| { dialog.update(cx, |dialog, cx| dialog.show(section, window, cx)) }, ); let body_inset = if self.section == SettingsSection::Agents { metrics.padding + ROW_INSET - PAGE_PADDING } else { metrics.padding }; let help_preview = self .help .as_ref() .is_some_and(|help| help.read(cx).is_previewing()); let content = div() .debug_selector(|| "settings/main/body/content".into()) .w_full() .max_w(px(CONTENT_MAX_WIDTH)) .when(help_preview, |content| content.h_full().min_h_0()) .child(self.content(cx)); let scroll_area = if help_preview { div() .id(SharedString::from(format!( "settings-body-{}", label(self.section) ))) .w_full() .flex_1() .min_h_0() .overflow_hidden() .px(px(body_inset.max(0.))) .pb(px(metrics.padding)) .child(content) .into_any_element() } else { div() .id(SharedString::from(format!( "settings-body-{}", label(self.section) ))) .w_full() .flex_1() .min_h_0() .overflow_y_scrollbar() .track_scroll(&self.body_scroll) .px(px(body_inset.max(0.))) .pb(px(metrics.padding)) .child(content) .into_any_element() }; let body = div() .debug_selector(|| "settings/main/body".into()) .w_full() .flex_1() .min_h_0() .flex() .flex_col() .child(scroll_area); div() .id("settings-layer") .absolute() .top_0() .left_0() .size_full() .child( div() .id("settings-backdrop") .absolute() .top_0() .left_0() .size_full() .bg(black().opacity(BACKDROP_OPACITY)) .occlude() .on_click(cx.listener(|_, _, _, cx| cx.emit(SettingsDialogEvent::Close))), ) .child( h_flex() .id("settings-dialog") .debug_selector(|| "settings".into()) .track_focus(&self.focus_handle) .on_key_down(cx.listener(Self::on_key_down)) .absolute() .top(px(metrics.inset)) .left(px(metrics.inset)) .right(px(metrics.inset)) .bottom(px(metrics.inset)) .overflow_hidden() .rounded(px(PANEL_RADIUS)) .dialog_surface() .text_color(theme::text()) .occlude() .child(nav) .child( v_flex() .debug_selector(|| "settings/main".into()) .flex_1() .min_w_0() .h_full() .child(self.header(metrics, cx)) .child(with_top_edge(body, &self.body_scroll, BODY_SCROLL_DIVIDER)), ), ) } } #[cfg(test)] mod tests { use super::{available, metrics}; use crate::settings_section::SettingsSection; #[test] fn small_windows_get_the_compact_inset() { assert_eq!(metrics(1280., 800.).inset, 48.); assert_eq!(metrics(960., 640.).inset, 24.); assert_eq!(metrics(1280., 640.).inset, 24.); } #[test] fn nav_keeps_the_teams_list_pane_minimum_width() { for (width, height) in [(640., 480.), (960., 640.), (1280., 800.), (1920., 1080.)] { let metrics = metrics(width, height); assert_eq!(metrics.nav_width, 280.); assert_eq!(metrics.padding, 24.); } } #[test] fn agents_falls_back_to_general_without_agent_runs() { assert_eq!( available(SettingsSection::Agents, false), SettingsSection::General ); assert_eq!( available(SettingsSection::Agents, true), SettingsSection::Agents ); assert_eq!(available(SettingsSection::Mcp, false), SettingsSection::Mcp); } }