Skip to content

Add embedder event for preferred color scheme and respond to it in the LayoutThread - #34532

Merged
nicoburns merged 4 commits into
servo:mainfrom
arthmis:lmassiah/issue-34425
Dec 12, 2024
Merged

nicoburns merged 4 commits into
servo:mainfrom
arthmis:lmassiah/issue-34425

Conversation

@arthmis

@arthmis arthmis commented Dec 8, 2024 •

Copy link
Copy Markdown
Contributor

This addresses the first 2 checkboxes for issue #34425. I've added the 2 changes noted in that issue here

  • Add an embedder event for setting the preferred color scheme
  • Plumb this through to the layout thread where it should be set on the device, via a "reflow", similar to window size

  • There are tests for these changes OR
  • These changes do not require tests because ___

Comment thread components/shared/script/lib.rs Resolved
Comment thread components/compositing/windowing.rs Resolved
@arthmis

arthmis commented Dec 10, 2024 •

Copy link
Copy Markdown
Contributor Author

@nicoburns I've made the requested changes, removed the dbgs and updated the comments. I've also confirmed, through manual testing, that the feature works now. I still have to add tests though

@nicoburns

Copy link
Copy Markdown
Contributor

@arthmis I will review. But one thing which you definitely need to do for this to be merged is to pass the "DCO" CI check by adding a "Signed-Off-By:" line to the bottom of each commit. The easiest way to do this is to run git rebase --signoff main and then force push your branch.

@arthmis
arthmis force-pushed the lmassiah/issue-34425 branch from 07da8b9 to a564d71 Compare December 10, 2024 22:14
@arthmis
arthmis marked this pull request as ready for review December 10, 2024 22:15

@nicoburns nicoburns left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about merging function in layout_thread. Otherwise this looks good to me :)

Comment thread components/layout_thread_2020/lib.rs Outdated
Comment on lines +735 to +738
self.handle_theme_change(data.theme, data.window_size, &guards);
let had_used_viewport_units = self.stylist.device().used_viewport_units();
let viewport_size_changed = self.handle_viewport_change(data.window_size, &guards);
let viewport_size_changed =
self.handle_viewport_change(data.window_size, data.theme, &guards);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing as we get both inputs from script, I think the handle_theme_change and handle_viewport_change methods in layout_thread_2020 probably ought to be merged. The logic should be:

  • Check whether viewport has changed (as handle_viewport_change does)
  • Check whether theme has changed (simple == check with new theme vs. theme from Device)
  • If either have changed then update the Device
  • Return value should be bool indicating only if viewport has changed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that would be ideal but there isn't a way to get the theme from the current device that's why I decided to make a separate handle_theme_change function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there could be a pub fn color_scheme(&self) -> PrefersColorScheme added to device I can make this change

@nicoburns nicoburns Dec 10, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there could be a pub fn color_scheme(&self) -> PrefersColorScheme added to device I can make this change

There can! You can add this yourself in a PR to the https://github.com/servo/stylo repo. The relevant file is style/servo/media_queries.rs

@arthmis arthmis Dec 11, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually looking at this again, is there any benefit in unifying these functions since they handle different events anyways? After adding the color_scheme fn there wouldn't be a need to pass the theme into the handle_viewport_function. I feel like treating them as two different functions would be better, or at least easier to understand and update or refactor if they need changes in the future.

I guess the one downside to not unifying is that there is a potential for Device to be created twice during a reflow. Would that be a big issue? Viewport size changing could happen frequently but the color scheme wouldn't change that often, and probably rarely

@arthmis arthmis Dec 11, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example they can look like this if I keep them separate and add the pub fn color_scheme(&self) -> PrefersColorScheme to Device

    /// Update document styles when platform theme changes.
    fn handle_theme_change(
        &mut self,
        theme: PrefersColorScheme,
        window_size_data: WindowSizeData, // need this because device doesn't have a fn to return window size data
        guards: &StylesheetGuards,
    ) {
        if theme == self.device().color_scheme() { // avoid creating device if theme hasn't changed
            return;
        }

        let device = Device::new(
            MediaType::screen(),
            self.stylist.quirks_mode(),
            window_size_data.initial_viewport,
            Scale::new(self.stylist.device().device_pixel_ratio().get()),
            Box::new(LayoutFontMetricsProvider(self.font_context.clone())),
            self.stylist.device().default_computed_values().to_arc(),
            theme,
        );

        let sheet_origins_affected_by_device_change = self.stylist.set_device(device, guards);
        self.stylist
            .force_stylesheet_origins_dirty(sheet_origins_affected_by_device_change);
    }

    /// Update layout given a new viewport. Returns true if the viewport changed or false if it didn't.
    fn handle_viewport_change(
        &mut self,
        window_size_data: WindowSizeData,
        // removed theme parameter because now the color scheme can be accessed through the device
        guards: &StylesheetGuards,
    ) -> bool {
        ....// other code
        let device = Device::new(
            MediaType::screen(),
            self.stylist.quirks_mode(),
            window_size_data.initial_viewport,
            Scale::new(window_size_data.device_pixel_ratio.get()),
            Box::new(LayoutFontMetricsProvider(self.font_context.clone())),
            self.stylist.device().default_computed_values().to_arc(),
            self.device().color_scheme(), // access current color scheme
        );
        ...// rest of function
    }

The alternative looks like this

    /// Update layout given a new viewport. Returns true if the viewport changed or false if it didn't.
    fn handle_viewport_and_theme_change(
        &mut self,
        window_size_data: WindowSizeData,
        theme: PrefersColorScheme,
        guards: &StylesheetGuards,
    ) -> bool {
        // If the viewport size and device pixel ratio has not changed, do not make any changes.
        let au_viewport_size = Size2D::new(
            Au::from_f32_px(window_size_data.initial_viewport.width),
            Au::from_f32_px(window_size_data.initial_viewport.height),
        );

        let viewport_did_change = self.stylist.device().au_viewport_size() == au_viewport_size &&
            self.stylist.device().device_pixel_ratio().get() ==
                window_size_data.device_pixel_ratio.get();

        let color_scheme_did_change = theme == self.device().color_scheme();

        if viewport_did_change || color_scheme_did_change {
            let device = Device::new(
                MediaType::screen(),
                self.stylist.quirks_mode(),
                window_size_data.initial_viewport,
                Scale::new(window_size_data.device_pixel_ratio.get()),
                Box::new(LayoutFontMetricsProvider(self.font_context.clone())),
                self.stylist.device().default_computed_values().to_arc(),
                theme,
            );

            // Preserve any previously computed root font size.
            device.set_root_font_size(self.stylist.device().root_font_size().px()); // this line is only relevant to resize but will get called even if the theme changes unless we hide it behind an if statement

            let sheet_origins_affected_by_device_change = self.stylist.set_device(device, guards);
            self.stylist
                .force_stylesheet_origins_dirty(sheet_origins_affected_by_device_change);
            
            self.viewport_size = au_viewport_size; // this line is only relevant to resize but will get called even if the theme changes unless we hide it behind an if statement
        }
        viewport_did_change
    }

I feel like unifying would cloud some of handle_viewport_change logic, noted by the comments left on 2 lines left at the end of the function handle_viewport_and_theme_change

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about splitting into 3 functions like so:

    fn viewport_did_change(&mut self, window_size_data: WindowSizeData) {
        let new_pixel_ratio = window_size_data.device_pixel_ratio.get();
        let new_viewport_size = Size2D::new(
            Au::from_f32_px(window_size_data.initial_viewport.width),
            Au::from_f32_px(window_size_data.initial_viewport.height),
        );

        // TODO: eliminate self.viewport_size in favour of using self.device.au_viewport_size()
        self.viewport_size = au_viewport_size;

        let device = self.stylist.device();
        let size_did_change = device.au_viewport_size() != new_viewport_size;
        let pixel_ratio_did_change = device.device_pixel_ratio().get() != new_pixel_ratio;

        size_did_change || pixel_ratio_did_change
    }

    fn theme_did_change(&self, theme: PrefersColorScheme,) {
        theme != self.device().color_scheme()
    }
    
    /// Update layout given a new viewport. Returns true if the viewport changed or false if it didn't.
    fn update_device(
        &mut self,
        window_size_data: WindowSizeData,
        theme: PrefersColorScheme,
        guards: &StylesheetGuards,
    ) -> bool {
        let device = Device::new(
            MediaType::screen(),
            self.stylist.quirks_mode(),
            window_size_data.initial_viewport,
            Scale::new(window_size_data.device_pixel_ratio.get()),
            Box::new(LayoutFontMetricsProvider(self.font_context.clone())),
            self.stylist.device().default_computed_values().to_arc(),
            theme,
        );

        // Preserve any previously computed root font size.
        device.set_root_font_size(self.stylist.device().root_font_size().px());

        let sheet_origins_affected_by_device_change = self.stylist.set_device(device, guards);
        self.stylist
            .force_stylesheet_origins_dirty(sheet_origins_affected_by_device_change);
    }

Then the top-level code in handle_reflow would be something like:

    let had_used_viewport_units = self.stylist.device().used_viewport_units();
    let viewport_size_changed = self.viewport_did_change(data.window_size);
    let theme_changed = self.theme_did_change(data.theme);

    if viewport_size_changed || theme_changed {
        self.update_device(data.window_size_data, data.theme, &guards)
    }
    if viewport_size_changed {
        if had_used_viewport_units {
            if let Some(mut data) = root_element.mutate_data() {
                data.hint.insert(RestyleHint::recascade_subtree());
            }
        }
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to avoid updating the device twice. Partly because it's a duplicated block of code. Partly because I'm not sure how cheap/expensive the invalidation Stylo does when you set the Device is.

I would also note that device.set_root_font_size(self.stylist.device().root_font_size().px()); does apply to the theme change case. The idea here is just to preserve the existing state of the Device regardless of any changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this sounds good and thanks for the info on device.set_root_font_size

Comment thread components/script/script_thread.rs Resolved
Comment thread components/layout_thread_2020/lib.rs Outdated
Comment on lines +735 to +738
self.handle_theme_change(data.theme, data.window_size, &guards);
let had_used_viewport_units = self.stylist.device().used_viewport_units();
let viewport_size_changed = self.handle_viewport_change(data.window_size, &guards);
let viewport_size_changed =
self.handle_viewport_change(data.window_size, data.theme, &guards);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about splitting into 3 functions like so:

    fn viewport_did_change(&mut self, window_size_data: WindowSizeData) {
        let new_pixel_ratio = window_size_data.device_pixel_ratio.get();
        let new_viewport_size = Size2D::new(
            Au::from_f32_px(window_size_data.initial_viewport.width),
            Au::from_f32_px(window_size_data.initial_viewport.height),
        );

        // TODO: eliminate self.viewport_size in favour of using self.device.au_viewport_size()
        self.viewport_size = au_viewport_size;

        let device = self.stylist.device();
        let size_did_change = device.au_viewport_size() != new_viewport_size;
        let pixel_ratio_did_change = device.device_pixel_ratio().get() != new_pixel_ratio;

        size_did_change || pixel_ratio_did_change
    }

    fn theme_did_change(&self, theme: PrefersColorScheme,) {
        theme != self.device().color_scheme()
    }
    
    /// Update layout given a new viewport. Returns true if the viewport changed or false if it didn't.
    fn update_device(
        &mut self,
        window_size_data: WindowSizeData,
        theme: PrefersColorScheme,
        guards: &StylesheetGuards,
    ) -> bool {
        let device = Device::new(
            MediaType::screen(),
            self.stylist.quirks_mode(),
            window_size_data.initial_viewport,
            Scale::new(window_size_data.device_pixel_ratio.get()),
            Box::new(LayoutFontMetricsProvider(self.font_context.clone())),
            self.stylist.device().default_computed_values().to_arc(),
            theme,
        );

        // Preserve any previously computed root font size.
        device.set_root_font_size(self.stylist.device().root_font_size().px());

        let sheet_origins_affected_by_device_change = self.stylist.set_device(device, guards);
        self.stylist
            .force_stylesheet_origins_dirty(sheet_origins_affected_by_device_change);
    }

Then the top-level code in handle_reflow would be something like:

    let had_used_viewport_units = self.stylist.device().used_viewport_units();
    let viewport_size_changed = self.viewport_did_change(data.window_size);
    let theme_changed = self.theme_did_change(data.theme);

    if viewport_size_changed || theme_changed {
        self.update_device(data.window_size_data, data.theme, &guards)
    }
    if viewport_size_changed {
        if had_used_viewport_units {
            if let Some(mut data) = root_element.mutate_data() {
                data.hint.insert(RestyleHint::recascade_subtree());
            }
        }
    }

Comment thread components/layout_thread_2020/lib.rs Outdated
Comment on lines +735 to +738
self.handle_theme_change(data.theme, data.window_size, &guards);
let had_used_viewport_units = self.stylist.device().used_viewport_units();
let viewport_size_changed = self.handle_viewport_change(data.window_size, &guards);
let viewport_size_changed =
self.handle_viewport_change(data.window_size, data.theme, &guards);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to avoid updating the device twice. Partly because it's a duplicated block of code. Partly because I'm not sure how cheap/expensive the invalidation Stylo does when you set the Device is.

I would also note that device.set_root_font_size(self.stylist.device().root_font_size().px()); does apply to the theme change case. The idea here is just to preserve the existing state of the Device regardless of any changes.

Comment thread components/layout_thread_2020/lib.rs Resolved
@arthmis

arthmis commented Dec 12, 2024 •

Copy link
Copy Markdown
Contributor Author

What would be a good test to use as a reference to write a test for this feature?

Also the change to add color_scheme to Device was merged in stylo how would I go about making that available to my code in this PR? I'm getting errors that it isn't available. Should I run cargo update only the stylo package?

@arthmis
arthmis force-pushed the lmassiah/issue-34425 branch from 5d18932 to 17b303e Compare December 12, 2024 01:26
@nicoburns

Copy link
Copy Markdown
Contributor

@arthmis You need to run cargo update style and commit the Cargo.lock changes to get the new theme getter method on Device.

Also the DCO check is failing again (not sure why as you do seem to have signoff lines in each commit message). Maybe you could rebase this PR against latest main to see if that jolts the CI into passing the check?

@mukilan

mukilan commented Dec 12, 2024 •

Copy link
Copy Markdown
Member

Also the DCO check is failing again (not sure why as you do seem to have signoff lines in each commit message).

Could be due to the author info from git being different from the signed-off by info:

From https://github.com/servo/servo/pull/34532/checks?check_run_id=34291174153

Commit sha: [ddf306a](https://github.com/servo/servo/pull/34532/commits/ddf306a3a393eea8fdb557f1e9f698db53e4e8fc), Author: lazypassion, Committer: arthmis; Expected "lazypassion [[email protected]](mailto:[email protected])", but got "Lloyd Massiah [[email protected]](mailto:[email protected])".

@arthmis
arthmis force-pushed the lmassiah/issue-34425 branch from 17b303e to 4815b2d Compare December 12, 2024 05:31
@nicoburns
nicoburns enabled auto-merge December 12, 2024 06:02
@nicoburns
nicoburns added this pull request to the merge queue Dec 12, 2024
@arthmis
arthmis disabled auto-merge December 12, 2024 06:47
@arthmis

arthmis commented Dec 12, 2024

Copy link
Copy Markdown
Contributor Author

oh sorry @nicoburns I didn't realize you added automerge. I was wondering, did you want me to squash the commits before merging?

@nicoburns

Copy link
Copy Markdown
Contributor

@arthmis We squash merge, so it doesn't make any difference!

Merged via the queue into servo:main with commit 26f6110 Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support prefers-color-scheme (dark mode)

4 participants