android and windows targets

This commit is contained in:
yves committed 2026-11-07 15:48:01 +01:00
1 parent c069b8945d
commit 1d974b42a1
58 files changed
+2601 -111

No files matched your search

+10 -2
View File
@@ -28,11 +28,19 @@ String buildCesiumHtml({String? ionToken}) {
<body>
<div id="cesiumContainer"></div>
<script>
// JS -> Dart bridge (WebKitGTK script message handler).
// JS -> Dart bridge: WebKitGTK script message handler (Linux), Java
// interface (Android) or WebView2 web messages (Windows).
window.CesiumChannel = {
postMessage: function (msg) {
if (typeof msg !== 'string') msg = JSON.stringify(msg);
window.webkit.messageHandlers.cesium.postMessage(msg);
if (window.webkit && window.webkit.messageHandlers &&
window.webkit.messageHandlers.cesium) {
window.webkit.messageHandlers.cesium.postMessage(msg);
} else if (window.CesiumAndroid) {
window.CesiumAndroid.postMessage(msg);
} else if (window.chrome && window.chrome.webview) {
window.chrome.webview.postMessage(msg);
}
}
};
window.addEventListener('error', function (e) {
+62 -10
View File
@@ -1,6 +1,8 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'cesium_controller.dart';
import 'cesium_html.dart';
@@ -8,8 +10,15 @@ import 'native_channel.dart';
/// Displays a CesiumJS globe.
///
/// On Linux the globe is a native WebKitGTK view stacked *above* the Flutter
/// surface and kept aligned with this widget's rectangle. Consequences:
/// Supported on Linux (WebKitGTK), Windows (WebView2) and Android (system
/// WebView).
///
/// On Android the web view is a regular platform view: it is composed with
/// the Flutter UI like any other widget.
///
/// On Linux and Windows the globe is a native view stacked *above* the
/// Flutter surface and kept aligned with this widget's rectangle.
/// Consequences:
/// * Flutter widgets cannot be drawn on top of the globe (put overlays
/// around it, or set [visible] to false while a dialog/menu is open).
/// * The rectangle is in window coordinates, so the widget should not be
@@ -47,10 +56,19 @@ class _CesiumViewerState extends State<CesiumViewer> {
bool _disposed = false;
Rect? _sentRect;
bool? _sentVisible;
double? _sentScale;
Rect _currentRect = Rect.zero;
bool get _supported =>
!kIsWeb && defaultTargetPlatform == TargetPlatform.linux;
static bool get _supported =>
!kIsWeb &&
(defaultTargetPlatform == TargetPlatform.linux ||
defaultTargetPlatform == TargetPlatform.windows ||
defaultTargetPlatform == TargetPlatform.android);
/// Android composes the web view as a platform view; the desktop
/// platforms place a native view over the reported rectangle.
static bool get _isPlatformView =>
defaultTargetPlatform == TargetPlatform.android;
@override
void initState() {
@@ -67,8 +85,9 @@ class _CesiumViewerState extends State<CesiumViewer> {
await CesiumNativeChannel.instance.dispose(id);
return;
}
_viewId = id;
_controller.attach(id);
// Rebuild: on Android the platform view can be created now.
setState(() => _viewId = id);
_sync();
}
@@ -89,14 +108,28 @@ class _CesiumViewerState extends State<CesiumViewer> {
_sync();
}
bool get _effectiveVisible =>
widget.visible && TickerMode.valuesOf(context).enabled;
void _sync() {
final id = _viewId;
if (id == null || _disposed) return;
final visible = widget.visible && TickerMode.valuesOf(context).enabled;
if (_sentRect == _currentRect && _sentVisible == visible) return;
if (id == null || _disposed || _isPlatformView) return;
final visible = _effectiveVisible;
final scale = MediaQuery.devicePixelRatioOf(context);
if (_sentRect == _currentRect &&
_sentVisible == visible &&
_sentScale == scale) {
return;
}
_sentRect = _currentRect;
_sentVisible = visible;
CesiumNativeChannel.instance.setRect(id, _currentRect, visible: visible);
_sentScale = scale;
CesiumNativeChannel.instance.setRect(
id,
_currentRect,
visible: visible,
scale: scale,
);
}
@override
@@ -129,12 +162,31 @@ class _CesiumViewerState extends State<CesiumViewer> {
color: Colors.black,
child: Center(
child: Text(
'CesiumViewer currently supports Linux desktop only.',
'CesiumViewer supports Linux, Windows and Android only.',
style: TextStyle(color: Colors.white70),
),
),
);
}
if (_isPlatformView) {
final id = _viewId;
if (id == null) {
return const SizedBox.expand(child: ColoredBox(color: Colors.black));
}
return Visibility(
visible: _effectiveVisible,
maintainState: true,
child: AndroidView(
viewType: CesiumNativeChannel.viewType,
creationParams: {'id': id},
creationParamsCodec: const StandardMessageCodec(),
// The globe handles every gesture that starts on it.
gestureRecognizers: {
Factory<OneSequenceGestureRecognizer>(EagerGestureRecognizer.new),
},
),
);
}
// SizedBox.expand: a childless ColoredBox would size to 0x0 under loose
// constraints, which hides the native view.
return _RectReporter(
+15 -3
View File
@@ -2,8 +2,8 @@ import 'package:flutter/services.dart';
typedef NativeEventHandler = void Function(String event, String message);
/// Thin wrapper over the `cesiumjs/webview` method channel implemented in
/// `linux/cesiumjs_plugin.cc`.
/// Thin wrapper over the `cesiumjs/webview` method channel implemented by
/// the native plugins (`linux/`, `windows/`, `android/`).
class CesiumNativeChannel {
CesiumNativeChannel._() {
_channel.setMethodCallHandler(_onCall);
@@ -11,6 +11,9 @@ class CesiumNativeChannel {
static final CesiumNativeChannel instance = CesiumNativeChannel._();
/// Android platform view type (same name as the channel).
static const String viewType = 'cesiumjs/webview';
final MethodChannel _channel = const MethodChannel('cesiumjs/webview');
final Map<int, NativeEventHandler> _handlers = {};
int _nextId = 0;
@@ -34,7 +37,15 @@ class CesiumNativeChannel {
return id;
}
Future<void> setRect(int id, Rect rect, {required bool visible}) =>
/// [rect] is in logical pixels relative to the Flutter view; [scale] is
/// its device pixel ratio (used by Windows, where windows use physical
/// pixels).
Future<void> setRect(
int id,
Rect rect, {
required bool visible,
required double scale,
}) =>
_channel.invokeMethod<void>('setRect', {
'id': id,
'x': rect.left,
@@ -42,6 +53,7 @@ class CesiumNativeChannel {
'width': rect.width,
'height': rect.height,
'visible': visible,
'scale': scale,
});
Future<String?> runJavaScript(int id, String script) => _channel