Skip to content

Commit 1a2221f

Browse files
Add delay multiplier based on length of file.
1 parent 9a29457 commit 1a2221f

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

SCXcodeMinimap/SCMiniMapView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ extern const CGFloat kDefaultZoomLevel;
1919
@property (nonatomic, weak) NSScrollView *editorScrollView;
2020
@property (nonatomic, strong) NSTextView *editorTextView;
2121

22+
@property (nonatomic, readonly) NSInteger lastCalculatedLines;
23+
2224
- (void)updateTextView;
2325
- (void)updateSelectionView;
2426

SCXcodeMinimap/SCMiniMapView.m

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ @interface SCMiniMapView () <NSLayoutManagerDelegate>
1818

1919
@property (nonatomic, strong) NSColor *backgroundColor;
2020
@property (nonatomic, strong) NSFont *font;
21+
@property (nonatomic, readwrite) NSInteger lastCalculatedLines;
2122

2223
@end
2324

@@ -185,29 +186,53 @@ - (void)updateTextView
185186
return;
186187
}
187188

188-
NSMutableAttributedString *mutableAttributedString = [self.editorTextView.textStorage mutableCopy];
189+
typeof(self) __weak weakSelf = self;
190+
NSOperationQueue *background = [[NSOperationQueue alloc] init];
189191

190-
if(mutableAttributedString.length == 0) {
191-
return;
192-
}
193-
194-
__block NSMutableParagraphStyle *style;
195-
196-
[mutableAttributedString enumerateAttribute:NSParagraphStyleAttributeName
197-
inRange:NSMakeRange(0, mutableAttributedString.length)
198-
options:0
199-
usingBlock:^(id value, NSRange range, BOOL *stop) {
200-
style = [value mutableCopy];
201-
*stop = YES;
202-
}];
203-
204-
205-
[style setTabStops:@[]];
206-
[style setDefaultTabInterval:style.defaultTabInterval * kDefaultZoomLevel];
207-
208-
[mutableAttributedString setAttributes:@{NSFontAttributeName: self.font, NSParagraphStyleAttributeName : style} range:NSMakeRange(0, mutableAttributedString.length)];
192+
//Run the attributed strings operation on a background thread
193+
[background addOperationWithBlock:^{
194+
NSMutableAttributedString *mutableAttributedString = [self.editorTextView.textStorage mutableCopy];
195+
196+
if(mutableAttributedString.length == 0) {
197+
//Nothing to do here.
198+
weakSelf.lastCalculatedLines = 0;
199+
return;
200+
}
201+
202+
__block NSMutableParagraphStyle *style;
203+
204+
[mutableAttributedString enumerateAttribute:NSParagraphStyleAttributeName
205+
inRange:NSMakeRange(0, mutableAttributedString.length)
206+
options:0
207+
usingBlock:^(id value, NSRange range, BOOL *stop) {
208+
style = [value mutableCopy];
209+
*stop = YES;
210+
}];
211+
212+
213+
[style setTabStops:@[]];
214+
[style setDefaultTabInterval:style.defaultTabInterval * kDefaultZoomLevel];
215+
216+
[mutableAttributedString setAttributes:@{NSFontAttributeName: weakSelf.font, NSParagraphStyleAttributeName : style} range:NSMakeRange(0, mutableAttributedString.length)];
217+
218+
//Send the text storage update off to the main thread
219+
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
220+
[weakSelf.textView.textStorage setAttributedString:mutableAttributedString];
221+
222+
}];
223+
224+
//Now that we've sent that off, let's see how many lines we have while
225+
//we're in here:
226+
[weakSelf calculateLinesFromString:[mutableAttributedString string]];
227+
}];
228+
}
229+
230+
- (void)calculateLinesFromString:(NSString *)string
231+
{
232+
NSArray *lines = [string componentsSeparatedByString:@"\n"];
209233

210-
[self.textView.textStorage setAttributedString:mutableAttributedString];
234+
//Cache the last calculated lines so we can figure out how long we should take to render this minimap.
235+
self.lastCalculatedLines = lines.count;
211236
}
212237

213238
- (void)resizeWithOldSuperviewSize:(NSSize)oldSize

SCXcodeMinimap/SCXcodeMinimap.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ - (void)onDocumentDidChange:(NSNotification*)sender
9797
{
9898
SCMiniMapView *miniMapView = objc_getAssociatedObject([sender object], &kKeyMiniMapView);
9999

100+
//150 lines per multiplier means a 1500 line file should have a delay of 2.5 seconds.
101+
CGFloat multiplier = ceilf((CGFloat)miniMapView.lastCalculatedLines / 150.0f);
102+
NSTimeInterval updateInterval = (CGFloat)multiplier * kDefaultUpdateInterval;
103+
100104
[NSObject cancelPreviousPerformRequestsWithTarget:miniMapView selector:@selector(updateTextView) object:nil];
101-
[miniMapView performSelector:@selector(updateTextView) withObject:nil afterDelay:kDefaultUpdateInterval];
105+
[miniMapView performSelector:@selector(updateTextView) withObject:nil afterDelay:updateInterval];
102106
}
103107

104108
- (void)onCodeEditorBoundsChange:(NSNotification*)sender

0 commit comments

Comments
 (0)