From b3abc5b28b03e501ac5cf4cad719cbfe087af65b Mon Sep 17 00:00:00 2001 From: Chris Laupama <42254688+chrislaupama@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:15:29 +0000 Subject: [PATCH 1/2] Move the macOS window buttons in from the window edge The close, minimise and zoom buttons sat at AppKit's default 9 pt from the top-left corner, tight against the edge and above the header's title row. Centre them on the title row instead: 17 pt clear of the left and top edges at 100 % interface zoom, following the zoom like the rest of the header. AppKit lays the titlebar out again on resize, focus and display changes, so the placement is re-applied on those window events. --- crates/dstudio-app/Cargo.toml | 5 +- crates/dstudio-app/src/main.rs | 24 +++- crates/dstudio-app/src/window_buttons.rs | 139 +++++++++++++++++++++++ 3 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 crates/dstudio-app/src/window_buttons.rs diff --git a/crates/dstudio-app/Cargo.toml b/crates/dstudio-app/Cargo.toml index 6d7fced0..1fbe0ac9 100644 --- a/crates/dstudio-app/Cargo.toml +++ b/crates/dstudio-app/Cargo.toml @@ -38,10 +38,11 @@ roxmltree = "0.21" serde_json.workspace = true serde.workspace = true -# Already in the tree through winit/muda; used to turn off the menu items AppKit adds to Edit and View. +# Already in the tree through winit/muda; used to turn off the menu items AppKit adds to Edit and View +# and to place the window buttons (window_buttons.rs). [target.'cfg(target_os = "macos")'.dependencies] objc2 = "0.6" -objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSUserDefaults", "NSDictionary", "NSString", "NSValue"] } +objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSUserDefaults", "NSDictionary", "NSString", "NSValue", "NSGeometry"] } [build-dependencies] slint-build = "=1.18.1" diff --git a/crates/dstudio-app/src/main.rs b/crates/dstudio-app/src/main.rs index 491ddd77..381c6a6e 100644 --- a/crates/dstudio-app/src/main.rs +++ b/crates/dstudio-app/src/main.rs @@ -83,6 +83,7 @@ mod transport_fields; mod typing; mod warpedit; mod wavgen; +mod window_buttons; mod zoom; use controller::{BottomTab, Controller, ImportTarget, gain_from_slider}; @@ -2656,13 +2657,24 @@ fn run() -> Result<(), Box> { } { // Files dropped from Finder: onto the selected track's sampler, else imported as clips. + // AppKit lays the titlebar out again on resize, focus and display changes: put the + // window buttons back on the header's title row. use slint::winit_030::{EventResult, WinitWindowAccessor, winit::event::WindowEvent}; let ctl = ctl.clone(); - app.window().on_winit_window_event(move |_, ev| { - if let WindowEvent::DroppedFile(path) = ev - && let Ok(mut c) = ctl.try_borrow_mut() - { - c.drop_files(vec![path.clone()]); + let zm = zoom.clone(); + app.window().on_winit_window_event(move |window, ev| { + match ev { + WindowEvent::DroppedFile(path) => { + if let Ok(mut c) = ctl.try_borrow_mut() { + c.drop_files(vec![path.clone()]); + } + } + WindowEvent::Resized(_) | WindowEvent::Focused(_) | WindowEvent::ScaleFactorChanged { .. } => { + if let Ok(z) = zm.try_borrow() { + window_buttons::place(window, z.percent); + } + } + _ => {} } EventResult::Propagate }); @@ -2764,6 +2776,7 @@ fn run() -> Result<(), Box> { }; if let Some(a) = aw.upgrade() { zm.borrow_mut().sync(a.window()); + window_buttons::place(a.window(), percent); a.window().request_redraw(); } if let Ok(mut c) = ctl.try_borrow_mut() { @@ -3142,6 +3155,7 @@ fn run() -> Result<(), Box> { slint::Timer::single_shot(Duration::ZERO, move || { if let Some(a) = aw.upgrade() { zm.borrow_mut().sync(a.window()); + window_buttons::place(a.window(), zm.borrow().percent); a.window().request_redraw(); } }); diff --git a/crates/dstudio-app/src/window_buttons.rs b/crates/dstudio-app/src/window_buttons.rs new file mode 100644 index 00000000..6df722b6 --- /dev/null +++ b/crates/dstudio-app/src/window_buttons.rs @@ -0,0 +1,139 @@ +//! The main window's close, minimise and zoom buttons (macOS). +//! +//! The window has a transparent, full-size titlebar, so AppKit draws its standard +//! buttons over the header. Left alone, AppKit puts them 9 pt from the window's +//! top-left corner, tight against the edge and above the header's title row. +//! Device Studio centres them on the title row instead: the close button's centre +//! sits 24 pt in from the left and top edges at 100 % interface zoom, and the +//! inset follows the zoom like the rest of the header. +//! +//! AppKit lays the titlebar out again when the window resizes, changes display or +//! focus, or leaves full screen, so `place` runs again on those window events. It +//! only reads frames when the buttons are already where they belong. + +/// Centre of the close button from the window's left and top edges, in points at +/// 100 % interface zoom (the header's title row is centred 23.5 pt down). +pub const CLOSE_CENTER_PT: f64 = 24.0; + +/// Centre of the close button (x from the left edge, y from the top edge), in +/// points, for an interface zoom `percent`. +pub fn close_center(percent: u32) -> (f64, f64) { + let c = CLOSE_CENTER_PT * percent as f64 / 100.0; + (c, c) +} + +/// Top-left corner of a `w` x `h` button centred on `center`, on whole points. +pub fn button_origin(center: (f64, f64), w: f64, h: f64) -> (f64, f64) { + ((center.0 - w / 2.0).round(), (center.1 - h / 2.0).round()) +} + +/// Moves the window buttons to their place for the interface zoom `percent`. +/// Does nothing in full screen (AppKit shows the buttons in the menu-bar overlay). +#[cfg(target_os = "macos")] +pub fn place(window: &slint::Window, percent: u32) { + use slint::winit_030::WinitWindowAccessor; + use slint::winit_030::winit::raw_window_handle::{HasWindowHandle, RawWindowHandle}; + window.with_winit_window(|w| { + let Ok(handle) = w.window_handle() else { return }; + if let RawWindowHandle::AppKit(h) = handle.as_raw() { + // SAFETY: winit hands out the window's live NSView; everything below runs + // on the main thread (the event loop) and only messages AppKit objects + // reached from that view. + unsafe { mac::place(h.ns_view.as_ptr().cast(), close_center(percent)) }; + } + }); +} + +#[cfg(not(target_os = "macos"))] +pub fn place(_window: &slint::Window, _percent: u32) {} + +#[cfg(target_os = "macos")] +mod mac { + use objc2::msg_send; + use objc2::runtime::AnyObject; + use objc2_foundation::{NSPoint, NSRect}; + + /// NSWindowStyleMaskFullScreen. + const FULL_SCREEN: usize = 1 << 14; + /// NSWindowCloseButton, NSWindowMiniaturizeButton, NSWindowZoomButton. + const BUTTONS: [usize; 3] = [0, 1, 2]; + + /// Moves differences under half a point are left alone (no relayout churn). + fn differs(a: f64, b: f64) -> bool { + (a - b).abs() > 0.5 + } + + pub(super) unsafe fn place(view: *mut AnyObject, center: (f64, f64)) { + unsafe { + let window: *mut AnyObject = msg_send![view, window]; + if window.is_null() { + return; + } + let mask: usize = msg_send![window, styleMask]; + if mask & FULL_SCREEN != 0 { + return; + } + let buttons = BUTTONS.map(|kind| -> *mut AnyObject { msg_send![window, standardWindowButton: kind] }); + if buttons.iter().any(|b| b.is_null()) { + return; + } + let close: NSRect = msg_send![buttons[0], frame]; + let mini: NSRect = msg_send![buttons[1], frame]; + // Keep AppKit's own spacing between the buttons (it differs between macOS versions). + let step = mini.origin.x - close.origin.x; + let (w, h) = (close.size.width, close.size.height); + let (left, top) = super::button_origin(center, w, h); + + // The titlebar (NSTitlebarContainerView, the buttons' grandparent) must be tall + // enough to hold the buttons, or clicks below its edge miss them. Give it the + // same margin under the buttons as above. + let bar: *mut AnyObject = msg_send![buttons[0], superview]; + if bar.is_null() { + return; + } + let container: *mut AnyObject = msg_send![bar, superview]; + if container.is_null() { + return; + } + let win_frame: NSRect = msg_send![window, frame]; + let height = top * 2.0 + h; + let mut c: NSRect = msg_send![container, frame]; + if differs(c.size.height, height) || differs(c.origin.y, win_frame.size.height - height) { + c.size.height = height; + c.origin.y = win_frame.size.height - height; + let _: () = msg_send![container, setFrame: c]; + } + + // Button rects in window coordinates (origin at the bottom-left corner), + // converted into the titlebar view's own (possibly flipped) coordinates. + for (i, button) in buttons.iter().enumerate() { + let x = left + step * i as f64; + let want = NSRect::new(NSPoint::new(x, win_frame.size.height - top - h), close.size); + let want: NSRect = msg_send![bar, convertRect: want, fromView: std::ptr::null_mut::()]; + let now: NSRect = msg_send![*button, frame]; + if differs(now.origin.x, want.origin.x) || differs(now.origin.y, want.origin.y) { + let _: () = msg_send![*button, setFrameOrigin: want.origin]; + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn close_button_sits_17_pt_in_at_100_percent() { + // A 14 pt button centred 24 pt from the corner: 17 pt clear of both edges. + assert_eq!(button_origin(close_center(100), 14.0, 14.0), (17.0, 17.0)); + } + + #[test] + fn inset_follows_interface_zoom() { + assert_eq!(close_center(200), (48.0, 48.0)); + assert_eq!(close_center(50), (12.0, 12.0)); + // Rounded to whole points at in-between levels. + assert_eq!(button_origin(close_center(85), 14.0, 14.0), (13.0, 13.0)); + } +} From 593baeb59a7831f6d103a8f8bd325dbd550bfba5 Mon Sep 17 00:00:00 2001 From: Chris Laupama <42254688+chrislaupama@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:19:43 +0000 Subject: [PATCH 2/2] Window buttons: re-place after leaving full screen, fix Linux dead-code error winit's last resize of the full-screen exit arrives while the window is still in full screen, so AppKit's own layout put the buttons back at the top edge until the next focus change. Observe NSWindowDidExitFullScreenNotification and place them again then (and once more after the next layout pass). The placement maths is only used by the macOS path and the tests, so allow dead code on other platforms. --- Cargo.lock | 1 + crates/dstudio-app/Cargo.toml | 3 +- crates/dstudio-app/src/window_buttons.rs | 71 ++++++++++++++++++++---- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index faf1dee1..843856be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1135,6 +1135,7 @@ dependencies = [ name = "dstudio-app" version = "0.1.0" dependencies = [ + "block2 0.6.2", "bytemuck", "chrono", "dstudio-core", diff --git a/crates/dstudio-app/Cargo.toml b/crates/dstudio-app/Cargo.toml index 1fbe0ac9..d12a9e69 100644 --- a/crates/dstudio-app/Cargo.toml +++ b/crates/dstudio-app/Cargo.toml @@ -42,7 +42,8 @@ serde.workspace = true # and to place the window buttons (window_buttons.rs). [target.'cfg(target_os = "macos")'.dependencies] objc2 = "0.6" -objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSUserDefaults", "NSDictionary", "NSString", "NSValue", "NSGeometry"] } +block2 = "0.6" +objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSUserDefaults", "NSDictionary", "NSString", "NSValue", "NSGeometry", "objc2-core-foundation"] } [build-dependencies] slint-build = "=1.18.1" diff --git a/crates/dstudio-app/src/window_buttons.rs b/crates/dstudio-app/src/window_buttons.rs index 6df722b6..ef705a62 100644 --- a/crates/dstudio-app/src/window_buttons.rs +++ b/crates/dstudio-app/src/window_buttons.rs @@ -8,8 +8,13 @@ //! inset follows the zoom like the rest of the header. //! //! AppKit lays the titlebar out again when the window resizes, changes display or -//! focus, or leaves full screen, so `place` runs again on those window events. It -//! only reads frames when the buttons are already where they belong. +//! focus, or leaves full screen, so `place` runs again on those window events and +//! when AppKit reports that the window left full screen (winit's last resize of +//! that transition arrives while the window is still in full screen). It only +//! reads frames when the buttons are already where they belong. + +// The placement maths is only called from the macOS path (and the tests). +#![cfg_attr(not(target_os = "macos"), allow(dead_code))] /// Centre of the close button from the window's left and top edges, in points at /// 100 % interface zoom (the header's title row is centred 23.5 pt down). @@ -39,7 +44,7 @@ pub fn place(window: &slint::Window, percent: u32) { // SAFETY: winit hands out the window's live NSView; everything below runs // on the main thread (the event loop) and only messages AppKit objects // reached from that view. - unsafe { mac::place(h.ns_view.as_ptr().cast(), close_center(percent)) }; + unsafe { mac::place(h.ns_view.as_ptr().cast(), percent) }; } }); } @@ -49,9 +54,59 @@ pub fn place(_window: &slint::Window, _percent: u32) {} #[cfg(target_os = "macos")] mod mac { - use objc2::msg_send; + use std::cell::Cell; + use std::ptr::NonNull; + use std::time::Duration; + use objc2::runtime::AnyObject; - use objc2_foundation::{NSPoint, NSRect}; + use objc2::{class, msg_send}; + use objc2_foundation::{NSPoint, NSRect, ns_string}; + + thread_local! { + /// Interface zoom the buttons were last placed for. + static PERCENT: Cell = const { Cell::new(100) }; + /// The NSWindow whose full-screen exit is observed (null: none yet). + static OBSERVED: Cell<*mut AnyObject> = const { Cell::new(std::ptr::null_mut()) }; + } + + /// Places the buttons for `percent` and, the first time it sees `view`'s window, + /// asks AppKit to say when that window leaves full screen. + pub(super) unsafe fn place(view: *mut AnyObject, percent: u32) { + PERCENT.with(|p| p.set(percent)); + unsafe { + let window: *mut AnyObject = msg_send![view, window]; + if window.is_null() { + return; + } + if OBSERVED.with(|o| o.get()) != window { + OBSERVED.with(|o| o.set(window)); + observe_full_screen_exit(window, view); + } + place_buttons(window, super::close_center(percent)); + } + } + + /// On NSWindowDidExitFullScreenNotification, places the buttons again now and once + /// more after AppKit's next layout pass. + unsafe fn observe_full_screen_exit(window: *mut AnyObject, view: *mut AnyObject) { + let block = block2::RcBlock::new(move |_note: NonNull| { + // SAFETY: the observer only fires on the main thread for this window, which + // the app keeps open for its whole run. + unsafe { place(view, PERCENT.with(|p| p.get())) }; + slint::Timer::single_shot(Duration::from_millis(50), move || unsafe { place(view, PERCENT.with(|p| p.get())) }); + }); + unsafe { + let center: *mut AnyObject = msg_send![class!(NSNotificationCenter), defaultCenter]; + // The returned token stays registered with the centre for the app's lifetime. + let _token: *mut AnyObject = msg_send![ + center, + addObserverForName: ns_string!("NSWindowDidExitFullScreenNotification"), + object: window, + queue: std::ptr::null_mut::(), + usingBlock: &*block + ]; + } + } /// NSWindowStyleMaskFullScreen. const FULL_SCREEN: usize = 1 << 14; @@ -63,12 +118,8 @@ mod mac { (a - b).abs() > 0.5 } - pub(super) unsafe fn place(view: *mut AnyObject, center: (f64, f64)) { + unsafe fn place_buttons(window: *mut AnyObject, center: (f64, f64)) { unsafe { - let window: *mut AnyObject = msg_send![view, window]; - if window.is_null() { - return; - } let mask: usize = msg_send![window, styleMask]; if mask & FULL_SCREEN != 0 { return;