5
5
/// JavaScript API a Flutter Web application can use to configure the Web
6
6
/// Engine.
7
7
///
8
- /// The configuration is a plain JavaScript object set as the
9
- /// `flutterConfiguration` property of the top-level `window` object.
8
+ /// The configuration is passed from JavaScript to the engine as part of the
9
+ /// bootstrap process, through the `FlutterEngineInitializer.initializeEngine`
10
+ /// JS method, with an (optional) object of type [JsFlutterConfiguration] .
11
+ ///
12
+ /// This library also supports the legacy method of setting a plain JavaScript
13
+ /// object set as the `flutterConfiguration` property of the top-level `window`
14
+ /// object, but that approach is now deprecated and will warn users.
15
+ ///
16
+ /// Both methods are **disallowed** to be used at the same time.
10
17
///
11
18
/// Example:
12
19
///
13
- /// <head>
14
- /// <script>
15
- /// window.flutterConfiguration = {
16
- /// canvasKitBaseUrl: "https://example.com/my-custom-canvaskit/"
17
- /// };
18
- /// </script>
19
- /// </head>
20
+ /// _flutter.loader.loadEntrypoint({
21
+ /// // ...
22
+ /// onEntrypointLoaded: async function(engineInitializer) {
23
+ /// let appRunner = await engineInitializer.initializeEngine({
24
+ /// // JsFlutterConfiguration goes here...
25
+ /// canvasKitBaseUrl: "https://example.com/my-custom-canvaskit/",
26
+ /// });
27
+ /// appRunner.runApp();
28
+ /// }
29
+ /// });
30
+ ///
31
+ /// Example of the **deprecated** style (this will issue a JS console warning!):
32
+ ///
33
+ /// <script>
34
+ /// window.flutterConfiguration = {
35
+ /// canvasKitBaseUrl: "https://example.com/my-custom-canvaskit/"
36
+ /// };
37
+ /// </script>
20
38
///
21
- /// Configuration properties supplied via `window.flutterConfiguration`
22
- /// override those supplied using the corresponding environment variables. For
23
- /// example, if both `window.flutterConfiguration.canvasKitBaseUrl` and the
24
- /// `FLUTTER_WEB_CANVASKIT_URL` environment variables are provided,
25
- /// `window.flutterConfiguration.canvasKitBaseUrl` is used.
39
+ /// Configuration properties supplied via this object override those supplied
40
+ /// using the corresponding environment variables. For example, if both the
41
+ /// `canvasKitBaseUrl` config entry and the `FLUTTER_WEB_CANVASKIT_URL`
42
+ /// environment variables are provided, the `canvasKitBaseUrl` entry is used.
26
43
27
44
@JS ()
28
45
library configuration;
29
46
30
47
import 'package:js/js.dart' ;
48
+ import 'package:meta/meta.dart' ;
49
+ import 'dom.dart' ;
31
50
32
51
/// The version of CanvasKit used by the web engine by default.
33
52
// DO NOT EDIT THE NEXT LINE OF CODE MANUALLY
34
53
// See `lib/web_ui/README.md` for how to roll CanvasKit to a new version.
35
54
const String _canvaskitVersion = '0.37.0' ;
36
55
37
56
/// The Web Engine configuration for the current application.
38
- FlutterConfiguration get configuration => _configuration ?? = FlutterConfiguration (_jsConfiguration);
57
+ FlutterConfiguration get configuration =>
58
+ _configuration ?? = FlutterConfiguration .legacy (_jsConfiguration);
39
59
FlutterConfiguration ? _configuration;
40
60
41
61
/// Sets the given configuration as the current one.
42
62
///
43
63
/// This must be called before the engine is initialized. Calling it after the
44
64
/// engine is initialized will result in some of the properties not taking
45
65
/// effect because they are consumed during initialization.
66
+ @visibleForTesting
46
67
void debugSetConfiguration (FlutterConfiguration configuration) {
47
68
_configuration = configuration;
48
69
}
49
70
50
71
/// Supplies Web Engine configuration properties.
51
72
class FlutterConfiguration {
52
- /// Constructs a configuration from a JavaScript object containing
53
- /// runtime-supplied properties.
54
- FlutterConfiguration (this ._js );
73
+ /// Constructs an unitialized configuration object.
74
+ @visibleForTesting
75
+ FlutterConfiguration ();
55
76
56
- final JsFlutterConfiguration ? _js;
77
+ /// Constucts a "tainted by JS globals" configuration object.
78
+ ///
79
+ /// This configuration style is deprecated. It will warn the user about the
80
+ /// new API (if used)
81
+ FlutterConfiguration .legacy (JsFlutterConfiguration ? config) {
82
+ if (config != null ) {
83
+ _usedLegacyConfigStyle = true ;
84
+ _configuration = config;
85
+ }
86
+ // Warn the user of the deprecated behavior.
87
+ assert (() {
88
+ if (config != null ) {
89
+ domWindow.console.warn ('window.flutterConfiguration is now deprecated.\n '
90
+ 'Use engineInitializer.initializeEngine(config) instead.\n '
91
+ 'See: https://docs.flutter.dev/development/platform-integration/web/initialization' );
92
+ }
93
+ if (_requestedRendererType != null ) {
94
+ domWindow.console.warn ('window.flutterWebRenderer is now deprecated.\n '
95
+ 'Use engineInitializer.initializeEngine(config) instead.\n '
96
+ 'See: https://docs.flutter.dev/development/platform-integration/web/initialization' );
97
+ }
98
+ return true ;
99
+ }());
100
+ }
101
+
102
+ bool _usedLegacyConfigStyle = false ;
103
+ JsFlutterConfiguration ? _configuration;
104
+
105
+ /// Sets a value for [_configuration] .
106
+ ///
107
+ /// This method is called by the engine initialization process, through the
108
+ /// [initEngineServices] method.
109
+ ///
110
+ /// This method throws an AssertionError, if the _configuration object has
111
+ /// been set to anything non-null through the [FlutterConfiguration.legacy]
112
+ /// constructor.
113
+ void setUserConfiguration (JsFlutterConfiguration ? configuration) {
114
+ if (configuration != null ) {
115
+ assert (! _usedLegacyConfigStyle,
116
+ 'Use engineInitializer.initializeEngine(config) only. '
117
+ 'Using the (deprecated) window.flutterConfiguration and initializeEngine '
118
+ 'configuration simultaneously is not supported.' );
119
+ assert (_requestedRendererType == null || configuration.renderer == null ,
120
+ 'Use engineInitializer.initializeEngine(config) only. '
121
+ 'Using the (deprecated) window.flutterWebRenderer and initializeEngine '
122
+ 'configuration simultaneously is not supported.' );
123
+ _configuration = configuration;
124
+ }
125
+ }
57
126
58
127
// Static constant parameters.
59
128
//
60
129
// These properties affect tree shaking and therefore cannot be supplied at
61
- // runtime. They must be static constants for the compiler to remove dead
130
+ // runtime. They must be static constants for the compiler to remove dead code
62
131
// effectively.
63
132
64
133
/// Auto detect which rendering backend to use.
@@ -110,7 +179,7 @@ class FlutterConfiguration {
110
179
/// --web-renderer=canvaskit \
111
180
/// --dart-define=FLUTTER_WEB_CANVASKIT_URL=https://example.com/custom-canvaskit-build/
112
181
/// ```
113
- String get canvasKitBaseUrl => _js ? .canvasKitBaseUrl ?? _defaultCanvasKitBaseUrl;
182
+ String get canvasKitBaseUrl => _configuration ? .canvasKitBaseUrl ?? _defaultCanvasKitBaseUrl;
114
183
static const String _defaultCanvasKitBaseUrl = String .fromEnvironment (
115
184
'FLUTTER_WEB_CANVASKIT_URL' ,
116
185
defaultValue: 'https://unpkg.com/canvaskit-wasm@$_canvaskitVersion /bin/' ,
@@ -121,7 +190,7 @@ class FlutterConfiguration {
121
190
///
122
191
/// This is mainly used for testing or for apps that want to ensure they
123
192
/// run on devices which don't support WebGL.
124
- bool get canvasKitForceCpuOnly => _js ? .canvasKitForceCpuOnly ?? _defaultCanvasKitForceCpuOnly;
193
+ bool get canvasKitForceCpuOnly => _configuration ? .canvasKitForceCpuOnly ?? _defaultCanvasKitForceCpuOnly;
125
194
static const bool _defaultCanvasKitForceCpuOnly = bool .fromEnvironment (
126
195
'FLUTTER_WEB_CANVASKIT_FORCE_CPU_ONLY' ,
127
196
);
@@ -135,7 +204,7 @@ class FlutterConfiguration {
135
204
///
136
205
/// This value can be specified using either the `FLUTTER_WEB_MAXIMUM_SURFACES`
137
206
/// environment variable, or using the runtime configuration.
138
- int get canvasKitMaximumSurfaces => _js ? .canvasKitMaximumSurfaces ?? _defaultCanvasKitMaximumSurfaces;
207
+ int get canvasKitMaximumSurfaces => _configuration ? .canvasKitMaximumSurfaces ?? _defaultCanvasKitMaximumSurfaces;
139
208
static const int _defaultCanvasKitMaximumSurfaces = int .fromEnvironment (
140
209
'FLUTTER_WEB_MAXIMUM_SURFACES' ,
141
210
defaultValue: 8 ,
@@ -152,10 +221,23 @@ class FlutterConfiguration {
152
221
/// ```
153
222
/// flutter run -d chrome --profile --dart-define=FLUTTER_WEB_DEBUG_SHOW_SEMANTICS=true
154
223
/// ```
155
- bool get debugShowSemanticsNodes => _js ? .debugShowSemanticsNodes ?? _defaultDebugShowSemanticsNodes;
224
+ bool get debugShowSemanticsNodes => _configuration ? .debugShowSemanticsNodes ?? _defaultDebugShowSemanticsNodes;
156
225
static const bool _defaultDebugShowSemanticsNodes = bool .fromEnvironment (
157
226
'FLUTTER_WEB_DEBUG_SHOW_SEMANTICS' ,
158
227
);
228
+
229
+ /// Returns the [hostElement] in which the Flutter Application is supposed
230
+ /// to render, or `null` if the user hasn't specified anything.
231
+ DomElement ? get hostElement => _configuration? .hostElement;
232
+
233
+ /// Returns the [requestedRendererType] to be used with the current Flutter
234
+ /// application, normally 'canvaskit' or 'auto'.
235
+ ///
236
+ /// This value may come from the JS configuration, but also a specific JS value:
237
+ /// `window.flutterWebRenderer` .
238
+ ///
239
+ /// This is used by the Renderer class to decide how to initialize the engine.
240
+ String ? get requestedRendererType => _configuration? .renderer ?? _requestedRendererType;
159
241
}
160
242
161
243
@JS ('window.flutterConfiguration' )
@@ -169,13 +251,13 @@ class JsFlutterConfiguration {}
169
251
extension JsFlutterConfigurationExtension on JsFlutterConfiguration {
170
252
external String ? get canvasKitBaseUrl;
171
253
external bool ? get canvasKitForceCpuOnly;
172
- external bool ? get debugShowSemanticsNodes;
173
-
174
254
external int ? get canvasKitMaximumSurfaces;
175
- external set canvasKitMaximumSurfaces (int ? maxSurfaces);
255
+ external bool ? get debugShowSemanticsNodes;
256
+ external DomElement ? get hostElement;
257
+ external String ? get renderer;
176
258
}
177
259
178
260
/// A JavaScript entrypoint that allows developer to set rendering backend
179
261
/// at runtime before launching the application.
180
262
@JS ('window.flutterWebRenderer' )
181
- external String ? get requestedRendererType ;
263
+ external String ? get _requestedRendererType ;
0 commit comments