Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 054ec84

Browse files
committed
[dart:ui] Introduce PlatformDispatcher.implicitView
1 parent 9a40a38 commit 054ec84

17 files changed

+124
-9
lines changed

lib/ui/dart_ui.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ typedef CanvasPath Path;
9595
V(IsolateNameServerNatives::RemovePortNameMapping, 1) \
9696
V(NativeStringAttribute::initLocaleStringAttribute, 4) \
9797
V(NativeStringAttribute::initSpellOutStringAttribute, 3) \
98+
V(PlatformConfigurationNativeApi::ImplicitViewEnabled, 0) \
9899
V(PlatformConfigurationNativeApi::DefaultRouteName, 0) \
99100
V(PlatformConfigurationNativeApi::ScheduleFrame, 0) \
100101
V(PlatformConfigurationNativeApi::Render, 1) \

lib/ui/platform_dispatcher.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,23 @@ class PlatformDispatcher {
168168
// A map of opaque platform view identifiers to view configurations.
169169
final Map<Object, ViewConfiguration> _viewConfigurations = <Object, ViewConfiguration>{};
170170

171+
/// The implicit [FlutterView] provided by the platform, if any.
172+
///
173+
/// This is used to bootstrap the framework for applications where the
174+
/// platform provides a view, such as applications designed for
175+
/// single-display mobile devices.
176+
///
177+
/// While the [FlutterView]'s properties may change, this reference is
178+
/// guaranteed to never change.
179+
///
180+
/// See also:
181+
///
182+
/// * [View.of], for accessing the current view.
183+
FlutterView? get implicitView => _implicitViewEnabled() ? _views[0] : null;
184+
185+
@Native<Handle Function()>(symbol: 'PlatformConfigurationNativeApi::ImplicitViewEnabled')
186+
external static bool _implicitViewEnabled();
187+
171188
/// A callback that is invoked whenever the [ViewConfiguration] of any of the
172189
/// [views] changes.
173190
///

lib/ui/window.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ enum Brightness {
896896
/// * [PlatformDispatcher.views], contains the current list of Flutter windows
897897
/// belonging to the application, including top level application windows like
898898
/// this one.
899+
/// * [PlatformDispatcher.implicitView], this window's view.
899900
final SingletonFlutterWindow window = SingletonFlutterWindow._(0, PlatformDispatcher.instance);
900901

901902
/// Additional data available on each flutter frame.

lib/ui/window/platform_configuration.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
namespace flutter {
2323
namespace {
2424

25+
const int kImplicitViewId = 0;
26+
2527
Dart_Handle ToByteData(const fml::Mapping& buffer) {
2628
return tonic::DartByteData::Create(buffer.GetMapping(), buffer.GetSize());
2729
}
@@ -67,8 +69,10 @@ void PlatformConfiguration::DidCreateIsolate() {
6769
Dart_GetField(library, tonic::ToDart("_drawFrame")));
6870
report_timings_.Set(tonic::DartState::Current(),
6971
Dart_GetField(library, tonic::ToDart("_reportTimings")));
70-
windows_.insert(std::make_pair(
71-
0, std::make_unique<Window>(0, ViewportMetrics{1.0, 0.0, 0.0, -1})));
72+
windows_.insert(
73+
std::make_pair(kImplicitViewId,
74+
std::make_unique<Window>(
75+
kImplicitViewId, ViewportMetrics{1.0, 0.0, 0.0, -1})));
7276
}
7377

7478
void PlatformConfiguration::UpdateLocales(
@@ -427,6 +431,16 @@ Dart_Handle PlatformConfigurationNativeApi::ComputePlatformResolvedLocale(
427431
return tonic::DartConverter<std::vector<std::string>>::ToDart(results);
428432
}
429433

434+
Dart_Handle PlatformConfigurationNativeApi::ImplicitViewEnabled() {
435+
UIDartState::ThrowIfUIOperationsProhibited();
436+
bool enabled = UIDartState::Current()
437+
->platform_configuration()
438+
->client()
439+
->ImplicitViewEnabled();
440+
441+
return Dart_NewBoolean(enabled);
442+
}
443+
430444
std::string PlatformConfigurationNativeApi::DefaultRouteName() {
431445
UIDartState::ThrowIfUIOperationsProhibited();
432446
return UIDartState::Current()

lib/ui/window/platform_configuration.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ enum class AccessibilityFeatureFlag : int32_t {
4949
///
5050
class PlatformConfigurationClient {
5151
public:
52+
//--------------------------------------------------------------------------
53+
/// @brief Whether the platform provides an implicit view. If true,
54+
/// the Framework may assume that it can always render into
55+
/// the view with ID 0.
56+
///
57+
/// This value must not change for the lifetime of the
58+
/// application.
59+
///
60+
virtual bool ImplicitViewEnabled() = 0;
61+
5262
//--------------------------------------------------------------------------
5363
/// @brief The route or path that the embedder requested when the
5464
/// application was launched.
@@ -71,7 +81,7 @@ class PlatformConfigurationClient {
7181
virtual void Render(Scene* scene) = 0;
7282

7383
//--------------------------------------------------------------------------
74-
/// @brief Receives a updated semantics tree from the Framework.
84+
/// @brief Receives an updated semantics tree from the Framework.
7585
///
7686
/// @param[in] update The updated semantic tree to apply.
7787
///
@@ -469,6 +479,8 @@ class PlatformMessageHandlerStorage {
469479
//----------------------------------------------------------------------------
470480
class PlatformConfigurationNativeApi {
471481
public:
482+
static Dart_Handle ImplicitViewEnabled();
483+
472484
static std::string DefaultRouteName();
473485

474486
static void ScheduleFrame();

lib/web_ui/lib/platform_dispatcher.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ abstract class PlatformDispatcher {
3333

3434
Iterable<FlutterView> get views;
3535

36+
FlutterView? get implicitView;
37+
3638
VoidCallback? get onMetricsChanged;
3739
set onMetricsChanged(VoidCallback? callback);
3840

lib/web_ui/lib/src/engine/platform_dispatcher.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
148148
final Map<Object, ui.ViewConfiguration> _windowConfigurations =
149149
<Object, ui.ViewConfiguration>{};
150150

151+
/// The implicit [FlutterView] provided by the platform, if any.
152+
///
153+
/// This is used to bootstrap the framework for applications where the
154+
/// platform provides a view, such as applications designed for
155+
/// single-display mobile devices.
156+
///
157+
/// While the [FlutterView]'s properties may change, this reference is
158+
/// guaranteed to never change.
159+
///
160+
/// See also:
161+
///
162+
/// * [View.of], for accessing the current view.
163+
@override
164+
ui.FlutterView? get implicitView => viewData[kImplicitViewId];
165+
151166
/// A callback that is invoked whenever the platform's [devicePixelRatio],
152167
/// [physicalSize], [padding], [viewInsets], or [systemGestureInsets]
153168
/// values change, for example when the device is rotated or when the
@@ -479,7 +494,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
479494
// TODO(a-wallen): As multi-window support expands, the pop call
480495
// will need to include the view ID. Right now only one view is
481496
// supported.
482-
(viewData[kSingletonViewId]! as EngineFlutterWindow)
497+
(viewData[kImplicitViewId]! as EngineFlutterWindow)
483498
.browserHistory
484499
.exit()
485500
.then((_) {
@@ -584,7 +599,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
584599
// TODO(a-wallen): As multi-window support expands, the navigation call
585600
// will need to include the view ID. Right now only one view is
586601
// supported.
587-
(viewData[kSingletonViewId]! as EngineFlutterWindow)
602+
(viewData[kImplicitViewId]! as EngineFlutterWindow)
588603
.handleNavigationMessage(data)
589604
.then((bool handled) {
590605
if (handled) {
@@ -1173,7 +1188,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
11731188
@override
11741189
String get defaultRouteName {
11751190
return _defaultRouteName ??=
1176-
(viewData[kSingletonViewId]! as EngineFlutterWindow).browserHistory.currentPath;
1191+
(viewData[kImplicitViewId]! as EngineFlutterWindow).browserHistory.currentPath;
11771192
}
11781193

11791194
/// Lazily initialized when the `defaultRouteName` getter is invoked.

lib/web_ui/lib/src/engine/window.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ typedef _HandleMessageCallBack = Future<bool> Function();
2727
/// When set to true, all platform messages will be printed to the console.
2828
const bool debugPrintPlatformMessages = false;
2929

30-
/// The view ID for a singleton flutter window.
31-
const int kSingletonViewId = 0;
30+
/// The view ID for the implicit flutter view provided by the platform.
31+
const int kImplicitViewId = 0;
3232

3333
/// Whether [_customUrlStrategy] has been set or not.
3434
///
@@ -349,7 +349,7 @@ class EngineSingletonFlutterWindow extends EngineFlutterWindow {
349349
/// API surface, providing Web-specific functionality that the standard
350350
/// `dart:ui` version does not.
351351
final EngineSingletonFlutterWindow window =
352-
EngineSingletonFlutterWindow(kSingletonViewId, EnginePlatformDispatcher.instance);
352+
EngineSingletonFlutterWindow(kImplicitViewId, EnginePlatformDispatcher.instance);
353353

354354
/// The Web implementation of [ui.WindowPadding].
355355
class WindowPadding implements ui.WindowPadding {

lib/web_ui/test/window_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ void testMain() {
4141
await window.resetHistory();
4242
});
4343

44+
test('EnginePlatformDispatcher.instance.implicitView should be non-null', () async {
45+
expect(EnginePlatformDispatcher.instance.implicitView, isNotNull);
46+
expect(EnginePlatformDispatcher.instance.implicitView?.viewId, 0);
47+
expect(window.viewId, 0);
48+
});
49+
4450
test('window.defaultRouteName should work with JsUrlStrategy', () async {
4551
dynamic state = <dynamic, dynamic>{};
4652
final JsUrlStrategy jsUrlStrategy = JsUrlStrategy(

runtime/runtime_controller.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ RuntimeController::GetPlatformConfigurationIfAvailable() {
298298
return root_isolate ? root_isolate->platform_configuration() : nullptr;
299299
}
300300

301+
// |PlatformConfigurationClient|
302+
bool RuntimeController::ImplicitViewEnabled() {
303+
return client_.ImplicitViewEnabled();
304+
}
305+
301306
// |PlatformConfigurationClient|
302307
std::string RuntimeController::DefaultRouteName() {
303308
return client_.DefaultRouteName();

0 commit comments

Comments
 (0)