Skip to content

Commit 31b0d02

Browse files
committed
Proper custom theme support .
1 parent a136f85 commit 31b0d02

File tree

4 files changed

+105
-32
lines changed

4 files changed

+105
-32
lines changed

SCXcodeMinimap/SCXcodeMinimap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const CGFloat kDefaultZoomLevel;
1313
extern NSString *const SCXodeMinimapShowNotification;
1414
extern NSString *const SCXodeMinimapHideNotification;
1515

16+
extern NSString *const SCXodeMinimapThemeChangeNotification;
17+
extern NSString *const SCXodeMinimapTheme;
18+
1619
@interface SCXcodeMinimap : NSObject
1720

1821
@end

SCXcodeMinimap/SCXcodeMinimap.m

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@
1313
#import "IDESourceCodeEditor.h"
1414
#import "DVTSourceTextView.h"
1515

16+
#import "DVTPreferenceSetManager.h"
17+
#import "DVTFontAndColorTheme.h"
18+
1619
const CGFloat kDefaultZoomLevel = 0.1f;
1720

18-
static NSString * const IDESourceCodeEditorDidFinishSetupNotification = @"IDESourceCodeEditorDidFinishSetup";
21+
NSString *const IDESourceCodeEditorDidFinishSetupNotification = @"IDESourceCodeEditorDidFinishSetup";
22+
23+
NSString *const SCXodeMinimapShowNotification = @"SCXodeMinimapShowNotification";
24+
NSString *const SCXodeMinimapHideNotification = @"SCXodeMinimapHideNotification";
1925

20-
NSString * const SCXodeMinimapShowNotification = @"SCXodeMinimapShowNotification";
21-
NSString * const SCXodeMinimapHideNotification = @"SCXodeMinimapHideNotification";
26+
NSString *const SCXodeMinimapThemeChangeNotification = @"SCXodeMinimapThemeChangeNotification";
2227

23-
NSString * const SCXodeMinimapIsInitiallyHidden = @"SCXodeMinimapIsInitiallyHidden";
28+
NSString *const SCXodeMinimapIsInitiallyHidden = @"SCXodeMinimapIsInitiallyHidden";
29+
NSString *const SCXodeMinimapTheme = @"SCXodeMinimapTheme";
2430

2531
@implementation SCXcodeMinimap
2632

@@ -54,24 +60,62 @@ - (void)createMenuItem
5460
return;
5561
}
5662

57-
NSMenuItem *miniMapItem = [[NSMenuItem alloc] initWithTitle:@""
58-
action:NULL
59-
keyEquivalent:@"M"];
60-
[miniMapItem setKeyEquivalentModifierMask:NSControlKeyMask | NSShiftKeyMask];
63+
[editMenuItem.submenu addItem:[NSMenuItem separatorItem]];
6164

62-
miniMapItem.target = self;
65+
NSMenuItem *minimapMenuItem = [[NSMenuItem alloc] initWithTitle:@"Minimap" action:nil keyEquivalent:@""];
66+
[editMenuItem.submenu addItem:minimapMenuItem];
6367

64-
[editMenuItem.submenu insertItem:[NSMenuItem separatorItem]
65-
atIndex:[editMenuItem.submenu numberOfItems]];
66-
[editMenuItem.submenu insertItem:miniMapItem
67-
atIndex:[editMenuItem.submenu numberOfItems]];
68-
69-
if ([[NSUserDefaults standardUserDefaults] boolForKey:SCXodeMinimapIsInitiallyHidden]) {
70-
[self hideMiniMap:miniMapItem];
68+
NSMenu *minimapMenu = [[NSMenu alloc] init];
69+
{
70+
NSMenuItem *showHideMinimapItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@"M"];
71+
[showHideMinimapItem setKeyEquivalentModifierMask:NSControlKeyMask | NSShiftKeyMask];
72+
[showHideMinimapItem setTarget:self];
73+
[minimapMenu addItem:showHideMinimapItem];
74+
75+
if ([[NSUserDefaults standardUserDefaults] boolForKey:SCXodeMinimapIsInitiallyHidden]) {
76+
[self hideMiniMap:showHideMinimapItem];
77+
}
78+
else {
79+
[self showMiniMap:showHideMinimapItem];
80+
}
81+
82+
[minimapMenu addItem:[NSMenuItem separatorItem]];
7183
}
72-
else {
73-
[self showMiniMap:miniMapItem];
84+
85+
{
86+
NSMenuItem *themesMenuItem = [[NSMenuItem alloc] initWithTitle:@"Theme" action:nil keyEquivalent:@""];
87+
[minimapMenu addItem:themesMenuItem];
88+
89+
NSMenu *themesMenu = [[NSMenu alloc] init];
90+
{
91+
NSMenuItem *editorThemeMenuItem = [[NSMenuItem alloc] initWithTitle:@"Editor Theme" action:@selector(setMinimapTheme:) keyEquivalent:@""];
92+
[editorThemeMenuItem setTarget:self];
93+
[themesMenu addItem:editorThemeMenuItem];
94+
95+
[themesMenu addItem:[NSMenuItem separatorItem]];
96+
97+
NSArray *themes = [[DVTFontAndColorTheme preferenceSetsManager] availablePreferenceSets];
98+
NSArray *builtInThemes = [themes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.isBuiltIn == YES"]];
99+
NSArray *userThemes = [themes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.isBuiltIn == NO"]];
100+
101+
for(DVTFontAndColorTheme *theme in builtInThemes) {
102+
NSMenuItem *themeMenuItem = [[NSMenuItem alloc] initWithTitle:theme.localizedName action:@selector(setMinimapTheme:) keyEquivalent:@""];
103+
[themeMenuItem setTarget:self];
104+
[themesMenu addItem:themeMenuItem];
105+
}
106+
107+
[themesMenu addItem:[NSMenuItem separatorItem]];
108+
109+
for(DVTFontAndColorTheme *theme in userThemes) {
110+
NSMenuItem *themeMenuItem = [[NSMenuItem alloc] initWithTitle:theme.localizedName action:@selector(setMinimapTheme:) keyEquivalent:@""];
111+
[themeMenuItem setTarget:self];
112+
[themesMenu addItem:themeMenuItem];
113+
}
114+
}
115+
[themesMenuItem setSubmenu:themesMenu];
116+
74117
}
118+
[minimapMenuItem setSubmenu:minimapMenu];
75119
}
76120

77121
- (void)hideMiniMap:(NSMenuItem *)sender
@@ -94,6 +138,17 @@ - (void)showMiniMap:(NSMenuItem *)sender
94138
[[NSNotificationCenter defaultCenter] postNotificationName:SCXodeMinimapShowNotification object:nil];
95139
}
96140

141+
- (void)setMinimapTheme:(NSMenuItem *)sender
142+
{
143+
if([sender.menu indexOfItem:sender] == 0) {
144+
[[NSUserDefaults standardUserDefaults] removeObjectForKey:SCXodeMinimapTheme];
145+
} else {
146+
[[NSUserDefaults standardUserDefaults] setObject:sender.title forKey:SCXodeMinimapTheme];
147+
}
148+
149+
[[NSNotificationCenter defaultCenter] postNotificationName:SCXodeMinimapThemeChangeNotification object:nil];
150+
}
151+
97152
#pragma mark - Xcode Notification
98153

99154
- (void)onDidFinishSetup:(NSNotification*)sender

SCXcodeMinimap/SCXcodeMinimapView.m

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
const CGFloat kBackgroundColorShadowLevel = 0.1f;
2626
const CGFloat kHighlightColorAlphaLevel = 0.3f;
27+
const CGFloat kDurationBetweenInvalidations = 0.5f;
2728

2829
static NSString * const kXcodeSyntaxCommentNodeName = @"xcode.syntax.comment";
2930
static NSString * const kXcodeSyntaxCommentDocNodeName = @"xcode.syntax.comment.doc";
@@ -115,6 +116,10 @@ - (instancetype)initWithFrame:(NSRect)frame editor:(IDESourceCodeEditor *)editor
115116
[weakSelf updateTheme];
116117
}];
117118

119+
[[NSNotificationCenter defaultCenter] addObserverForName:SCXodeMinimapThemeChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
120+
[weakSelf updateTheme];
121+
}];
122+
118123
[[NSNotificationCenter defaultCenter] addObserverForName:IDESourceCodeEditorTextViewBoundsDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
119124
if([note.object isEqual:weakSelf.editor]) {
120125
[weakSelf updateOffset];
@@ -171,16 +176,16 @@ - (NSDictionary *)layoutManager:(NSLayoutManager *)layoutManager
171176
if(charIndex > visibleEditorRange.location + visibleEditorRange.length ) {
172177
*effectiveCharRange = NSMakeRange(visibleEditorRange.location + visibleEditorRange.length,
173178
layoutManager.textStorage.length - visibleEditorRange.location - visibleEditorRange.length);
174-
175-
[self performBlock:invalidationBlock afterDelay:0.5f cancelPreviousRequest:YES];
179+
180+
[self performBlock:invalidationBlock afterDelay:kDurationBetweenInvalidations cancelPreviousRequest:YES];
176181

177182
return @{NSForegroundColorAttributeName : [self.theme sourcePlainTextColor]};
178183
}
179184

180185
if(charIndex < visibleEditorRange.location) {
181186
*effectiveCharRange = NSMakeRange(0, visibleEditorRange.location);
182187

183-
[self performBlock:invalidationBlock afterDelay:0.5f cancelPreviousRequest:YES];
188+
[self performBlock:invalidationBlock afterDelay:kDurationBetweenInvalidations cancelPreviousRequest:YES];
184189

185190
return @{NSForegroundColorAttributeName : [self.theme sourcePlainTextColor]};
186191
}
@@ -191,16 +196,19 @@ - (NSDictionary *)layoutManager:(NSLayoutManager *)layoutManager
191196
NSColor *color = [storage colorAtCharacterIndex:charIndex effectiveRange:effectiveCharRange context:nil];
192197

193198
// Background color for comments and preprocessor directives. Could query for nodeTypeAtCharacterIndex: but it's too slow.
194-
DVTPointerArray *colors = [[DVTFontAndColorTheme currentTheme] syntaxColorsByNodeType];
195-
if([color isEqual:[colors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxCommentNodeName]]] ||
196-
[color isEqual:[colors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxCommentDocNodeName]]] ||
197-
[color isEqual:[colors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxCommentDocKeywordNodeName]]])
199+
DVTPointerArray *editorColors = [[DVTFontAndColorTheme currentTheme] syntaxColorsByNodeType];
200+
if([color isEqual:[editorColors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxCommentNodeName]]] ||
201+
[color isEqual:[editorColors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxCommentDocNodeName]]] ||
202+
[color isEqual:[editorColors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxCommentDocKeywordNodeName]]])
198203
{
199204
return @{NSForegroundColorAttributeName : [self.theme sourceTextBackgroundColor], NSBackgroundColorAttributeName : self.commentColor};
200-
} else if([color isEqual:[colors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxPreprocessorNodeName]]]) {
205+
} else if([color isEqual:[editorColors pointerAtIndex:[DVTSourceNodeTypes registerNodeTypeNamed:kXcodeSyntaxPreprocessorNodeName]]]) {
201206
return @{NSForegroundColorAttributeName : [self.theme sourceTextBackgroundColor], NSBackgroundColorAttributeName : self.preprocessorColor};
202207
}
203208

209+
DVTPointerArray *minimapColors = [self.theme syntaxColorsByNodeType];
210+
color = [minimapColors pointerAtIndex:[editorColors indexOfPointerIdenticalTo:color]];
211+
204212
return @{NSForegroundColorAttributeName : color};
205213
}
206214

@@ -271,10 +279,19 @@ - (void)handleMouseEvent:(NSEvent *)theEvent
271279

272280
- (void)updateTheme
273281
{
274-
//DVTPreferenceSetManager *preferenceSetManager = [DVTFontAndColorTheme preferenceSetsManager];
275-
//NSArray *preferenceSet = [preferenceSetManager availablePreferenceSets];
276-
//self.theme = [preferenceSet lastObject];
277-
self.theme = [DVTFontAndColorTheme currentTheme];
282+
DVTPreferenceSetManager *preferenceSetManager = [DVTFontAndColorTheme preferenceSetsManager];
283+
NSArray *preferenceSet = [preferenceSetManager availablePreferenceSets];
284+
285+
NSString *themeName = [[NSUserDefaults standardUserDefaults] objectForKey:SCXodeMinimapTheme];
286+
NSUInteger themeIndex = [preferenceSet indexesOfObjectsPassingTest:^BOOL(DVTFontAndColorTheme *theme, NSUInteger idx, BOOL *stop) {
287+
return [theme.localizedName isEqualTo:themeName];
288+
}].lastIndex;
289+
290+
if(themeIndex == NSNotFound) {
291+
self.theme = [DVTFontAndColorTheme currentTheme];
292+
} else {
293+
self.theme = preferenceSet[themeIndex];
294+
}
278295

279296
NSColor *backgroundColor = [self.theme.sourceTextBackgroundColor shadowWithLevel:kBackgroundColorShadowLevel];
280297

SCXcodeMinimap/Xcode Headers/DVTPreferenceSetManager.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
NSMutableSet *_specifierNamesInDataStore;
1515
NSMutableArray *_userPreferenceSets;
1616
NSMutableArray *_availablePreferenceSets;
17-
id <DVTPreferenceSet> _currentPreferenceSet;
1817
DVTMapTable *_preferenceSetObserverTokens;
1918
DVTDelayedInvocation *_autosavePreferenceSetsInvocation;
2019
}
@@ -36,7 +35,6 @@
3635
- (id)_availablePreferenceSetWithLocalizedName:(id)arg1;
3736
- (id)preferenceSetForName:(id)arg1;
3837
@property(readonly) NSArray *builtInPreferenceSets;
39-
@property(retain) id <DVTPreferenceSet> currentPreferenceSet;
4038
- (void)_rebuildAvailableandUserSetsList;
4139
- (void)dealloc;
4240
- (id)initWithPreferenceSetClass:(Class)arg1;

0 commit comments

Comments
 (0)