[go: nahoru, domu]

Skip to content

Commit

Permalink
Expose registered widget libraries and local widget library widgets. (#…
Browse files Browse the repository at this point in the history
…5936)

This exposes the necessary information to allow rfwpg to display registered libraries and widgets in a list or tree.
  • Loading branch information
johnmccutchan committed Jan 19, 2024
1 parent da6d872 commit 7901015
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/rfw/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.18

* Exposes `WidgetLibrary`s registered in `Runtime`.
* Exposes widgets map in `LocalWidgetLibrary`.

## 1.0.17

* Adds support for tracking source locations of `BlobNode`s and
Expand Down
39 changes: 38 additions & 1 deletion packages/rfw/lib/src/flutter/runtime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

// This file is hand-formatted.

import 'dart:collection';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

import '../dart/model.dart';
import '../../formats.dart';

import 'content.dart';

/// Signature of builders for local widgets.
Expand Down Expand Up @@ -169,6 +172,22 @@ class LocalWidgetLibrary extends WidgetLibrary {
LocalWidgetBuilder? findConstructor(String name) {
return _widgets[name];
}

/// The widgets defined by this [LocalWidgetLibrary].
///
/// The returned map is an immutable view of the map provided to the constructor.
/// They keys are the unqualified widget names, and the values are the corresponding
/// [LocalWidgetBuilder]s.
///
/// The map never changes during the lifetime of the [LocalWidgetLibrary], but a new
/// instance of an [UnmodifiableMapView] is returned each time this getter is used.
///
/// See also:
///
/// * [createCoreWidgets], a function that creates a [Map] of local widgets.
UnmodifiableMapView<String, LocalWidgetBuilder> get widgets {
return UnmodifiableMapView<String, LocalWidgetBuilder>(_widgets);
}
}

class _ResolvedConstructor {
Expand Down Expand Up @@ -226,6 +245,24 @@ class Runtime extends ChangeNotifier {
_clearCache();
}

/// The widget libraries imported in this [Runtime].
///
/// The returned map is an immutable view of the map updated by calls to
/// [update] and [clearLibraries].
///
/// The keys are instances [LibraryName] which encode fully qualified library
/// names, and the values are the corresponding [WidgetLibrary]s.
///
/// The returned map is an immutable copy of the registered libraries
/// at the time of this call.
///
/// See also:
///
/// * [update] and [clearLibraries], functions that populate this map.
UnmodifiableMapView<LibraryName, WidgetLibrary> get libraries {
return UnmodifiableMapView<LibraryName, WidgetLibrary>(Map<LibraryName, WidgetLibrary>.from(_libraries));
}

final Map<FullyQualifiedWidgetName, _ResolvedConstructor?> _cachedConstructors = <FullyQualifiedWidgetName, _ResolvedConstructor?>{};
final Map<FullyQualifiedWidgetName, _CurriedWidget> _widgets = <FullyQualifiedWidgetName, _CurriedWidget>{};

Expand Down
2 changes: 1 addition & 1 deletion packages/rfw/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: rfw
description: "Remote Flutter widgets: a library for rendering declarative widget description files at runtime."
repository: https://github.com/flutter/packages/tree/main/packages/rfw
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+rfw%22
version: 1.0.17
version: 1.0.18

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
12 changes: 12 additions & 0 deletions packages/rfw/test/runtime_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ void main() {
expect(tester.widget<ColoredBox>(find.byType(ColoredBox)).color, const Color(0x00000002));
});

testWidgets('Runtime', (WidgetTester tester) async {
final Runtime runtime = Runtime()
..update(const LibraryName(<String>['core']), createCoreWidgets());
expect(runtime.libraries.length, 1);
final LibraryName libraryName = runtime.libraries.entries.first.key;
expect('$libraryName', 'core');
final WidgetLibrary widgetLibrary = runtime.libraries.entries.first.value;
expect(widgetLibrary, isA<LocalWidgetLibrary>());
widgetLibrary as LocalWidgetLibrary;
expect(widgetLibrary.widgets.length, greaterThan(1));
});

testWidgets('Runtime', (WidgetTester tester) async {
final Runtime runtime = Runtime()
..update(const LibraryName(<String>['core']), createCoreWidgets());
Expand Down
2 changes: 1 addition & 1 deletion packages/rfw/test_coverage/bin/test_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'package:meta/meta.dart';

// Please update these targets when you update this package.
// Please ensure that test coverage continues to be 100%.
const int targetLines = 3219;
const int targetLines = 3223;
const String targetPercent = '100';
const String lastUpdate = '2023-06-29';

Expand Down

0 comments on commit 7901015

Please sign in to comment.