@effindomv2/fui-as 0.1.17 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/nodes/Svg.ts +97 -1
package/package.json
CHANGED
package/src/nodes/Svg.ts
CHANGED
|
@@ -8,8 +8,9 @@ import {
|
|
|
8
8
|
getSvgAssetWidth,
|
|
9
9
|
releaseSvgAsset,
|
|
10
10
|
} from "../core/Assets";
|
|
11
|
+
import { Action, HandlerAction } from "../core/Action";
|
|
11
12
|
import * as ui from "../bindings/ui";
|
|
12
|
-
import { HandleValue, NodeType, SemanticRole } from "../core/ffi";
|
|
13
|
+
import { HandleValue, NodeType, SemanticRole, Unit } from "../core/ffi";
|
|
13
14
|
import { Signal } from "../core/Signal";
|
|
14
15
|
import { FlexBox, FlexBoxProps } from "./FlexBox";
|
|
15
16
|
|
|
@@ -18,11 +19,20 @@ export class Svg extends FlexBox {
|
|
|
18
19
|
private sourceUrlValue: string = "";
|
|
19
20
|
private tintColorValue: u32 = 0;
|
|
20
21
|
private ownedSvgAssetId: u32 = 0;
|
|
22
|
+
private requestedWidthValue: f32 = 0.0;
|
|
23
|
+
private requestedWidthUnit: Unit = Unit.Pixel;
|
|
24
|
+
private hasRequestedWidth: bool = false;
|
|
25
|
+
private requestedHeightValue: f32 = 0.0;
|
|
26
|
+
private requestedHeightUnit: Unit = Unit.Pixel;
|
|
27
|
+
private hasRequestedHeight: bool = false;
|
|
28
|
+
private assetStateAction: Action<AssetLoadState> | null = null;
|
|
29
|
+
private trackedSvgAssetId: u32 = 0;
|
|
21
30
|
|
|
22
31
|
constructor(svgId: u32 = 0, tintColor: u32 = 0) {
|
|
23
32
|
super();
|
|
24
33
|
this.svgIdValue = svgId;
|
|
25
34
|
this.tintColorValue = tintColor;
|
|
35
|
+
this.attachAssetStateListener();
|
|
26
36
|
}
|
|
27
37
|
|
|
28
38
|
static from(props: FlexBoxProps, svgId: u32 = 0, tintColor: u32 = 0): Svg {
|
|
@@ -42,6 +52,8 @@ export class Svg extends FlexBox {
|
|
|
42
52
|
this.releaseOwnedSourceAsset();
|
|
43
53
|
this.sourceUrlValue = "";
|
|
44
54
|
this.svgIdValue = svgId;
|
|
55
|
+
this.attachAssetStateListener();
|
|
56
|
+
this.applyResolvedSizing();
|
|
45
57
|
if (this.hasBuiltHandle()) {
|
|
46
58
|
this.applySvgSource();
|
|
47
59
|
this.notifyRetainedMutation();
|
|
@@ -60,6 +72,8 @@ export class Svg extends FlexBox {
|
|
|
60
72
|
this.sourceUrlValue = url;
|
|
61
73
|
this.svgIdValue = acquireSvgAsset(url);
|
|
62
74
|
this.ownedSvgAssetId = this.svgIdValue;
|
|
75
|
+
this.attachAssetStateListener();
|
|
76
|
+
this.applyResolvedSizing();
|
|
63
77
|
if (this.hasBuiltHandle()) {
|
|
64
78
|
this.applySvgSource();
|
|
65
79
|
this.notifyRetainedMutation();
|
|
@@ -71,6 +85,8 @@ export class Svg extends FlexBox {
|
|
|
71
85
|
this.releaseOwnedSourceAsset();
|
|
72
86
|
this.sourceUrlValue = "";
|
|
73
87
|
this.svgIdValue = 0;
|
|
88
|
+
this.attachAssetStateListener();
|
|
89
|
+
this.applyResolvedSizing();
|
|
74
90
|
if (this.hasBuiltHandle()) {
|
|
75
91
|
this.applySvgSource();
|
|
76
92
|
this.notifyRetainedMutation();
|
|
@@ -78,6 +94,22 @@ export class Svg extends FlexBox {
|
|
|
78
94
|
return this;
|
|
79
95
|
}
|
|
80
96
|
|
|
97
|
+
width(value: f32, unit: Unit = Unit.Pixel): this {
|
|
98
|
+
this.requestedWidthValue = value;
|
|
99
|
+
this.requestedWidthUnit = unit;
|
|
100
|
+
this.hasRequestedWidth = true;
|
|
101
|
+
this.applyResolvedSizing();
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
height(value: f32, unit: Unit = Unit.Pixel): this {
|
|
106
|
+
this.requestedHeightValue = value;
|
|
107
|
+
this.requestedHeightUnit = unit;
|
|
108
|
+
this.hasRequestedHeight = true;
|
|
109
|
+
this.applyResolvedSizing();
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
81
113
|
tint(color: u32): this {
|
|
82
114
|
this.tintColorValue = color;
|
|
83
115
|
if (this.hasBuiltHandle()) {
|
|
@@ -95,11 +127,13 @@ export class Svg extends FlexBox {
|
|
|
95
127
|
|
|
96
128
|
build(): u64 {
|
|
97
129
|
this.buildStyledNode(NodeType.Svg, false);
|
|
130
|
+
this.applyResolvedSizing();
|
|
98
131
|
this.applySvgSource();
|
|
99
132
|
return this.handle;
|
|
100
133
|
}
|
|
101
134
|
|
|
102
135
|
dispose(): void {
|
|
136
|
+
this.detachAssetStateListener();
|
|
103
137
|
this.releaseOwnedSourceAsset();
|
|
104
138
|
super.dispose();
|
|
105
139
|
}
|
|
@@ -132,6 +166,68 @@ export class Svg extends FlexBox {
|
|
|
132
166
|
ui.setSvg(this.handle, this.svgIdValue, this.tintColorValue);
|
|
133
167
|
}
|
|
134
168
|
|
|
169
|
+
private applyResolvedSizing(): void {
|
|
170
|
+
if (!this.hasRequestedWidth && !this.hasRequestedHeight) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const assetWidth = this.assetWidth();
|
|
175
|
+
const assetHeight = this.assetHeight();
|
|
176
|
+
const hasIntrinsicSize = assetWidth > 0.0 && assetHeight > 0.0;
|
|
177
|
+
|
|
178
|
+
if (this.hasRequestedWidth) {
|
|
179
|
+
let resolvedWidthValue = this.requestedWidthValue;
|
|
180
|
+
let resolvedWidthUnit = this.requestedWidthUnit;
|
|
181
|
+
if (this.requestedWidthUnit == Unit.Auto && hasIntrinsicSize) {
|
|
182
|
+
if (this.hasRequestedHeight && this.requestedHeightUnit == Unit.Pixel) {
|
|
183
|
+
resolvedWidthValue = this.requestedHeightValue * (assetWidth / assetHeight);
|
|
184
|
+
} else {
|
|
185
|
+
resolvedWidthValue = assetWidth;
|
|
186
|
+
}
|
|
187
|
+
resolvedWidthUnit = Unit.Pixel;
|
|
188
|
+
}
|
|
189
|
+
super.width(resolvedWidthValue, resolvedWidthUnit);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (this.hasRequestedHeight) {
|
|
193
|
+
let resolvedHeightValue = this.requestedHeightValue;
|
|
194
|
+
let resolvedHeightUnit = this.requestedHeightUnit;
|
|
195
|
+
if (this.requestedHeightUnit == Unit.Auto && hasIntrinsicSize) {
|
|
196
|
+
if (this.hasRequestedWidth && this.requestedWidthUnit == Unit.Pixel) {
|
|
197
|
+
resolvedHeightValue = this.requestedWidthValue * (assetHeight / assetWidth);
|
|
198
|
+
} else {
|
|
199
|
+
resolvedHeightValue = assetHeight;
|
|
200
|
+
}
|
|
201
|
+
resolvedHeightUnit = Unit.Pixel;
|
|
202
|
+
}
|
|
203
|
+
super.height(resolvedHeightValue, resolvedHeightUnit);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private attachAssetStateListener(): void {
|
|
208
|
+
if (this.trackedSvgAssetId == this.svgIdValue) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
this.detachAssetStateListener();
|
|
212
|
+
this.trackedSvgAssetId = this.svgIdValue;
|
|
213
|
+
if (this.svgIdValue == 0) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this.assetStateAction = getSvgAssetState(this.svgIdValue).addAction(
|
|
217
|
+
new HandlerAction<Svg, AssetLoadState>(this, (svg: Svg, _state: AssetLoadState): void => {
|
|
218
|
+
svg.applyResolvedSizing();
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private detachAssetStateListener(): void {
|
|
224
|
+
if (this.assetStateAction !== null) {
|
|
225
|
+
changetype<Action<AssetLoadState>>(this.assetStateAction).dispose();
|
|
226
|
+
this.assetStateAction = null;
|
|
227
|
+
}
|
|
228
|
+
this.trackedSvgAssetId = 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
135
231
|
private releaseOwnedSourceAsset(): void {
|
|
136
232
|
if (this.ownedSvgAssetId == 0) {
|
|
137
233
|
return;
|