Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

fix(window): keep macOS traffic lights still during live resize (#1712)

l0ng-aib1d8d61cb17d
diff --git a/Cargo.lock b/Cargo.lock
index 8d338f08b..69fe6374d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6012,7 +6012,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags 2.13.2",
- "block2 0.6.2",
"dispatch2",
"objc2 0.6.5",
]
@@ -6403,7 +6402,6 @@ dependencies = [
"ide-workspace",
"objc2 0.6.5",
"objc2-app-kit 0.3.2",
- "objc2-core-foundation",
"objc2-foundation 0.3.2",
"objc2-web-kit 0.3.2",
"parking_lot",
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 0e78c99f0..fa3850a25 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -82,8 +82,7 @@ block2 = "0.6"
# Plugin secrets in the login keychain.
security-framework = "3.7"
objc2 = "0.6"
-objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSButton", "NSEvent", "NSResponder", "NSScreen", "NSView", "NSWindow"] }
-objc2-core-foundation = { version = "0.3", default-features = false, features = ["std", "block2", "CFRunLoop"] }
+objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSEvent", "NSResponder", "NSScreen", "NSToolbar", "NSView", "NSWindow"] }
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "block2", "NSNotification", "NSOperation", "NSString"] }
# Used only by the debug font-restriction override; Cargo dependency cfg cannot test debug_assertions.
objc2-web-kit = { version = "0.3", default-features = false, features = ["std", "WKWebViewConfiguration", "WKPreferences"] }
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index 147cdd751..86dc6dc25 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -200,7 +200,6 @@ pub fn run() {
tauri::WindowEvent::Destroyed => {
#[cfg(target_os = "macos")]
{
- titlebar::forget(window);
macos::forget_fullscreen(window.label());
}
if windows::auxiliary_parent(window.label()).is_none() {
diff --git a/src-tauri/src/titlebar.rs b/src-tauri/src/titlebar.rs
index fdf68376b..9ab69d0d2 100644
--- a/src-tauri/src/titlebar.rs
+++ b/src-tauri/src/titlebar.rs
@@ -1,106 +1,30 @@
-//! Keep the native window buttons aligned with the web toolbar, in AppKit points.
+//! Size the native title bar around the window buttons to match the web toolbar.
+//!
+//! An empty unified toolbar makes AppKit lay out and vertically centre the buttons itself, so
+//! nothing moves them behind its back and they stay still during live resize.
-use std::cell::{Cell, RefCell};
-use std::collections::HashMap;
-use std::rc::Rc;
+use objc2::MainThreadMarker;
+use objc2_app_kit::{NSToolbar, NSWindow, NSWindowToolbarStyle};
-use block2::RcBlock;
-use objc2_app_kit::{NSWindow, NSWindowButton, NSWindowStyleMask};
-use objc2_core_foundation::{
- CFIndex, CFOptionFlags, CFRetained, CFRunLoop, CFRunLoopActivity, CFRunLoopObserver, kCFRunLoopCommonModes,
-};
-use objc2_foundation::NSPoint;
-
-/// AppKit lays out the title bar in a run loop observer and puts the buttons back at their
-/// defaults on resize, title changes and fullscreen exit. Core Animation commits at 2000000, so
-/// correcting the frames just before it keeps AppKit's positions off screen.
-const BEFORE_COMMIT: CFIndex = 1_999_999;
-
-struct Titlebar {
- height: Rc<Cell<f64>>,
- spacing: f64,
- observer: CFRetained<CFRunLoopObserver>,
-}
-
-impl Drop for Titlebar {
- fn drop(&mut self) {
- self.observer.invalidate();
- }
-}
-
-thread_local! {
- static TITLEBARS: RefCell<HashMap<String, Titlebar>> = RefCell::default();
-}
+/// Unified compact (38pt) and unified (52pt) toolbars; pick the one closer to the web bar.
+const COMPACT_MAX_HEIGHT: f64 = 45.0;
pub fn set_height(window: &tauri::WebviewWindow, height: f64) -> tauri::Result<()> {
- let label = window.label().to_owned();
- // with_webview runs on the AppKit thread; the observer is also delivered there.
+ // with_webview runs on the AppKit thread.
window.with_webview(move |webview| unsafe {
+ let Some(mtm) = MainThreadMarker::new() else { return };
let native = &*webview.ns_window().cast::<NSWindow>();
- let Some(close) = native.standardWindowButton(NSWindowButton::CloseButton) else { return };
- let Some(minimize) = native.standardWindowButton(NSWindowButton::MiniaturizeButton) else { return };
- let Some(run_loop) = CFRunLoop::main() else { return };
- let spacing = minimize.frame().origin.x - close.frame().origin.x;
- TITLEBARS.with_borrow_mut(|bars| {
- if let Some(bar) = bars.get(&label) {
- bar.height.set(height);
- center_buttons(native, height, bar.spacing);
- return;
- }
- let shared = Rc::new(Cell::new(height));
- let current = shared.clone();
- let pointer = native as *const NSWindow;
- let block = RcBlock::new(move |_: *mut CFRunLoopObserver, _: CFRunLoopActivity| {
- center_buttons(&*pointer, current.get(), spacing)
- });
- let activities = CFRunLoopActivity::BeforeWaiting.0 as CFOptionFlags;
- let Some(observer) = CFRunLoopObserver::with_handler(None, activities, true, BEFORE_COMMIT, Some(&block))
- else {
- return;
- };
- run_loop.add_observer(Some(&observer), kCFRunLoopCommonModes);
- center_buttons(native, height, spacing);
- bars.insert(label, Titlebar { height: shared, spacing, observer });
- });
- })
-}
-
-pub fn forget(window: &tauri::Window) {
- let label = window.label().to_owned();
- let _ = window.run_on_main_thread(move || {
- TITLEBARS.with_borrow_mut(|bars| bars.remove(&label));
- });
-}
-
-unsafe fn center_buttons(window: &NSWindow, height: f64, spacing: f64) {
- // Fullscreen uses AppKit's own revealable title bar.
- if window.styleMask().contains(NSWindowStyleMask::FullScreen) {
- return;
- }
- let Some(close) = window.standardWindowButton(NSWindowButton::CloseButton) else { return };
- let Some(container) = (unsafe { close.superview().and_then(|view| view.superview()) }) else { return };
- let Some(content) = window.contentView() else { return };
- let mut frame = container.frame();
- let top = frame.origin.y + frame.size.height;
- if (frame.size.height - height).abs() > 0.01 {
- frame.size.height = height;
- frame.origin.y = top - height;
- container.setFrame(frame);
- }
- let bounds = content.bounds();
- let y = bounds.origin.y + if content.isFlipped() { height / 2.0 } else { bounds.size.height - height / 2.0 };
- for (index, kind) in [NSWindowButton::CloseButton, NSWindowButton::MiniaturizeButton, NSWindowButton::ZoomButton]
- .into_iter()
- .enumerate()
- {
- let Some(button) = window.standardWindowButton(kind) else { continue };
- let Some(parent) = (unsafe { button.superview() }) else { continue };
- let center = parent.convertPoint_fromView(NSPoint::new(0.0, y), Some(&content));
- let frame = button.frame();
- let x = 14.0 + index as f64 * spacing;
- let target = center.y - frame.size.height / 2.0;
- if (frame.origin.x - x).abs() > 0.01 || (frame.origin.y - target).abs() > 0.01 {
- button.setFrameOrigin(NSPoint::new(x, target));
+ if native.toolbar().is_none() {
+ let toolbar = NSToolbar::new(mtm);
+ native.setToolbar(Some(&toolbar));
+ }
+ let style = if height < COMPACT_MAX_HEIGHT {
+ NSWindowToolbarStyle::UnifiedCompact
+ } else {
+ NSWindowToolbarStyle::Unified
+ };
+ if native.toolbarStyle() != style {
+ native.setToolbarStyle(style);
}
- }
+ })
}