Merge pull request #972 from storytold/fix/968-traffic-lights
#968: centre the macOS window buttons on the app bar
This commit is contained in:
commit
9f659195c3
|
|
@ -4335,6 +4335,7 @@ dependencies = [
|
|||
"objc2-core-foundation",
|
||||
"objc2-core-text",
|
||||
"objc2-foundation 0.3.2",
|
||||
"raw-window-handle",
|
||||
"rfd",
|
||||
"serde_json",
|
||||
"vectorcraft-engine",
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -51,10 +51,13 @@ windows = { version = "0.62", features = ["Win32_Graphics_DirectWrite"] }
|
|||
# with default features off). Pinned exactly: review the diff before bumping.
|
||||
muda = { version = "=0.21.1", default-features = false }
|
||||
objc2 = "0.6"
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSMenu", "NSEvent", "NSPasteboard", "NSResponder", "NSTextInputContext"] }
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSButton", "NSControl", "NSMenu", "NSEvent", "NSPasteboard", "NSResponder", "NSTextInputContext", "NSView", "NSWindow"] }
|
||||
# Files opened from Finder and the Dock (src/open_documents.rs) and pasted bitmaps (src/clipboard.rs); both already come with winit, muda and rfd.
|
||||
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "block2", "NSArray", "NSData", "NSNotification", "NSOperation", "NSString", "NSURL"] }
|
||||
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "block2", "NSArray", "NSData", "NSGeometry", "NSNotification", "NSOperation", "NSString", "NSURL"] }
|
||||
block2 = "0.6"
|
||||
# The window's AppKit view, to centre its buttons on the app bar (src/mac_window.rs). MIT OR
|
||||
# Apache-2.0 OR Zlib, already in the build through eframe and winit.
|
||||
raw-window-handle = "0.6"
|
||||
# CoreText's font manager (src/mac_fonts.rs): fonts that apps and font managers register from their
|
||||
# own folders (#579). Zlib OR Apache-2.0 OR MIT; objc2-core-foundation already comes with
|
||||
# objc2-foundation.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
//! macOS: the window buttons (close, minimize, zoom) centred on the app bar (#968).
|
||||
//!
|
||||
//! AppKit puts the buttons in a standard title bar, shorter than the app bar they sit over, so they
|
||||
//! rode high on it. [`center_buttons`] moves them to the middle of the bar, inset from its left as
|
||||
//! far as from its top, and makes the title bar as tall as the app bar so they stay clickable.
|
||||
//! AppKit lays its title bar out again when the window resizes or leaves full screen, so this runs
|
||||
//! every frame and moves the buttons only when they are out of place.
|
||||
//!
|
||||
//! Reaching the title bar's views from the window's content view has no safe API, hence the scoped
|
||||
//! `unsafe_code` allowance.
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use objc2::runtime::AnyObject;
|
||||
use objc2_app_kit::{NSView, NSWindowButton, NSWindowStyleMask};
|
||||
use objc2_foundation::NSPoint;
|
||||
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
|
||||
|
||||
/// The room the app bar keeps at its left for the buttons in AppKit's own place, before they are
|
||||
/// centred (and in full screen, where AppKit shows them elsewhere): their width and a gap.
|
||||
pub const DEFAULT_INSET: f32 = 78.0;
|
||||
|
||||
/// Differences below this (in points) leave a frame as it is, so a frame in place isn't set again.
|
||||
const SLACK: f64 = 0.25;
|
||||
|
||||
/// Centre the window's buttons on an app bar `bar` egui points tall, at the window's `zoom` factor
|
||||
/// (native points per egui point). Returns the room the app bar keeps clear at its left for them,
|
||||
/// in egui points, or `None` when they aren't over the bar (full screen) or can't be found.
|
||||
pub fn center_buttons(frame: &eframe::Frame, zoom: f32, bar: f32) -> Option<f32> {
|
||||
let RawWindowHandle::AppKit(handle) = frame.window_handle().ok()?.as_raw() else {
|
||||
return None;
|
||||
};
|
||||
// SAFETY: the handle's view is the window's content view, which AppKit keeps alive while the
|
||||
// window is open, and eframe hands out the handle only while it is.
|
||||
let view = unsafe { handle.ns_view.cast::<NSView>().as_ref() };
|
||||
let window = view.window()?;
|
||||
if window.styleMask().contains(NSWindowStyleMask::FullScreen) {
|
||||
return None;
|
||||
}
|
||||
let close = window.standardWindowButton(NSWindowButton::CloseButton)?;
|
||||
let minimize = window.standardWindowButton(NSWindowButton::MiniaturizeButton)?;
|
||||
let zoom_button = window.standardWindowButton(NSWindowButton::ZoomButton)?;
|
||||
// The buttons sit in the title bar view, inside the title bar container that sets its height.
|
||||
// SAFETY: both views belong to the window, alive while it is; used right away.
|
||||
let container = unsafe { close.superview().and_then(|titlebar| titlebar.superview()) }?;
|
||||
// Another layout (a later macOS) is left as AppKit made it.
|
||||
if AnyObject::class(&container).name() != c"NSTitlebarContainerView" {
|
||||
return None;
|
||||
}
|
||||
let size = close.frame().size;
|
||||
let gap = minimize.frame().origin.x - (close.frame().origin.x + size.width);
|
||||
let height = f64::from(bar * zoom);
|
||||
let margin = (height - size.height) / 2.0;
|
||||
if !(margin.is_finite() && margin > 0.0 && gap.is_finite() && gap >= 0.0 && zoom > 0.0) {
|
||||
return None;
|
||||
}
|
||||
let mut bounds = container.frame();
|
||||
let top = window.frame().size.height - height;
|
||||
if (bounds.size.height - height).abs() > SLACK || (bounds.origin.y - top).abs() > SLACK {
|
||||
bounds.size.height = height;
|
||||
bounds.origin.y = top;
|
||||
container.setFrame(bounds);
|
||||
}
|
||||
let mut x = margin;
|
||||
for button in [close, minimize, zoom_button] {
|
||||
let at = NSPoint::new(x, (height - button.frame().size.height) / 2.0);
|
||||
let now = button.frame().origin;
|
||||
if (now.x - at.x).abs() > SLACK || (now.y - at.y).abs() > SLACK {
|
||||
button.setFrameOrigin(at);
|
||||
}
|
||||
x += size.width + gap;
|
||||
}
|
||||
// The last button's right edge (`x` steps one gap past it) and the margin again after it.
|
||||
Some(((x - gap + margin) / f64::from(zoom)) as f32)
|
||||
}
|
||||
|
|
@ -31,6 +31,8 @@ mod mac_fonts;
|
|||
#[cfg(target_os = "macos")]
|
||||
mod mac_menu;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod mac_window;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod open_documents;
|
||||
mod prefs_dir;
|
||||
mod printing;
|
||||
|
|
@ -69,7 +71,14 @@ impl eframe::App for App {
|
|||
}
|
||||
file_access::unconfined(|| self.app.raw_input_hook(raw));
|
||||
}
|
||||
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
||||
#[cfg_attr(not(target_os = "macos"), allow(unused_variables))]
|
||||
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
let bar = vectorcraft_ui_egui::chrome::APP_BAR_HEIGHT;
|
||||
let inset = mac_window::center_buttons(frame, ui.ctx().zoom_factor(), bar);
|
||||
self.app.titlebar_inset = inset.unwrap_or(mac_window::DEFAULT_INSET);
|
||||
}
|
||||
file_access::unconfined(|| self.app.ui(ui));
|
||||
#[cfg(target_os = "macos")]
|
||||
if self.app.take_ime_discard() {
|
||||
|
|
@ -566,7 +575,6 @@ fn main() -> std::process::ExitCode {
|
|||
{
|
||||
app.graphics_adapter = Some("OpenGL (Windows 7 compatibility)".into());
|
||||
}
|
||||
app.integrated_titlebar = cfg!(target_os = "macos");
|
||||
app.custom_titlebar = CUSTOM_TITLEBAR;
|
||||
if let Some(port) = control_port {
|
||||
let rx = control_server::start(port, cc.egui_ctx.clone());
|
||||
|
|
|
|||
|
|
@ -12,15 +12,18 @@ use crate::theme::{self, Tokens};
|
|||
use crate::widgets;
|
||||
use crate::{VectorcraftApp, icons, menus, titlebar};
|
||||
|
||||
/// The application bar's height, in points.
|
||||
pub const APP_BAR_HEIGHT: f32 = 44.0;
|
||||
|
||||
/// The application bar: brand mark, Home, menus, then Discord, search and the workspace switcher
|
||||
/// at the right. With [`VectorcraftApp::custom_titlebar`] it is also the window's title bar
|
||||
/// ([`titlebar`]): the caption buttons take the right end and the rest of the bar drags the window.
|
||||
pub fn app_bar(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
let custom = app.custom_titlebar;
|
||||
let left = if app.integrated_titlebar { 78 } else { 8 };
|
||||
let left = app.titlebar_inset.clamp(8.0, f32::from(i8::MAX)) as i8;
|
||||
let frame = egui::Frame::NONE.fill(t.app_bar).inner_margin(egui::Margin { left, right: if custom { 0 } else { 14 }, top: 0, bottom: 0 });
|
||||
let bar = egui::Panel::top("app_bar").exact_size(44.0).frame(frame.stroke(Stroke::new(1.0, t.border))).show(ui, |ui| {
|
||||
let bar = egui::Panel::top("app_bar").exact_size(APP_BAR_HEIGHT).frame(frame.stroke(Stroke::new(1.0, t.border))).show(ui, |ui| {
|
||||
if custom {
|
||||
titlebar::drag_area(ui, ui.max_rect());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -314,8 +314,9 @@ pub struct VectorcraftApp {
|
|||
pub services: Services,
|
||||
pub canvas: CanvasCache,
|
||||
pub perf: Perf,
|
||||
/// macOS: draw our own title strip under the traffic lights.
|
||||
pub integrated_titlebar: bool,
|
||||
/// macOS: the room the app bar keeps at its left for the window buttons over it, in points
|
||||
/// (0 where the OS draws no buttons over the app bar).
|
||||
pub titlebar_inset: f32,
|
||||
/// Last applied effect (Effect → Apply Last Effect).
|
||||
pub last_effect: Option<(String, serde_json::Value)>,
|
||||
/// Commands run through [`Self::run`] so far: the native menu bar reads its rows again when it
|
||||
|
|
@ -440,7 +441,7 @@ impl VectorcraftApp {
|
|||
cursors: Default::default(),
|
||||
},
|
||||
perf: Perf::default(),
|
||||
integrated_titlebar: false,
|
||||
titlebar_inset: 0.0,
|
||||
last_effect: None,
|
||||
run_count: 0,
|
||||
clipboard_out: None,
|
||||
|
|
|
|||
Loading…
Reference in New Issue