Repository: servo/servo
File: components/layout/layout_impl.rs (Rust)

Code before:
    fn create(&self, config: LayoutConfig) -> Box<dyn Layout> {
        Box::new(LayoutThread::new(config))
    }

    fn parse_user_stylesheets(
        &self,
        user_stylesheets: &[UserStyleSheet],
    ) -> Vec<DocumentStyleSheet> {
        let shared_lock = &GLOBAL_STYLE_DATA.shared_lock;
        user_stylesheets
            .iter()
            .map(|user_stylesheet| {
                DocumentStyleSheet(ServoArc::new(Stylesheet::from_str(
                    user_stylesheet.source(),
                    user_stylesheet.url().into(),
                    Origin::User,
                    ServoArc::new(shared_lock.wrap(MediaList::empty())),
                    shared_lock.clone(),
                    None,
                    Some(&RustLogReporter),
                    QuirksMode::NoQuirks,
                    AllowImportRules::Yes,
                )))
            })
            .collect()
    }
}

impl Drop for LayoutThread {
    fn drop(&mut self) {
        let (keys, instance_keys) = self
            .font_context
            .collect_unused_webrender_resources(true /* all */);
        self.paint_api
            .remove_unused_font_resources(self.webview_id.into(), keys, instance_keys)
    }
}

impl Layout for LayoutThread {
    fn device(&self) -> &Device {
        self.stylist.device()
    }

    fn set_theme(&mut self, theme: Theme) -> bool {
        let theme: PrefersColorScheme = theme.into();
        let device = self.stylist.device_mut();
        if theme == device.color_scheme() {
            return false;
        }

        device.set_color_scheme(theme);

Review comment (refactor):
I think this could probably move to `ScriptThread`. It's a bit odd to have this on the `LayoutFactory` as it's not involved in the creation of a `LayoutThread` and it might complicate any future refactoring. `ScriptThread` already depends strongly on Stylo, so this shouldn't be an issue.

EDIT: It seems you just forgot to remove this actually. ☺

Code after:
    }
}

impl Drop for LayoutThread {
    fn drop(&mut self) {
        let (keys, instance_keys) = self
            .font_context
            .collect_unused_webrender_resources(true /* all */);
        self.paint_api
            .remove_unused_font_resources(self.webview_id.into(), keys, instance_keys)
    }
}

impl Layout for LayoutThread {
    fn device(&self) -> &Device {
        self.stylist.device()
    }

    fn set_theme(&mut self, theme: Theme) -> bool {
        let theme: PrefersColorScheme = theme.into();
        let device = self.stylist.device_mut();
        if theme == device.color_scheme() {
            return false;
        }

        device.set_color_scheme(theme);
        self.device_has_changed = true;
        true
    }

    fn set_viewport_details(&mut self, viewport_details: ViewportDetails) -> bool {
        let device = self.stylist.device_mut();
        let device_pixel_ratio = Scale::new(viewport_details.hidpi_scale_factor.get());
        if device.viewport_size() == viewport_details.size &&
            device.device_pixel_ratio() == device_pixel_ratio
        {
            return false;
        }

        device.set_viewport_size(viewport_details.size);
        device.set_device_pixel_ratio(device_pixel_ratio);
        self.device_has_changed = true;
        true
    }

    fn load_web_fonts_from_stylesheet(
        &self,
        stylesheet: &ServoArc<Stylesheet>,
        document_context: &WebFontDocumentContext,
    ) {
        let guard = stylesheet.shared_lock.read();