More cleanup, less unwrap() (#35249)

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2025-02-01 05:00:22 -08:00 committed by GitHub
parent 3122de08f3
commit 6fd63b6c7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 40 additions and 40 deletions

View file

@ -47,7 +47,7 @@ pub struct App {
servo_shell_preferences: ServoShellPreferences, servo_shell_preferences: ServoShellPreferences,
clipboard: Option<Clipboard>, clipboard: Option<Clipboard>,
servo: Option<Servo>, servo: Option<Servo>,
webviews: Option<WebViewManager>, webviews: WebViewManager,
suspended: Cell<bool>, suspended: Cell<bool>,
windows: HashMap<WindowId, Rc<dyn WindowPortsMethods>>, windows: HashMap<WindowId, Rc<dyn WindowPortsMethods>>,
minibrowser: Option<Minibrowser>, minibrowser: Option<Minibrowser>,
@ -93,7 +93,7 @@ impl App {
preferences, preferences,
servo_shell_preferences, servo_shell_preferences,
clipboard: Clipboard::new().ok(), clipboard: Clipboard::new().ok(),
webviews: None, webviews: WebViewManager::new(),
servo: None, servo: None,
suspended: Cell::new(false), suspended: Cell::new(false),
windows: HashMap::new(), windows: HashMap::new(),
@ -151,7 +151,7 @@ impl App {
}; };
// Create window's context // Create window's context
self.webviews = Some(WebViewManager::new(window.clone())); self.webviews.set_window(window.clone());
if window.winit_window().is_some() { if window.winit_window().is_some() {
self.minibrowser = Some(Minibrowser::new( self.minibrowser = Some(Minibrowser::new(
&rendering_context, &rendering_context,
@ -164,8 +164,8 @@ impl App {
// Servo is not yet initialised, so there is no `servo_framebuffer_id`. // Servo is not yet initialised, so there is no `servo_framebuffer_id`.
minibrowser.update( minibrowser.update(
window.winit_window().unwrap(), window.winit_window().unwrap(),
self.webviews.as_mut().unwrap(), &mut self.webviews,
self.servo.as_mut(), self.servo.as_ref(),
"init", "init",
); );
window.set_toolbar_height(minibrowser.toolbar_height); window.set_toolbar_height(minibrowser.toolbar_height);
@ -226,7 +226,7 @@ impl App {
servo.setup_logging(); servo.setup_logging();
let webview = servo.new_webview(self.initial_url.clone().into_url()); let webview = servo.new_webview(self.initial_url.clone().into_url());
self.webviews.as_mut().unwrap().add(webview); self.webviews.add(webview);
self.servo = Some(servo); self.servo = Some(servo);
} }
@ -242,12 +242,10 @@ impl App {
/// ///
/// In the future, these tasks may be decoupled. /// In the future, these tasks may be decoupled.
fn handle_events(&mut self) -> PumpResult { fn handle_events(&mut self) -> PumpResult {
let webviews = self.webviews.as_mut().unwrap();
// If the Gamepad API is enabled, handle gamepad events from GilRs. // If the Gamepad API is enabled, handle gamepad events from GilRs.
// Checking for focused_webview_id should ensure we'll have a valid browsing context. // Checking for focused_webview_id should ensure we'll have a valid browsing context.
if pref!(dom_gamepad_enabled) && webviews.focused_webview_id().is_some() { if pref!(dom_gamepad_enabled) && self.webviews.focused_webview_id().is_some() {
webviews.handle_gamepad_events(); self.webviews.handle_gamepad_events();
} }
// Take any new embedder messages from Servo. // Take any new embedder messages from Servo.
@ -258,7 +256,7 @@ impl App {
let mut need_update = false; let mut need_update = false;
loop { loop {
// Consume and handle those embedder messages. // Consume and handle those embedder messages.
let servo_event_response = webviews.handle_servo_events( let servo_event_response = self.webviews.handle_servo_events(
servo, servo,
&mut self.clipboard, &mut self.clipboard,
&self.opts, &self.opts,
@ -269,7 +267,7 @@ impl App {
// Runs the compositor, and receives and collects embedder messages from various Servo components. // Runs the compositor, and receives and collects embedder messages from various Servo components.
need_resize |= servo.handle_events(vec![]); need_resize |= servo.handle_events(vec![]);
if webviews.shutdown_requested() { if self.webviews.shutdown_requested() {
return PumpResult::Shutdown; return PumpResult::Shutdown;
} }
@ -311,14 +309,13 @@ impl App {
PumpResult::Continue { update, present } => { PumpResult::Continue { update, present } => {
if update { if update {
if let Some(ref mut minibrowser) = self.minibrowser { if let Some(ref mut minibrowser) = self.minibrowser {
let webviews = self.webviews.as_mut().unwrap(); if minibrowser.update_webview_data(&mut self.webviews) {
if minibrowser.update_webview_data(webviews) {
// Update the minibrowser immediately. While we could update by requesting a // Update the minibrowser immediately. While we could update by requesting a
// redraw, doing so would delay the location update by two frames. // redraw, doing so would delay the location update by two frames.
minibrowser.update( minibrowser.update(
window.winit_window().unwrap(), window.winit_window().unwrap(),
webviews, &mut self.webviews,
self.servo.as_mut(), self.servo.as_ref(),
"update_location_in_toolbar", "update_location_in_toolbar",
); );
} }
@ -335,8 +332,8 @@ impl App {
if let Some(ref mut minibrowser) = self.minibrowser { if let Some(ref mut minibrowser) = self.minibrowser {
minibrowser.update( minibrowser.update(
window.winit_window().unwrap(), window.winit_window().unwrap(),
self.webviews.as_mut().unwrap(), &mut self.webviews,
self.servo.as_mut(), self.servo.as_ref(),
"PumpResult::Present::Immediate", "PumpResult::Present::Immediate",
); );
minibrowser.paint(window.winit_window().unwrap()); minibrowser.paint(window.winit_window().unwrap());
@ -408,9 +405,6 @@ impl App {
/// Takes any events generated during `egui` updates and performs their actions. /// Takes any events generated during `egui` updates and performs their actions.
fn handle_servoshell_ui_events(&mut self) { fn handle_servoshell_ui_events(&mut self) {
let Some(webviews) = self.webviews.as_mut() else {
return;
};
let Some(minibrowser) = self.minibrowser.as_ref() else { let Some(minibrowser) = self.minibrowser.as_ref() else {
return; return;
}; };
@ -429,34 +423,34 @@ impl App {
warn!("failed to parse location"); warn!("failed to parse location");
break; break;
}; };
if let Some(focused_webview) = webviews.focused_webview() { if let Some(focused_webview) = self.webviews.focused_webview() {
focused_webview.servo_webview.load(url.into_url()); focused_webview.servo_webview.load(url.into_url());
} }
}, },
MinibrowserEvent::Back => { MinibrowserEvent::Back => {
if let Some(focused_webview) = webviews.focused_webview() { if let Some(focused_webview) = self.webviews.focused_webview() {
focused_webview.servo_webview.go_back(1); focused_webview.servo_webview.go_back(1);
} }
}, },
MinibrowserEvent::Forward => { MinibrowserEvent::Forward => {
if let Some(focused_webview) = webviews.focused_webview() { if let Some(focused_webview) = self.webviews.focused_webview() {
focused_webview.servo_webview.go_forward(1); focused_webview.servo_webview.go_forward(1);
} }
}, },
MinibrowserEvent::Reload => { MinibrowserEvent::Reload => {
minibrowser.update_location_dirty(false); minibrowser.update_location_dirty(false);
if let Some(focused_webview) = webviews.focused_webview() { if let Some(focused_webview) = self.webviews.focused_webview() {
focused_webview.servo_webview.reload(); focused_webview.servo_webview.reload();
} }
}, },
MinibrowserEvent::NewWebView => { MinibrowserEvent::NewWebView => {
minibrowser.update_location_dirty(false); minibrowser.update_location_dirty(false);
let webview = servo.new_webview(Url::parse("servo:newtab").unwrap()); let webview = servo.new_webview(Url::parse("servo:newtab").unwrap());
webviews.add(webview); self.webviews.add(webview);
}, },
MinibrowserEvent::CloseWebView(id) => { MinibrowserEvent::CloseWebView(id) => {
minibrowser.update_location_dirty(false); minibrowser.update_location_dirty(false);
webviews.close_webview(servo, id); self.webviews.close_webview(servo, id);
}, },
} }
} }
@ -484,9 +478,9 @@ impl ApplicationHandler<WakerEvent> for App {
self.t = now; self.t = now;
// If self.servo is None here, it means that we're in the process of shutting down, // If self.servo is None here, it means that we're in the process of shutting down,
// let's ignore events. // let's ignore events.
if self.servo.is_none() { let Some(ref mut servo) = self.servo else {
return; return;
} };
let Some(window) = self.windows.get(&window_id) else { let Some(window) = self.windows.get(&window_id) else {
return; return;
@ -502,14 +496,14 @@ impl ApplicationHandler<WakerEvent> for App {
if let Some(ref mut minibrowser) = self.minibrowser { if let Some(ref mut minibrowser) = self.minibrowser {
minibrowser.update( minibrowser.update(
window.winit_window().unwrap(), window.winit_window().unwrap(),
self.webviews.as_mut().unwrap(), &mut self.webviews,
self.servo.as_mut(), Some(servo),
"RedrawRequested", "RedrawRequested",
); );
minibrowser.paint(window.winit_window().unwrap()); minibrowser.paint(window.winit_window().unwrap());
} }
self.servo.as_mut().unwrap().present(); servo.present();
} }
// Handle the event // Handle the event
@ -544,8 +538,8 @@ impl ApplicationHandler<WakerEvent> for App {
if let WindowEvent::Resized(_) = event { if let WindowEvent::Resized(_) = event {
minibrowser.update( minibrowser.update(
window.winit_window().unwrap(), window.winit_window().unwrap(),
self.webviews.as_mut().unwrap(), &mut self.webviews,
self.servo.as_mut(), Some(servo),
"Sync WebView size with Window Resize event", "Sync WebView size with Window Resize event",
); );
} }
@ -562,9 +556,7 @@ impl ApplicationHandler<WakerEvent> for App {
} }
} }
if !consumed { if !consumed {
if let (Some(servo), Some(webviews)) = (self.servo.as_ref(), self.webviews.as_mut()) { window.handle_winit_event(servo, &mut self.clipboard, &mut self.webviews, event);
window.handle_winit_event(servo, &mut self.clipboard, webviews, event);
}
} }
let animating = self.is_animating(); let animating = self.is_animating();

View file

@ -58,6 +58,10 @@ impl Window {
Rc::new(window) Rc::new(window)
} }
pub fn new_uninit() -> Rc<dyn WindowPortsMethods> {
Self::new(Default::default(), None, None)
}
} }
impl WindowPortsMethods for Window { impl WindowPortsMethods for Window {

View file

@ -257,7 +257,7 @@ impl Minibrowser {
&mut self, &mut self,
window: &Window, window: &Window,
webviews: &mut WebViewManager, webviews: &mut WebViewManager,
servo: Option<&mut Servo>, servo: Option<&Servo>,
reason: &'static str, reason: &'static str,
) { ) {
let now = Instant::now(); let now = Instant::now();

View file

@ -94,13 +94,13 @@ pub struct HapticEffect {
} }
impl WebViewManager { impl WebViewManager {
pub fn new(window: Rc<dyn WindowPortsMethods>) -> WebViewManager { pub fn new() -> WebViewManager {
WebViewManager { WebViewManager {
status_text: None, status_text: None,
webviews: HashMap::default(), webviews: HashMap::default(),
creation_order: vec![], creation_order: vec![],
focused_webview_id: None, focused_webview_id: None,
window, window: crate::desktop::headless_window::Window::new_uninit(),
gamepad: match Gilrs::new() { gamepad: match Gilrs::new() {
Ok(g) => Some(g), Ok(g) => Some(g),
Err(e) => { Err(e) => {
@ -113,6 +113,10 @@ impl WebViewManager {
} }
} }
pub fn set_window(&mut self, window: Rc<dyn WindowPortsMethods>) {
self.window = window;
}
pub fn get_mut(&mut self, webview_id: WebViewId) -> Option<&mut WebView> { pub fn get_mut(&mut self, webview_id: WebViewId) -> Option<&mut WebView> {
self.webviews.get_mut(&webview_id) self.webviews.get_mut(&webview_id)
} }