From 8ea49e45f59711eaad390757edb93f8da66c056c Mon Sep 17 00:00:00 2001 From: Hoemr <48789513+Hoemr@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:19:15 +0800 Subject: [PATCH] Fix macOS dual-display presentation and fullscreen --- Cargo.lock | 53 +++++++++++++++++++++++++++++++++- Cargo.toml | 13 ++++++++- THIRD-PARTY-LICENSES.md | 5 +++- src/app.rs | 6 ++-- src/main.rs | 6 ++++ src/ui_present.rs | 63 ++++++++++++++++++++++++++++++++++++++--- 6 files changed, 137 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74c3c67..67fac0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -732,6 +732,7 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", + "pollster 1.0.3", "profiling", "raw-window-handle", "send_wrapper", @@ -739,6 +740,7 @@ dependencies = [ "wasm-bindgen", "web-sys", "web-time", + "wgpu", "windows-sys 0.61.2", "winit", ] @@ -2358,6 +2360,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" dependencies = [ "bitflags 2.13.2", + "block2 0.6.2", "objc2 0.6.5", "objc2-foundation 0.3.2", ] @@ -2383,7 +2386,10 @@ checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" dependencies = [ "bitflags 2.13.2", "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", ] [[package]] @@ -2663,6 +2669,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f3a9f18d041e6d0e102a0a46750538147e5e8992d3b4873aaafee2520b00ce3" +[[package]] +name = "pollster" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9b9cf600136f5b976fbc5727d58c26e1ecab8ee23088d44ef1c7af793550ab" + [[package]] name = "polycool" version = "0.4.0" @@ -2804,6 +2816,18 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" +[[package]] +name = "raw-window-metal" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40d213455a5f1dc59214213c7330e074ddf8114c9a42411eb890c767357ce135" +dependencies = [ + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", +] + [[package]] name = "read-fonts" version = "0.41.0" @@ -2874,7 +2898,7 @@ dependencies = [ "objc2-core-foundation", "objc2-foundation 0.3.2", "percent-encoding", - "pollster", + "pollster 0.4.0", "raw-window-handle", "wasm-bindgen", "wasm-bindgen-futures", @@ -3324,6 +3348,9 @@ dependencies = [ "jxl-oxide", "md-5", "miniz_oxide 0.8.9", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-foundation 0.3.2", "pdfium-render", "rfd", "serde", @@ -3331,6 +3358,7 @@ dependencies = [ "toml 0.9.12+spec-1.1.0", "unicode-bidi", "ureq", + "wgpu", ] [[package]] @@ -4073,12 +4101,22 @@ dependencies = [ "rustc-hash 1.1.0", "smallvec", "thiserror 2.0.21", + "wgpu-core-deps-apple", "wgpu-core-deps-windows-linux-android", "wgpu-hal", "wgpu-naga-bridge", "wgpu-types", ] +[[package]] +name = "wgpu-core-deps-apple" +version = "30.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d3f2f24aee039ffe9ef527300daf6b7428c58edeefdaa1700f4fa0fd754df16" +dependencies = [ + "wgpu-hal", +] + [[package]] name = "wgpu-core-deps-windows-linux-android" version = "30.0.1" @@ -4094,7 +4132,10 @@ version = "30.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6b7fb58561a792bc237628ba0792e332de418fefe145f13b5ed8201e6d52f58" dependencies = [ + "arrayvec", "bitflags 2.13.2", + "block2 0.6.2", + "bytemuck", "cfg-if", "cfg_aliases", "hashbrown 0.17.1", @@ -4102,10 +4143,20 @@ dependencies = [ "log", "naga", "naga-types", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", + "objc2-quartz-core 0.3.2", + "parking_lot", "portable-atomic", "portable-atomic-util", + "profiling", "raw-window-handle", + "raw-window-metal", "renderdoc-sys", + "smallvec", "static_assertions", "thiserror 2.0.21", "wgpu-naga-bridge", diff --git a/Cargo.toml b/Cargo.toml index 7eaf856..6aebaad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ name = "TeXora" path = "src/main.rs" [dependencies] -eframe = { version = "0.37", default-features = false, features = ["glow", "default_fonts", "wayland", "x11", "clipboard", "links", "system_fonts"] } +eframe = { version = "0.37", default-features = false, features = ["default_fonts", "wayland", "x11", "clipboard", "links", "system_fonts"] } egui = "0.37" pdfium-render = "0.9.5" image = { version = "0.25", default-features = false, features = ["png", "gif", "jpeg", "tiff", "bmp", "webp"] } @@ -32,6 +32,17 @@ avif-decode = { version = "3", default-features = false } jxl-oxide = { version = "0.12", default-features = false } toml = { version = "0.9", default-features = false, features = ["parse", "serde"] } +# Use Metal on macOS to avoid OpenGL texture creation failures when opening a second viewport. +[target.'cfg(target_os = "macos")'.dependencies] +eframe = { version = "0.37", default-features = false, features = ["wgpu_no_default_features"] } +objc2 = "0.6" +objc2-app-kit = { version = "0.3", default-features = false, features = ["NSApplication", "NSScreen", "NSWindow"] } +objc2-foundation = { version = "0.3", default-features = false, features = ["NSValue"] } +wgpu = { version = "30", default-features = false, features = ["metal"] } + +[target.'cfg(not(target_os = "macos"))'.dependencies] +eframe = { version = "0.37", default-features = false, features = ["glow"] } + # Fonts for other writing systems (Arabic, Hebrew, Chinese, Japanese, Korean, Hindi, Thai...) come from # the computer itself. On Linux, fontconfig is opened when the program runs instead of being linked, so # the program starts even on a system without it, and the cross-build needs no fontconfig files. diff --git a/THIRD-PARTY-LICENSES.md b/THIRD-PARTY-LICENSES.md index 006f772..cacdee9 100644 --- a/THIRD-PARTY-LICENSES.md +++ b/THIRD-PARTY-LICENSES.md @@ -18,7 +18,7 @@ ffmpeg and VLC have their own licences (LGPL/GPL), which apply to the copies you | Inter font (Rasmus Andersson) | SIL Open Font License 1.1 | https://rsms.me/inter/ | | Hack and Ubuntu Light fonts (inside egui) | MIT; Ubuntu Font Licence | https://github.com/emilk/egui | -## Rust libraries built into the program (432) +## Rust libraries built into the program (435) | Library | Version | Licence | Source | |---|---|---|---| @@ -267,6 +267,7 @@ ffmpeg and VLC have their own licences (LGPL/GPL), which apply to the copies you | poll-promise | 0.3.0 | MIT OR Apache-2.0 | https://github.com/EmbarkStudios/poll-promise | | polling | 3.11.0 | Apache-2.0 OR MIT | https://github.com/smol-rs/polling | | pollster | 0.4.0 | Apache-2.0/MIT | https://github.com/zesterer/pollster | +| pollster | 1.0.3 | MIT OR Apache-2.0 | https://github.com/zesterer/pollster | | polycool | 0.4.0 | MIT OR Apache-2.0 | https://github.com/linebender/kurbo | | portable-atomic | 1.15.0 | Apache-2.0 OR MIT | https://github.com/taiki-e/portable-atomic | | portable-atomic-util | 0.2.8 | Apache-2.0 OR MIT | https://github.com/taiki-e/portable-atomic-util | @@ -283,6 +284,7 @@ ffmpeg and VLC have their own licences (LGPL/GPL), which apply to the copies you | rav1d | 1.1.0 | BSD-2-Clause | https://github.com/memorysafety/rav1d | | raw-cpuid | 11.6.0 | MIT | https://github.com/gz/rust-cpuid | | raw-window-handle | 0.6.2 | MIT OR Apache-2.0 OR Zlib | https://github.com/rust-windowing/raw-window-handle | +| raw-window-metal | 1.1.0 | MIT OR Apache-2.0 | https://github.com/rust-windowing/raw-window-metal | | read-fonts | 0.41.0 | MIT OR Apache-2.0 | https://github.com/googlefonts/fontations | | redox_syscall | 0.4.1 | MIT | https://gitlab.redox-os.org/redox-os/syscall | | redox_syscall | 0.5.18 | MIT | https://gitlab.redox-os.org/redox-os/syscall | @@ -395,6 +397,7 @@ ffmpeg and VLC have their own licences (LGPL/GPL), which apply to the copies you | weezl | 0.1.12 | MIT OR Apache-2.0 | https://github.com/image-rs/weezl | | wgpu | 30.0.1 | MIT OR Apache-2.0 | https://github.com/gfx-rs/wgpu | | wgpu-core | 30.0.1 | MIT OR Apache-2.0 | https://github.com/gfx-rs/wgpu | +| wgpu-core-deps-apple | 30.0.2 | MIT OR Apache-2.0 | https://github.com/gfx-rs/wgpu | | wgpu-core-deps-windows-linux-android | 30.0.1 | MIT OR Apache-2.0 | https://github.com/gfx-rs/wgpu | | wgpu-hal | 30.0.1 | MIT OR Apache-2.0 | https://github.com/gfx-rs/wgpu | | wgpu-naga-bridge | 30.0.1 | MIT OR Apache-2.0 | https://github.com/gfx-rs/wgpu | diff --git a/src/app.rs b/src/app.rs index fad8fc8..11beee0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1856,7 +1856,7 @@ impl App { self.audience_open = true; self.audience_tab = self.active_id(); self.audience_age = 0; - self.audience_full = false; + self.audience_full = cfg!(target_os = "macos") && self.displays.len() >= 2; self.start_timer(); if self.displays.len() < 2 { self.status = "One display found. Press F11 for fullscreen slides.".to_string(); @@ -1866,7 +1866,9 @@ impl App { Fullscreen => { if self.audience_open { self.audience_full = !self.audience_full; - ctx.send_viewport_cmd_to(audience_id(), ViewportCommand::Fullscreen(self.audience_full)); + if !cfg!(target_os = "macos") || self.displays.len() < 2 { + ctx.send_viewport_cmd_to(audience_id(), ViewportCommand::Fullscreen(self.audience_full)); + } } else { let on = ctx.input(|i| i.viewport().fullscreen.unwrap_or(false)); ctx.send_viewport_cmd_to(ViewportId::ROOT, ViewportCommand::Fullscreen(!on)); diff --git a/src/main.rs b/src/main.rs index 13c96be..2b17694 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,6 +80,12 @@ impl eframe::App for Root { self.app.script_input(raw); } + #[cfg(target_os = "macos")] + fn on_exit(&mut self) { + self.app.on_exit(); + } + + #[cfg(not(target_os = "macos"))] fn on_exit(&mut self, _gl: Option<&eframe::glow::Context>) { self.app.on_exit(); } diff --git a/src/ui_present.rs b/src/ui_present.rs index e45ff49..ffd43e3 100644 --- a/src/ui_present.rs +++ b/src/ui_present.rs @@ -193,6 +193,46 @@ fn soft_button(ui: &Ui, r: Rect, id: &str, tip: &str, enabled: bool, active: boo resp } +#[cfg(target_os = "macos")] +fn set_macos_audience_fullscreen(title: &str, display_id: u32, fullscreen: bool) { + use objc2::MainThreadMarker; + use objc2_app_kit::{NSApplication, NSScreen, NSWindowStyleMask, NSMainMenuWindowLevel, NSNormalWindowLevel}; + use objc2_foundation::{NSNumber, NSString}; + + let Some(mtm) = MainThreadMarker::new() else { return }; + let title = NSString::from_str(title); + let windows = NSApplication::sharedApplication(mtm).windows(); + let fullscreen_level = NSMainMenuWindowLevel + 1; + let Some(window) = windows.iter().find(|window| { + window.title().isEqualToString(&title) + && (window.level() == fullscreen_level + || !window.styleMask().intersects(NSWindowStyleMask::Titled | NSWindowStyleMask::FullScreen)) + }) else { return }; + if !fullscreen { + if window.level() == fullscreen_level { + window.setLevel(NSNormalWindowLevel); + } + return; + } + + let screen_number = NSString::from_str("NSScreenNumber"); + let screens = NSScreen::screens(mtm); + let Some(screen) = screens.iter().find(|screen| { + screen.deviceDescription().objectForKey(&screen_number) + .and_then(|number| number.downcast::().ok()) + .is_some_and(|number| number.unsignedIntValue() == display_id) + }) else { return }; + + // Above the menu bar, AppKit can use the display frame instead of its visible work area. + if window.level() != fullscreen_level { + window.setLevel(fullscreen_level); + } + let frame = screen.frame(); + if window.frame() != frame { + window.setFrame_display(frame, true); + } +} + impl App { /// Draws one PDF page region. Returns false while no picture is available yet. pub fn paint_page(&mut self, ui: &Ui, rect: Rect, page: usize, region: Region, uv: Rect) -> bool { @@ -1797,6 +1837,7 @@ impl App { } pub fn audience_window(&mut self, ctx: &Context) { + const TITLE: &str = "TeXora Audience View"; if !self.audience_open { return; } @@ -1807,15 +1848,23 @@ impl App { self.activate(i); } let multi = self.displays.len() >= 2; - let mut vb = ViewportBuilder::default().with_title("TeXora Audience View").with_inner_size([960.0, 720.0]); + let mut vb = ViewportBuilder::default().with_title(TITLE) + .with_decorations(true).with_inner_size([960.0, 720.0]); if multi { if let Some(d) = self.displays.get(self.display) { - let scale = if cfg!(target_os = "macos") { 1.0 } else { d.scale_factor.max(1.0) }; - vb = vb.with_position(pos2(d.x as f32 / scale + 20.0, d.y as f32 / scale + 20.0)); + let scale = if cfg!(target_os = "macos") { ctx.zoom_factor() } else { d.scale_factor.max(1.0) }; + let origin = pos2(d.x as f32 / scale, d.y as f32 / scale); + if cfg!(target_os = "macos") && self.audience_full { + // A native fullscreen Space can hide the presenter on the other display. + vb = vb.with_decorations(false).with_position(origin) + .with_inner_size([d.width as f32 / scale, d.height as f32 / scale]); + } else { + vb = vb.with_position(origin + vec2(20.0, 20.0)); + } } } let id = audience_id(); - if multi && self.audience_age == 12 && !self.audience_full { + if !cfg!(target_os = "macos") && multi && self.audience_age == 12 && !self.audience_full { self.audience_full = true; ctx.send_viewport_cmd_to(id, ViewportCommand::Fullscreen(true)); } @@ -1834,6 +1883,12 @@ impl App { c.set_cursor_icon(CursorIcon::None); } }); + #[cfg(target_os = "macos")] + if multi && self.audience_open { + if let Some(display) = self.displays.get(self.display) { + set_macos_audience_fullscreen(TITLE, display.id, self.audience_full); + } + } if let Some(i) = self.tab_index(front) { self.activate(i); }