Repository navigation
Expand file tree
/
Copy pathMinimapView.m
More file actions
454 lines (395 loc) · 14.1 KB
/
Copy pathMinimapView.m
File metadata and controls
454 lines (395 loc) · 14.1 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
// MinimapView.m
// TextmateMinimap
//
// Created by Julian Eberius on 09.02.10.
// Copyright 2010 Julian Eberius. All rights reserved.
//
#import "MinimapView.h"
#import "NSView+Minimap.h"
#import "NSWindowController+Minimap.h"
#import "TextMate.h"
#import "BackgroundUpdater.h"
#import "TextmateMinimap.h"
#import "AsyncDrawOperation.h"
#import "AsyncDrawOperation.h"
int const scaleDownThreshold = 5;
int const scaleDownTo = 5;
@interface MinimapView (Private_MinimapView)
- (void)updateViewableRange;
- (void)drawVisRect:(NSRect)rect;
- (void)updateVisiblePartOfTextView;
- (NSColor*)currentBackgroundColor;
- (void)fillWithBackground;
//- (void)firstRefresh;
@end
// stuff implemented by the TextMate textview
@interface NSView (MM_NSView_OnlyByOakTextView)
- (unsigned int)lineHeight;
- (id)currentStyleSheet;
@end
@implementation MinimapView
@synthesize windowController, textView, theImage, timer,
viewableRangeScaling, dirty, minimapLinesStart, gutterSize, visiblePartOfTextView, updater;
//@synthesize drawLock;
#pragma mark init
- (id)initWithTextView:(NSView*) tv andWindowController:(NSWindowController*) controller;
{
self = [super init];
if (self) {
queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
gutterSize = -1;
visRectPosBeforeScrolling = -1;
firstDraw = YES;
[self setViewableRangeScaling:1.0];
//drawLock = [[NSLock alloc] init];
textView = tv;
[self setUpdater: [[[BackgroundUpdater alloc] initWithMinimapView:self andOperationQueue:queue] autorelease]];
[self setWindowController:controller];
}
return self;
}
- (void)dealloc
{
[queue cancelAllOperations];
[theImage release];
theImage = nil;
[self setUpdater:nil];
[queue release];
// [drawLock release];
queue = nil;
[super dealloc];
}
#pragma mark drawing routines
- (void)drawRect:(NSRect)rect
{
if (firstDraw) {
[windowController updateTrailingSpace];
firstDraw = NO;
theImage = [[textView emptySnapshotImageFor:self] retain];
[self fillWithBackground];
[self setNeedsDisplay:NO];
//Defer first drawing by small interval ... don't know a better way to wait till the tv is fully initialized
if (firstDrawTimer != nil && [firstDrawTimer isValid]) {
[firstDrawTimer invalidate];
firstDrawTimer = nil;
}
firstDrawTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(firstRefresh) userInfo:nil repeats:NO];
return;
}
int numLines = [self numberOfLines];
float ppl = [self bounds].size.height / numLines;
float scaleUpThreshold = [[NSUserDefaults standardUserDefaults] floatForKey:@"Minimap_scaleUpThreshold"];
NSRect drawTo = [self bounds];
if (requestRedraw)
{
AsyncDrawOperation* op;
if (ppl < scaleUpThreshold)
{
minimapIsScrollable = YES;
[self updateVisiblePartOfTextView];
op = [[AsyncDrawOperation alloc] initWithMinimapView:self andMode:MM_PARTIAL_IMAGE];
[op setPartToDraw:visiblePartOfTextView];
requestRedraw = NO;
} else {
minimapIsScrollable = NO;
op = [[AsyncDrawOperation alloc] initWithMinimapView:self andMode:MM_COMPLETE_IMAGE];
}
[queue cancelAllOperations];
[queue addOperation:op];
//NSLog(@"adding op!");
[op release];
requestRedraw = NO;
return;
}
if (ppl < scaleUpThreshold)
{
NSRect rectToSnapshot = visiblePartOfTextView;
[theImage drawInRect:drawTo fromRect:rectToSnapshot operation:NSCompositeSourceOver fraction:1.0];
}
else
{
if (ppl > scaleDownThreshold) {
drawTo.size.height = numLines*scaleDownTo;
[[self currentBackgroundColor] set];
NSRectFill(NSMakeRect(drawTo.origin.x, drawTo.size.height,
drawTo.size.width, [self bounds].size.height - drawTo.size.height));
}
[self setMinimapLinesStart:0];
[self setViewableRangeScaling:1.0];
[theImage drawInRect:drawTo fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
}
drawnRect = drawTo;
[self drawVisRect:drawTo];
}
- (void)updateVisiblePartOfTextView
{
NSRect bounds = [self visibleRect];
int numLines = [self numberOfLines];
NSSize imgSize = [theImage size];
NSRect tvBounds = [textView bounds];
int scaleUpTo = [[NSUserDefaults standardUserDefaults] integerForKey:@"Minimap_scaleUpTo"];
float visiblePercentage = bounds.size.height / (numLines*scaleUpTo);
[self setViewableRangeScaling:(1/visiblePercentage)];
NSScrollView* sv = (NSScrollView*)[[textView superview] superview];
float percentage = [[sv verticalScroller] floatValue];
float scaleFactor = [theImage size].height / tvBounds.size.height;
float middle = (percentage*(tvBounds.size.height-[textView visibleRect].size.height) + [textView visibleRect].size.height/2);
float mysteriousHeight = ((visiblePercentage * tvBounds.size.height) - [textView visibleRect].size.height);
float begin = middle - (percentage*mysteriousHeight) - ([textView visibleRect].size.height/2);
NSRect visRect = NSMakeRect(0,
begin*scaleFactor,
[theImage size].width,
(visiblePercentage * imgSize.height));
[self setMinimapLinesStart:(begin/tvBounds.size.height)*numLines];
visiblePartOfTextView = visRect;
}
- (void)updateViewableRange
{
NSScrollView* sv = (NSScrollView*)[[textView superview] superview];
float proportion = [[sv verticalScroller] knobProportion];
float percentage = [[sv verticalScroller] floatValue];
int numLines = [self numberOfLines];
int numVisLines = round(numLines * proportion);
int middleLine = round(percentage*(numLines-numVisLines)+numVisLines/2);
NSRange range = NSMakeRange(round(middleLine-(numVisLines/2)), numVisLines);
if (range.location+range.length > [self numberOfLines])
{
range.location = [self numberOfLines]-range.length;
}
// find out whether a vertical scroller is displayed... strangely [sv hasVeticalScroller] always returns YES
NSClipView* clipView = (NSClipView*)[[sv subviews] objectAtIndex:0];
if ([clipView documentVisibleRect].size.height == [textView bounds].size.height)
{
range.location = 0;
range.length = numLines;
}
viewableRange = range;
}
- (void)drawVisRect:(NSRect)drawTo
{
// drawing the vis rect
[self updateViewableRange];
NSRect bounds = [self bounds];
float visRectHeight = viewableRange.length * pixelPerLine;
float visRectPos;
if ([self viewableRangeScaling] != 1.0)
{
float rectProportion = visRectHeight / bounds.size.height;
NSScrollView* sv = (NSScrollView*)[[textView superview] superview];
float percentage = [[sv verticalScroller] floatValue];
float middle = percentage*(bounds.size.height-visRectHeight) + visRectHeight/2;
float mysteriousHeight = (rectProportion * bounds.size.height) - visRectHeight;
visRectPos = middle - (percentage*mysteriousHeight) - (visRectHeight/2);
if (visRectPosBeforeScrolling != -1) {
float scrolledPixels = (visiblePartOfTextView.origin.y - visRectPosBeforeScrolling);
visRectPos -= scrolledPixels*(bounds.size.height/visiblePartOfTextView.size.height);
}
}
else
{
visRectPos = (viewableRange.location*pixelPerLine);
}
float drawToScaling = drawTo.size.height / bounds.size.height;
NSRect visibleHighlightRect = NSMakeRect(0,
visRectPos*drawToScaling,
drawTo.size.width-1,
visRectHeight*drawToScaling);
[NSGraphicsContext saveGraphicsState];
[[NSColor colorWithCalibratedRed:0.549 green:0.756 blue:1 alpha:0.9] set];
[NSBezierPath setDefaultLineWidth:1];
[NSBezierPath strokeRect:visibleHighlightRect];
[NSGraphicsContext restoreGraphicsState];
}
#pragma mark overridden-methods
- (void)mouseUp:(NSEvent *)theEvent
{
NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
unsigned int absoluteLineIdx = [self absoluteLineIdxFromPoint:mouseLoc];
if ([windowController isSoftWrapEnabled]) {
// we use slightly "weaker" mode: scroll by percentage then centerCaretInDisplay...
// can not correctly select the first and last few lines of the document, because
// it always selects the center line... but will always scroll correctly at least
float y = absoluteLineIdx * [textView lineHeight];
float percentage = y / [textView bounds].size.height;
[windowController scrollToYPercentage:percentage];
} else {
// will select all lines correctly, but does not work in "soft wrap" mode, because
// "absoluteLineIdx" will not really be the absolute line (wrapping can not be
// taken into account in the calculation)
[windowController scrollToLine:absoluteLineIdx];
}
[self refresh];
}
- (void)scrollWheel:(NSEvent *)theEvent
{
if (minimapIsScrollable) {
NSRect tvBounds = [textView bounds];
float scaleFactor = [theImage size].height / tvBounds.size.height;
NSRect newVisRect = visiblePartOfTextView;
if (visRectPosBeforeScrolling == -1)
visRectPosBeforeScrolling = visiblePartOfTextView.origin.y;
float newBegin = (newVisRect.origin.y / scaleFactor) + ([theEvent deltaY]/scaleFactor)*(-5);
if (newBegin < 0)
newBegin = 0;
float lowerBound = tvBounds.size.height-(newVisRect.size.height/scaleFactor);
if (newBegin > lowerBound)
newBegin = lowerBound;
newVisRect.origin.y = newBegin*scaleFactor;
[self setMinimapLinesStart:(newBegin/tvBounds.size.height)*[self numberOfLines]];
visiblePartOfTextView = newVisRect;
[self setNeedsDisplay:YES];
}
}
- (void) viewDidEndLiveResize
{
[self refresh];
[self setDirty:YES];
}
- (BOOL)isFlipped
{
return YES;
}
- (BOOL)isOpaque
{
return YES;
}
#pragma mark public API
/*
Repaint, and also request a redraw of theImage
*/
- (void)refresh {
requestRedraw = YES;
visRectPosBeforeScrolling = -1;
[self setNeedsDisplayInRect:[self visibleRect]];
}
/*
Repaints the existing Image to the minimap-view(no drawing operation is scheduled)
*/
- (void)repaint
{
[self setNeedsDisplayInRect:[self visibleRect]];
}
/*
Called when the minimap is initialized for a document, similar to refresh, more initialization
*/
- (void)firstRefresh {
firstDrawTimer = nil;
requestRedraw = YES;
visRectPosBeforeScrolling = -1;
[self setDirty:YES];
[self updateGutterSize];
[[self updater] setDirtyExceptForVisiblePart];
[self setNeedsDisplayInRect:[self visibleRect]];
}
/*
Called whenever the textview scrolls. Scrolls the minimap using the existing image
of the whole document. no redrawing -> fluent scrolling
*/
- (void)reactToScrollingTextView {
[self updateVisiblePartOfTextView];
visRectPosBeforeScrolling = -1;
[self setNeedsDisplayInRect:[self visibleRect]];
if (minimapIsScrollable) {
NSTimer* old_timer = [self timer];
if (old_timer != NULL && [old_timer isValid]) {
[old_timer invalidate];
}
// do not refresh instantly, wait until scrolling finished
NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(refresh) userInfo:nil repeats:NO];
[self setTimer:t];
}
}
- (void)setNewDocument
{
[queue setSuspended:NO];
[queue cancelAllOperations];
[theImage release];
theImage = NULL;
firstDraw = YES;
if (firstDrawTimer != nil && [firstDrawTimer isValid]) {
[firstDrawTimer invalidate];
firstDrawTimer = nil;
}
}
- (void)fillWithBackground
{
[theImage lockFocus];
[[self currentBackgroundColor] set];
NSRectFill(NSMakeRect(0, 0, [theImage size].width, [theImage size].height));
[theImage unlockFocus];
}
- (NSColor*)currentBackgroundColor
{
id stylesheet = [textView currentStyleSheet];
NSDictionary* firstEntry = [(NSArray*)stylesheet objectAtIndex:0]; // the first entry seems to always contain the main settings
NSString* bgColor = [[firstEntry objectForKey:@"settings"] objectForKey:@"background"];
unsigned aValue = strtoul([[bgColor substringWithRange:NSMakeRange(1, 6)] UTF8String], NULL, 16);
CGFloat red = ((CGFloat)((aValue & 0xFF0000) >> 16)) / 255.0f;
CGFloat green = ((CGFloat)((aValue & 0xFF00) >> 8)) / 255.0f;
CGFloat blue = (CGFloat)(aValue & 0xFF) / 255.0f;
CGFloat alpha = 1.0;
if ([bgColor length] == 9) // if a alpha was given
{
unsigned alphaValue = strtoul([[bgColor substringWithRange:NSMakeRange(7, 2)] UTF8String], NULL, 16);
alpha = (CGFloat)(alphaValue & 0xFF) / 255.0f;
}
return [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
}
- (void)asyncDrawFinished: (NSImage*) bitmap
{
[theImage release];
theImage = nil;
theImage = [bitmap retain];
if (minimapIsScrollable && [self dirty]) {
[[self updater] setDirtyExceptForVisiblePart];
[[self updater] startRedrawInBackground];
[self setDirty:NO];
}
if (minimapIsScrollable)
pixelPerLine = [[NSUserDefaults standardUserDefaults] floatForKey:@"Minimap_scaleUpTo"];
else
pixelPerLine = [self bounds].size.height / [self numberOfLines];
[self setNeedsDisplay:YES];
}
- (int)numberOfLines
{
unsigned int lineHeight = [textView lineHeight];
float h = [textView bounds].size.height;
int totalLines = round(h/lineHeight);
return totalLines;
}
#pragma mark private-methods
- (void)updateGutterSize
{
int w = [textView bounds].size.width;
NSBitmapImageRep* rawImg = [textView snapshotInRect:NSMakeRect(0,0,w,1)];
NSColor* refColor = [[rawImg colorAtX:0 y:0] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];//[self currentBackgroundColor];
int i = 1;
NSColor* color = [[rawImg colorAtX:i y:0] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
int imgWidth = [rawImg size].width;
while ([color isEqual:refColor] && i < imgWidth) {
i++;
color = [[rawImg colorAtX:i y:0] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
}
if (i == imgWidth)
gutterSize = 0;
else
gutterSize = i+1;
}
- (unsigned int)absoluteLineIdxFromPoint:(NSPoint)mouseLoc
{
float ratio = drawnRect.size.height / [self bounds].size.height;
unsigned int relativeLineIdx;
if (ratio < 1.0) {
relativeLineIdx = floor(mouseLoc.y / scaleDownTo);
}
else {
relativeLineIdx = floor(mouseLoc.y / pixelPerLine);
}
unsigned int absoluteLineIdx = minimapLinesStart + relativeLineIdx;
return absoluteLineIdx;
}
@end