mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-06 14:04:01 -05:00
Allow a third Metal drawable in native macOS fullscreen
(cherry picked from commit 7db123a913f49177ce3d53e54bb9f2b5044861ee)
This commit is contained in:
1 parent
1c539fb496
commit
7d4c3cfa11
3 files changed
+67
No files matched your search
@@ -32,6 +32,8 @@ mod frame;
|
||||
mod input;
|
||||
mod keys;
|
||||
mod links;
|
||||
#[cfg(any(target_os = "macos", test))]
|
||||
mod metal_fullscreen;
|
||||
pub(crate) mod picks;
|
||||
mod prefs;
|
||||
// Reloading a document whose file changed on disk; the web watches no files.
|
||||
@@ -39,6 +41,8 @@ mod prefs;
|
||||
pub(crate) mod reload;
|
||||
pub mod settings;
|
||||
mod window;
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) use metal_fullscreen::MetalFullscreen;
|
||||
|
||||
pub(crate) use clipboard::{ObjectClipMark, copy_refused, print_refused};
|
||||
pub(crate) use files::same_file;
|
||||
@@ -374,6 +378,8 @@ impl PdfCraftApp {
|
||||
update_check_at_start: true,
|
||||
at_exit: None,
|
||||
startup_time: None,
|
||||
#[cfg(target_os = "macos")]
|
||||
metal_fullscreen: MetalFullscreen::default(),
|
||||
show_secured_notice: true,
|
||||
text_links: true,
|
||||
always_default_view: false,
|
||||
@@ -742,6 +748,8 @@ impl eframe::App for PdfCraftApp {
|
||||
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
embed::before_ui(ui.ctx(), frame);
|
||||
#[cfg(target_os = "macos")]
|
||||
self.metal_fullscreen.apply(ui.ctx(), frame);
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
let _ = frame;
|
||||
self.begin_ui();
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
//! Drawable queue depth for the root AppKit window.
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct MetalFullscreen {
|
||||
fullscreen: bool,
|
||||
raised_latency: bool,
|
||||
}
|
||||
|
||||
impl MetalFullscreen {
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(super) fn apply(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
||||
let fullscreen = ctx.input(|i| i.viewport().fullscreen);
|
||||
let Some(mut surface) = frame.wgpu_surface_config() else { return };
|
||||
let latency = surface.desired_maximum_frame_latency;
|
||||
if let Some(depth) = self.next_latency(fullscreen, latency, || {
|
||||
frame.wgpu_render_state().is_some_and(|state| state.adapter.get_info().backend == eframe::wgpu::Backend::Metal)
|
||||
}) {
|
||||
surface.desired_maximum_frame_latency = Some(depth);
|
||||
frame.set_wgpu_surface_config(surface);
|
||||
}
|
||||
}
|
||||
|
||||
fn next_latency(&mut self, fullscreen: Option<bool>, latency: Option<u32>, is_metal: impl FnOnce() -> bool) -> Option<u32> {
|
||||
let fullscreen = fullscreen?;
|
||||
if self.fullscreen == fullscreen {
|
||||
return None;
|
||||
}
|
||||
self.fullscreen = fullscreen;
|
||||
if fullscreen {
|
||||
// With two CAMetalLayer drawables, nextDrawable can wait for the compositor
|
||||
// to return the previous frame. Allow a third while in native fullscreen.
|
||||
self.raised_latency = latency == Some(1) && is_metal();
|
||||
self.raised_latency.then_some(2)
|
||||
} else {
|
||||
(std::mem::take(&mut self.raised_latency) && latency == Some(2)).then_some(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn native_fullscreen_uses_a_third_metal_drawable_then_restores_windowed_latency() {
|
||||
let mut state = MetalFullscreen::default();
|
||||
assert_eq!(state.next_latency(Some(false), Some(1), || true), None);
|
||||
assert_eq!(state.next_latency(Some(true), Some(1), || true), Some(2));
|
||||
assert_eq!(state.next_latency(None, Some(2), || true), None);
|
||||
assert_eq!(state.next_latency(Some(true), Some(2), || true), None);
|
||||
assert_eq!(state.next_latency(Some(false), Some(2), || true), Some(1));
|
||||
for (latency, metal) in [(Some(2), true), (None, true), (Some(1), false)] {
|
||||
assert_eq!(state.next_latency(Some(true), latency, || metal), None);
|
||||
assert_eq!(state.next_latency(Some(false), latency, || metal), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -446,6 +446,8 @@ pub struct PdfCraftApp {
|
||||
pub at_exit: Option<Box<dyn FnOnce()>>,
|
||||
/// Native launch time, consumed after the first UI pass for startup diagnostics.
|
||||
pub startup_time: Option<std::time::Instant>,
|
||||
#[cfg(target_os = "macos")]
|
||||
metal_fullscreen: app::MetalFullscreen,
|
||||
/// Preferences: tell the user, above the pages, that a document is secured (its security
|
||||
/// settings restrict changes). Off only hides that notice (#4035); kept in the settings.
|
||||
pub show_secured_notice: bool,
|
||||
|
||||
Reference in new issue
Block a user