Size the rail to the traffic lights instead of guessing

The collapsed rail was a fixed 72px, which left the cluster with more
room on its left than its right. The width now comes from the buttons
themselves - the close button's own inset plus the zoom button's right
edge - so the margin matches on both sides. That inset is macOS's to
choose and has changed between releases, so it is asked for rather than
assumed.

Measured at startup rather than on demand. The answer arrives on the main
thread, and a command waiting for it there deadlocks until the timeout
and silently returns the fallback - which is what the first attempt did.
This commit is contained in:
2026-09-02 10:07:32 +02:00
parent 0ae5746a30
commit e1ac3d7509
7 changed files with 99 additions and 10 deletions
+23
View File
@@ -20,6 +20,10 @@ pub struct AppState {
/// Title bar height: how far a child webview's origin sits above the
/// content the shell measures from.
pub chrome: Mutex<f64>,
/// Narrowest the collapsed rail can be while the traffic lights keep equal
/// margins. Measured at startup: asking on demand deadlocks, because the
/// answer arrives on the main thread and a command may be waiting on it.
pub rail: Mutex<f64>,
/// When each app last raised a notification in its own words, so a count
/// does not immediately say the same thing again in worse words.
pub last_spoke: Mutex<std::collections::HashMap<String, std::time::Instant>>,
@@ -83,6 +87,7 @@ pub fn build_state(handle: &AppHandle) -> Result<AppState, String> {
dir,
radius: Mutex::new(0.0),
chrome: Mutex::new(0.0),
rail: Mutex::new(72.0),
last_spoke: Mutex::new(std::collections::HashMap::new()),
pending_password: Mutex::new(None),
config: Mutex::new(config),
@@ -724,6 +729,24 @@ pub fn paint_window_chrome(app: &AppHandle, dark: bool) {
#[cfg(not(target_os = "macos"))]
pub fn paint_window_chrome(_: &AppHandle, _: bool) {}
/// The narrowest the collapsed rail can be while the traffic lights keep the
/// same margin on their right as macOS gave them on their left.
#[tauri::command]
pub fn rail_width(state: State<'_, AppState>) -> f64 {
*state.rail.lock().unwrap()
}
/// Works out that width, once, while nothing is waiting on the main thread.
pub fn measure_rail(app: &AppHandle) -> f64 {
match webviews::traffic_lights(app) {
// Equal by construction: the left margin is the cluster's own inset, so
// the same gap on the right means a rail of `right + left`.
Some((left, right)) => right + left,
// Nothing to measure against; the old fixed width, which was close.
None => 72.0,
}
}
/// How tall the title bar is, so the shell can keep clear of it.
#[tauri::command]
pub fn chrome_height(app: AppHandle) -> f64 {
+4
View File
@@ -91,6 +91,9 @@ pub fn run() {
let chrome = webviews::measure_chrome(&handle);
*handle.state::<commands::AppState>().chrome.lock().unwrap() = chrome;
let rail = commands::measure_rail(&handle);
*handle.state::<commands::AppState>().rail.lock().unwrap() = rail;
let theme = app.state::<commands::AppState>().cfg().settings.theme;
commands::apply_window_theme(&app.handle().clone(), &theme);
Ok(())
@@ -120,6 +123,7 @@ pub fn run() {
commands::reset_config,
commands::set_window_chrome,
commands::chrome_height,
commands::rail_width,
commands::unread_counts,
commands::focus_window,
commands::save_password,
+55
View File
@@ -805,6 +805,61 @@ pub fn measure_chrome(_: &AppHandle) -> f64 {
0.0
}
/// Where the traffic lights actually sit, as (left inset, right edge) in
/// logical pixels.
///
/// Asked rather than assumed. The collapsed rail has to be wide enough to give
/// the cluster the same margin on its right as macOS gives it on its left, and
/// those numbers are macOS's to choose — they differ by window style and have
/// changed between releases.
#[cfg(target_os = "macos")]
pub fn traffic_lights(handle: &AppHandle) -> Option<(f64, f64)> {
use objc2::runtime::AnyObject;
use objc2_foundation::NSRect;
let wv = handle.get_webview_window("main")?;
let (tx, rx) = std::sync::mpsc::channel::<Option<(f64, f64)>>();
let sent = wv.with_webview(move |platform| unsafe {
let view = platform.inner() as *mut AnyObject;
if view.is_null() {
let _ = tx.send(None);
return;
}
let window: *mut AnyObject = objc2::msg_send![view, window];
if window.is_null() {
let _ = tx.send(None);
return;
}
// NSWindowCloseButton = 0, NSWindowZoomButton = 2.
let close: *mut AnyObject = objc2::msg_send![window, standardWindowButton: 0isize];
let zoom: *mut AnyObject = objc2::msg_send![window, standardWindowButton: 2isize];
if close.is_null() || zoom.is_null() {
let _ = tx.send(None);
return;
}
let nil: *mut AnyObject = std::ptr::null_mut();
let cb: NSRect = objc2::msg_send![close, bounds];
let zb: NSRect = objc2::msg_send![zoom, bounds];
let c: NSRect = objc2::msg_send![close, convertRect: cb, toView: nil];
let z: NSRect = objc2::msg_send![zoom, convertRect: zb, toView: nil];
let _ = tx.send(Some((c.origin.x, z.origin.x + z.size.width)));
});
if sent.is_err() {
return None;
}
rx.recv_timeout(std::time::Duration::from_millis(400)).ok().flatten()
}
#[cfg(not(target_os = "macos"))]
pub fn traffic_lights(_: &AppHandle) -> Option<(f64, f64)> {
None
}
/// The title bar's height, for the shell to keep clear of.
///
/// Not an offset for positioning apps. The window's content view runs the full