Repository: servo/servo
File: ports/servoshell/egl/app_state.rs (Rust)

Code before:
        *self.callbacks.coordinates.borrow_mut() = coordinates;
        self.perform_updates();
    }

    /// Scroll.
    /// x/y are scroll coordinates.
    /// dx/dy are scroll deltas.
    pub fn scroll(&self, dx: f32, dy: f32, x: f32, y: f32) {
        let scroll = Scroll::Delta(DeviceVector2D::new(dx, dy).into());
        let point = DevicePoint::new(x, y).into();
        self.active_webview().notify_scroll_event(scroll, point);
        self.perform_updates();
    }

    /// WebDriver message handling methods
    pub(crate) fn handle_webdriver_messages(self: &Rc<Self>) {
        if let Some(webdriver_receiver) = &self.webdriver_receiver {
            while let Ok(msg) = webdriver_receiver.try_recv() {
                match msg {
                    WebDriverCommandMsg::LoadUrl(webview_id, url, load_status_sender) => {
                        info!("Loading URL in webview {}: {}", webview_id, url);

                        if let Some(webview) = self.webview_by_id(webview_id) {
                            self.set_load_status_sender(webview_id, load_status_sender.clone());
                            self.inner_mut().focused_webview_id = Some(webview_id);
                            webview.focus();
                            let url_string = url.to_string();
                            webview.load(url.into_url());
                            info!(
                                "Successfully loaded URL {} in focused webview {}",
                                url_string, webview_id
                            );
                        } else {
                            warn!("WebView {} not found for LoadUrl command", webview_id);
                        }
                    },
                    WebDriverCommandMsg::NewWebView(response_sender, load_status_sender) => {
                        info!("Creating new webview via WebDriver");
                        let new_webview = self
                            .create_and_focus_toplevel_webview(Url::parse("about:blank").unwrap());

                        if let Err(error) = response_sender.send(new_webview.id()) {
                            warn!("Failed to send response of NewWebview: {error}");
                        }
                        if let Some(load_status_sender) = load_status_sender {
                            self.set_load_status_sender(new_webview.id(), load_status_sender);
                        }
                    },
                    WebDriverCommandMsg::CloseWebView(webview_id, response_sender) => {
                        info!("(Not Implemented) Closing webview {}", webview_id);
                    },

Review comment (bug):
These were quite messy and wrong. We should've never set `focused_webview_id` by ourself after calling focus method. We relies on IPC feedback for it.

Code after:
        *self.callbacks.coordinates.borrow_mut() = coordinates;
        self.perform_updates();
    }

    /// Scroll.
    /// x/y are scroll coordinates.
    /// dx/dy are scroll deltas.
    pub fn scroll(&self, dx: f32, dy: f32, x: f32, y: f32) {
        let scroll = Scroll::Delta(DeviceVector2D::new(dx, dy).into());
        let point = DevicePoint::new(x, y).into();
        self.active_webview().notify_scroll_event(scroll, point);
        self.perform_updates();
    }

    /// WebDriver message handling methods
    pub(crate) fn handle_webdriver_messages(self: &Rc<Self>) {
        if let Some(webdriver_receiver) = &self.webdriver_receiver {
            while let Ok(msg) = webdriver_receiver.try_recv() {
                match msg {
                    WebDriverCommandMsg::LoadUrl(webview_id, url, load_status_sender) => {
                        self.handle_webdriver_load_url(webview_id, url, load_status_sender);
                    },
                    WebDriverCommandMsg::NewWebView(response_sender, load_status_sender) => {
                        info!("Creating new webview via WebDriver");
                        let new_webview = self
                            .create_and_focus_toplevel_webview(Url::parse("about:blank").unwrap());

                        if let Err(error) = response_sender.send(new_webview.id()) {
                            warn!("Failed to send response of NewWebview: {error}");
                        }
                        if let Some(load_status_sender) = load_status_sender {
                            self.set_load_status_sender(new_webview.id(), load_status_sender);
                        }
                    },
                    WebDriverCommandMsg::CloseWebView(webview_id, response_sender) => {
                        info!("(Not Implemented) Closing webview {}", webview_id);
                    },
                    WebDriverCommandMsg::FocusWebView(webview_id) => {
                        if let Some(webview) = self.webview_by_id(webview_id) {
                            let focus_id = webview.focus();
                            info!("Successfully focused webview {}", webview_id);
                        }
                    },
                    WebDriverCommandMsg::IsWebViewOpen(webview_id, response_sender) => {
                        let context = self.webview_by_id(webview_id);

                        if let Err(error) = response_sender.send(context.is_some()) {
                            warn!("Failed to send response of IsWebViewOpen: {error}");
                        }
                    },
                    WebDriverCommandMsg::IsBrowsingContextOpen(..) => {