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

* respond to winit platform theme changed event and send it to the layout thread

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* refactoring viewport and theme change handling functions based on feedback

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* fixing issues reported by test-tidy

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

* update stylo in order to use color_scheme function on Device

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>

---------

Signed-off-by: Lloyd Massiah <artmis9@protonmail.com>
Co-authored-by: lazypassion <25536767+lazypassion@users.noreply.github.com>
This commit is contained in:
arthmis 2024-12-12 01:17:02 -05:00 committed by GitHub
parent dfcbb18a8b
commit 26f61103d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 144 additions and 43 deletions

View file

@ -57,7 +57,7 @@ use script_layout_interface::{
use script_traits::webdriver_msg::{WebDriverJSError, WebDriverJSResult};
use script_traits::{
ConstellationControlMsg, DocumentState, HistoryEntryReplacement, LoadData, ScriptMsg,
ScriptToConstellationChan, ScrollState, StructuredSerializedData, TimerSchedulerMsg,
ScriptToConstellationChan, ScrollState, StructuredSerializedData, Theme, TimerSchedulerMsg,
WindowSizeData, WindowSizeType,
};
use selectors::attr::CaseSensitivity;
@ -180,6 +180,7 @@ pub enum ReflowReason {
UpdateTheRendering,
Viewport,
WorkletLoaded,
ThemeChange,
}
#[dom_struct]
@ -223,6 +224,10 @@ pub struct Window {
#[no_trace]
unhandled_resize_event: DomRefCell<Option<(WindowSizeData, WindowSizeType)>>,
/// Platform theme.
#[no_trace]
theme: Cell<Theme>,
/// Parent id associated with this page, if any.
#[no_trace]
parent_info: Option<PipelineId>,
@ -1887,6 +1892,11 @@ impl Window {
.or_else(|| document.GetDocumentElement())
.map(|root| root.upcast::<Node>().to_trusted_node_address());
let theme = match self.theme.get() {
Theme::Light => style::queries::values::PrefersColorScheme::Light,
Theme::Dark => style::queries::values::PrefersColorScheme::Dark,
};
// Send new document and relevant styles to layout.
let reflow = ScriptReflow {
reflow_info: Reflow {
@ -1903,6 +1913,7 @@ impl Window {
pending_restyles,
animation_timeline_value: document.current_animation_timeline_value(),
animations: document.animations().sets.clone(),
theme,
};
self.layout.borrow_mut().reflow(reflow);
@ -2353,6 +2364,10 @@ impl Window {
self.window_size.get()
}
pub fn set_theme(&self, theme: Theme) {
self.theme.set(theme);
}
pub fn get_url(&self) -> ServoUrl {
self.Document().url()
}
@ -2728,6 +2743,7 @@ impl Window {
throttled: Cell::new(false),
layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
current_event: DomRefCell::new(None),
theme: Cell::new(Theme::Light),
});
unsafe { WindowBinding::Wrap(JSContext::from_ptr(runtime.cx()), win) }

View file

@ -85,8 +85,8 @@ use script_traits::{
EventResult, HistoryEntryReplacement, InitialScriptState, JsEvalResult, LayoutMsg, LoadData,
LoadOrigin, MediaSessionActionType, MouseButton, MouseEventType, NewLayoutInfo, Painter,
ProgressiveWebMetricType, ScriptMsg, ScriptToConstellationChan, ScrollState,
StructuredSerializedData, TimerSchedulerMsg, TouchEventType, TouchId, UntrustedNodeAddress,
UpdatePipelineIdReason, WheelDelta, WindowSizeData, WindowSizeType,
StructuredSerializedData, Theme, TimerSchedulerMsg, TouchEventType, TouchId,
UntrustedNodeAddress, UpdatePipelineIdReason, WheelDelta, WindowSizeData, WindowSizeType,
};
use servo_atoms::Atom;
use servo_config::opts;
@ -2021,6 +2021,7 @@ impl ScriptThread {
.parent_info
.or(Some(new_layout_info.new_pipeline_id)),
Resize(id, ..) => Some(id),
ThemeChange(id, ..) => Some(id),
ResizeInactive(id, ..) => Some(id),
UnloadDocument(id) => Some(id),
ExitPipeline(id, ..) => Some(id),
@ -2265,6 +2266,9 @@ impl ScriptThread {
ConstellationControlMsg::ResizeInactive(id, new_size) => {
self.handle_resize_inactive_msg(id, new_size)
},
ConstellationControlMsg::ThemeChange(_, theme) => {
self.handle_theme_change(theme);
},
ConstellationControlMsg::GetTitle(pipeline_id) => {
self.handle_get_title_msg(pipeline_id)
},
@ -2856,6 +2860,15 @@ impl ScriptThread {
})
}
fn handle_theme_change(&self, theme: Theme) {
let docs = self.documents.borrow();
for (_, document) in docs.iter() {
let window = document.window();
window.set_theme(theme);
window.force_reflow(ReflowGoal::Full, ReflowReason::ThemeChange, None);
}
}
// exit_fullscreen creates a new JS promise object, so we need to have entered a realm
fn handle_exit_fullscreen(&self, id: PipelineId, can_gc: CanGc) {
let document = self.documents.borrow().find_document(id);