Skip to content

Commit 801cb0c

Browse files
committed
[web] #2
1 parent 7f7be6c commit 801cb0c

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

lib/web_ui/lib/src/engine/browser_detection.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:html' as html;
6-
75
import 'package:meta/meta.dart';
86

7+
import 'dom.dart';
8+
99
// iOS 15 launched WebGL 2.0, but there's something broken about it, which
1010
// leads to apps failing to load. For now, we're forcing WebGL 1 on iOS.
1111
//
@@ -65,8 +65,8 @@ BrowserEngine get browserEngine {
6565
}
6666

6767
BrowserEngine _detectBrowserEngine() {
68-
final String vendor = html.window.navigator.vendor;
69-
final String agent = html.window.navigator.userAgent.toLowerCase();
68+
final String vendor = domWindow.navigator.vendor;
69+
final String agent = domWindow.navigator.userAgent.toLowerCase();
7070
return detectBrowserEngineByVendorAgent(vendor, agent);
7171
}
7272

@@ -167,14 +167,14 @@ OperatingSystem detectOperatingSystem({
167167
String? overrideUserAgent,
168168
int? overrideMaxTouchPoints,
169169
}) {
170-
final String platform = overridePlatform ?? html.window.navigator.platform!;
171-
final String userAgent = overrideUserAgent ?? html.window.navigator.userAgent;
170+
final String platform = overridePlatform ?? domWindow.navigator.platform!;
171+
final String userAgent = overrideUserAgent ?? domWindow.navigator.userAgent;
172172

173173
if (platform.startsWith('Mac')) {
174174
// iDevices requesting a "desktop site" spoof their UA so it looks like a Mac.
175175
// This checks if we're in a touch device, or on a real mac.
176176
final int maxTouchPoints =
177-
overrideMaxTouchPoints ?? html.window.navigator.maxTouchPoints ?? 0;
177+
overrideMaxTouchPoints ?? domWindow.navigator.maxTouchPoints ?? 0;
178178
if (maxTouchPoints > 2) {
179179
return OperatingSystem.iOs;
180180
}
@@ -233,7 +233,7 @@ bool get isIOS15 {
233233
return debugIsIOS15!;
234234
}
235235
return operatingSystem == OperatingSystem.iOs &&
236-
html.window.navigator.userAgent.contains('OS 15_');
236+
domWindow.navigator.userAgent.contains('OS 15_');
237237
}
238238

239239
/// Use in tests to simulate the detection of iOS 15.
@@ -256,7 +256,7 @@ int get webGLVersion =>
256256
///
257257
/// Our CanvasKit backend is affected due to: https://github.com/emscripten-core/emscripten/issues/11819
258258
int _detectWebGLVersion() {
259-
final html.CanvasElement canvas = html.CanvasElement(
259+
final DomCanvasElement canvas = DomCanvasElement(
260260
width: 1,
261261
height: 1,
262262
);

lib/web_ui/lib/src/engine/dom.dart

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,30 @@ class DomWindow {}
1111

1212
extension DomWindowExtension on DomWindow {
1313
external DomDocument get document;
14+
external DomNavigator get navigator;
1415
}
1516

1617
@JS('window')
1718
external DomWindow get domWindow;
1819

20+
@JS()
21+
@staticInterop
22+
class DomNavigator {}
23+
24+
extension DomNavigatorExtension on DomNavigator {
25+
external int? get maxTouchPoints;
26+
external String get vendor;
27+
external String? get platform;
28+
external String get userAgent;
29+
}
30+
1931
@JS()
2032
@staticInterop
2133
class DomDocument {}
2234

2335
extension DomDocumentExtension on DomDocument {
2436
external /* List<Node> */ List<Object?> querySelectorAll(String selectors);
37+
external DomHTMLElement createElement(String name, [dynamic options]);
2538
}
2639

2740
@JS()
@@ -42,9 +55,38 @@ class DomHTMLMetaElement {}
4255

4356
extension DomHTMLMetaElementExtension on DomHTMLMetaElement {
4457
external String get name;
58+
external set name(String value);
4559
external String get content;
60+
}
4661

47-
external set name(String value);
62+
@JS()
63+
@staticInterop
64+
class DomCanvasElement extends DomHTMLElement {
65+
factory DomCanvasElement({int? width, int? height}) {
66+
final DomCanvasElement canvas =
67+
domWindow.document.createElement('canvas') as DomCanvasElement;
68+
if (width != null) {
69+
canvas.width = width;
70+
}
71+
if (height != null) {
72+
canvas.height = height;
73+
}
74+
return canvas;
75+
}
76+
}
77+
78+
extension DomCanvasElementExtension on DomCanvasElement {
79+
external int? get width;
80+
external set width(int? value);
81+
external int? get height;
82+
external set height(int? value);
83+
84+
Object? getContext(String contextType, [Map<dynamic, dynamic>? attributes]) {
85+
return js_util.callMethod(this, 'getContext', <Object?>[
86+
contextType,
87+
if (attributes != null) js_util.jsify(attributes)
88+
]);
89+
}
4890
}
4991

5092
Object? domGetConstructor(String constructorName) =>

0 commit comments

Comments
 (0)