|
| 1 | +// |
| 2 | +// SCMiniMapView.m |
| 3 | +// SCXcodeMinimap |
| 4 | +// |
| 5 | +// Created by Jérôme ALVES on 30/04/13. |
| 6 | +// Copyright (c) 2013 Stefan Ceriu. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "SCMiniMapView.h" |
| 10 | +#import "SCXcodeMinimap.h" |
| 11 | + |
| 12 | +@implementation SCMiniMapView |
| 13 | + |
| 14 | +- (id)initWithFrame:(NSRect)frame |
| 15 | +{ |
| 16 | + self = [super initWithFrame:frame]; |
| 17 | + if (self) { |
| 18 | + |
| 19 | + /* Configure ScrollView */ |
| 20 | + [self setWantsLayer:YES]; |
| 21 | + [self setAutoresizingMask: NSViewMinXMargin | NSViewWidthSizable | NSViewHeightSizable]; |
| 22 | + [self setDrawsBackground:NO]; |
| 23 | + [self setHorizontalScrollElasticity:NSScrollElasticityNone]; |
| 24 | + [self setVerticalScrollElasticity:NSScrollElasticityNone]; |
| 25 | + |
| 26 | + /* Configure Text View */ |
| 27 | + SCTextView *miniMapTextView = [[SCTextView alloc] initWithFrame:self.bounds]; |
| 28 | + [miniMapTextView setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin | NSViewWidthSizable | NSViewHeightSizable]; |
| 29 | + [miniMapTextView.textContainer setLineFragmentPadding:0.0f]; |
| 30 | + [miniMapTextView setSelectable:NO]; |
| 31 | + [miniMapTextView.layoutManager setDelegate:self]; |
| 32 | + [miniMapTextView setDelegate:self]; |
| 33 | + |
| 34 | + [self setDocumentView:miniMapTextView]; |
| 35 | + |
| 36 | + NSColor *miniMapBackgroundColor = [NSColor clearColor]; |
| 37 | + Class DVTFontAndColorThemeClass = NSClassFromString(@"DVTFontAndColorTheme"); |
| 38 | + if([DVTFontAndColorThemeClass respondsToSelector:@selector(currentTheme)]) { |
| 39 | + NSObject *theme = [DVTFontAndColorThemeClass performSelector:@selector(currentTheme)]; |
| 40 | + |
| 41 | + if([theme respondsToSelector:@selector(sourceTextBackgroundColor)]) { |
| 42 | + miniMapBackgroundColor = [theme performSelector:@selector(sourceTextBackgroundColor)]; |
| 43 | + |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + [miniMapTextView setBackgroundColor:[miniMapBackgroundColor shadowWithLevel:kDefaultShadowLevel]]; |
| 48 | + |
| 49 | + self.textView = miniMapTextView; |
| 50 | + |
| 51 | + [miniMapTextView release]; |
| 52 | + |
| 53 | + /* Configure Selection View */ |
| 54 | + SCSelectionView *miniMapSelectionView = [[SCSelectionView alloc] init]; |
| 55 | + [miniMapSelectionView setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin | NSViewWidthSizable | NSViewHeightSizable | NSViewMinYMargin | NSViewMaxYMargin]; |
| 56 | + //[miniMapSelectionView setShouldInverseColors:YES]; |
| 57 | + [self addSubview:miniMapSelectionView]; |
| 58 | + |
| 59 | + self.selectionView = miniMapSelectionView; |
| 60 | + [miniMapSelectionView release]; |
| 61 | + |
| 62 | + /* Subscribe to show/hide notifications */ |
| 63 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 64 | + selector:@selector(show) |
| 65 | + name:SCXodeMinimapWantsToBeShownNotification |
| 66 | + object:nil]; |
| 67 | + |
| 68 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 69 | + selector:@selector(hide) |
| 70 | + name:SCXodeMinimapWantsToBeHiddenNotification |
| 71 | + object:nil]; |
| 72 | + } |
| 73 | + return self; |
| 74 | +} |
| 75 | + |
| 76 | +- (void)dealloc |
| 77 | +{ |
| 78 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 79 | + |
| 80 | + [_selectionView release]; |
| 81 | + [_textView release]; |
| 82 | + [super dealloc]; |
| 83 | +} |
| 84 | + |
| 85 | +#pragma mark - Show/Hide |
| 86 | + |
| 87 | +- (void) show |
| 88 | +{ |
| 89 | + self.hidden = NO; |
| 90 | + |
| 91 | + NSRect editorTextViewFrame = self.editorTextView.frame; |
| 92 | + editorTextViewFrame.size.width = self.editorTextView.superview.frame.size.width - self.bounds.size.width - kRightSidePadding; |
| 93 | + self.editorTextView.frame = editorTextViewFrame; |
| 94 | +} |
| 95 | + |
| 96 | +- (void) hide |
| 97 | +{ |
| 98 | + self.hidden = YES; |
| 99 | + |
| 100 | + NSRect editorTextViewFrame = self.editorTextView.frame; |
| 101 | + editorTextViewFrame.size.width = self.editorTextView.superview.frame.size.width; |
| 102 | + self.editorTextView.frame = editorTextViewFrame; |
| 103 | +} |
| 104 | + |
| 105 | +#pragma mark - Updating |
| 106 | + |
| 107 | +- (void)updateTextView |
| 108 | +{ |
| 109 | + NSMutableAttributedString *mutableAttributedString = [self.editorTextView.textStorage mutableCopy]; |
| 110 | + |
| 111 | + if(mutableAttributedString == nil) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + [mutableAttributedString enumerateAttributesInRange:NSMakeRange(0, mutableAttributedString.length) options:NSAttributedStringEnumerationReverse usingBlock: |
| 116 | + ^(NSDictionary *attributes, NSRange range, BOOL *stop) { |
| 117 | + |
| 118 | + NSFont *font = [attributes objectForKey:NSFontAttributeName]; |
| 119 | + NSFont *newFont = [NSFont fontWithName:font.familyName size:font.pointSize * kDefaultZoomLevel]; |
| 120 | + |
| 121 | + NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes]; |
| 122 | + [mutableAttributes setObject:newFont forKey:NSFontAttributeName]; |
| 123 | + [mutableAttributedString setAttributes:mutableAttributes range:range]; |
| 124 | + }]; |
| 125 | + |
| 126 | + [self.textView.textStorage setAttributedString:mutableAttributedString]; |
| 127 | + [mutableAttributedString release]; |
| 128 | +} |
| 129 | + |
| 130 | +- (void)updateSelectionView |
| 131 | +{ |
| 132 | + NSRect selectionViewFrame = NSMakeRect(0, |
| 133 | + 0, |
| 134 | + self.bounds.size.width, |
| 135 | + self.editorScrollView.visibleRect.size.height * kDefaultZoomLevel); |
| 136 | + |
| 137 | + |
| 138 | + CGFloat editorContentHeight = [self.editorScrollView.documentView frame].size.height - self.editorScrollView.bounds.size.height; |
| 139 | + |
| 140 | + if(editorContentHeight == 0) { |
| 141 | + selectionViewFrame.origin.y = 0; |
| 142 | + } |
| 143 | + else { |
| 144 | + CGFloat ratio = ([self.documentView frame].size.height - self.bounds.size.height) / editorContentHeight; |
| 145 | + [self.contentView scrollToPoint:NSMakePoint(0, floorf(self.editorScrollView.contentView.bounds.origin.y * ratio))]; |
| 146 | + |
| 147 | + CGFloat textHeight = [self.textView.layoutManager usedRectForTextContainer:self.textView.textContainer].size.height; |
| 148 | + ratio = (textHeight - self.selectionView.bounds.size.height) / editorContentHeight; |
| 149 | + selectionViewFrame.origin.y = self.editorScrollView.contentView.bounds.origin.y * ratio; |
| 150 | + } |
| 151 | + |
| 152 | + self.selectionView.frame = selectionViewFrame; |
| 153 | +} |
| 154 | + |
| 155 | +#pragma mark - NSLayoutManagerDelegate |
| 156 | + |
| 157 | +- (void)layoutManager:(NSLayoutManager *)layoutManager didCompleteLayoutForTextContainer:(NSTextContainer *)textContainer atEnd:(BOOL)layoutFinished |
| 158 | +{ |
| 159 | + if(layoutFinished) { |
| 160 | + [self updateSelectionView]; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +- (NSDictionary *)layoutManager:(NSLayoutManager *)layoutManager shouldUseTemporaryAttributes:(NSDictionary *)attrs forDrawingToScreen:(BOOL)toScreen atCharacterIndex:(NSUInteger)charIndex effectiveRange:(NSRangePointer)effectiveCharRange |
| 165 | +{ |
| 166 | + return [(id<NSLayoutManagerDelegate>)self.editorTextView layoutManager:layoutManager |
| 167 | + shouldUseTemporaryAttributes:attrs |
| 168 | + forDrawingToScreen:toScreen |
| 169 | + atCharacterIndex:charIndex |
| 170 | + effectiveRange:effectiveCharRange]; |
| 171 | +} |
| 172 | + |
| 173 | +#pragma mark - SCTextViewDelegate |
| 174 | + |
| 175 | +- (void)textView:(SCTextView *)textView goAtRelativePosition:(NSPoint)position |
| 176 | +{ |
| 177 | + CGFloat documentHeight = [self.editorScrollView.documentView frame].size.height; |
| 178 | + CGSize boundsSize = self.editorScrollView.bounds.size; |
| 179 | + CGFloat maxOffset = documentHeight - boundsSize.height; |
| 180 | + |
| 181 | + CGFloat offset = floor(documentHeight * position.y - boundsSize.height/2); |
| 182 | + |
| 183 | + offset = MIN(MAX(0, offset), maxOffset); |
| 184 | + |
| 185 | + [self.editorTextView scrollRectToVisible:NSMakeRect(0, offset, boundsSize.width, boundsSize.height)]; |
| 186 | +} |
| 187 | + |
| 188 | +@end |
0 commit comments