Skip to content

Commit 67546f6

Browse files
committed
- cleaned up autoresizing masks and fixed sizing issues + exposed minimap size in the minimap menu
- fixed retain cycles
1 parent cea4fae commit 67546f6

File tree

4 files changed

+98
-47
lines changed

4 files changed

+98
-47
lines changed

SCXcodeMinimap/SCXcodeMinimap.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
#import <Cocoa/Cocoa.h>
1010

11-
const CGFloat kDefaultZoomLevel;
12-
1311
extern NSString *const SCXcodeMinimapShouldDisplayChangeNotification;
1412
extern NSString *const SCXcodeMinimapShouldDisplayKey;
1513

14+
extern NSString *const SCXcodeMinimapZoomLevelChangeNotification;
15+
extern NSString *const SCXcodeMinimapZoomLevelKey;
16+
1617
extern NSString *const SCXcodeMinimapHighlightBreakpointsChangeNotification;
1718
extern NSString *const SCXcodeMinimapShouldHighlightBreakpointsKey;
1819

SCXcodeMinimap/SCXcodeMinimap.m

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88

99
#import "SCXcodeMinimap.h"
1010
#import "SCXcodeMinimapView.h"
11-
#import <objc/runtime.h>
1211

1312
#import "IDESourceCodeEditor.h"
1413
#import "DVTSourceTextView.h"
1514

1615
#import "DVTPreferenceSetManager.h"
1716
#import "DVTFontAndColorTheme.h"
1817

19-
const CGFloat kDefaultZoomLevel = 0.1f;
20-
2118
NSString *const IDESourceCodeEditorDidFinishSetupNotification = @"IDESourceCodeEditorDidFinishSetup";
2219

2320
NSString *const SCXcodeMinimapShouldDisplayChangeNotification = @"SCXcodeMinimapShouldDisplayChangeNotification";
2421
NSString *const SCXcodeMinimapShouldDisplayKey = @"SCXcodeMinimapShouldDisplayKey";
2522

23+
NSString *const SCXcodeMinimapZoomLevelChangeNotification = @"SCXcodeMinimapZoomLevelChangeNotification";
24+
NSString *const SCXcodeMinimapZoomLevelKey = @"SCXcodeMinimapZoomLevelKey";
25+
2626
NSString *const SCXcodeMinimapHighlightBreakpointsChangeNotification = @"SCXcodeMinimapHighlightBreakpointsChangeNotification";
2727
NSString *const SCXcodeMinimapShouldHighlightBreakpointsKey = @"SCXcodeMinimapShouldHighlightBreakpointsKey";
2828

@@ -81,7 +81,8 @@ - (id)init
8181

8282
- (void)registerUserDefaults
8383
{
84-
NSDictionary *userDefaults = @{SCXcodeMinimapShouldDisplayKey : @(YES),
84+
NSDictionary *userDefaults = @{SCXcodeMinimapZoomLevelKey : @(0.1f),
85+
SCXcodeMinimapShouldDisplayKey : @(YES),
8586
SCXcodeMinimapShouldHighlightBreakpointsKey : @(YES),
8687
SCXcodeMinimapShouldHighlightCommentsKey : @(YES),
8788
SCXcodeMinimapShouldHighlightPreprocessorKey : @(YES)};
@@ -111,6 +112,20 @@ - (void)createMenuItem
111112
[showHideMinimapItem setTarget:self];
112113
[minimapMenu addItem:showHideMinimapItem];
113114

115+
NSMenuItem *minimapSizeItem = [[NSMenuItem alloc] init];
116+
NSSlider *sizeSlider = [[NSSlider alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
117+
[sizeSlider setAutoresizingMask:NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin];
118+
[sizeSlider setMaxValue:0.35f];
119+
[sizeSlider setMinValue:0.05f];
120+
[sizeSlider setTarget:self];
121+
[sizeSlider setAction:@selector(onSizeSliderValueChanged:)];
122+
123+
CGFloat zoomLevel = [[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapZoomLevelKey] doubleValue];
124+
[sizeSlider setDoubleValue:zoomLevel];
125+
126+
[minimapSizeItem setView:sizeSlider];
127+
[minimapMenu addItem:minimapSizeItem];
128+
114129
BOOL shouldDisplayMinimap = [[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldDisplayKey] boolValue];
115130
[showHideMinimapItem setTitle:(shouldDisplayMinimap ? kHideMinimapMenuItemTitle : kShowMinimapMenuItemTitle)];
116131

@@ -294,6 +309,17 @@ - (void)setMinimapTheme:(NSMenuItem *)sender
294309
[[NSNotificationCenter defaultCenter] postNotificationName:SCXcodeMinimapThemeChangeNotification object:nil];
295310
}
296311

312+
- (void)onSizeSliderValueChanged:(NSSlider *)sender
313+
{
314+
NSEvent *event = [[NSApplication sharedApplication] currentEvent];
315+
if(event.type != NSLeftMouseUp) {
316+
return;
317+
}
318+
319+
[[NSUserDefaults standardUserDefaults] setObject:@(sender.doubleValue) forKey:SCXcodeMinimapZoomLevelKey];
320+
[[NSNotificationCenter defaultCenter] postNotificationName:SCXcodeMinimapZoomLevelChangeNotification object:nil];
321+
}
322+
297323
#pragma mark - Xcode Notification
298324

299325
- (void)onDidFinishSetup:(NSNotification*)sender
@@ -304,15 +330,12 @@ - (void)onDidFinishSetup:(NSNotification*)sender
304330
}
305331

306332
IDESourceCodeEditor *editor = (IDESourceCodeEditor *)[sender object];
307-
[editor.textView setAutoresizingMask:NSViewMinXMargin | NSViewMaxXMargin | NSViewWidthSizable | NSViewHeightSizable];
308-
309-
CGFloat width = editor.textView.bounds.size.width * kDefaultZoomLevel;
310-
NSRect miniMapScrollViewFrame = NSMakeRect(editor.containerView.bounds.size.width - width, 0, width, editor.scrollView.bounds.size.height);
311-
312-
SCXcodeMinimapView *miniMapView = [[SCXcodeMinimapView alloc] initWithFrame:miniMapScrollViewFrame editor:editor];
313-
[editor.containerView addSubview:miniMapView];
333+
[editor.textView setAutoresizingMask:NSViewMaxXMargin | NSViewMaxYMargin | NSViewWidthSizable | NSViewHeightSizable];
334+
[editor.scrollView setAutoresizingMask:NSViewMaxXMargin | NSViewMaxYMargin | NSViewWidthSizable | NSViewHeightSizable];
335+
[editor.containerView setAutoresizingMask:NSViewMaxXMargin | NSViewMaxYMargin | NSViewWidthSizable | NSViewHeightSizable];
314336

315-
[miniMapView setVisible:[[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldDisplayKey] boolValue]];
337+
SCXcodeMinimapView *minimapView = [[SCXcodeMinimapView alloc] initWithEditor:editor];
338+
[editor.containerView addSubview:minimapView];
316339
}
317340

318341
@end

SCXcodeMinimap/SCXcodeMinimapView.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
@interface SCXcodeMinimapView : NSView
1414

15-
- (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor;
16-
17-
- (void)setVisible:(BOOL)visible;
15+
- (instancetype)initWithEditor:(IDESourceCodeEditor *)editor;
1816

1917
@end

SCXcodeMinimap/SCXcodeMinimapView.m

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ @implementation SCXcodeMinimapTheme
6868

6969
@interface SCXcodeMinimapView () <NSLayoutManagerDelegate, DVTFoldingManagerDelegate, DBGBreakpointAnnotationProviderDelegate>
7070

71-
@property (nonatomic, strong) IDESourceCodeEditor *editor;
72-
@property (nonatomic, strong) NSScrollView *editorScrollView;
73-
@property (nonatomic, strong) DVTSourceTextView *editorTextView;
71+
@property (nonatomic, weak) IDESourceCodeEditor *editor;
72+
@property (nonatomic, assign) DVTSourceTextView *editorTextView;
7473

7574
@property (nonatomic, strong) NSScrollView *scrollView;
7675
@property (nonatomic, strong) DVTSourceTextView *textView;
@@ -92,47 +91,44 @@ @implementation SCXcodeMinimapView
9291
- (void)dealloc
9392
{
9493
[[NSNotificationCenter defaultCenter] removeObserver:self];
94+
[self.textView.textStorage removeLayoutManager:self.textView.layoutManager];
9595
}
9696

97-
- (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
97+
- (instancetype)initWithEditor:(IDESourceCodeEditor *)editor
9898
{
99-
if (self = [super initWithFrame:frame])
99+
if (self = [super init])
100100
{
101101
self.editor = editor;
102-
self.editorScrollView = editor.scrollView;
103102

104103
self.editorTextView = editor.textView;
105104
[self.editorTextView.foldingManager setDelegate:self];
106105

107106
[self setWantsLayer:YES];
108-
[self setAutoresizingMask:NSViewMinXMargin | NSViewHeightSizable];
107+
[self setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin | NSViewWidthSizable | NSViewHeightSizable];
109108

110109
self.scrollView = [[NSScrollView alloc] initWithFrame:self.bounds];
111-
[self.scrollView setAutoresizingMask:NSViewMinXMargin | NSViewHeightSizable];
110+
[self.scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
112111
[self.scrollView setDrawsBackground:NO];
112+
[self.scrollView setMinMagnification:0.0f];
113+
[self.scrollView setMaxMagnification:1.0f];
114+
[self.scrollView setAllowsMagnification:NO];
113115

114116
[self.scrollView setHorizontalScrollElasticity:NSScrollElasticityNone];
115117
[self.scrollView setVerticalScrollElasticity:NSScrollElasticityNone];
116118
[self addSubview:self.scrollView];
117119

118-
self.textView = [[DVTSourceTextView alloc] initWithFrame:self.editorTextView.bounds];
120+
self.textView = [[DVTSourceTextView alloc] init];
119121
[self.textView setTextStorage:self.editorTextView.textStorage];
120122
[self.textView setEditable:NO];
121123
[self.textView setSelectable:NO];
122124

123125
[self.scrollView setDocumentView:self.textView];
124126

125-
[self.scrollView setAllowsMagnification:YES];
126-
[self.scrollView setMinMagnification:kDefaultZoomLevel];
127-
[self.scrollView setMaxMagnification:kDefaultZoomLevel];
128-
[self.scrollView setMagnification:kDefaultZoomLevel];
129-
130127
self.selectionView = [[SCXcodeMinimapSelectionView alloc] init];
131128
[self.textView addSubview:_selectionView];
132129

133130
[self updateTheme];
134131

135-
136132
BOOL shouldHighlightBreakpoints = [[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldHighlightBreakpointsKey] boolValue];
137133
if(shouldHighlightBreakpoints) {
138134

@@ -148,13 +144,20 @@ - (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
148144
}
149145

150146
BOOL shouldHideEditorVerticalScroller = [[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldHideEditorScrollerKey] boolValue];
151-
[self.editorScrollView setHasVerticalScroller:!shouldHideEditorVerticalScroller];
147+
[self.editor.scrollView setHasVerticalScroller:!shouldHideEditorVerticalScroller];
148+
149+
// Notifications
152150

153151
__weak typeof(self) weakSelf = self;
154152
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapShouldDisplayChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
155153
[weakSelf setVisible:[[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldDisplayKey] boolValue]];
156154
}];
157155

156+
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapZoomLevelChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
157+
[weakSelf updateSize];
158+
[weakSelf invalidateDisplayForVisibleRange];
159+
}];
160+
158161
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapHighlightBreakpointsChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
159162
[weakSelf invalidateDisplayForVisibleRange];
160163
}];
@@ -180,7 +183,7 @@ - (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
180183
}];
181184

182185
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapHideEditorScrollerChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
183-
[weakSelf.editorScrollView setHasVerticalScroller:![[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldHideEditorScrollerKey] boolValue]];
186+
[weakSelf.editor.scrollView setHasVerticalScroller:![[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldHideEditorScrollerKey] boolValue]];
184187
}];
185188

186189
[[NSNotificationCenter defaultCenter] addObserverForName:SCXcodeMinimapThemeChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
@@ -201,20 +204,21 @@ - (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
201204
return self;
202205
}
203206

207+
- (void)viewDidMoveToWindow
208+
{
209+
[self setVisible:[[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldDisplayKey] boolValue]];
210+
}
211+
204212
#pragma mark - Show/Hide
205213

206214
- (void)setVisible:(BOOL)visible
207215
{
208-
self.hidden = !visible;
216+
self.hidden = !visible;
209217

210-
NSRect editorTextViewFrame = self.editorScrollView.frame;
211-
editorTextViewFrame.size.width = self.editorScrollView.superview.frame.size.width - (visible ? self.bounds.size.width : 0.0f);
212-
self.editorScrollView.frame = editorTextViewFrame;
218+
[self updateSize];
219+
[self updateOffset];
213220

214-
if(visible) {
215-
[self updateOffset];
216-
[self.textView.layoutManager setDelegate:self];
217-
}
221+
[self.textView.layoutManager setDelegate:(self.hidden ? nil : self)];
218222

219223
BOOL editorHighlightingEnabled = [[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapShouldHighlightEditorKey] boolValue];
220224
if(editorHighlightingEnabled) {
@@ -361,24 +365,24 @@ - (void)updateOffset
361365
CGFloat editorTextHeight = CGRectGetHeight([self.editorTextView.layoutManager usedRectForTextContainer:self.editorTextView.textContainer]);
362366
CGFloat minimapTextHeight = CGRectGetHeight([self.textView.layoutManager usedRectForTextContainer:self.textView.textContainer]);
363367

364-
CGFloat adjustedEditorContentHeight = editorTextHeight - CGRectGetHeight(self.editorScrollView.bounds);
368+
CGFloat adjustedEditorContentHeight = editorTextHeight - CGRectGetHeight(self.editor.scrollView.bounds);
365369
CGFloat adjustedMinimapContentHeight = minimapTextHeight - (CGRectGetHeight(self.scrollView.bounds) * (1 / self.scrollView.magnification));
366370

367-
NSRect selectionViewFrame = NSMakeRect(0, 0, self.bounds.size.width * (1 / self.scrollView.magnification), self.editorScrollView.visibleRect.size.height);
371+
NSRect selectionViewFrame = NSMakeRect(0, 0, self.bounds.size.width * (1 / self.scrollView.magnification), self.editor.scrollView.visibleRect.size.height);
368372

369373
if(adjustedEditorContentHeight == 0.0f) {
370374
[self.selectionView setFrame:selectionViewFrame];
371375
return;
372376
}
373377

374378
CGFloat ratio = (adjustedMinimapContentHeight / adjustedEditorContentHeight) * (1 / self.scrollView.magnification);
375-
CGPoint offset = NSMakePoint(0, MAX(0, floorf(self.editorScrollView.contentView.bounds.origin.y * ratio * self.scrollView.magnification)));
379+
CGPoint offset = NSMakePoint(0, MAX(0, floorf(self.editor.scrollView.contentView.bounds.origin.y * ratio * self.scrollView.magnification)));
376380

377381
[self.scrollView.documentView scrollPoint:offset];
378382

379383

380384
ratio = (minimapTextHeight - self.selectionView.bounds.size.height) / adjustedEditorContentHeight;
381-
selectionViewFrame.origin.y = self.editorScrollView.contentView.bounds.origin.y * ratio;
385+
selectionViewFrame.origin.y = self.editor.scrollView.contentView.bounds.origin.y * ratio;
382386

383387
[self.selectionView setFrame:selectionViewFrame];
384388
}
@@ -470,11 +474,36 @@ - (SCXcodeMinimapTheme *)minimapThemeWithTheme:(DVTFontAndColorTheme *)theme
470474
return minimapTheme;
471475
}
472476

473-
#pragma mark - Autoresizing
477+
#pragma mark - Sizing
478+
479+
- (void)updateSize
480+
{
481+
CGFloat zoomLevel = [[[NSUserDefaults standardUserDefaults] objectForKey:SCXcodeMinimapZoomLevelKey] doubleValue];
482+
483+
CGFloat minimapWidth = (self.hidden ? 0.0f : self.editor.containerView.bounds.size.width * zoomLevel);
484+
485+
NSRect editorScrollViewFrame = self.editor.scrollView.frame;
486+
editorScrollViewFrame.size.width = self.editor.scrollView.superview.frame.size.width - minimapWidth + 10.0f;
487+
self.editor.scrollView.frame = editorScrollViewFrame;
488+
489+
[self setFrame:NSMakeRect(CGRectGetMaxX(editorScrollViewFrame), 0, minimapWidth, CGRectGetHeight(self.editor.containerView.bounds))];
490+
491+
CGRect frame = self.textView.bounds;
492+
frame.size.width = CGRectGetWidth(self.editorTextView.bounds);
493+
[self.textView setFrame:frame];
494+
495+
CGFloat actualZoomLevel = CGRectGetWidth(self.bounds) / CGRectGetWidth(self.editor.scrollView.bounds);
496+
[self.scrollView setMagnification:actualZoomLevel];
497+
}
474498

475499
- (void)resizeWithOldSuperviewSize:(NSSize)oldSize
476500
{
477501
[super resizeWithOldSuperviewSize:oldSize];
502+
503+
CGRect frame = self.textView.bounds;
504+
frame.size.width = CGRectGetWidth(self.editorTextView.bounds);
505+
[self.textView setFrame:frame];
506+
478507
[self updateOffset];
479508
}
480509

0 commit comments

Comments
 (0)