diff --git a/assets/settings/default.json b/assets/settings/default.json index eb13c39315f..d72a7123730 100644 --- a/assets/settings/default.json +++ b/assets/settings/default.json @@ -618,6 +618,8 @@ "fullscreen_mode": "native", // Titlebar related settings "title_bar": { + // When to show the title bar: "always" | "never" | "hide_in_full_screen". + "show": "always", // Whether to show git status indicators on the branch icon in the titlebar. "show_branch_status_icon": false, // Whether to show the branch name button in the titlebar. diff --git a/crates/agent_ui/src/threads_archive_view.rs b/crates/agent_ui/src/threads_archive_view.rs index 2e909f1ea31..9df8ed454f3 100644 --- a/crates/agent_ui/src/threads_archive_view.rs +++ b/crates/agent_ui/src/threads_archive_view.rs @@ -28,7 +28,7 @@ use picker::{ Picker, PickerDelegate, highlighted_match_with_paths::{HighlightedMatch, HighlightedMatchWithPaths}, }; -use platform_title_bar::apply_title_bar_insets; +use platform_title_bar::{SidebarWindowControls, apply_title_bar_insets}; use project::{AgentId, AgentServerStore}; use settings::Settings as _; use theme::ActiveTheme; @@ -859,16 +859,11 @@ impl ThreadsArchiveView { fn render_header(&self, window: &Window, cx: &mut Context) -> impl IntoElement { let has_query = !self.filter_editor.read(cx).text(cx).is_empty(); - let sidebar_on_left = matches!( - AgentSettings::get_global(cx).sidebar_side(), - settings::SidebarSide::Left - ); - let sidebar_on_right = !sidebar_on_left; - let not_fullscreen = !window.is_fullscreen() && !window.is_simple_fullscreen(); - let traffic_lights = cfg!(target_os = "macos") && not_fullscreen && sidebar_on_left; - let left_window_controls = !cfg!(target_os = "macos") && not_fullscreen && sidebar_on_left; - let right_window_controls = - !cfg!(target_os = "macos") && not_fullscreen && sidebar_on_right; + let SidebarWindowControls { + traffic_lights, + left: left_window_controls, + right: right_window_controls, + } = SidebarWindowControls::new(AgentSettings::get_global(cx).sidebar_side(), window, cx); let header_height = platform_title_bar_height(window); let show_focus_keybinding = self.selection.is_some() && !self.filter_editor.focus_handle(cx).is_focused(window); @@ -906,6 +901,7 @@ impl ThreadsArchiveView { }) .child( h_flex() + .debug_selector(|| "threads-archive-header-search".into()) .ml_1() .min_w_0() .w_full() diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index d1797d5ff18..beff5ea7a8f 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -1,12 +1,13 @@ use crate::{ Action, AnyView, AnyWindowHandle, App, AppCell, AppContext, AsyncApp, AvailableSpace, - BackgroundExecutor, BorrowAppContext, Bounds, Capslock, ClipboardItem, DisplayId, DrawPhase, - Drawable, Element, Empty, EntityId, EventEmitter, ForegroundExecutor, Global, InputEvent, - Keystroke, Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMoveEvent, - MouseUpEvent, Pixels, Platform, PlatformTextSystem, Point, Render, Result, SharedString, Size, - SystemNotification, SystemNotificationResponse, Task, TestDispatcher, TestPlatform, - TestScreenCaptureSource, TestWindow, TextSystem, VisualContext, Window, WindowBounds, - WindowHandle, WindowOptions, WindowVisibility, app::GpuiMode, window::ElementArenaScope, + BackgroundExecutor, BorrowAppContext, Bounds, Capslock, ClipboardItem, Decorations, DisplayId, + DrawPhase, Drawable, Element, Empty, EntityId, EventEmitter, ForegroundExecutor, Global, + InputEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, + MouseMoveEvent, MouseUpEvent, Pixels, Platform, PlatformTextSystem, Point, Render, Result, + SharedString, Size, SystemNotification, SystemNotificationResponse, Task, TestDispatcher, + TestPlatform, TestScreenCaptureSource, TestWindow, TextSystem, VisualContext, Window, + WindowBounds, WindowHandle, WindowOptions, WindowVisibility, app::GpuiMode, + window::ElementArenaScope, }; use anyhow::{anyhow, bail}; use futures::{Stream, StreamExt, channel::oneshot}; @@ -477,6 +478,16 @@ impl TestAppContext { .simulate_visibility_change(visibility); } + /// Simulates the platform changing how the window is decorated. + pub fn simulate_window_decorations_change( + &self, + window_handle: AnyWindowHandle, + decorations: Decorations, + ) { + self.test_window(window_handle) + .simulate_decorations_change(decorations); + } + /// Simulates connecting a display. pub fn simulate_display_added( &self, diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index a1bbff3cd24..916f2d4dc7e 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -1377,6 +1377,8 @@ pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle { } #[cfg(target_os = "macos")] fn set_traffic_light_position(&self, _position: Point) {} + #[cfg(target_os = "macos")] + fn set_native_titlebar_visibility(&self, _visible: bool) {} fn show_character_palette(&self) {} fn titlebar_double_click(&self, _is_resizable: bool, _is_minimizable: bool) {} fn on_move_tab_to_new_window(&self, _callback: Box) {} diff --git a/crates/gpui/src/platform/test/window.rs b/crates/gpui/src/platform/test/window.rs index 4a8ccc67ee4..ff1c4cd3805 100644 --- a/crates/gpui/src/platform/test/window.rs +++ b/crates/gpui/src/platform/test/window.rs @@ -1,10 +1,10 @@ use crate::{ - AnyWindowHandle, Bounds, DevicePixels, DispatchEventResult, DisplayId, GpuSpecs, HeadlessAtlas, - Pixels, PlatformAtlas, PlatformDisplay, PlatformHeadlessRenderer, PlatformInput, - PlatformInputHandler, PlatformWindow, Point, PromptButton, RequestFrameOptions, Scene, Size, - TestDisplay, TestPlatform, TextInputConfiguration, TextInputStateChange, WindowAppearance, - WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowInsets, WindowParams, - WindowVisibility, + AnyWindowHandle, Bounds, Decorations, DevicePixels, DispatchEventResult, DisplayId, GpuSpecs, + HeadlessAtlas, Pixels, Platform, PlatformAtlas, PlatformDisplay, PlatformHeadlessRenderer, + PlatformInput, PlatformInputHandler, PlatformWindow, Point, PromptButton, RequestFrameOptions, + Scene, Size, TestDisplay, TestPlatform, TextInputConfiguration, TextInputStateChange, + WindowAppearance, WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowInsets, + WindowParams, WindowVisibility, }; use gpui_util::ResultExt as _; #[cfg(any(test, feature = "test-support"))] @@ -57,6 +57,7 @@ pub(crate) struct TestWindowState { is_fullscreen: bool, scale_factor: f32, appearance: WindowAppearance, + decorations: Decorations, external_drag_files: Vec<(PathBuf, bool)>, start_external_drag_result: bool, } @@ -133,6 +134,7 @@ impl TestWindow { // Preserve the test platform's historical 2x default. scale_factor: 2.0, appearance: WindowAppearance::Light, + decorations: Decorations::default(), external_drag_files: Vec::new(), start_external_drag_result: false, }))) @@ -277,6 +279,17 @@ impl TestWindow { self.0.lock().appearance_change_callback = Some(callback); } + pub fn simulate_decorations_change(&self, decorations: Decorations) { + let mut lock = self.0.lock(); + lock.decorations = decorations; + let Some(mut callback) = lock.appearance_change_callback.take() else { + return; + }; + drop(lock); + callback(); + self.0.lock().appearance_change_callback = Some(callback); + } + /// Returns how many times this window's frame waker has been invoked. pub fn frame_wake_count(&self) -> usize { self.0.lock().frame_wake_count.get() @@ -478,8 +491,21 @@ impl PlatformWindow for TestWindow { } fn toggle_fullscreen(&self) { - let mut lock = self.0.lock(); - lock.is_fullscreen = !lock.is_fullscreen; + let platform = { + let mut lock = self.0.lock(); + lock.is_fullscreen = !lock.is_fullscreen; + lock.platform.upgrade() + }; + if let Some(platform) = platform { + let mut window = self.clone(); + platform + .foreground_executor() + .spawn(async move { + let size = window.0.lock().bounds.size; + window.simulate_resize(size); + }) + .detach(); + } } fn is_fullscreen(&self) -> bool { @@ -619,4 +645,8 @@ impl PlatformWindow for TestWindow { fn gpu_specs(&self) -> Option { None } + + fn window_decorations(&self) -> Decorations { + self.0.lock().decorations + } } diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index 0bf91d489c3..7b87bf6b63a 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -633,6 +633,15 @@ impl FocusHandle { self.id.contains(other.id, window) } + /// Obtains whether the element associated with this handle is part of the most recently rendered frame. + pub fn is_rendered(&self, window: &Window) -> bool { + window + .rendered_frame + .dispatch_tree + .focusable_node_id(self.id) + .is_some() + } + /// Dispatch an action on the element that rendered this focus handle pub fn dispatch_action(&self, action: &dyn Action, window: &mut Window, cx: &mut App) { if let Some(node_id) = window @@ -3305,6 +3314,12 @@ impl Window { self.platform_window.set_traffic_light_position(position); } + /// Shows or hides the native macOS titlebar, including the traffic light buttons. + #[cfg(target_os = "macos")] + pub fn set_native_titlebar_visibility(&self, visible: bool) { + self.platform_window.set_native_titlebar_visibility(visible); + } + /// Sets the application identifier. pub fn set_app_id(&mut self, app_id: &str) { self.platform_window.set_app_id(app_id); @@ -8982,6 +8997,50 @@ mod tests { } } + #[gpui::test] + fn test_fullscreen_toggle_reports_current_bounds(cx: &mut TestAppContext) { + let window = cx.add_window(|_, _| EmptyView); + let handle = AnyWindowHandle::from(window); + let notifications = Rc::new(Cell::new(0)); + window + .update(cx, { + let notifications = notifications.clone(); + |_, window, cx| { + cx.observe_window_bounds(window, move |_, _, _| { + notifications.set(notifications.get() + 1) + }) + .detach(); + } + }) + .unwrap(); + let window_state = |cx: &mut TestAppContext| { + cx.update_window(handle, |_, window, _| { + ( + window.is_fullscreen(), + window.bounds().size, + window.viewport_size(), + ) + }) + .unwrap() + }; + let initial_size = window_state(cx).1; + + window + .update(cx, |_, window, _| window.toggle_fullscreen()) + .unwrap(); + cx.run_until_parked(); + assert_eq!(window_state(cx), (true, initial_size, initial_size)); + assert_eq!(notifications.get(), 1); + + let resized_size = size(px(800.), px(600.)); + window + .update(cx, |_, window, _| window.toggle_fullscreen()) + .unwrap(); + cx.simulate_window_resize(handle, resized_size); + cx.run_until_parked(); + assert_eq!(window_state(cx), (false, resized_size, resized_size)); + } + /// Platforms that stop requesting frames for idle windows (currently web) /// rely on the frame waker firing whenever frame demand arises; a demand /// source that skips the waker shows up there as a window that silently diff --git a/crates/gpui_linux/src/linux/wayland/window.rs b/crates/gpui_linux/src/linux/wayland/window.rs index ae4b522d361..1f7d22d9a89 100644 --- a/crates/gpui_linux/src/linux/wayland/window.rs +++ b/crates/gpui_linux/src/linux/wayland/window.rs @@ -1114,8 +1114,8 @@ impl WaylandWindowStatePtr { if let Some(mut configure) = state.in_progress_configure.take() { let got_unmaximized = state.maximized && !configure.maximized; - let mode_changed = state.fullscreen != configure.fullscreen - || state.maximized != configure.maximized; + let fullscreen_changed = state.fullscreen != configure.fullscreen; + let mode_changed = fullscreen_changed || state.maximized != configure.maximized; state.fullscreen = configure.fullscreen; state.maximized = configure.maximized; state.tiling = configure.tiling; @@ -1148,14 +1148,17 @@ impl WaylandWindowStatePtr { if visibility_changed { self.report_visibility(configure.visibility); } + if !throttled && let Some(size) = configure.size { + self.resize(size); + } + if fullscreen_changed { + self.notify_resize(); + } if throttled { return; } - if let Some(size) = configure.size { - self.resize(size); - } // Same-size mode changes skip the resize callback, so report them as a move. - if mode_changed && !resized { + if mode_changed && !resized && !fullscreen_changed { let callback = self.callbacks.borrow_mut().moved.take(); if let Some(mut fun) = callback { fun(); @@ -1559,6 +1562,18 @@ impl WaylandWindowStatePtr { update_window(state); } + fn notify_resize(&self) { + let (size, scale) = { + let state = self.state.borrow(); + (state.bounds.size, state.scale) + }; + let callback = self.callbacks.borrow_mut().resize.take(); + if let Some(mut fun) = callback { + fun(size, scale); + self.callbacks.borrow_mut().resize = Some(fun); + } + } + pub fn resize(&self, size: Size) { self.set_size_and_scale(Some(size), None); } diff --git a/crates/gpui_linux/src/linux/x11/window.rs b/crates/gpui_linux/src/linux/x11/window.rs index b32047cea1b..33fac161705 100644 --- a/crates/gpui_linux/src/linux/x11/window.rs +++ b/crates/gpui_linux/src/linux/x11/window.rs @@ -1068,7 +1068,11 @@ impl X11WindowStatePtr { pub fn property_notify(&self, event: xproto::PropertyNotifyEvent) -> anyhow::Result<()> { let state = self.state.borrow_mut(); if event.atom == state.atoms._NET_WM_STATE { + let was_fullscreen = state.fullscreen; self.set_wm_properties(state)?; + if self.state.borrow().fullscreen != was_fullscreen { + self.notify_resize(); + } } else if event.atom == state.atoms._GTK_EDGE_CONSTRAINTS { self.set_edge_constraints(state)?; } @@ -1351,6 +1355,18 @@ impl X11WindowStatePtr { Ok(()) } + fn notify_resize(&self) { + let (content_size, scale_factor) = { + let state = self.state.borrow(); + (state.content_size(), state.scale_factor) + }; + let callback = self.callbacks.borrow_mut().resize.take(); + if let Some(mut fun) = callback { + fun(content_size, scale_factor); + self.callbacks.borrow_mut().resize = Some(fun); + } + } + pub fn set_active(&self, focus: bool) { let callback = self.callbacks.borrow_mut().active_status_change.take(); if let Some(mut fun) = callback { diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs index 32a6a540092..bdf3f130243 100644 --- a/crates/gpui_macos/src/window.rs +++ b/crates/gpui_macos/src/window.rs @@ -434,6 +434,10 @@ unsafe fn build_window_class(name: &CStr, superclass: &AnyClass) -> *const AnyCl sel!(windowWillEnterFullScreen:), window_will_enter_fullscreen as extern "C" fn(_, Sel, *mut AnyObject), ); + decl.add_method( + sel!(windowDidFailToEnterFullScreen:), + window_did_fail_to_enter_fullscreen as extern "C" fn(_, Sel, *mut AnyObject), + ); decl.add_method( sel!(windowWillExitFullScreen:), window_will_exit_fullscreen as extern "C" fn(_, Sel, *mut AnyObject), @@ -677,6 +681,7 @@ struct MacWindowState { last_left_mouse_down_event: Option>, synthetic_drag_counter: usize, traffic_light_position: Option>, + native_titlebar_visible: bool, traffic_light_frames: Option, pre_fullscreen_traffic_light_frames: Option, transparent_titlebar: bool, @@ -708,6 +713,10 @@ struct MacWindowState { impl MacWindowState { fn move_traffic_light(&mut self) { + if !self.native_titlebar_visible { + self.update_native_titlebar_visibility(); + } + if let Some(traffic_light_position) = self.traffic_light_position { if self.is_fullscreen() && !self.is_exiting_fullscreen { self.restore_traffic_light(); @@ -767,6 +776,46 @@ impl MacWindowState { } } + fn update_native_titlebar_visibility(&self) { + let Some(close_button) = self + .native_window() + .standardWindowButton(NSWindowButton::CloseButton) + else { + return; + }; + if let Some(titlebar_container) = Self::titlebar_container(&close_button) { + titlebar_container.setHidden(!self.native_titlebar_visible); + } + } + + fn apply_fullscreen_titlebar_opacity(&self) { + let min_version = NSOperatingSystemVersion { + majorVersion: 15, + minorVersion: 3, + patchVersion: 0, + }; + + if is_macos_version_at_least(min_version) { + unsafe { + let _: () = msg_send![self.native_window, setTitlebarAppearsTransparent: Bool::NO]; + } + } + } + + fn restore_windowed_titlebar_transparency(&self) { + let min_version = NSOperatingSystemVersion { + majorVersion: 15, + minorVersion: 3, + patchVersion: 0, + }; + + if is_macos_version_at_least(min_version) && self.transparent_titlebar { + unsafe { + let _: () = msg_send![self.native_window, setTitlebarAppearsTransparent: Bool::YES]; + } + } + } + fn capture_traffic_light_frames(&self) -> Option { let buttons = self.traffic_light_buttons()?; let titlebar_container = Self::titlebar_container(&buttons.close)?; @@ -1142,6 +1191,7 @@ impl MacWindow { traffic_light_position: titlebar .as_ref() .and_then(|titlebar| titlebar.traffic_light_position), + native_titlebar_visible: true, traffic_light_frames: None, pre_fullscreen_traffic_light_frames: None, transparent_titlebar: titlebar @@ -1659,6 +1709,12 @@ impl PlatformWindow for MacWindow { } } + fn set_native_titlebar_visibility(&self, visible: bool) { + let mut state = self.0.lock(); + state.native_titlebar_visible = visible; + state.update_native_titlebar_visibility(); + } + fn set_traffic_light_position(&self, position: Point) { let mut state = self.0.lock(); state.traffic_light_position = Some(position); @@ -3083,37 +3139,22 @@ extern "C" fn window_will_enter_fullscreen(this: &AnyObject, _: Sel, _: *mut Any lock.is_exiting_fullscreen = false; lock.pre_fullscreen_traffic_light_frames = lock.traffic_light_frames; lock.restore_traffic_light(); + lock.apply_fullscreen_titlebar_opacity(); +} - let min_version = NSOperatingSystemVersion { - majorVersion: 15, - minorVersion: 3, - patchVersion: 0, - }; - - if is_macos_version_at_least(min_version) { - unsafe { - let _: () = msg_send![lock.native_window, setTitlebarAppearsTransparent: Bool::NO]; - } - } +extern "C" fn window_did_fail_to_enter_fullscreen(this: &AnyObject, _: Sel, _: *mut AnyObject) { + let window_state = unsafe { get_window_state(this) }; + let mut lock = window_state.as_ref().lock(); + lock.restore_windowed_titlebar_transparency(); + lock.traffic_light_frames = lock.pre_fullscreen_traffic_light_frames.take(); + lock.move_traffic_light(); } extern "C" fn window_will_exit_fullscreen(this: &AnyObject, _: Sel, _: *mut AnyObject) { let window_state = unsafe { get_window_state(this) }; let mut lock = window_state.as_ref().lock(); lock.is_exiting_fullscreen = true; - - let min_version = NSOperatingSystemVersion { - majorVersion: 15, - minorVersion: 3, - patchVersion: 0, - }; - - if is_macos_version_at_least(min_version) && lock.transparent_titlebar { - unsafe { - let _: () = msg_send![lock.native_window, setTitlebarAppearsTransparent: Bool::YES]; - } - } - + lock.restore_windowed_titlebar_transparency(); lock.move_traffic_light(); } @@ -3121,6 +3162,9 @@ extern "C" fn window_did_fail_to_exit_fullscreen(this: &AnyObject, _: Sel, _: *m let window_state = unsafe { get_window_state(this) }; let mut lock = window_state.as_ref().lock(); lock.is_exiting_fullscreen = false; + if lock.is_fullscreen() { + lock.apply_fullscreen_titlebar_opacity(); + } lock.restore_traffic_light(); } diff --git a/crates/platform_title_bar/src/platform_title_bar.rs b/crates/platform_title_bar/src/platform_title_bar.rs index b69bb0d1ff0..16203410e48 100644 --- a/crates/platform_title_bar/src/platform_title_bar.rs +++ b/crates/platform_title_bar/src/platform_title_bar.rs @@ -1,5 +1,6 @@ pub mod platforms; mod system_window_tabs; +mod title_bar_settings; use gpui::{ Action, AnyElement, App, Context, Decorations, Entity, Hsla, InteractiveElement, IntoElement, @@ -16,14 +17,13 @@ use ui::{ }; use workspace::{MultiWorkspace, SidebarRenderState, SidebarSide}; -use crate::{ - platforms::{platform_linux, platform_windows}, - system_window_tabs::SystemWindowTabs, -}; +use crate::platforms::{platform_linux, platform_windows}; pub use system_window_tabs::{ - DraggedWindowTab, MergeAllWindows, MoveTabToNewWindow, ShowNextWindowTab, ShowPreviousWindowTab, + DraggedWindowTab, MergeAllWindows, MoveTabToNewWindow, ShowNextWindowTab, + ShowPreviousWindowTab, SystemWindowTabs, }; +pub use title_bar_settings::TitleBarSettings; /// With client-side decorations, the title bar is pulled over the window frame's /// border to avoid a transparent gap in the rounded corners, and a border in the @@ -48,12 +48,34 @@ pub fn apply_title_bar_insets( .when(right_window_controls, |header| header.pr(inset)) } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct SidebarWindowControls { + pub traffic_lights: bool, + pub left: bool, + pub right: bool, +} + +impl SidebarWindowControls { + pub fn new(sidebar_side: SidebarSide, window: &Window, cx: &App) -> Self { + let visible = !window.is_fullscreen() + && !window.is_simple_fullscreen() + && TitleBarSettings::is_title_bar_visible(window, cx); + let is_macos = cfg!(target_os = "macos"); + let on_left = sidebar_side == SidebarSide::Left; + Self { + traffic_lights: visible && is_macos && on_left, + left: visible && !is_macos && on_left, + right: visible && !is_macos && !on_left, + } + } +} + pub struct PlatformTitleBar { id: ElementId, platform_style: PlatformStyle, children: SmallVec<[AnyElement; 2]>, should_move: bool, - system_window_tabs: Entity, + system_window_tabs: Option>, button_layout: Option, multi_workspace: Option>, } @@ -61,7 +83,7 @@ pub struct PlatformTitleBar { impl PlatformTitleBar { pub fn new(id: impl Into, cx: &mut Context) -> Self { let platform_style = PlatformStyle::platform(); - let system_window_tabs = cx.new(|_cx| SystemWindowTabs::new()); + let system_window_tabs = Some(cx.new(|_cx| SystemWindowTabs::new())); Self { id: id.into(), @@ -74,6 +96,11 @@ impl PlatformTitleBar { } } + pub fn without_system_window_tabs(mut self) -> Self { + self.system_window_tabs = None; + self + } + pub fn with_multi_workspace(mut self, multi_workspace: WeakEntity) -> Self { self.multi_workspace = Some(multi_workspace); self @@ -353,7 +380,7 @@ impl Render for PlatformTitleBar { v_flex() .w_full() .child(title_bar) - .child(self.system_window_tabs.clone().into_any_element()) + .children(self.system_window_tabs.clone()) } } diff --git a/crates/platform_title_bar/src/system_window_tabs.rs b/crates/platform_title_bar/src/system_window_tabs.rs index 507dac96327..55936ef6821 100644 --- a/crates/platform_title_bar/src/system_window_tabs.rs +++ b/crates/platform_title_bar/src/system_window_tabs.rs @@ -425,6 +425,7 @@ impl Render for SystemWindowTabs { } h_flex() + .debug_selector(|| "system-window-tabs".into()) .w_full() .h(Tab::container_height(cx)) .bg(inactive_background_color) diff --git a/crates/title_bar/src/title_bar_settings.rs b/crates/platform_title_bar/src/title_bar_settings.rs similarity index 70% rename from crates/title_bar/src/title_bar_settings.rs rename to crates/platform_title_bar/src/title_bar_settings.rs index 7821dff303c..775532a2ced 100644 --- a/crates/title_bar/src/title_bar_settings.rs +++ b/crates/platform_title_bar/src/title_bar_settings.rs @@ -1,8 +1,9 @@ -use gpui::WindowButtonLayout; -use settings::{RegisterSetting, Settings, SettingsContent}; +use gpui::{App, Window, WindowButtonLayout}; +use settings::{RegisterSetting, Settings, SettingsContent, TitleBarVisibility}; #[derive(Copy, Clone, Debug, RegisterSetting)] pub struct TitleBarSettings { + pub show: TitleBarVisibility, pub show_branch_status_icon: bool, pub show_onboarding_banner: bool, pub show_user_picture: bool, @@ -16,10 +17,23 @@ pub struct TitleBarSettings { pub button_layout: Option, } +impl TitleBarSettings { + pub fn is_title_bar_visible(window: &Window, cx: &App) -> bool { + match Self::get_global(cx).show { + TitleBarVisibility::Always => true, + TitleBarVisibility::Never => false, + TitleBarVisibility::HideInFullScreen => { + !window.is_fullscreen() && !window.is_simple_fullscreen() + } + } + } +} + impl Settings for TitleBarSettings { fn from_settings(s: &SettingsContent) -> Self { let content = s.title_bar.clone().unwrap(); TitleBarSettings { + show: content.show.unwrap(), show_branch_status_icon: content.show_branch_status_icon.unwrap(), show_onboarding_banner: content.show_onboarding_banner.unwrap(), show_user_picture: content.show_user_picture.unwrap(), diff --git a/crates/settings_content/src/title_bar.rs b/crates/settings_content/src/title_bar.rs index d4fc6fe990d..6da390d86a5 100644 --- a/crates/settings_content/src/title_bar.rs +++ b/crates/settings_content/src/title_bar.rs @@ -78,9 +78,36 @@ impl From for WindowButtonLayoutContent { } } +#[derive( + Copy, + Clone, + Debug, + PartialEq, + Eq, + Serialize, + Deserialize, + JsonSchema, + MergeFrom, + strum::VariantArray, + strum::VariantNames, +)] +#[serde(rename_all = "snake_case")] +pub enum TitleBarVisibility { + /// Always show the title bar. + Always, + /// Never show the title bar. + Never, + /// Show the title bar, but hide it while the window is in full screen. + HideInFullScreen, +} + #[with_fallible_options] #[derive(Clone, PartialEq, Default, Serialize, Deserialize, JsonSchema, MergeFrom, Debug)] pub struct TitleBarSettingsContent { + /// When to show the title bar. + /// + /// Default: always + pub show: Option, /// Whether to show git status indicators on the branch icon in the title bar. /// When enabled, the branch icon changes to reflect the current repository /// status (e.g. modified, added, deleted, or conflict). diff --git a/crates/settings_ui/src/page_data.rs b/crates/settings_ui/src/page_data.rs index 3c96ecd45c6..82bdbbec380 100644 --- a/crates/settings_ui/src/page_data.rs +++ b/crates/settings_ui/src/page_data.rs @@ -4423,9 +4423,23 @@ fn window_and_layout_page() -> SettingsPage { ] } - fn title_bar_section() -> [SettingsPageItem; 12] { + fn title_bar_section() -> [SettingsPageItem; 13] { [ SettingsPageItem::SectionHeader("Title Bar"), + SettingsPageItem::SettingItem(SettingItem { + title: "Show Title Bar", + description: "When to show the title bar and its window controls.", + field: Box::new(SettingField { + organization_override: None, + json_path: Some("title_bar.show"), + pick: |settings_content| settings_content.title_bar.as_ref()?.show.as_ref(), + write: |settings_content, value, _| { + settings_content.title_bar.get_or_insert_default().show = value; + }, + }), + metadata: None, + files: USER, + }), SettingsPageItem::SettingItem(SettingItem { title: "Show Branch Status Icon", description: "Show git status indicators on the branch icon in the titlebar.", diff --git a/crates/settings_ui/src/settings_ui.rs b/crates/settings_ui/src/settings_ui.rs index 8d2de7f8b03..26a6e9b9269 100644 --- a/crates/settings_ui/src/settings_ui.rs +++ b/crates/settings_ui/src/settings_ui.rs @@ -670,6 +670,7 @@ fn init_renderers(cx: &mut App) { .add_basic_renderer::(render_dropdown) .add_basic_renderer::(render_dropdown) .add_basic_renderer::(render_dropdown) + .add_basic_renderer::(render_dropdown) .add_basic_renderer::(render_dropdown) .add_basic_renderer::(render_dropdown) .add_basic_renderer::(render_editable_number_field) diff --git a/crates/sidebar/src/sidebar.rs b/crates/sidebar/src/sidebar.rs index fe99729a61d..b033e667d82 100644 --- a/crates/sidebar/src/sidebar.rs +++ b/crates/sidebar/src/sidebar.rs @@ -39,7 +39,7 @@ use menu::{ Cancel, Confirm, SelectChild, SelectFirst, SelectLast, SelectNext, SelectParent, SelectPrevious, }; use notifications::status_toast::StatusToast; -use platform_title_bar::apply_title_bar_insets; +use platform_title_bar::{SidebarWindowControls, apply_title_bar_insets}; use project::{AgentId, AgentRegistryStore, Event as ProjectEvent, WorktreeId}; use recent_projects::sidebar_recent_projects::SidebarRecentProjects; use remote::RemoteConnectionOptions; @@ -7377,13 +7377,11 @@ impl Sidebar { cx: &mut Context, ) -> impl IntoElement { let has_query = self.has_filter_query(cx); - let sidebar_on_left = self.side(cx) == SidebarSide::Left; - let sidebar_on_right = self.side(cx) == SidebarSide::Right; - let not_fullscreen = !window.is_fullscreen() && !window.is_simple_fullscreen(); - let traffic_lights = cfg!(target_os = "macos") && not_fullscreen && sidebar_on_left; - let left_window_controls = !cfg!(target_os = "macos") && not_fullscreen && sidebar_on_left; - let right_window_controls = - !cfg!(target_os = "macos") && not_fullscreen && sidebar_on_right; + let SidebarWindowControls { + traffic_lights, + left: left_window_controls, + right: right_window_controls, + } = SidebarWindowControls::new(self.side(cx), window, cx); let header_height = platform_title_bar_height(window); h_flex() @@ -7419,11 +7417,14 @@ impl Sidebar { this.child(Divider::vertical().color(ui::DividerColor::Border)) }) .child( - div().ml_1().child( - Icon::new(IconName::MagnifyingGlass) - .size(IconSize::Small) - .color(Color::Muted), - ), + div() + .debug_selector(|| "sidebar-header-search".into()) + .ml_1() + .child( + Icon::new(IconName::MagnifyingGlass) + .size(IconSize::Small) + .color(Color::Muted), + ), ) .child(self.render_filter_input(cx)) .child( diff --git a/crates/sidebar/src/sidebar_tests.rs b/crates/sidebar/src/sidebar_tests.rs index 8c18bfe172f..c48d6b7d488 100644 --- a/crates/sidebar/src/sidebar_tests.rs +++ b/crates/sidebar/src/sidebar_tests.rs @@ -16,15 +16,25 @@ use agent_ui::{ }; use chrono::DateTime; use fs::{FakeFs, Fs}; -use gpui::{App, Bounds, Hsla, Point, Rgba, TestAppContext, UpdateGlobal, VisualTestContext}; +use gpui::{ + AnyView, App, Bounds, Corners, Decorations, Hsla, Point, Rgba, ScaledPixels, TestAppContext, + Tiling, UpdateGlobal, VisualTestContext, point, +}; +use platform_title_bar::PlatformTitleBar; use pretty_assertions::assert_eq; use project::AgentId; -use settings::{Settings, SettingsStore}; +#[cfg(target_os = "macos")] +use settings::TitleBarVisibility; +use settings::{Settings, SettingsStore, SidebarDockPosition}; use std::{ path::{Path, PathBuf}, sync::Arc, }; use util::{path_list::PathList, rel_path::rel_path}; +use workspace::{ + dock::{DockPosition, PanelEvent, test::TestPanel}, + item::test::TestItem, +}; fn use_unique_metadata_databases(cx: &mut TestAppContext) { use std::sync::atomic::{AtomicUsize, Ordering}; @@ -1931,6 +1941,188 @@ async fn test_refocus_sidebar_with_selection_preserves_it(cx: &mut TestAppContex }); } +#[gpui::test] +async fn test_leaving_sidebar_after_previous_focus_target_is_removed(cx: &mut TestAppContext) { + let project = init_test_project("/my-project", cx).await; + let (multi_workspace, cx) = + cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx)); + let sidebar = setup_sidebar_closed(&multi_workspace, cx); + let workspace = multi_workspace.read_with(cx, |multi_workspace, _cx| { + multi_workspace.workspace().clone() + }); + let title_bar = cx.new(TestItem::new); + let panel = workspace.update_in(cx, |workspace, window, cx| { + workspace.set_titlebar_item(AnyView::from(title_bar.clone()), window, cx); + let panel = cx.new(|cx| TestPanel::new(DockPosition::Right, 100, cx)); + workspace.add_panel(panel.clone(), window, cx); + workspace.toggle_dock(DockPosition::Right, window, cx); + panel + }); + cx.run_until_parked(); + panel.update(cx, |_, cx| cx.emit(PanelEvent::ZoomIn)); + cx.run_until_parked(); + title_bar.update_in(cx, |title_bar, window, cx| { + title_bar.focus_handle(cx).focus(window, cx); + }); + cx.run_until_parked(); + let focus_state = |cx: &mut VisualTestContext| { + cx.update(|window, cx| { + let focused_region = [ + (FocusedRegion::TitleBar, title_bar.focus_handle(cx)), + (FocusedRegion::Sidebar, sidebar.focus_handle(cx)), + (FocusedRegion::Panel, panel.focus_handle(cx)), + (FocusedRegion::Pane, workspace.focus_handle(cx)), + ] + .into_iter() + .find_map(|(region, focus_handle)| { + focus_handle.contains_focused(window, cx).then_some(region) + }); + (focused_region, panel.read(cx).zoomed) + }) + }; + assert_eq!(focus_state(cx), (Some(FocusedRegion::TitleBar), true)); + + cx.dispatch_action(FocusWorkspaceSidebar); + cx.run_until_parked(); + assert_eq!(focus_state(cx), (Some(FocusedRegion::Sidebar), true)); + + cx.dispatch_action(FocusWorkspaceSidebar); + cx.run_until_parked(); + assert_eq!(focus_state(cx), (Some(FocusedRegion::TitleBar), true)); + + cx.dispatch_action(FocusWorkspaceSidebar); + cx.run_until_parked(); + workspace.update_in(cx, |workspace, window, cx| { + workspace.clear_titlebar_item(window, cx); + }); + cx.run_until_parked(); + assert_eq!(focus_state(cx), (Some(FocusedRegion::Sidebar), true)); + + cx.dispatch_action(FocusWorkspaceSidebar); + cx.run_until_parked(); + assert_eq!(focus_state(cx), (Some(FocusedRegion::Panel), true)); +} + +#[gpui::test] +async fn test_window_frame_corners_follow_content(cx: &mut TestAppContext) { + let project = init_test_project("/my-project", cx).await; + let (multi_workspace, cx) = + cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx)); + setup_sidebar_closed(&multi_workspace, cx); + let workspace = multi_workspace.read_with(cx, |multi_workspace, _cx| { + multi_workspace.workspace().clone() + }); + let window_handle = cx.update(|window, _| window.window_handle()); + cx.simulate_window_decorations_change( + window_handle, + Decorations::Client { + tiling: Tiling::default(), + }, + ); + let title_bar = cx.new(|cx| { + PlatformTitleBar::new("title-bar", cx).with_multi_workspace(multi_workspace.downgrade()) + }); + workspace.update_in(cx, |workspace, window, cx| { + workspace.set_titlebar_item(AnyView::from(title_bar), window, cx); + }); + cx.run_until_parked(); + assert_eq!( + window_corners(cx), + WindowCorners { + top_left: WindowCorner::Rounded, + top_right: WindowCorner::Rounded, + bottom_left: WindowCorner::Rounded, + bottom_right: WindowCorner::Rounded, + }, + "title bar and status bar shown" + ); + + workspace.update_in(cx, |workspace, window, cx| { + workspace.clear_titlebar_item(window, cx); + }); + cx.run_until_parked(); + assert_eq!( + window_corners(cx), + WindowCorners { + top_left: WindowCorner::Square, + top_right: WindowCorner::Square, + bottom_left: WindowCorner::Rounded, + bottom_right: WindowCorner::Rounded, + }, + "title bar hidden" + ); + + cx.update_global::(|store, cx| { + store.update_user_settings(cx, |settings| { + settings.status_bar.get_or_insert_default().show = Some(false); + }); + }); + cx.run_until_parked(); + assert_eq!( + window_corners(cx), + WindowCorners { + top_left: WindowCorner::Square, + top_right: WindowCorner::Square, + bottom_left: WindowCorner::Square, + bottom_right: WindowCorner::Square, + }, + "title bar and status bar hidden" + ); + + multi_workspace.update_in(cx, |multi_workspace, window, cx| { + multi_workspace.toggle_sidebar(window, cx); + }); + cx.run_until_parked(); + assert_eq!( + window_corners(cx), + WindowCorners { + top_left: WindowCorner::Rounded, + top_right: WindowCorner::Square, + bottom_left: WindowCorner::Rounded, + bottom_right: WindowCorner::Square, + }, + "title bar and status bar hidden, sidebar open on the left" + ); + + cx.update(|_, cx| { + let mut settings = AgentSettings::get_global(cx).clone(); + settings.threads_sidebar.position = SidebarDockPosition::Right; + AgentSettings::override_global(settings, cx); + }); + multi_workspace.update(cx, |_, cx| cx.notify()); + cx.run_until_parked(); + assert_eq!( + window_corners(cx), + WindowCorners { + top_left: WindowCorner::Square, + top_right: WindowCorner::Rounded, + bottom_left: WindowCorner::Square, + bottom_right: WindowCorner::Rounded, + }, + "title bar and status bar hidden, sidebar open on the right" + ); +} + +#[cfg(target_os = "macos")] +#[gpui::test] +async fn test_headers_follow_title_bar_visibility(cx: &mut TestAppContext) { + let project = init_test_project_with_agent_panel("/my-project", cx).await; + cx.update(|cx| AgentRegistryStore::init_test_global(cx, Vec::new())); + let (multi_workspace, cx) = + cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx)); + let (sidebar, _panel) = setup_sidebar_with_agent_panel(&multi_workspace, cx); + + let thread_list = header_search_offsets("sidebar-header-search", cx); + assert_eq!(thread_list.hidden, thread_list.fullscreen); + assert_ne!(thread_list.hidden, thread_list.shown); + + sidebar.update_in(cx, |sidebar, window, cx| sidebar.show_archive(window, cx)); + cx.run_until_parked(); + let archive = header_search_offsets("threads-archive-header-search", cx); + assert_eq!(archive.hidden, archive.fullscreen); + assert_ne!(archive.hidden, archive.shown); +} + #[gpui::test] async fn test_keyboard_confirm_on_project_header_toggles_collapse(cx: &mut TestAppContext) { let project = init_test_project("/my-project", cx).await; @@ -16110,3 +16302,150 @@ fn sidebar_painted_background_at(position: Point, cx: &mut VisualTestCon u32::from(Rgba::from(color)) }) } + +#[derive(Debug, PartialEq)] +enum FocusedRegion { + TitleBar, + Sidebar, + Panel, + Pane, +} + +#[derive(Debug, PartialEq)] +struct WindowCorners { + top_left: WindowCorner, + top_right: WindowCorner, + bottom_left: WindowCorner, + bottom_right: WindowCorner, +} + +#[derive(Debug, PartialEq)] +enum WindowCorner { + Rounded, + Square, + Overflow, + Gap, +} + +fn window_corners(cx: &mut VisualTestContext) -> WindowCorners { + cx.update(|window, _| { + let scale_factor = window.scale_factor(); + let frame = Bounds::new(Point::default(), window.viewport_size()) + .inset(theme::CLIENT_SIDE_DECORATION_SHADOW) + .scale(scale_factor); + let quads = window.painted_quads(); + let outline = quads + .iter() + .find(|quad| { + quad.bounds == frame + && quad.border_widths.top.0 > 0. + && !quad.border_color.is_transparent() + }) + .expect("window frame border"); + let probe_offset = 1.5 * scale_factor; + let corner = |x: ScaledPixels, y: ScaledPixels, direction_x: f32, direction_y: f32| { + let probe = point( + ScaledPixels(x.0 + direction_x * probe_offset), + ScaledPixels(y.0 + direction_y * probe_offset), + ); + let inside_outline = + rounded_bounds_contain(outline.bounds, outline.corner_radii, probe); + let painted = quads.iter().any(|quad| { + !quad.background.is_transparent() + && quad.content_mask.bounds.contains(&probe) + && rounded_bounds_contain(quad.bounds, quad.corner_radii, probe) + }); + match (inside_outline, painted) { + (false, false) => WindowCorner::Rounded, + (true, true) => WindowCorner::Square, + (false, true) => WindowCorner::Overflow, + (true, false) => WindowCorner::Gap, + } + }; + WindowCorners { + top_left: corner(frame.left(), frame.top(), 1., 1.), + top_right: corner(frame.right(), frame.top(), -1., 1.), + bottom_right: corner(frame.right(), frame.bottom(), -1., -1.), + bottom_left: corner(frame.left(), frame.bottom(), 1., -1.), + } + }) +} + +fn rounded_bounds_contain( + bounds: Bounds, + corner_radii: Corners, + position: Point, +) -> bool { + bounds.contains(&position) + && [ + (corner_radii.top_left, bounds.left(), bounds.top(), 1., 1.), + ( + corner_radii.top_right, + bounds.right(), + bounds.top(), + -1., + 1., + ), + ( + corner_radii.bottom_right, + bounds.right(), + bounds.bottom(), + -1., + -1., + ), + ( + corner_radii.bottom_left, + bounds.left(), + bounds.bottom(), + 1., + -1., + ), + ] + .into_iter() + .all( + |(radius, x, y, direction_x, direction_y): (_, _, _, f32, f32)| { + let center_x = x.0 + direction_x * radius.0; + let center_y = y.0 + direction_y * radius.0; + let distance_x = (center_x - position.x.0) * direction_x; + let distance_y = (center_y - position.y.0) * direction_y; + distance_x <= 0. || distance_y <= 0. || distance_x.hypot(distance_y) <= radius.0 + }, + ) +} + +#[cfg(target_os = "macos")] +struct HeaderSearchOffsets { + shown: Pixels, + hidden: Pixels, + fullscreen: Pixels, +} + +#[cfg(target_os = "macos")] +fn header_search_offsets( + selector: &'static str, + cx: &mut VisualTestContext, +) -> HeaderSearchOffsets { + let offset = |visibility, cx: &mut VisualTestContext| { + cx.update_global::(|store, cx| { + store.update_user_settings(cx, |settings| { + settings.title_bar.get_or_insert_default().show = Some(visibility); + }); + }); + cx.run_until_parked(); + cx.debug_bounds(selector) + .expect("header search is rendered") + .left() + }; + let shown = offset(TitleBarVisibility::Always, cx); + let hidden = offset(TitleBarVisibility::Never, cx); + cx.update(|window, _| window.toggle_fullscreen()); + cx.run_until_parked(); + let fullscreen = offset(TitleBarVisibility::Always, cx); + cx.update(|window, _| window.toggle_fullscreen()); + cx.run_until_parked(); + HeaderSearchOffsets { + shown, + hidden, + fullscreen, + } +} diff --git a/crates/title_bar/src/application_menu.rs b/crates/title_bar/src/application_menu.rs index ced552d53ff..bde4ff6f2b5 100644 --- a/crates/title_bar/src/application_menu.rs +++ b/crates/title_bar/src/application_menu.rs @@ -9,7 +9,7 @@ use serde::Deserialize; use smallvec::SmallVec; use ui::{ContextMenu, PopoverMenu, PopoverMenuHandle, Tooltip, prelude::*}; -use crate::title_bar_settings::TitleBarSettings; +use platform_title_bar::TitleBarSettings; actions!( app_menu, diff --git a/crates/title_bar/src/title_bar.rs b/crates/title_bar/src/title_bar.rs index 4f1b09ca13b..dceb1a5a489 100644 --- a/crates/title_bar/src/title_bar.rs +++ b/crates/title_bar/src/title_bar.rs @@ -2,7 +2,6 @@ mod application_menu; pub mod collab; mod onboarding_banner; mod plan_chip; -mod title_bar_settings; mod update_version; use crate::application_menu::{ApplicationMenu, show_menus}; @@ -15,6 +14,7 @@ pub use platform_title_bar::{ self, DraggedWindowTab, MergeAllWindows, MoveTabToNewWindow, PlatformTitleBar, ShowNextWindowTab, ShowPreviousWindowTab, }; +use platform_title_bar::{SystemWindowTabs, TitleBarSettings}; use project::{linked_worktree_short_name, repo_identity_path}; #[cfg(not(target_os = "macos"))] @@ -28,8 +28,8 @@ use client::{Client, UserStore, zed_urls}; use command_palette_hooks::CommandPaletteFilter; use gpui::{ - Action, Anchor, Animation, AnimationExt, AnyElement, App, Context, Element, Entity, Focusable, - InteractiveElement, IntoElement, MouseButton, ParentElement, Render, + Action, Anchor, Animation, AnimationExt, AnyElement, AnyView, App, Context, Element, Entity, + Focusable, InteractiveElement, IntoElement, MouseButton, ParentElement, Render, StatefulInteractiveElement, Styled, Subscription, TaskExt, WeakEntity, Window, actions, div, pulsating_between, }; @@ -45,7 +45,6 @@ use std::any::TypeId; use std::sync::Arc; use std::time::Duration; use theme::ActiveTheme; -use title_bar_settings::TitleBarSettings; use ui::{ Avatar, ButtonLike, ContextMenu, ContextMenuEntry, IconWithIndicator, Indicator, PopoverMenu, PopoverMenuHandle, TintColor, Tooltip, prelude::*, utils::platform_title_bar_height, @@ -101,9 +100,14 @@ pub fn init(cx: &mut App) { let Some(window) = window else { return; }; - let multi_workspace = workspace.multi_workspace().cloned(); - let item = cx.new(|cx| TitleBar::new("title-bar", workspace, multi_workspace, window, cx)); - workspace.set_titlebar_item(item.into(), window, cx); + let window_tabs = cx.new(|_| SystemWindowTabs::new()); + workspace.set_window_tabs_item(AnyView::from(window_tabs), cx); + apply_title_bar_visibility(workspace, window, cx); + + cx.observe_global_in::(window, apply_title_bar_visibility) + .detach(); + cx.observe_window_bounds(window, apply_title_bar_visibility) + .detach(); workspace.register_action(|_workspace, _: &UseClassicLayout, _window, cx| { set_window_layout(WindowLayout::Editor(None), cx); @@ -173,6 +177,25 @@ pub fn init(cx: &mut App) { .detach(); } +fn apply_title_bar_visibility( + workspace: &mut Workspace, + window: &mut Window, + cx: &mut Context, +) { + let visible = TitleBarSettings::is_title_bar_visible(window, cx); + + if !visible { + workspace.clear_titlebar_item(window, cx); + } else if workspace.titlebar_item().is_none() { + let multi_workspace = workspace.multi_workspace().cloned(); + let item = cx.new(|cx| TitleBar::new("title-bar", workspace, multi_workspace, window, cx)); + workspace.set_titlebar_item(AnyView::from(item), window, cx); + } + + #[cfg(target_os = "macos")] + window.set_native_titlebar_visibility(visible); +} + /// Hides or shows the panel layout actions in the command palette based on /// whether AI is currently disabled. fn update_layout_action_filter(cx: &mut App) { @@ -500,7 +523,7 @@ impl TitleBar { let update_version = cx.new(|cx| UpdateVersion::new(cx)); let platform_titlebar = cx.new(|cx| { - let mut titlebar = PlatformTitleBar::new(id, cx); + let mut titlebar = PlatformTitleBar::new(id, cx).without_system_window_tabs(); if let Some(mw) = multi_workspace.clone() { titlebar = titlebar.with_multi_workspace(mw); } @@ -1425,3 +1448,185 @@ impl TitleBar { .anchor(Anchor::TopRight) } } + +#[cfg(test)] +mod tests { + use super::*; + use gpui::{ + AnyWindowHandle, Empty, EntityId, SystemWindowTab, SystemWindowTabController, + TestAppContext, VisualTestContext, + }; + use platform_title_bar::SidebarWindowControls; + use settings::{SidebarSide, TitleBarVisibility}; + + #[gpui::test] + async fn test_title_bar_visibility(cx: &mut TestAppContext) { + let (workspace, cx) = init_test(TitleBarVisibility::Never, cx).await; + assert_eq!(chrome(&workspace, cx), Chrome::HIDDEN); + + set_visibility(Some(TitleBarVisibility::Always), cx); + assert_eq!(chrome(&workspace, cx), Chrome::SHOWN); + let title_bar = title_bar_id(&workspace, cx); + + cx.update_global::(|store, cx| { + store.update_user_settings(cx, |settings| { + settings.title_bar.get_or_insert_default().show_menus = Some(true); + }); + }); + cx.run_until_parked(); + assert_eq!(title_bar_id(&workspace, cx), title_bar); + + toggle_fullscreen(&workspace, cx); + assert_eq!( + chrome(&workspace, cx), + Chrome { + title_bar: true, + ..Chrome::HIDDEN + } + ); + + set_visibility(Some(TitleBarVisibility::HideInFullScreen), cx); + assert_eq!(chrome(&workspace, cx), Chrome::HIDDEN); + + toggle_fullscreen(&workspace, cx); + assert_eq!(chrome(&workspace, cx), Chrome::SHOWN); + + toggle_fullscreen(&workspace, cx); + assert_eq!(chrome(&workspace, cx), Chrome::HIDDEN); + + toggle_fullscreen(&workspace, cx); + set_visibility(Some(TitleBarVisibility::Never), cx); + assert_eq!(chrome(&workspace, cx), Chrome::HIDDEN); + + set_visibility(None, cx); + assert_eq!(chrome(&workspace, cx), Chrome::SHOWN); + } + + #[gpui::test] + async fn test_window_tabs_stay_mounted_without_title_bar(cx: &mut TestAppContext) { + let other_window = cx.add_window(|_, _| Empty); + let (workspace, cx) = init_test(TitleBarVisibility::Never, cx).await; + workspace.update_in(cx, |_, window, cx| { + let workspace_window = window.window_handle(); + SystemWindowTabController::add_tab( + cx, + workspace_window.window_id(), + Vec::from([ + SystemWindowTab::new(SharedString::from("workspace"), workspace_window), + SystemWindowTab::new( + SharedString::from("other"), + AnyWindowHandle::from(other_window), + ), + ]), + ); + SystemWindowTabController::set_visible(cx, true); + cx.notify(); + }); + cx.run_until_parked(); + assert_eq!(chrome(&workspace, cx), Chrome::HIDDEN); + assert!(window_tabs_mounted(cx)); + + set_visibility(Some(TitleBarVisibility::HideInFullScreen), cx); + assert_eq!(chrome(&workspace, cx), Chrome::SHOWN); + assert!(window_tabs_mounted(cx)); + + toggle_fullscreen(&workspace, cx); + assert_eq!(chrome(&workspace, cx), Chrome::HIDDEN); + assert!(window_tabs_mounted(cx)); + } + + #[derive(Debug, PartialEq)] + struct Chrome { + title_bar: bool, + left_sidebar: SidebarWindowControls, + right_sidebar: SidebarWindowControls, + } + + impl Chrome { + const SHOWN: Self = Self { + title_bar: true, + left_sidebar: SidebarWindowControls { + traffic_lights: cfg!(target_os = "macos"), + left: !cfg!(target_os = "macos"), + right: false, + }, + right_sidebar: SidebarWindowControls { + traffic_lights: false, + left: false, + right: !cfg!(target_os = "macos"), + }, + }; + const HIDDEN: Self = Self { + title_bar: false, + left_sidebar: NO_WINDOW_CONTROLS, + right_sidebar: NO_WINDOW_CONTROLS, + }; + } + + const NO_WINDOW_CONTROLS: SidebarWindowControls = SidebarWindowControls { + traffic_lights: false, + left: false, + right: false, + }; + + async fn init_test( + visibility: TitleBarVisibility, + cx: &mut TestAppContext, + ) -> (Entity, &mut VisualTestContext) { + cx.update(|cx| { + let mut settings = SettingsStore::test(cx); + settings.update_user_settings(cx, |settings| { + settings.title_bar.get_or_insert_default().show = Some(visibility); + }); + cx.set_global(settings); + cx.set_global(db::AppDatabase::test_new()); + theme_settings::init(theme::LoadThemes::JustBase, cx); + command_palette_hooks::init(cx); + }); + let project = Project::test(fs::FakeFs::new(cx.executor()), [], cx).await; + cx.update(|cx| { + let project = project.read(cx); + call::init(project.client(), project.user_store(), cx); + init(cx); + }); + let (workspace, cx) = + cx.add_window_view(|window, cx| Workspace::test_new(project, window, cx)); + cx.run_until_parked(); + (workspace, cx) + } + + fn set_visibility(visibility: Option, cx: &mut VisualTestContext) { + cx.update_global::(|store, cx| { + store.update_user_settings(cx, |settings| { + settings.title_bar.get_or_insert_default().show = visibility; + }); + }); + cx.run_until_parked(); + } + + fn toggle_fullscreen(workspace: &Entity, cx: &mut VisualTestContext) { + workspace.update_in(cx, |_, window, _| window.toggle_fullscreen()); + cx.run_until_parked(); + } + + fn chrome(workspace: &Entity, cx: &mut VisualTestContext) -> Chrome { + workspace.update_in(cx, |workspace, window, cx| Chrome { + title_bar: workspace.titlebar_item().is_some(), + left_sidebar: SidebarWindowControls::new(SidebarSide::Left, window, cx), + right_sidebar: SidebarWindowControls::new(SidebarSide::Right, window, cx), + }) + } + + fn title_bar_id(workspace: &Entity, cx: &mut VisualTestContext) -> Option { + workspace.read_with(cx, |workspace, _| { + workspace.titlebar_item().map(|item| item.entity_id()) + }) + } + + fn window_tabs_mounted(cx: &mut VisualTestContext) -> bool { + cx.update(|window, _| window.refresh()); + cx.run_until_parked(); + cx.debug_bounds("system-window-tabs") + .is_some_and(|bounds| bounds.size.height > px(0.)) + } +} diff --git a/crates/workspace/src/multi_workspace.rs b/crates/workspace/src/multi_workspace.rs index ec35dd8ff11..44498442346 100644 --- a/crates/workspace/src/multi_workspace.rs +++ b/crates/workspace/src/multi_workspace.rs @@ -2,9 +2,9 @@ use anyhow::{Context as _, Result}; use fs::Fs; use gpui::{ - AnyView, App, Context, DragMoveEvent, Entity, EntityId, EventEmitter, FocusHandle, Focusable, - ManagedView, MouseButton, Pixels, Render, Subscription, Task, TaskExt, WeakEntity, Window, - WindowId, actions, deferred, px, + AnyView, App, Context, Corners, DragMoveEvent, Entity, EntityId, EventEmitter, FocusHandle, + Focusable, ManagedView, MouseButton, Pixels, Render, Subscription, Task, TaskExt, WeakEntity, + Window, WindowId, actions, deferred, px, }; pub use project::ProjectGroupKey; use project::{DisableAiSettings, Project}; @@ -28,8 +28,8 @@ const SIDEBAR_RESIZE_HANDLE_SIZE: Pixels = px(6.0); use crate::open_remote_project_with_existing_connection; use crate::{ CloseIntent, CloseWindow, DockPosition, Event as WorkspaceEvent, Item, ModalView, OpenMode, - OpenOptions, OpenVisible, Panel, Workspace, WorkspaceId, client_side_decorations, - persistence::model::MultiWorkspaceState, + OpenOptions, OpenVisible, Panel, Workspace, WorkspaceId, + client_side_decorations_with_rounded_corners, persistence::model::MultiWorkspaceState, }; actions!( @@ -544,11 +544,9 @@ impl MultiWorkspace { self.previous_focus_handle.clone() }; - if let Some(previous_focus) = focus_handle { - previous_focus.focus(window, cx); - } else { - let pane = self.workspace().read(cx).active_pane().clone(); - window.focus(&pane.read(cx).focus_handle(cx), cx); + match focus_handle.filter(|focus_handle| focus_handle.is_rendered(window)) { + Some(previous_focus) => previous_focus.focus(window, cx), + None => self.focus_active_workspace(window, cx), } } @@ -2141,8 +2139,21 @@ impl Render for MultiWorkspace { let workspace = self.workspace().clone(); let workspace_key_context = workspace.update(cx, |workspace, cx| workspace.key_context(cx)); let root = workspace.update(cx, |workspace, cx| workspace.actions(h_flex(), window, cx)); + let (has_title_bar, has_status_bar) = { + let workspace = workspace.read(cx); + ( + workspace.titlebar_item().is_some(), + workspace.status_bar_visible(cx), + ) + }; + let rounded_corners = Corners { + top_left: has_title_bar || left_sidebar.is_some(), + top_right: has_title_bar || right_sidebar.is_some(), + bottom_right: has_status_bar || right_sidebar.is_some(), + bottom_left: has_status_bar || left_sidebar.is_some(), + }; - client_side_decorations( + client_side_decorations_with_rounded_corners( root.key_context(workspace_key_context) .relative() .size_full() @@ -2263,6 +2274,7 @@ impl Render for MultiWorkspace { )) .with_priority(2) })), + rounded_corners, window, cx, ) diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index a80a045d59b..00c0ce27def 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -142,7 +142,7 @@ use std::{ time::Duration, }; use task::{DebugScenario, SharedTaskContext, SpawnInTerminal}; -use theme::{ActiveTheme, ClientDecorationsExt, SystemAppearance}; +use theme::{ActiveTheme, SystemAppearance}; use theme_settings::ThemeSettings; pub use toolbar::{ PaneSearchBarCallbacks, Toolbar, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, @@ -1671,6 +1671,7 @@ pub struct Workspace { pub(crate) modal_layer: Entity, toast_layer: Entity, titlebar_item: Option, + window_tabs_item: Option, titlebar_focus_handle: FocusHandle, region_focus_handles: RegionFocusHandles, notifications: Notifications, @@ -2177,6 +2178,7 @@ impl Workspace { modal_layer, toast_layer, titlebar_item: None, + window_tabs_item: None, titlebar_focus_handle: cx.focus_handle(), region_focus_handles: RegionFocusHandles::new(cx), notifications: Notifications::default(), @@ -3521,6 +3523,20 @@ impl Workspace { cx.notify(); } + pub fn clear_titlebar_item(&mut self, window: &mut Window, cx: &mut Context) { + if self.titlebar_item.take().is_some() { + if self.titlebar_focus_handle.contains_focused(window, cx) { + window.focus(&self.fallback_focus_handle(window, cx), cx); + } + cx.notify(); + } + } + + pub fn set_window_tabs_item(&mut self, item: AnyView, cx: &mut Context) { + self.window_tabs_item = Some(item); + cx.notify(); + } + pub fn set_prompt_for_new_path(&mut self, prompt: PromptForNewPath) { self.on_prompt_for_new_path = Some(prompt) } @@ -10014,6 +10030,7 @@ impl Render for Workspace { .child(item), ) }) + .children(self.window_tabs_item.clone()) .on_modifiers_changed(move |_, _, cx| { for &id in ¬ification_entities { cx.notify(id); @@ -12354,6 +12371,25 @@ pub fn client_side_decorations( element: impl IntoElement, window: &mut Window, cx: &mut App, +) -> Stateful
{ + client_side_decorations_with_rounded_corners( + element, + Corners { + top_left: true, + top_right: true, + bottom_right: true, + bottom_left: true, + }, + window, + cx, + ) +} + +pub(crate) fn client_side_decorations_with_rounded_corners( + element: impl IntoElement, + rounded_corners: Corners, + window: &mut Window, + cx: &mut App, ) -> Stateful
{ let decorations = window.window_decorations(); let is_resizable = window.is_resizable(); @@ -12361,23 +12397,24 @@ pub fn client_side_decorations( Decorations::Server => Tiling::default(), Decorations::Client { tiling } => tiling, }; + let radius = |rounded| { + if rounded { + theme::CLIENT_SIDE_DECORATION_ROUNDING + } else { + px(0.0) + } + }; + let corner_radii = Corners { + top_left: radius(rounded_corners.top_left && !tiling.top && !tiling.left), + top_right: radius(rounded_corners.top_right && !tiling.top && !tiling.right), + bottom_left: radius(rounded_corners.bottom_left && !tiling.bottom && !tiling.left), + bottom_right: radius(rounded_corners.bottom_right && !tiling.bottom && !tiling.right), + }; match decorations { Decorations::Client { .. } => { window.set_client_inset(theme::CLIENT_SIDE_DECORATION_SHADOW); - let radius = |rounded| { - if rounded { - theme::CLIENT_SIDE_DECORATION_ROUNDING - } else { - px(0.0) - } - }; - window.set_client_corner_radii(Corners { - top_left: radius(!tiling.top && !tiling.left), - top_right: radius(!tiling.top && !tiling.right), - bottom_left: radius(!tiling.bottom && !tiling.left), - bottom_right: radius(!tiling.bottom && !tiling.right), - }); + window.set_client_corner_radii(corner_radii); } Decorations::Server => { window.set_client_inset(px(0.0)); @@ -12393,8 +12430,7 @@ pub fn client_side_decorations( .bg(transparent_black()) .map(|div| match decorations { Decorations::Server => div, - Decorations::Client { .. } => div - .rounded_client_corners(tiling) + Decorations::Client { .. } => with_corner_radii(div, corner_radii) .when(!tiling.top, |div| { div.pt(theme::CLIENT_SIDE_DECORATION_SHADOW) }) @@ -12449,9 +12485,8 @@ pub fn client_side_decorations( .cursor(CursorStyle::Arrow) .map(|div| match decorations { Decorations::Server => div, - Decorations::Client { .. } => div + Decorations::Client { .. } => with_corner_radii(div, corner_radii) .border_color(cx.theme().colors().border) - .rounded_client_corners(tiling) .when(!tiling.top, |div| { div.border_t(theme::CLIENT_SIDE_DECORATION_BORDER) }) @@ -12532,6 +12567,14 @@ pub fn client_side_decorations( }) } +fn with_corner_radii(element: E, corner_radii: Corners) -> E { + element + .rounded_tl(corner_radii.top_left) + .rounded_tr(corner_radii.top_right) + .rounded_bl(corner_radii.bottom_left) + .rounded_br(corner_radii.bottom_right) +} + fn resize_edge( pos: Point, shadow_size: Pixels, @@ -13050,6 +13093,74 @@ mod tests { assert!(!noise.branch); } + #[gpui::test] + async fn test_clearing_title_bar_restores_editor_focus(cx: &mut TestAppContext) { + init_test(cx); + + let fs = FakeFs::new(cx.executor()); + let project = Project::test(fs, [], cx).await; + let (workspace, cx) = + cx.add_window_view(|window, cx| Workspace::test_new(project, window, cx)); + let title_bar = cx.new(|cx| TestItem::new(cx)); + workspace.update_in(cx, |workspace, window, cx| { + workspace.set_titlebar_item(title_bar.clone().into(), window, cx); + }); + cx.run_until_parked(); + + workspace.update_in(cx, |workspace, window, cx| { + window.focus(&workspace.titlebar_focus_handle, cx); + assert!(workspace.titlebar_focus_handle.contains_focused(window, cx)); + + workspace.clear_titlebar_item(window, cx); + assert!(workspace.titlebar_item().is_none()); + assert!(workspace.active_pane.focus_handle(cx).is_focused(window)); + + workspace.clear_titlebar_item(window, cx); + assert!(workspace.active_pane.focus_handle(cx).is_focused(window)); + }); + + let panel = workspace.update_in(cx, |workspace, window, cx| { + workspace.set_titlebar_item(title_bar.into(), window, cx); + let panel = + cx.new(|cx| TestPanel::new_with_activation_child(DockPosition::Right, 100, cx)); + workspace.add_panel(panel.clone(), window, cx); + workspace.toggle_dock(DockPosition::Right, window, cx); + panel + }); + cx.run_until_parked(); + panel.update(cx, |_, cx| cx.emit(PanelEvent::ZoomIn)); + cx.run_until_parked(); + + workspace.update_in(cx, |workspace, window, cx| { + window.focus(&workspace.titlebar_focus_handle, cx); + }); + cx.run_until_parked(); + + workspace.update_in(cx, |workspace, window, cx| { + assert_eq!(workspace.zoomed_position, Some(DockPosition::Right)); + workspace.clear_titlebar_item(window, cx); + }); + cx.run_until_parked(); + workspace.update(cx, |_, cx| cx.notify()); + cx.run_until_parked(); + + workspace.update_in(cx, |workspace, window, cx| { + assert!(workspace.titlebar_item().is_none()); + assert!(workspace.right_dock().read(cx).is_open()); + assert_eq!(workspace.zoomed, Some(panel.to_any().downgrade())); + assert_eq!(workspace.zoomed_position, Some(DockPosition::Right)); + assert!( + panel + .read(cx) + .activation_focus_handle + .as_ref() + .unwrap() + .is_focused(window) + ); + assert!(panel.read(cx).focus_handle(cx).contains_focused(window, cx)); + }); + } + #[gpui::test] async fn test_tab_disambiguation(cx: &mut TestAppContext) { init_test(cx); diff --git a/docs/src/reference/all-settings.md b/docs/src/reference/all-settings.md index 5de21223ed6..a72e40ac7e7 100644 --- a/docs/src/reference/all-settings.md +++ b/docs/src/reference/all-settings.md @@ -6442,6 +6442,7 @@ Run the {#action theme_selector::Toggle} action in the command palette to see a ```json [settings] { "title_bar": { + "show": "always", "show_branch_status_icon": false, "show_branch_name": true, "show_worktree_name": true, @@ -6459,6 +6460,7 @@ Run the {#action theme_selector::Toggle} action in the command palette to see a **Options** +- `show`: When to show the title bar: `"always"` (default), `"never"`, or `"hide_in_full_screen"`. - `show_branch_status_icon`: Whether to show git status indicators on the branch icon in the titlebar - `show_branch_name`: Whether to show the branch name button in the titlebar - `show_worktree_name`: Whether to show the worktree name button in the titlebar diff --git a/docs/src/visual-customization.md b/docs/src/visual-customization.md index 6253f5d5cf5..c6be9dd5c4f 100644 --- a/docs/src/visual-customization.md +++ b/docs/src/visual-customization.md @@ -142,6 +142,7 @@ To disable this behavior use: ```json [settings] // Control which items are shown/hidden in the title bar "title_bar": { + "show": "always", // When to show: always | never | hide_in_full_screen "show_branch_status_icon": false, // Show git status on branch icon "show_branch_name": true, // Show/hide branch name "show_worktree_name": true, // Show/hide worktree name