forked from facebook/componentkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCKTextComponent.mm
More file actions
115 lines (99 loc) · 3.91 KB
/
Copy pathCKTextComponent.mm
File metadata and controls
115 lines (99 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "CKTextComponent.h"
#import <memory>
#import <vector>
#import <ComponentKit/CKComponentInternal.h>
#import <ComponentKit/CKTextKitRenderer.h>
#import <ComponentKit/CKTextKitRendererCache.h>
#import <ComponentKit/CKInternalHelpers.h>
#import "CKTextComponentView.h"
static CK::TextKit::Renderer::Cache *sharedRendererCache()
{
// This cache is sized arbitrarily
static CK::TextKit::Renderer::Cache *__rendererCache (new CK::TextKit::Renderer::Cache("CKTextComponentRendererCache", 500, 0.2));
return __rendererCache;
}
/**
The concept here is that neither the component nor layout should ever have a strong reference to the renderer object.
This is to reduce memory load when loading thousands and thousands of text components into memory at once. Instead
we maintain a LRU renderer cache that is queried via stack-allocated keys.
*/
static CKTextKitRenderer *rendererForAttributes(CKTextKitAttributes &attributes, CGSize constrainedSize)
{
CK::TextKit::Renderer::Cache *cache = sharedRendererCache();
const CK::TextKit::Renderer::Key key {
attributes,
constrainedSize
};
CKTextKitRenderer *renderer = cache->objectForKey(key);
if (!renderer) {
renderer =
[[CKTextKitRenderer alloc]
initWithTextKitAttributes:attributes
constrainedSize:constrainedSize];
cache->cacheObject(key, renderer, 1);
}
return renderer;
}
@implementation CKTextComponent
{
CKTextKitAttributes _attributes;
CKTextComponentAccessibilityContext _accessibilityContext;
}
+ (instancetype)newWithTextAttributes:(const CKTextKitAttributes &)attributes
viewAttributes:(const CKViewComponentAttributeValueMap &)viewAttributes
accessibilityContext:(const CKTextComponentAccessibilityContext &)accessibilityContext
{
CKTextKitAttributes copyAttributes = attributes.copy();
CKViewComponentAttributeValueMap copiedMap = viewAttributes;
CKTextComponent *c = [super newWithView:{
[CKTextComponentView class],
std::move(copiedMap),
{
.isAccessibilityElement = accessibilityContext.isAccessibilityElement,
.accessibilityIdentifier = accessibilityContext.accessibilityIdentifier,
.accessibilityLabel = accessibilityContext.accessibilityLabel.hasText()
? accessibilityContext.accessibilityLabel : ^{ return copyAttributes.attributedString.string; }
}
} size:{}];
if (c) {
c->_attributes = copyAttributes;
c->_accessibilityContext = accessibilityContext;
}
return c;
}
- (CKComponentLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize
{
const CKTextKitRenderer *renderer = rendererForAttributes(_attributes, constrainedSize.max);
return {
self,
constrainedSize.clamp({
CKCeilPixelValue(renderer.size.width),
CKCeilPixelValue(renderer.size.height)
}),
{}
};
}
- (CK::Component::MountResult)mountInContext:(const CK::Component::MountContext &)context
size:(const CGSize)size
children:(std::shared_ptr<const std::vector<CKComponentLayoutChild>>)children
supercomponent:(CKComponent *)supercomponent
{
CK::Component::MountResult result = [super mountInContext:context
size:size
children:children
supercomponent:supercomponent];
CKTextComponentView *view = (CKTextComponentView *)result.contextForChildren.viewManager->view;
CKTextKitRenderer *renderer = rendererForAttributes(_attributes, size);
view.renderer = renderer;
return result;
}
@end