Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 59 additions & 20 deletions packages/flet/lib/src/controls/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import '../models/page_design.dart';
import '../routing/route_parser.dart';
import '../routing/route_state.dart';
import '../routing/router_delegate.dart';
import '../services/service_binding.dart';
import '../services/service_registry.dart';
import '../utils/device_info.dart';
import '../utils/locale.dart';
Expand Down Expand Up @@ -57,6 +58,9 @@ class _PageControlState extends State<PageControl> with WidgetsBindingObserver {
late final AppLifecycleListener _appLifecycleListener;
ServiceRegistry? _pageServices;
ServiceRegistry? _userServices;
String? _userServicesUid;
ServiceBinding? _windowService;
Control? _windowControl;
bool? _prevOnKeyboardEvent;
bool _keyboardHandlerSubscribed = false;
String? _prevViewRoutes;
Expand Down Expand Up @@ -95,12 +99,15 @@ class _PageControlState extends State<PageControl> with WidgetsBindingObserver {

_attachKeyboardListenerIfNeeded();
widget.control.addInvokeMethodListener(_invokeMethod);
widget.control.addListener(_onPageControlChanged);
_ensureServiceRegistries();
}

@override
void didChangeDependencies() {
debugPrint("Page.didChangeDependencies: ${widget.control.id}");
super.didChangeDependencies();
_ensureServiceRegistries();
_loadFontsIfNeeded(FletBackend.of(context));
}

Expand All @@ -109,26 +116,7 @@ class _PageControlState extends State<PageControl> with WidgetsBindingObserver {
debugPrint("Page.didUpdateWidget: ${widget.control.id}");
super.didUpdateWidget(oldWidget);
_updateMultiViews();

// page services
_pageServices ??= ServiceRegistry(
control: widget.control,
propertyName: "_services",
backend: FletBackend.of(context));

// user services
var userServicesControl = widget.control.child("_user_services");
if (userServicesControl != null) {
if (_userServices == null ||
_userServices?.control.internals?["uid"] !=
userServicesControl.internals?["uid"]) {
_userServices = ServiceRegistry(
control: userServicesControl,
propertyName: "_services",
backend: FletBackend.of(context));
}
}

_ensureServiceRegistries();
_attachKeyboardListenerIfNeeded();
_loadFontsIfNeeded(FletBackend.of(context));
}
Expand All @@ -148,9 +136,60 @@ class _PageControlState extends State<PageControl> with WidgetsBindingObserver {
HardwareKeyboard.instance.removeHandler(_handleKeyDown);
}
widget.control.removeInvokeMethodListener(_invokeMethod);
widget.control.removeListener(_onPageControlChanged);
_pageServices?.dispose();
_userServices?.dispose();
_windowService?.dispose();
super.dispose();
}

void _onPageControlChanged() {
_ensureServiceRegistries();
}

void _ensureServiceRegistries() {
if (!mounted) {
return;
}
var backend = FletBackend.of(context);

_pageServices ??= ServiceRegistry(
control: widget.control,
propertyName: "_services",
backend: backend);

var userServicesControl = widget.control.child("_user_services");
if (userServicesControl != null) {
var uid = userServicesControl.internals?["uid"];
if (_userServices == null || _userServicesUid != uid) {
_userServices?.dispose();
_userServices = ServiceRegistry(
control: userServicesControl,
propertyName: "_services",
backend: backend);
_userServicesUid = uid;
}
} else if (_userServices != null) {
_userServices?.dispose();
_userServices = null;
_userServicesUid = null;
}

var windowControl = widget.control.child("window", visibleOnly: false);
if (windowControl != null) {
if (!identical(windowControl, _windowControl)) {
_windowService?.dispose();
_windowService =
ServiceBinding(control: windowControl, backend: backend);
_windowControl = windowControl;
}
} else if (_windowService != null) {
_windowService?.dispose();
_windowService = null;
_windowControl = null;
}
}

Future<dynamic> _invokeMethod(String name, dynamic args) async {
debugPrint("Page.$name($args)");

Expand Down
4 changes: 0 additions & 4 deletions packages/flet/lib/src/controls/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ class _ViewControlState extends State<ViewControl> {
overlayWidgets.add(PageMedia(view: widget.control.parent));
}

var windowControl = control.parent?.get("window");
if (windowControl != null && isRootView && isDesktopPlatform()) {
overlayWidgets.add(ControlWidget(control: windowControl));
}
}

Widget body = Stack(children: [
Expand Down
8 changes: 4 additions & 4 deletions packages/flet/lib/src/flet_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class FletBackend extends ChangeNotifier {
}

_registerClient() {
debugPrint("Registering web client: $page");
_send(
Message(
action: MessageAction.registerClient,
Expand All @@ -211,7 +212,7 @@ class FletBackend extends ChangeNotifier {
"width": page.get("width"),
"height": page.get("height"),
"platform": page.get("platform"),
"window": page.child("window")!.toMap(),
"window": page.child("window", visibleOnly: false)!.toMap(),
"media": page.get("media"),
}).toMap()),
unbuffered: true);
Expand Down Expand Up @@ -333,7 +334,7 @@ class FletBackend extends ChangeNotifier {
if (isDesktopPlatform()) {
var windowState = await getWindowState();
debugPrint("Window state updated: $windowState");
var window = page.child("window")!;
var window = page.child("window", visibleOnly: false)!;
updateControl(window.id, windowState.toMap());
triggerControlEvent(window, "event", {"type": "resized"});
}
Expand Down Expand Up @@ -470,8 +471,7 @@ class FletBackend extends ChangeNotifier {
var template = appErrorMessage ?? defaultAppErrorMessageTemplate;
final lines = const LineSplitter().convert(rawError);
final message = lines.isNotEmpty ? lines.first : "";
final details =
lines.length > 1 ? lines.sublist(1).join("\n") : "";
final details = lines.length > 1 ? lines.sublist(1).join("\n") : "";
template = template.replaceAll("{message}", message);
if (details.isEmpty) {
template = template.replaceAll(RegExp(r'(\r?\n)*\{details\}'), "");
Expand Down
6 changes: 3 additions & 3 deletions packages/flet/lib/src/flet_core_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ import 'controls/time_picker.dart';
import 'controls/transparent_pointer.dart';
import 'controls/vertical_divider.dart';
import 'controls/view.dart';
import 'controls/window.dart';
import 'controls/window_drag_area.dart';
import 'flet_extension.dart';
import 'flet_service.dart';
Expand All @@ -116,6 +115,7 @@ import 'services/shared_preferences.dart';
import 'services/storage_paths.dart';
import 'services/tester.dart';
import 'services/url_launcher.dart';
import 'services/window.dart';
import 'utils/cupertino_icons.dart';
import 'utils/material_icons.dart';

Expand Down Expand Up @@ -355,8 +355,6 @@ class FletCoreExtension extends FletExtension {
return VerticalDividerControl(key: key, control: control);
case "View":
return ViewControl(key: key, control: control);
case "Window":
return WindowControl(key: key, control: control);
case "WindowDragArea":
return WindowDragAreaControl(key: key, control: control);
default:
Expand All @@ -383,6 +381,8 @@ class FletCoreExtension extends FletExtension {
return SemanticsServiceControl(control: control);
case "StoragePaths":
return StoragePaths(control: control);
case "Window":
return WindowService(control: control);
case "Tester":
return TesterService(control: control);
case "UrlLauncher":
Expand Down
Loading
Loading