From 014a8cf8b7b54fec324c59cf5df6585cab87e691 Mon Sep 17 00:00:00 2001 From: mileswa1q22 Date: Fri, 21 Aug 2026 18:42:25 -0500 Subject: [PATCH] feat: update dependencies and enhance window initialization for rendering context --- Cargo.lock | 7 ++++ Cargo.toml | 2 ++ src/app.rs | 93 ++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 79 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 134a6a3..cbd4fda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -676,6 +676,8 @@ dependencies = [ "image", "keyboard-types", "log", + "objc2 0.6.4", + "objc2-quartz-core 0.3.2", "pollster", "rustls", "servo", @@ -4527,8 +4529,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d425caf1df73233f29fd8a5c3e5edbc30d2d4307870f802d18f00d83dc5141a6" dependencies = [ "bitflags 2.13.1", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", + "objc2-io-surface", ] [[package]] @@ -4629,9 +4633,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" dependencies = [ "bitflags 2.13.1", + "block2 0.6.2", + "libc", "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", + "objc2-core-video", "objc2-foundation 0.3.2", "objc2-metal 0.3.2", ] diff --git a/Cargo.toml b/Cargo.toml index 756b478..212ba2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,8 @@ euclid = "0.22" image = "0.25" keyboard-types = "0.8" log = "0.4.33" +objc2 = "0.6" +objc2-quartz-core = "0.3" pollster = "0.4" rustls = "0.23" servo = "0.5.0" diff --git a/src/app.rs b/src/app.rs index e75f2f5..3b283c2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,13 +6,16 @@ use std::time::{Duration, Instant}; use egui_wgpu::{Renderer, RendererOptions, ScreenDescriptor}; use egui_winit::State as EguiWinitState; use keyboard_types::Modifiers; +use objc2::msg_send; +use objc2::runtime::AnyObject; +use objc2_quartz_core::CAMetalLayer; use servo::RenderingContext; use url::Url; use winit::application::ApplicationHandler; -use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize}; +use winit::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize}; use winit::event::{ElementState, WindowEvent}; use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoopProxy}; -use winit::raw_window_handle::{HasDisplayHandle, HasWindowHandle}; +use winit::raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; use winit::window::{Window, WindowId}; use crate::servo_host::{ @@ -75,12 +78,48 @@ impl BrowserApp { } fn init(&mut self, event_loop: &ActiveEventLoop) -> Result<(), Box> { - let attrs = - Window::default_attributes() - .with_title("Fox") - .with_inner_size(LogicalSize::new(1360.0, 900.0)); + let attrs = Window::default_attributes() + .with_title("Fox") + .with_inner_size(LogicalSize::new(1360.0, 900.0)) + .with_position(LogicalPosition::new(40.0, 40.0)); let window = Arc::new(event_loop.create_window(attrs)?); self.scale_factor = window.scale_factor(); + let size = window.inner_size(); + + // The Servo (surfman) rendering context REPLACES the view's root CALayer + // with its own plain layer. It must be created and detached again before + // wgpu grabs the view's layer, otherwise wgpu presents into an orphaned + // CAMetalLayer and nothing ever appears on screen. + let display_handle = event_loop + .display_handle() + .map_err(|e| format!("no display handle: {e:?}"))?; + let window_handle = window + .window_handle() + .map_err(|e| format!("no window handle: {e:?}"))?; + let window_context = Rc::new(servo::WindowRenderingContext::new( + display_handle, + window_handle, + size, + ) + .map_err(|e| format!("failed to create Servo rendering context: {e:?}"))?); + window_context.make_current().ok(); + window_context.take_window().ok(); + + // take_window() removes surfman's content sublayer but leaves its empty + // root superlayer installed on the view; swap in a fresh CAMetalLayer. + { + let ns_view = match window.window_handle()?.as_raw() { + RawWindowHandle::AppKit(h) => h.ns_view.as_ptr() as *mut AnyObject, + other => { + return Err(format!("unsupported window handle: {other:?}").into()); + } + }; + let metal_layer = CAMetalLayer::new(); + unsafe { + let _: () = msg_send![ns_view, setLayer: &*metal_layer]; + let _: () = msg_send![ns_view, setWantsLayer: true]; + } + } let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); let surface = instance.create_surface(window.clone())?; @@ -93,7 +132,6 @@ impl BrowserApp { let caps = surface.get_capabilities(&adapter); let format = caps.formats[0]; - let size = window.inner_size(); let config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, format, @@ -122,9 +160,6 @@ impl BrowserApp { let _ = proxy.send_event(UserEvent::RepaintAfter(info.delay)); }); } - let display_handle = event_loop - .display_handle() - .map_err(|e| format!("no display handle: {e:?}"))?; let mut egui_winit = EguiWinitState::new( egui_ctx.clone(), egui::ViewportId::ROOT, @@ -134,28 +169,38 @@ impl BrowserApp { None, ); egui_winit.set_max_texture_side(device.limits().max_texture_dimension_2d as usize); - let renderer = Renderer::new(&device, format, RendererOptions::default()); + let mut renderer = Renderer::new(&device, format, RendererOptions::default()); + // Warm up the context so `max_texture_side` from RawInput is applied + // before the first page-texture upload (otherwise egui validates + // against its built-in default of 2048 and panics on large textures). + // The pass also emits the font atlas delta, which must be uploaded. + let mut warmup = egui_ctx.run_ui( + egui::RawInput { + max_texture_side: Some(device.limits().max_texture_dimension_2d as usize), + ..egui::RawInput::default() + }, + |_| {}, + ); + for (id, deltas) in &warmup.textures_delta.set { + for delta in deltas { + renderer.update_texture(&device, &queue, *id, delta); + } + } + for id in &warmup.textures_delta.free { + renderer.free_texture(id); + } + warmup.textures_delta.clear(); self.apply_style(&egui_ctx); - let window_handle = window - .window_handle() - .map_err(|e| format!("no window handle: {e:?}"))?; - let window_context = Rc::new(servo::WindowRenderingContext::new( - display_handle, - window_handle, - size, - ) - .map_err(|e| format!("failed to create Servo rendering context: {e:?}"))?); - window_context.make_current().ok(); let waker = EguiWaker(egui_ctx.clone()); let engine = Rc::new(Engine::new(waker, window_context)); - engine.window_context.take_window().ok(); let start_url = Url::parse(START_PAGE)?; let tab = engine.create_tab(start_url, self.scale_factor as f32); self.tabs.push(tab); self.active = 0; + self.engine = Some(engine); self.gpu = Some(Gpu { surface, @@ -173,6 +218,7 @@ impl BrowserApp { } fn apply_style(&self, ctx: &egui::Context) { + ctx.set_theme(egui::ThemePreference::Light); let mut visuals = egui::Visuals::light(); visuals.panel_fill = CHROME_BG; visuals.widgets.noninteractive.bg_fill = CHROME_BG; @@ -650,7 +696,7 @@ impl BrowserApp { (screen, raw_input, gpu.egui_ctx.clone()) }; - let full_output = egui_ctx.run_ui(raw_input, |ui| { + let mut full_output = egui_ctx.run_ui(raw_input, |ui| { self.build_ui(ui); }); let paint_jobs = egui_ctx.tessellate(full_output.shapes, full_output.pixels_per_point); @@ -724,6 +770,7 @@ impl BrowserApp { for id in &full_output.textures_delta.free { gpu.renderer.free_texture(id); } + full_output.textures_delta.clear(); gpu.queue.submit( buffers