Skip to content

Commit e6c7492

Browse files
SaadArdatiBirjuVachhani
authored andcommitted
Birju&Saad: General fixes & icon support for dynamic settings panel.
1 parent 9efb25a commit e6c7492

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

lib/src/api/field_access.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ final class ObjectFieldAccess<T extends FieldsHolder> extends FieldAccess<T> {
179179
}
180180
}
181181

182+
/// A field accessor for a [IconModel] field.
183+
final class IconFieldAccess extends FieldAccess<MultiSourceIconModel> {
184+
/// Constructs a new [IconFieldAccess] instance with the given parameters.
185+
const IconFieldAccess(
186+
super.label,
187+
super.description,
188+
super.getValue,
189+
super.setter, {
190+
super.defaultValue,
191+
});
192+
193+
@override
194+
String get dynamicKeyType => 'icon';
195+
196+
@override
197+
dynamic serialize(MultiSourceIconModel? obj) => obj?.toJson();
198+
199+
@override
200+
void setValue(Object? value) {
201+
if (value is MultiSourceIconModel) setter(value);
202+
}
203+
}
204+
182205
/// A field accessor for an [Enum] field.
183206
final class EnumFieldAccess<T extends Enum> extends FieldAccess<T> {
184207
/// Constructs a new [EnumFieldAccess] instance with the given parameters.

lib/src/api/nodes/icon_node.dart

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:codelessly_json_annotation/codelessly_json_annotation.dart';
22

3+
import '../field_access.dart';
34
import '../mixins.dart';
45
import '../models/models.dart';
56
import 'nodes.dart';
@@ -19,11 +20,7 @@ const MaterialIcon defaultIcon = MaterialIcon(
1920
/// A node that displays an icon.
2021
@JsonSerializable()
2122
class IconNode extends SceneNode
22-
with
23-
BlendMixin,
24-
SingleColorMixin,
25-
FixedAspectRatioMixin,
26-
CustomPropertiesMixin {
23+
with BlendMixin, FixedAspectRatioMixin, CustomPropertiesMixin {
2724
/// Strictly used for previews. e.g in components panel.
2825
IconNode.empty()
2926
: this(
@@ -45,6 +42,16 @@ class IconNode extends SceneNode
4542
@override
4643
double get aspectRatio => 1;
4744

45+
@override
46+
late final Map<String, FieldAccess<Object?>> fields = {
47+
'properties': ObjectFieldAccess<IconProperties>(
48+
() => 'Properties',
49+
() => 'Icon properties',
50+
() => properties,
51+
(value) => properties = value,
52+
),
53+
};
54+
4855
/// Creates an [IconNode] with the given data.
4956
IconNode({
5057
IconModel? icon,
@@ -74,15 +81,20 @@ class IconNode extends SceneNode
7481
InkWellModel? inkWell,
7582
List<Reaction> reactions = const [],
7683
IconProperties? properties,
77-
}) : properties = properties ?? IconProperties(icon: icon ?? defaultIcon) {
84+
}) : properties = properties ??
85+
IconProperties(
86+
icon: MultiSourceIconModel.icon(
87+
icon: icon ?? defaultIcon,
88+
color: color,
89+
),
90+
) {
7891
setBlendMixin(
7992
opacity: opacity,
8093
isMask: isMask,
8194
effects: effects,
8295
blendMode: blendMode,
8396
inkWell: inkWell,
8497
);
85-
setSingleColorMixin(color: color);
8698
setReactionMixin([...reactions]);
8799
}
88100

@@ -98,9 +110,21 @@ class IconNode extends SceneNode
98110
if (json.containsKey('icon') && !json.containsKey('properties')) {
99111
json['properties'] = <String, dynamic>{
100112
'icon': json['icon'],
113+
'show': true,
114+
'color': json['color'],
101115
};
102116
json.remove('icon');
103117
}
118+
if (json.containsKey('properties') && json['properties']['icon']['icon'] == null) {
119+
// Backwards compatibility for migration to MultiSourceIconModel.
120+
json['properties']['icon'] = {
121+
'icon': {
122+
'icon': json['properties']['icon'],
123+
'show': true,
124+
'color': json['color'],
125+
},
126+
};
127+
}
104128
return _$IconNodeFromJson(json);
105129
}
106130

@@ -110,9 +134,9 @@ class IconNode extends SceneNode
110134

111135
/// Properties for an [IconNode].
112136
@JsonSerializable()
113-
class IconProperties extends CustomProperties {
137+
class IconProperties extends CustomProperties with FieldsHolder {
114138
/// The icon to display.
115-
IconModel icon;
139+
MultiSourceIconModel icon;
116140

117141
/// Creates [IconProperties] with the given data.
118142
IconProperties({
@@ -127,4 +151,14 @@ class IconProperties extends CustomProperties {
127151

128152
@override
129153
List<Object?> get props => [icon];
154+
155+
@override
156+
late final Map<String, FieldAccess<Object?>> fields = {
157+
'icon': IconFieldAccess(
158+
() => 'Icon',
159+
() => 'Icon to display',
160+
() => icon,
161+
(value) => icon = value,
162+
),
163+
};
130164
}

lib/src/api/nodes/icon_node.g.dart

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)