@g9tm/lynxcontrols-native 2.5.6 → 2.5.8
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/README.md +6 -1
- package/android/build.gradle.kts +11 -2
- package/android/src/main/java/com/g9lynx/g9native/G9Bridge.kt +47 -2
- package/android/src/main/java/com/g9lynx/g9native/G9NativeImages.kt +24 -0
- package/android/src/main/java/com/g9lynx/g9native/blurhash/G9BlurhashUI.kt +76 -0
- package/android/src/main/java/com/g9lynx/g9native/blurhash/core/G9Blurhash.kt +100 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasModel.kt +94 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasPaths.kt +192 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasUI.kt +162 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasView.kt +1053 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9PictureSpan.kt +99 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichEditText.kt +458 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichStyle.kt +343 -0
- package/android/src/main/java/com/g9lynx/g9native/richtext/G9RichTextUI.kt +94 -0
- package/catalog/catalog.json +2 -2
- package/catalog/index.md +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/media.d.ts +1 -1
- package/dist/media.d.ts.map +1 -1
- package/dist/services.d.ts +8 -0
- package/dist/services.d.ts.map +1 -1
- package/dist/services.js +4 -2
- package/dist/services.js.map +1 -1
- package/ios/g9lynx-native-swift.podspec +20 -0
- package/ios/g9lynx-native.podspec +6 -2
- package/ios/src/G9LCBlurhashUI.h +14 -0
- package/ios/src/G9LCBlurhashUI.m +28 -0
- package/ios/src/G9LCCanvasUI.h +18 -0
- package/ios/src/G9LCCanvasUI.m +81 -0
- package/ios/src/G9LCRichTextUI.h +16 -0
- package/ios/src/G9LCRichTextUI.m +71 -0
- package/ios/src/{G9VideoUI.h → G9LCVideoUI.h} +5 -5
- package/ios/src/{G9VideoUI.m → G9LCVideoUI.m} +9 -8
- package/ios/src/G9PointerEventsHitTest.h +5 -1
- package/ios/src/G9PointerEventsHitTest.m +6 -0
- package/ios/swift/blurhash/G9LCBlurhashView.swift +165 -0
- package/ios/swift/canvas/G9LCCanvasCore.swift +848 -0
- package/ios/swift/canvas/G9LCCanvasView.swift +482 -0
- package/ios/swift/richtext/G9LCRichStyle.swift +238 -0
- package/ios/swift/richtext/G9LCRichTextView.swift +647 -0
- package/ios/swift/shared/G9LCColors.swift +78 -0
- package/ios/swift/shared/G9LCFonts.swift +38 -0
- package/ios/swift/shared/G9LCImages.swift +28 -0
- package/ios/swift/shared/G9LCJson.swift +73 -0
- package/package.json +6 -4
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#import "G9LCRichTextUI.h"
|
|
2
|
+
#import <Lynx/LynxEvent.h>
|
|
3
|
+
#import <Lynx/LynxEventEmitter.h>
|
|
4
|
+
#import <Lynx/LynxPropsProcessor.h>
|
|
5
|
+
#import <Lynx/LynxUIContext.h>
|
|
6
|
+
#import <Lynx/LynxUIMethodProcessor.h>
|
|
7
|
+
|
|
8
|
+
/// The Swift view (`@objc(G9LCRichTextView)` in richtext/G9LCRichTextView.swift), reached through the Objective-C
|
|
9
|
+
/// runtime: the class stays internal to the pod, so no generated Swift header is needed.
|
|
10
|
+
@protocol G9LCRichTextViewAPI <NSObject>
|
|
11
|
+
@property(nonatomic, copy, nullable) void (^emitter)(NSString *name, NSDictionary<NSString *, id> *detail);
|
|
12
|
+
- (void)applyEditable:(BOOL)value;
|
|
13
|
+
- (void)applyPlaceholder:(nullable NSString *)value;
|
|
14
|
+
- (void)applyPalette:(nullable NSString *)json;
|
|
15
|
+
- (void)applyPageDir:(nullable NSString *)value;
|
|
16
|
+
- (void)applyBottomInset:(double)value;
|
|
17
|
+
- (void)applyMessage:(nullable NSString *)json;
|
|
18
|
+
@end
|
|
19
|
+
|
|
20
|
+
typedef UIView<G9LCRichTextViewAPI> G9LCRichTextViewRef;
|
|
21
|
+
|
|
22
|
+
/// The `json` string of a UI method's params, or nil.
|
|
23
|
+
static NSString *_Nullable G9LCJsonParam(NSDictionary *params) {
|
|
24
|
+
id json = params[@"json"];
|
|
25
|
+
return [json isKindOfClass:[NSString class]] ? json : nil;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@implementation G9LCRichTextUI
|
|
29
|
+
|
|
30
|
+
- (UIView *)createView {
|
|
31
|
+
Class cls = NSClassFromString(@"G9LCRichTextView");
|
|
32
|
+
G9LCRichTextViewRef *view = (G9LCRichTextViewRef *)[[cls alloc] initWithFrame:CGRectZero];
|
|
33
|
+
__weak G9LCRichTextUI *weakSelf = self;
|
|
34
|
+
view.emitter = ^(NSString *name, NSDictionary<NSString *, id> *detail) {
|
|
35
|
+
G9LCRichTextUI *strongSelf = weakSelf;
|
|
36
|
+
if (strongSelf == nil) return;
|
|
37
|
+
LynxDetailEvent *event = [[LynxDetailEvent alloc] initWithName:name
|
|
38
|
+
targetSign:strongSelf.sign
|
|
39
|
+
detail:[detail mutableCopy]];
|
|
40
|
+
[strongSelf.context.eventEmitter dispatchCustomEvent:event];
|
|
41
|
+
};
|
|
42
|
+
return view;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
LYNX_PROP_SETTER("editable", setEditable, BOOL) {
|
|
46
|
+
[(G9LCRichTextViewRef *)self.view applyEditable:requestReset ? NO : value];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
LYNX_PROP_SETTER("placeholder", setPlaceholder, NSString *) {
|
|
50
|
+
[(G9LCRichTextViewRef *)self.view applyPlaceholder:requestReset ? nil : value];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
LYNX_PROP_SETTER("palette", setPalette, NSString *) {
|
|
54
|
+
[(G9LCRichTextViewRef *)self.view applyPalette:requestReset ? nil : value];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
LYNX_PROP_SETTER("page-dir", setPageDir, NSString *) {
|
|
58
|
+
[(G9LCRichTextViewRef *)self.view applyPageDir:requestReset ? nil : value];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
LYNX_PROP_SETTER("bottom-inset", setBottomInset, CGFloat) {
|
|
62
|
+
[(G9LCRichTextViewRef *)self.view applyBottomInset:requestReset ? 0 : value];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// The app's message: `invoke({ method: 'apply', params: { json } })`.
|
|
66
|
+
LYNX_UI_METHOD(apply) {
|
|
67
|
+
[(G9LCRichTextViewRef *)self.view applyMessage:G9LCJsonParam(params)];
|
|
68
|
+
callback(kUIMethodSuccess, nil);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@end
|
|
@@ -4,18 +4,18 @@
|
|
|
4
4
|
NS_ASSUME_NONNULL_BEGIN
|
|
5
5
|
|
|
6
6
|
/** The element's view: its backing layer is an `AVPlayerLayer`. */
|
|
7
|
-
@interface
|
|
7
|
+
@interface G9LCVideoView : UIView
|
|
8
8
|
@end
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* The `<g9-video>` element on iOS (plan/specs/06 §7), the same contract as Android's `
|
|
11
|
+
* The `<g9-video>` element on iOS (plan/specs/06 §7), the same contract as Android's `G9LCVideoUI.kt`: muted looping
|
|
12
12
|
* playback (`AVQueuePlayer` + `AVPlayerLooper`) for the intro carousel. Props: `src` (`file://…`, `http(s)://…`),
|
|
13
13
|
* `paused` (default YES), `loop`, `muted` (default YES), `start-seconds`, `near-end-seconds`, `object-fit`
|
|
14
14
|
* (`cover` | `contain`). Events: `ready` (first frame displayable), `nearend` `{ seconds }`, `looped`, `error`
|
|
15
|
-
* `{ code, message }`.
|
|
16
|
-
* `[config registerUI:
|
|
15
|
+
* `{ code, message }`. The host registers it on its `LynxConfig` (2.5.7: no longer process-wide, and prefixed `G9LC`
|
|
16
|
+
* so an app's own `G9VideoUI` does not clash): `[config registerUI:G9LCVideoUI.class withName:@"g9-video"]`.
|
|
17
17
|
*/
|
|
18
|
-
@interface
|
|
18
|
+
@interface G9LCVideoUI : LynxUI <G9LCVideoView *>
|
|
19
19
|
@end
|
|
20
20
|
|
|
21
21
|
NS_ASSUME_NONNULL_END
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#import "
|
|
1
|
+
#import "G9LCVideoUI.h"
|
|
2
2
|
#import <AVFoundation/AVFoundation.h>
|
|
3
3
|
#import <Lynx/LynxComponentRegistry.h>
|
|
4
4
|
#import <Lynx/LynxEvent.h>
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
static void *G9VideoKVO = &G9VideoKVO;
|
|
12
12
|
|
|
13
|
-
@implementation
|
|
13
|
+
@implementation G9LCVideoView
|
|
14
14
|
|
|
15
15
|
+ (Class)layerClass {
|
|
16
16
|
return AVPlayerLayer.class;
|
|
@@ -22,7 +22,7 @@ static void *G9VideoKVO = &G9VideoKVO;
|
|
|
22
22
|
|
|
23
23
|
@end
|
|
24
24
|
|
|
25
|
-
@implementation
|
|
25
|
+
@implementation G9LCVideoUI {
|
|
26
26
|
AVQueuePlayer *_player;
|
|
27
27
|
AVPlayerLooper *_looper;
|
|
28
28
|
id _timeObserver;
|
|
@@ -42,7 +42,8 @@ static void *G9VideoKVO = &G9VideoKVO;
|
|
|
42
42
|
G9VideoLoopTracker *_tracker;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
// Not self-registered (2.5.7, G9Hub): a process-wide `g9-video` would take the name from an app that has its own
|
|
46
|
+
// element. The host registers it on each LynxConfig: `[config registerUI:G9LCVideoUI.class withName:@"g9-video"]`.
|
|
46
47
|
|
|
47
48
|
- (instancetype)init {
|
|
48
49
|
if ((self = [super init])) {
|
|
@@ -55,8 +56,8 @@ LYNX_REGISTER_UI("g9-video")
|
|
|
55
56
|
return self;
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
- (
|
|
59
|
-
|
|
59
|
+
- (G9LCVideoView *)createView {
|
|
60
|
+
G9LCVideoView *view = [[G9LCVideoView alloc] init];
|
|
60
61
|
view.clipsToBounds = YES;
|
|
61
62
|
view.userInteractionEnabled = NO;
|
|
62
63
|
((AVPlayerLayer *)view.layer).videoGravity = AVLayerVideoGravityResizeAspectFill;
|
|
@@ -100,7 +101,7 @@ LYNX_PROP_SETTER("object-fit", setObjectFit, NSString *) {
|
|
|
100
101
|
- (void)scheduleApply {
|
|
101
102
|
if (_applyScheduled) return;
|
|
102
103
|
_applyScheduled = YES;
|
|
103
|
-
__weak
|
|
104
|
+
__weak G9LCVideoUI *weakSelf = self;
|
|
104
105
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
105
106
|
[weakSelf apply];
|
|
106
107
|
});
|
|
@@ -150,7 +151,7 @@ LYNX_PROP_SETTER("object-fit", setObjectFit, NSString *) {
|
|
|
150
151
|
AVPlayerLayer *layer = (AVPlayerLayer *)self.view.layer;
|
|
151
152
|
layer.player = _player;
|
|
152
153
|
[layer addObserver:self forKeyPath:@"readyForDisplay" options:NSKeyValueObservingOptionNew context:G9VideoKVO];
|
|
153
|
-
__weak
|
|
154
|
+
__weak G9LCVideoUI *weakSelf = self;
|
|
154
155
|
_timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, 600)
|
|
155
156
|
queue:dispatch_get_main_queue()
|
|
156
157
|
usingBlock:^(CMTime time) {
|
|
@@ -14,7 +14,11 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
14
14
|
*/
|
|
15
15
|
@interface G9PointerEventsHitTest : NSObject
|
|
16
16
|
|
|
17
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* Whether the hit test is patched: false when Lynx no longer has `UILynxView`, or when the app's Info.plist sets
|
|
19
|
+
* `G9LCPointerEventsHitTest` to NO. The patch is process-wide: it replaces `-[UILynxView hitTest:withEvent:]` at load,
|
|
20
|
+
* so it covers every LynxView of the app, the library's or not.
|
|
21
|
+
*/
|
|
18
22
|
+ (BOOL)installed;
|
|
19
23
|
|
|
20
24
|
@end
|
|
@@ -29,6 +29,12 @@ static UIView *_Nullable G9HitTest(UIView *self, SEL cmd, CGPoint point, UIEvent
|
|
|
29
29
|
static BOOL gInstalled;
|
|
30
30
|
|
|
31
31
|
+ (void)load {
|
|
32
|
+
// A host that does its own hit testing turns it off in Info.plist: `G9LCPointerEventsHitTest` = NO (2.5.7).
|
|
33
|
+
id flag = [NSBundle.mainBundle objectForInfoDictionaryKey:@"G9LCPointerEventsHitTest"];
|
|
34
|
+
if ([flag respondsToSelector:@selector(boolValue)] && ![flag boolValue]) {
|
|
35
|
+
NSLog(@"G9PointerEventsHitTest: off (Info.plist G9LCPointerEventsHitTest = NO)");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
32
38
|
// `UILynxView` is private to Lynx (LynxUIView.m), hence the lookup by name.
|
|
33
39
|
Class cls = NSClassFromString(@"UILynxView");
|
|
34
40
|
Method m = cls ? class_getInstanceMethod(cls, @selector(hitTest:withEvent:)) : NULL;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import UIKit
|
|
2
|
+
|
|
3
|
+
/// BlurHash decoding (https://blurha.sh, the reference algorithm; Android's G9Blurhash; 2.5.8, from G9Hub): a hash → RGBA pixels.
|
|
4
|
+
enum G9LCBlurhash {
|
|
5
|
+
private static let chars = Array("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~")
|
|
6
|
+
private static let index: [Character: Int] = {
|
|
7
|
+
var out: [Character: Int] = [:]
|
|
8
|
+
for (i, c) in chars.enumerated() { out[c] = i }
|
|
9
|
+
return out
|
|
10
|
+
}()
|
|
11
|
+
|
|
12
|
+
/// Well-formed: every character in the alphabet, the length matching the component counts.
|
|
13
|
+
static func isValid(_ hash: String?) -> Bool {
|
|
14
|
+
guard let hash = hash, hash.count >= 6 else { return false }
|
|
15
|
+
let text = Array(hash)
|
|
16
|
+
guard text.allSatisfy({ index[$0] != nil }) else { return false }
|
|
17
|
+
let size = decode83(text, 0, 1)
|
|
18
|
+
let numY = size / 9 + 1
|
|
19
|
+
let numX = size % 9 + 1
|
|
20
|
+
return text.count == 4 + 2 * numX * numY
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// `width`×`height` RGBA pixels (alpha 255), or nil for an invalid hash. `punch` > 1 strengthens the colours.
|
|
24
|
+
static func decode(_ hash: String?, width: Int, height: Int, punch: Double = 1) -> [UInt8]? {
|
|
25
|
+
guard let hash = hash, isValid(hash), width > 0, height > 0 else { return nil }
|
|
26
|
+
let text = Array(hash)
|
|
27
|
+
let size = decode83(text, 0, 1)
|
|
28
|
+
let numY = size / 9 + 1
|
|
29
|
+
let numX = size % 9 + 1
|
|
30
|
+
let maxValue = Double(decode83(text, 1, 2) + 1) / 166 * punch
|
|
31
|
+
var colors: [[Double]] = []
|
|
32
|
+
for i in 0..<(numX * numY) {
|
|
33
|
+
colors.append(i == 0 ? decodeDc(decode83(text, 2, 6)) : decodeAc(decode83(text, 4 + i * 2, 6 + i * 2), maxValue))
|
|
34
|
+
}
|
|
35
|
+
var pixels = [UInt8](repeating: 255, count: width * height * 4)
|
|
36
|
+
for y in 0..<height {
|
|
37
|
+
for x in 0..<width {
|
|
38
|
+
var r = 0.0, g = 0.0, b = 0.0
|
|
39
|
+
for j in 0..<numY {
|
|
40
|
+
let cy = cos(Double.pi * Double(y) * Double(j) / Double(height))
|
|
41
|
+
for i in 0..<numX {
|
|
42
|
+
let basis = cos(Double.pi * Double(x) * Double(i) / Double(width)) * cy
|
|
43
|
+
let color = colors[i + j * numX]
|
|
44
|
+
r += color[0] * basis
|
|
45
|
+
g += color[1] * basis
|
|
46
|
+
b += color[2] * basis
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let p = (x + y * width) * 4
|
|
50
|
+
pixels[p] = linearToSrgb(r)
|
|
51
|
+
pixels[p + 1] = linearToSrgb(g)
|
|
52
|
+
pixels[p + 2] = linearToSrgb(b)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return pixels
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private static func decode83(_ text: [Character], _ from: Int, _ to: Int) -> Int {
|
|
59
|
+
var value = 0
|
|
60
|
+
for i in from..<to { value = value * 83 + (index[text[i]] ?? 0) }
|
|
61
|
+
return value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private static func decodeDc(_ value: Int) -> [Double] {
|
|
65
|
+
[srgbToLinear(value >> 16), srgbToLinear((value >> 8) & 255), srgbToLinear(value & 255)]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private static func decodeAc(_ value: Int, _ maxValue: Double) -> [Double] {
|
|
69
|
+
let r = value / (19 * 19)
|
|
70
|
+
let g = (value / 19) % 19
|
|
71
|
+
let b = value % 19
|
|
72
|
+
return [signPow(Double(r - 9) / 9) * maxValue, signPow(Double(g - 9) / 9) * maxValue, signPow(Double(b - 9) / 9) * maxValue]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private static func signPow(_ value: Double) -> Double {
|
|
76
|
+
let magnitude = value * value
|
|
77
|
+
return value < 0 ? -magnitude : magnitude
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private static func srgbToLinear(_ value: Int) -> Double {
|
|
81
|
+
let v = Double(value) / 255
|
|
82
|
+
return v <= 0.04045 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private static func linearToSrgb(_ value: Double) -> UInt8 {
|
|
86
|
+
let v = min(max(value, 0), 1)
|
|
87
|
+
let out = v <= 0.0031308 ? v * 12.92 * 255 + 0.5 : (1.055 * pow(v, 1 / 2.4) - 0.055) * 255 + 0.5
|
|
88
|
+
return UInt8(min(max(Int(out), 0), 255))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// A UIImage of the decode (32×32, stretched smoothly by the view), cached by hash and punch.
|
|
92
|
+
static func image(_ hash: String, punch: Double) -> UIImage? {
|
|
93
|
+
let key = "\(hash)|\(punch)" as NSString
|
|
94
|
+
if let cached = cache.object(forKey: key) { return cached }
|
|
95
|
+
let side = 32
|
|
96
|
+
guard let pixels = decode(hash, width: side, height: side, punch: punch) else { return nil }
|
|
97
|
+
let data = Data(pixels) as CFData
|
|
98
|
+
guard let provider = CGDataProvider(data: data),
|
|
99
|
+
let cg = CGImage(width: side, height: side, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: side * 4,
|
|
100
|
+
space: CGColorSpaceCreateDeviceRGB(),
|
|
101
|
+
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipLast.rawValue),
|
|
102
|
+
provider: provider, decode: nil, shouldInterpolate: true, intent: .defaultIntent) else { return nil }
|
|
103
|
+
let image = UIImage(cgImage: cg)
|
|
104
|
+
cache.setObject(image, forKey: key)
|
|
105
|
+
return image
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private static let cache: NSCache<NSString, UIImage> = {
|
|
109
|
+
let cache = NSCache<NSString, UIImage>()
|
|
110
|
+
cache.countLimit = 256
|
|
111
|
+
return cache
|
|
112
|
+
}()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// `<g9-blurhash hash punch>` (G9LCBlurhashUI): an image placeholder decoded natively — a 32×32 decode stretched with
|
|
116
|
+
/// filtering over the element's box. Decoded off the main thread; an invalid or empty hash draws nothing.
|
|
117
|
+
@objc(G9LCBlurhashView)
|
|
118
|
+
final class G9LCBlurhashView: UIImageView {
|
|
119
|
+
private var hashText: String?
|
|
120
|
+
private var punch: Double = 1
|
|
121
|
+
private var generation = 0
|
|
122
|
+
|
|
123
|
+
override init(frame: CGRect) {
|
|
124
|
+
super.init(frame: frame)
|
|
125
|
+
contentMode = .scaleToFill
|
|
126
|
+
layer.minificationFilter = .linear
|
|
127
|
+
layer.magnificationFilter = .linear
|
|
128
|
+
isAccessibilityElement = false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
required init?(coder: NSCoder) {
|
|
132
|
+
fatalError("init(coder:) is not used")
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// The `hash` attribute (named apart from NSObject's `hash`).
|
|
136
|
+
@objc func applyHash(_ value: String?) {
|
|
137
|
+
guard value != hashText else { return }
|
|
138
|
+
hashText = value
|
|
139
|
+
redraw()
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@objc func applyPunch(_ value: Double) {
|
|
143
|
+
let next = value > 0 ? value : 1
|
|
144
|
+
guard next != punch else { return }
|
|
145
|
+
punch = next
|
|
146
|
+
redraw()
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private func redraw() {
|
|
150
|
+
generation += 1
|
|
151
|
+
let mine = generation
|
|
152
|
+
guard let hash = hashText, !hash.isEmpty else {
|
|
153
|
+
image = nil
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
let punch = self.punch
|
|
157
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
158
|
+
let decoded = G9LCBlurhash.image(hash, punch: punch)
|
|
159
|
+
DispatchQueue.main.async { [weak self] in
|
|
160
|
+
guard let self = self, self.generation == mine else { return }
|
|
161
|
+
self.image = decoded
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|