servoshell: Revert change that removed CentralPanel

This commit is contained in:
delan azabani
2025-11-03 17:13:11 +08:00
parent f174c53fee
commit 5f07ea33c6
2 changed files with 67 additions and 40 deletions

View File

@@ -317,7 +317,6 @@ impl Painter {
lcp_calculator: LargestContentfulPaintCalculator::new(),
};
painter.assert_gl_framebuffer_complete();
painter.clear_background();
painter
}

View File

@@ -10,7 +10,9 @@ use std::time::Instant;
use dpi::PhysicalSize;
use egui::text::{CCursor, CCursorRange};
use egui::text_edit::TextEditState;
use egui::{Button, Key, Label, LayerId, Modifiers, PaintCallback, TopBottomPanel, Vec2, pos2};
use egui::{
Button, CentralPanel, Frame, Key, Label, Modifiers, PaintCallback, TopBottomPanel, Vec2, pos2,
};
use egui_glow::{CallbackFn, EguiGlow};
use egui_winit::EventResponse;
use euclid::{Box2D, Length, Point2D, Rect, Scale, Size2D};
@@ -141,6 +143,9 @@ impl Minibrowser {
std::mem::take(&mut self.event_queue)
}
/// Preprocess the given [winit::event::WindowEvent], returning unconsumed for mouse events in
/// the Servo browser rect. This is needed because the CentralPanel we create for our webview
/// would otherwise make egui report events in that area as consumed.
pub fn on_window_event(
&mut self,
window: &Window,
@@ -149,6 +154,16 @@ impl Minibrowser {
) -> EventResponse {
let mut result = self.context.on_window_event(window, event);
// For some reason, egui is eating all PinchGesture events, even when they happen
// on top of a WebView. Detect this situation and avoid sending those events to
// egui.
if matches!(event, WindowEvent::PinchGesture { .. }) &&
self.last_mouse_position
.is_some_and(|point| !self.is_in_egui_toolbar_rect(point))
{
return Default::default();
}
if app_state.has_active_dialog() {
result.consumed = true;
return result;
@@ -281,6 +296,8 @@ impl Minibrowser {
}
/// Update the minibrowser, but dont paint.
/// If `servo_framebuffer_id` is given, set up a paint callback to blit its contents to our
/// CentralPanel when [`Minibrowser::paint`] is called.
pub fn update(
&mut self,
window: &dyn WindowPortsMethods,
@@ -434,48 +451,59 @@ impl Minibrowser {
let scale =
Scale::<_, DeviceIndependentPixel, DevicePixel>::new(ctx.pixels_per_point());
state.for_each_active_dialog(|dialog| dialog.update(ctx));
egui::CentralPanel::default().show(ctx, |_| {
state.for_each_active_dialog(|dialog| dialog.update(ctx));
});
// If the top parts of the GUI changed size, then update the size of the WebView and also
// the size of its RenderingContext.
let rect = ctx.available_rect();
let size = Size2D::new(rect.width(), rect.height()) * scale;
let rect = Box2D::from_origin_and_size(Point2D::origin(), size);
if let Some(webview) = state.focused_webview() &&
rect != webview.rect()
{
webview.move_resize(rect);
// `rect` is sized to just the WebView viewport, which is required by
// `OffscreenRenderingContext` See:
// <https://github.com/servo/servo/issues/38369#issuecomment-3138378527>
webview.resize(PhysicalSize::new(size.width as u32, size.height as u32))
}
let Some(webview) = state.focused_webview() else {
return;
};
CentralPanel::default().frame(Frame::NONE).show(ctx, |ui| {
// If the top parts of the GUI changed size, then update the size of the WebView and also
// the size of its RenderingContext.
let available_size = ui.available_size();
let size = Size2D::new(available_size.x, available_size.y) * scale;
let rect = Box2D::from_origin_and_size(Point2D::origin(), size);
if rect != webview.rect() {
webview.move_resize(rect);
// `rect` is sized to just the WebView viewport, which is required by
// `OffscreenRenderingContext` See:
// <https://github.com/servo/servo/issues/38369#issuecomment-3138378527>
webview.resize(PhysicalSize::new(size.width as u32, size.height as u32))
}
if let Some(status_text) = &self.status_text {
egui::Tooltip::always_open(
ctx.clone(),
LayerId::background(),
"tooltip layer".into(),
pos2(0.0, ctx.available_rect().max.y),
)
.show(|ui| ui.add(Label::new(status_text.clone()).extend()));
}
let min = ui.cursor().min;
let size = ui.available_size();
let rect = egui::Rect::from_min_size(min, size);
ui.allocate_space(size);
state.repaint_servo_if_necessary();
if let Some(status_text) = &self.status_text {
egui::Tooltip::always_open(
ctx.clone(),
ui.layer_id(),
"tooltip layer".into(),
pos2(0.0, ctx.available_rect().max.y),
)
.show(|ui| ui.add(Label::new(status_text.clone()).extend()))
.map(|response| response.inner);
}
if let Some(render_to_parent) = rendering_context.render_to_parent_callback() {
ctx.layer_painter(LayerId::background()).add(PaintCallback {
rect: ctx.available_rect(),
callback: Arc::new(CallbackFn::new(move |info, painter| {
let clip = info.viewport_in_pixels();
let rect_in_parent = Rect::new(
Point2D::new(clip.left_px, clip.from_bottom_px),
Size2D::new(clip.width_px, clip.height_px),
);
render_to_parent(painter.gl(), rect_in_parent)
})),
});
}
state.repaint_servo_if_necessary();
if let Some(render_to_parent) = rendering_context.render_to_parent_callback() {
ui.painter().add(PaintCallback {
rect,
callback: Arc::new(CallbackFn::new(move |info, painter| {
let clip = info.viewport_in_pixels();
let rect_in_parent = Rect::new(
Point2D::new(clip.left_px, clip.from_bottom_px),
Size2D::new(clip.width_px, clip.height_px),
);
render_to_parent(painter.gl(), rect_in_parent)
})),
});
}
});
*last_update = now;
});