Repository navigation
Add embedder event for preferred color scheme and respond to it in the LayoutThread - #34532
Conversation
|
@nicoburns I've made the requested changes, removed the |
|
@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 |
07da8b9 to
a564d71
Compare
nicoburns
left a comment
There was a problem hiding this comment.
See comment about merging function in layout_thread. Otherwise this looks good to me :)
| 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); |
There was a problem hiding this comment.
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_changedoes) - 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
boolindicating only if viewport has changed.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
if there could be a pub fn color_scheme(&self) -> PrefersColorScheme added to device I can make this change
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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());
}
}
}There was a problem hiding this comment.
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.
There was a problem hiding this comment.
ok, this sounds good and thanks for the info on device.set_root_font_size
| 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); |
There was a problem hiding this comment.
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());
}
}
}| 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); |
There was a problem hiding this comment.
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.
|
What would be a good test to use as a reference to write a test for this feature? Also the change to add |
5d18932 to
17b303e
Compare
|
@arthmis You need to run 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 |
…ut thread Signed-off-by: Lloyd Massiah <[email protected]>
…dback Signed-off-by: Lloyd Massiah <[email protected]>
Signed-off-by: Lloyd Massiah <[email protected]>
Signed-off-by: Lloyd Massiah <[email protected]>
Could be due to the author info from git being different from the signed-off by info: |
17b303e to
4815b2d
Compare
|
oh sorry @nicoburns I didn't realize you added automerge. I was wondering, did you want me to squash the commits before merging? |
|
@arthmis We squash merge, so it doesn't make any difference! |
This addresses the first 2 checkboxes for issue #34425. I've added the 2 changes noted in that issue here
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsprefers-color-scheme(dark mode) #34425