Skip to content

Commit 725bd0b

Browse files
committed
[app] re-add the background queue link detection thing
1 parent 7c48793 commit 725bd0b

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

NewTerm/Controllers/TerminalController.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class TerminalController: VT100 {
2424
var delegate: TerminalControllerDelegate?
2525

2626
private var updateQueue: DispatchQueue!
27+
private var secondaryUpdateQueue: DispatchQueue!
2728

2829
private var stringSupplier = VT100StringSupplier()
2930

@@ -57,6 +58,7 @@ class TerminalController: VT100 {
5758
super.init()
5859

5960
updateQueue = DispatchQueue(label: String(format: "au.com.hbang.NewTerm.update-queue-%p", self))
61+
secondaryUpdateQueue = DispatchQueue(label: String(format: "au.com.hbang.NewTerm.update-queue-secondary-%p", self))
6062

6163
stringSupplier.screenBuffer = self
6264

@@ -126,6 +128,14 @@ extension TerminalController {
126128

127129
DispatchQueue.main.async {
128130
self.delegate?.refresh(attributedString: attributedString, backgroundColor: backgroundColor)
131+
132+
self.secondaryUpdateQueue.async {
133+
self.stringSupplier.detectLinks(for: attributedString)
134+
135+
DispatchQueue.main.async {
136+
self.delegate?.refresh(attributedString: attributedString, backgroundColor: backgroundColor)
137+
}
138+
}
129139
}
130140
}
131141
}

NewTerm/UI/Keyboard/TerminalTextView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TerminalTextView: UITextView {
1616
backgroundColor = .black
1717
indicatorStyle = .white
1818
showsHorizontalScrollIndicator = false
19-
dataDetectorTypes = .link
19+
dataDetectorTypes = [] // none
2020
isEditable = false
2121
textContainerInset = .zero
2222
self.textContainer.lineFragmentPadding = 0

NewTerm/VT100/VT100StringSupplier.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
- (NSString *)stringForLine:(int)rowIndex;
1717
- (NSMutableAttributedString *)attributedString;
1818

19+
- (void)detectLinksForAttributedString:(NSMutableAttributedString *)attributedString NS_SWIFT_NAME(detectLinks(for:));
20+
1921
@end

NewTerm/VT100/VT100StringSupplier.m

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,17 @@ - (NSMutableAttributedString *)attributedString {
151151
startOffset += width;
152152
}
153153

154-
// if links are supported, create links in all the locations we found last time we scanned for
155-
// links
156-
if (IS_IOS_OR_NEWER(iOS_7_0)) {
157-
for (NSValue *value in _lastLinkRanges) {
158-
NSRange range = value.rangeValue;
154+
// create links in all the locations we found last time we scanned for links
155+
for (NSValue *value in _lastLinkRanges) {
156+
NSRange range = value.rangeValue;
159157

160-
if (range.location + range.length <= attributedString.string.length) {
161-
NSString *urlString = [attributedString.string substringWithRange:range];
162-
NSURL *url = [NSURL URLWithString:urlString];
158+
if (range.location + range.length <= attributedString.string.length) {
159+
NSString *urlString = [attributedString.string substringWithRange:range];
160+
NSURL *url = [NSURL URLWithString:urlString];
163161

164-
// if NSURL thinks this is a valid url,
165-
if (url) {
166-
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
167-
}
162+
// if NSURL thinks this is a valid url, it’s good enough for us
163+
if (url) {
164+
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
168165
}
169166
}
170167
}
@@ -173,16 +170,11 @@ - (NSMutableAttributedString *)attributedString {
173170
}
174171

175172
- (void)detectLinksForAttributedString:(NSMutableAttributedString *)attributedString {
176-
// links are only natively supported as of iOS 7, i probably won’t bother to add all the support
177-
// needed for links on iOS 6
178-
if (!IS_IOS_OR_NEWER(iOS_7_0)) {
179-
return;
180-
}
181-
182173
static dispatch_once_t onceToken;
183174
dispatch_once(&onceToken, ^{
184175
_lastLinkRanges = [NSMutableSet set];
185176

177+
// not exactly sure why a data detector would fail to init…
186178
NSError *error = nil;
187179
_linkDataDetector = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:&error];
188180
NSAssert(!error, @"%@", error.description);
@@ -224,7 +216,6 @@ - (void)detectLinksForAttributedString:(NSMutableAttributedString *)attributedSt
224216
if (![_lastLinkRanges containsObject:value]) {
225217
NSRange range = value.rangeValue;
226218
NSURL *url = [NSURL URLWithString:[attributedString.string substringWithRange:range]];
227-
HBLogDebug(@"adding2 %@ = %@", url, NSStringFromRange(range));
228219
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
229220
}
230221
}

0 commit comments

Comments
 (0)