Repository: versotile-org/verso
File: src/window.rs (Rust)

Code before:
                ));
            }
            WindowEvent::CloseRequested => {
                events.push(EmbedderEvent::Quit);
            }
            WindowEvent::ModifiersChanged(modifier) => self.modifiers_state.set(modifier.state()),
            WindowEvent::KeyboardInput { event, .. } => {
                let event = keyboard_event_from_winit(&event, self.modifiers_state.get());
                log::trace!("Verso is handling {:?}", event);
                events.push(EmbedderEvent::Keyboard(event));
            }
            e => log::warn!("Verso Window isn't supporting this window event yet: {e:?}"),
        }
    }

    /// Handle servo messages and return a boolean to indicate servo needs to present or not.
    pub fn handle_servo_messages(
        &mut self,
        servo: &mut Servo<GLWindow>,
        events: &mut Vec<EmbedderEvent>,
        status: &mut Status,
    ) -> bool {
        let mut need_present = false;
        servo.get_events().into_iter().for_each(|(w, m)| {

            // Window-wise events
            match &m {
                EmbedderMsg::GetClipboardContents(sender) => {
                    if let Some(clipboard) = self.clipboard.as_mut() {
                        let contents = clipboard.get_text().unwrap_or_else(|e| {
                            log::warn!("Failed to get clipboard content: {}", e);
                            String::new()
                        });
                        if let Err(e) = sender.send(contents) {
                            log::warn!("Failed to send clipboard content: {}", e);
                        }
                    } else {
                        log::trace!("Clipboard is not supported on this platform.");
                    }
                },
                EmbedderMsg::SetClipboardContents(text) => {
                    if let Some(clipboard) = self.clipboard.as_mut() {
                        if let Err(e) = clipboard.set_text(text) {
                            log::warn!("Failed to set clipboard contents: {}", e);
                        }
                    } else {
                        log::trace!("Clipboard is not supported on this platform.");
                    }
                },
                _ => ()
            }

Review comment (refactor):
Want some suggestions about this section. I extracted the section because the clipboard event should be handled at a higher level (whether it's sent from the panel or the main view).

And maybe we should early return if the event is matched in this section?



Code after:
            }
            e => log::warn!("Verso Window isn't supporting this window event yet: {e:?}"),
        }
    }

    /// Handle servo messages and return a boolean to indicate servo needs to present or not.
    pub fn handle_servo_messages(
        &mut self,
        servo: &mut Servo<GLWindow>,
        events: &mut Vec<EmbedderEvent>,
        status: &mut Status,
    ) -> bool {
        let mut need_present = false;
        servo.get_events().into_iter().for_each(|(w, m)| {
            match w {
                // Handle message in Verso Panel
                Some(p) if p == self.panel.id() => {
                    log::trace!("Verso Panel {p:?} is handling servo message: {m:?}",);
                    match m {
                        EmbedderMsg::LoadStart | EmbedderMsg::HeadParsed => {
                            need_present = false;
                        }
                        EmbedderMsg::LoadComplete => {
                            need_present = true;
                            // let demo_url = ServoUrl::parse("https://demo.versotile.org").unwrap();
                            let demo_url = ServoUrl::parse("https://keyboard-test.space").unwrap();
                            let demo_id = TopLevelBrowsingContextId::new();
                            events.push(EmbedderEvent::NewWebView(demo_url, demo_id));
                        }
                        EmbedderMsg::AllowNavigationRequest(id, _url) => {
                            // The panel shouldn't navigate to other pages.
                            events.push(EmbedderEvent::AllowNavigationResponse(id, false));
                        }
                        EmbedderMsg::WebViewOpened(w) => {
                            let size = self.window.inner_size();
                            let size = Size2D::new(size.width as i32, size.height as i32);
                            let rect = DeviceIntRect::from_size(size).to_f32();
                            events.push(EmbedderEvent::FocusWebView(w));
                            events.push(EmbedderEvent::MoveResizeWebView(w, rect));
                        }
                        EmbedderMsg::WebViewClosed(_w) => {
                            events.push(EmbedderEvent::Quit);
                        }
                        EmbedderMsg::WebViewFocused(w) => {
                            events.push(EmbedderEvent::ShowWebView(w, false));
                        }
                        EmbedderMsg::HistoryChanged(..)
                        | EmbedderMsg::ChangePageTitle(..) => {
                            log::trace!("Verso Panel ignores this message: {m:?}")
                        }
                        EmbedderMsg::Prompt(definition, _origin) => match definition {