diff --git a/lib/stub_ui/hooks.dart b/lib/stub_ui/hooks.dart index 6e0820832939b30e58f392b384caaac9f9f9e304..4ea381bfb8d34adeaa41412f21e3179f0c8cb300 100644 --- a/lib/stub_ui/hooks.dart +++ b/lib/stub_ui/hooks.dart @@ -84,6 +84,10 @@ void _updateUserSettingsData(String jsonData) { _updateAlwaysUse24HourFormat(data['alwaysUse24HourFormat']); } +void _updateLifecycleState(String state) { + window._initialLifecycleState ??= state; +} + void _updateTextScaleFactor(double textScaleFactor) { window._textScaleFactor = textScaleFactor; _invoke(window.onTextScaleFactorChanged, window._onTextScaleFactorChangedZone); diff --git a/lib/stub_ui/window.dart b/lib/stub_ui/window.dart index bc08ca95c33133b5ea02b7f04b04cb59617e5aa6..f3dedc642887ebbb5f723d3b1f49f69c3eb8ac87 100644 --- a/lib/stub_ui/window.dart +++ b/lib/stub_ui/window.dart @@ -551,6 +551,15 @@ class Window { _onLocaleChangedZone = Zone.current; } + /// The lifecycle state immediately after dart isolate initialization. + /// + /// This property will not be updated as the lifecycle changes. + /// + /// It is used to initialize [SchedulerBinding.lifecycleState] at startup + /// with any buffered lifecycle state events. + String get initialLifecycleState => _initialLifecycleState; + String _initialLifecycleState; + /// The setting indicating the current brightness mode of the host platform. /// If the platform has no preference, [platformBrightness] defaults to [Brightness.light]. Brightness get platformBrightness => _platformBrightness; diff --git a/lib/ui/hooks.dart b/lib/ui/hooks.dart index e88580a95ce06e5ac64610d11b478f1bbfcb83c2..4d299c91a24ede9617aed9fac432a3cbb8d25fbd 100644 --- a/lib/ui/hooks.dart +++ b/lib/ui/hooks.dart @@ -89,6 +89,13 @@ void _updateUserSettingsData(String jsonData) { _updatePlatformBrightness(data['platformBrightness']); } +@pragma('vm:entry-point') +// ignore: unused_element +void _updateLifecycleState(String state) { + window._initialLifecycleState ??= state; +} + + void _updateTextScaleFactor(double textScaleFactor) { window._textScaleFactor = textScaleFactor; _invoke(window.onTextScaleFactorChanged, window._onTextScaleFactorChangedZone); diff --git a/lib/ui/window.dart b/lib/ui/window.dart index d89b668b7037d6ddfc5557fe0e4db5c40d73abed..6abceceb0261c96fffdf3575d98fcf1ecaa52d4b 100644 --- a/lib/ui/window.dart +++ b/lib/ui/window.dart @@ -556,6 +556,15 @@ class Window { _onLocaleChangedZone = Zone.current; } + /// The lifecycle state immediately after dart isolate initialization. + /// + /// This property will not be updated as the lifecycle changes. + /// + /// It is used to initialize [SchedulerBinding.lifecycleState] at startup + /// with any buffered lifecycle state events. + String get initialLifecycleState => _initialLifecycleState; + String _initialLifecycleState; + /// The system-reported text scale. /// /// This establishes the text scaling factor to use when rendering text, diff --git a/lib/ui/window/window.cc b/lib/ui/window/window.cc index 0a90d6fcbf2d9ef93e5c5abf500b8dd24f73557e..bfeb86cef66f4a6dda249df9870fa276db175f0b 100644 --- a/lib/ui/window/window.cc +++ b/lib/ui/window/window.cc @@ -217,6 +217,18 @@ void Window::UpdateUserSettingsData(const std::string& data) { })); } +void Window::UpdateLifecycleState(const std::string& data) { + std::shared_ptr dart_state = library_.dart_state().lock(); + if (!dart_state) + return; + tonic::DartState::Scope scope(dart_state); + tonic::LogIfError(tonic::DartInvokeField(library_.value(), + "_updateLifecycleState", + { + tonic::StdStringToDart(data), + })); +} + void Window::UpdateSemanticsEnabled(bool enabled) { std::shared_ptr dart_state = library_.dart_state().lock(); if (!dart_state) diff --git a/lib/ui/window/window.h b/lib/ui/window/window.h index 94a506fb5fa4dca3bfff293cbf6e10c2ea0554f1..f5624e4bad6afc5958d1ecf0cf106b7bc7561bb3 100644 --- a/lib/ui/window/window.h +++ b/lib/ui/window/window.h @@ -65,6 +65,7 @@ class Window final { void UpdateWindowMetrics(const ViewportMetrics& metrics); void UpdateLocales(const std::vector& locales); void UpdateUserSettingsData(const std::string& data); + void UpdateLifecycleState(const std::string& data); void UpdateSemanticsEnabled(bool enabled); void UpdateAccessibilityFeatures(int32_t flags); void DispatchPlatformMessage(fml::RefPtr message); diff --git a/runtime/runtime_controller.cc b/runtime/runtime_controller.cc index b57f2232acc4772e516b2a87f033026110cc3b21..83a848e87955c74cc236f7260631eab63f6440db 100644 --- a/runtime/runtime_controller.cc +++ b/runtime/runtime_controller.cc @@ -128,7 +128,8 @@ bool RuntimeController::FlushRuntimeStateToIsolate() { SetLocales(window_data_.locale_data) && SetSemanticsEnabled(window_data_.semantics_enabled) && SetAccessibilityFeatures(window_data_.accessibility_feature_flags_) && - SetUserSettingsData(window_data_.user_settings_data); + SetUserSettingsData(window_data_.user_settings_data) && + SetLifecycleState(window_data_.lifecycle_state); } bool RuntimeController::SetViewportMetrics(const ViewportMetrics& metrics) { @@ -164,6 +165,17 @@ bool RuntimeController::SetUserSettingsData(const std::string& data) { return false; } +bool RuntimeController::SetLifecycleState(const std::string& data) { + window_data_.lifecycle_state = data; + + if (auto* window = GetWindowIfAvailable()) { + window->UpdateLifecycleState(window_data_.lifecycle_state); + return true; + } + + return false; +} + bool RuntimeController::SetSemanticsEnabled(bool enabled) { window_data_.semantics_enabled = enabled; diff --git a/runtime/runtime_controller.h b/runtime/runtime_controller.h index c913795b8eba2bcdbcf7ee9d6c39e828832cb063..03f1389d8c613f8df6442ecd093e6451372d5b01 100644 --- a/runtime/runtime_controller.h +++ b/runtime/runtime_controller.h @@ -49,6 +49,8 @@ class RuntimeController final : public WindowClient { bool SetUserSettingsData(const std::string& data); + bool SetLifecycleState(const std::string& data); + bool SetSemanticsEnabled(bool enabled); bool SetAccessibilityFeatures(int32_t flags); @@ -111,6 +113,7 @@ class RuntimeController final : public WindowClient { std::string variant_code; std::vector locale_data; std::string user_settings_data = "{}"; + std::string lifecycle_state; bool semantics_enabled = false; bool assistive_technology_enabled = false; int32_t accessibility_feature_flags_ = 0; diff --git a/shell/common/engine.cc b/shell/common/engine.cc index 084216072e988c837486d4fa9e37e596231994ef..5b2d5a5c3dd37a3359d4efcce5cfa184201d7301 100644 --- a/shell/common/engine.cc +++ b/shell/common/engine.cc @@ -291,6 +291,8 @@ bool Engine::HandleLifecyclePlatformMessage(blink::PlatformMessage* message) { if (state == "AppLifecycleState.resumed" && have_surface_) { ScheduleFrame(); } + runtime_controller_->SetLifecycleState(state); + // Always forward these messages to the framework by returning false. return false; }