Skip to content

Commit c60649a

Browse files
committed
Better contrasts for length button
1 parent f0d6ec0 commit c60649a

File tree

3 files changed

+172
-129
lines changed

3 files changed

+172
-129
lines changed

Classes/HOStringHelper.m

Lines changed: 129 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,28 @@ - (NSString *)unescapeString:(NSString *)string {
108108
#pragma mark - Plugin Initialization
109109

110110
+ (void)pluginDidLoad:(NSBundle *)plugin {
111-
static id sharedPlugin = nil;
112-
static dispatch_once_t onceToken;
113-
dispatch_once(&onceToken, ^{
114-
sharedPlugin = [[self alloc] init];
115-
});
111+
static id sharedPlugin = nil;
112+
static dispatch_once_t onceToken;
113+
dispatch_once(&onceToken, ^{
114+
sharedPlugin = [[self alloc] init];
115+
});
116116
}
117117

118118
- (id)init {
119-
if (self = [super init]) {
120-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidFinishLaunching:) name:NSApplicationDidFinishLaunchingNotification object:nil];
121-
_selectedStringRange = NSMakeRange(NSNotFound, 0);
119+
if (self = [super init]) {
120+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidFinishLaunching:) name:NSApplicationDidFinishLaunchingNotification object:nil];
121+
_selectedStringRange = NSMakeRange(NSNotFound, 0);
122122
_stringRegex = [NSRegularExpression regularExpressionWithPattern:@"\"((\\\\\"|.)*?)\""
123-
options:0
124-
error:NULL];
125-
}
126-
return self;
123+
options:0
124+
error:NULL];
125+
}
126+
return self;
127127
}
128128

129129
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
130-
NSMenuItem *editMenuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"];
131-
if (editMenuItem) {
132-
[[editMenuItem submenu] addItem:[NSMenuItem separatorItem]];
130+
NSMenuItem *editMenuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"];
131+
if (editMenuItem) {
132+
[[editMenuItem submenu] addItem:[NSMenuItem separatorItem]];
133133

134134
{
135135
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Enable Strings Popover" action:@selector(toggleColorHighlightingEnabled:) keyEquivalent:@""];
@@ -146,130 +146,131 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification {
146146
// NSMenuItem *insertColorMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Insert Color..." action:@selector(insertColor:) keyEquivalent:@""] autorelease];
147147
// [insertColorMenuItem setTarget:self];
148148
// [[editMenuItem submenu] addItem:insertColorMenuItem];
149-
}
149+
}
150150

151-
BOOL highlightingEnabled = ![[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
152-
if (highlightingEnabled) {
151+
BOOL highlightingEnabled = ![[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
152+
if (highlightingEnabled) {
153153
[self activateColorHighlighting];
154-
}
154+
}
155155
}
156156

157157
#pragma mark - Preferences
158158

159159
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
160-
if ([menuItem action] == @selector(showPopover:)) {
160+
if ([menuItem action] == @selector(showPopover:)) {
161161
return ![_stringPopover isShown];
162-
}
162+
}
163163
else if ([menuItem action] == @selector(toggleColorHighlightingEnabled:)) {
164-
BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
165-
[menuItem setState:enabled ? NSOffState : NSOnState];
166-
return YES;
164+
BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
165+
[menuItem setState:enabled ? NSOffState : NSOnState];
166+
return YES;
167167
}
168-
return YES;
168+
return YES;
169169
}
170170

171171
- (void)toggleColorHighlightingEnabled:(id)sender {
172-
BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
173-
[[NSUserDefaults standardUserDefaults] setBool:!enabled forKey:kHOStringHelperHighlightingDisabled];
174-
if (enabled) {
175-
[self activateColorHighlighting];
176-
} else {
177-
[self deactivateColorHighlighting];
178-
}
172+
BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
173+
[[NSUserDefaults standardUserDefaults] setBool:!enabled forKey:kHOStringHelperHighlightingDisabled];
174+
if (enabled) {
175+
[self activateColorHighlighting];
176+
} else {
177+
[self deactivateColorHighlighting];
178+
}
179179
}
180180

181181
- (void)activateColorHighlighting {
182-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectionDidChange:) name:NSTextViewDidChangeSelectionNotification object:nil];
183-
if (!self.textView) {
184-
NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
185-
if ([firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [firstResponder isKindOfClass:[NSTextView class]]) {
186-
self.textView = (NSTextView *)firstResponder;
187-
}
188-
}
189-
if (self.textView) {
190-
NSNotification *notification = [NSNotification notificationWithName:NSTextViewDidChangeSelectionNotification object:self.textView];
191-
[self selectionDidChange:notification];
192-
193-
}
182+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectionDidChange:) name:NSTextViewDidChangeSelectionNotification object:nil];
183+
if (!self.textView) {
184+
NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
185+
if ([firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [firstResponder isKindOfClass:[NSTextView class]]) {
186+
self.textView = (NSTextView *)firstResponder;
187+
}
188+
}
189+
if (self.textView) {
190+
NSNotification *notification = [NSNotification notificationWithName:NSTextViewDidChangeSelectionNotification object:self.textView];
191+
[self selectionDidChange:notification];
192+
193+
}
194194
}
195195

196196
- (void)deactivateColorHighlighting {
197-
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSTextViewDidChangeSelectionNotification object:nil];
198-
[self dismissPopover];
199-
self.textView = nil;
197+
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSTextViewDidChangeSelectionNotification object:nil];
198+
[self dismissPopover];
199+
self.textView = nil;
200200
}
201201

202202
#pragma mark - Text Selection Handling
203203

204204
- (void)selectionDidChange:(NSNotification *)notification {
205205
// NSLog(@"%s", __PRETTY_FUNCTION__);
206-
if ([[notification object] isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [[notification object] isKindOfClass:[NSTextView class]]) {
207-
self.textView = (NSTextView *)[notification object];
208-
BOOL disabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
209-
if (disabled) {
206+
if ([[notification object] isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [[notification object] isKindOfClass:[NSTextView class]]) {
207+
self.textView = (NSTextView *)[notification object];
208+
BOOL disabled = [[NSUserDefaults standardUserDefaults] boolForKey:kHOStringHelperHighlightingDisabled];
209+
if (disabled) {
210210
return;
211211
}
212-
NSArray *selectedRanges = [self.textView selectedRanges];
213-
if (selectedRanges.count >= 1) {
214-
NSRange selectedRange = [[selectedRanges objectAtIndex:0] rangeValue];
215-
NSString *text = self.textView.textStorage.string;
216-
NSRange lineRange = [text lineRangeForRange:selectedRange];
217-
NSRange selectedRangeInLine = NSMakeRange(selectedRange.location - lineRange.location, selectedRange.length);
218-
NSString *line = [text substringWithRange:lineRange];
219-
NSRange stringRange = NSNullRange;
212+
NSArray *selectedRanges = [self.textView selectedRanges];
213+
if (selectedRanges.count >= 1) {
214+
NSRange selectedRange = [[selectedRanges objectAtIndex:0] rangeValue];
215+
NSString *text = self.textView.textStorage.string;
216+
NSRange lineRange = [text lineRangeForRange:selectedRange];
217+
NSRange selectedRangeInLine = NSMakeRange(selectedRange.location - lineRange.location, selectedRange.length);
218+
NSString *line = [text substringWithRange:lineRange];
219+
NSRange stringRange = NSNullRange;
220220

221221
self.selectedStringContent = [self stringInText:line selectedRange:selectedRangeInLine matchedRange:&stringRange];
222-
if (_selectedStringContent && [_selectedStringContent length] >= 2) {
222+
if (_selectedStringContent && [_selectedStringContent length] >= 2) {
223223

224224
// String's content
225225
NSInteger oldLocation = _selectedStringRange.location;
226226
self.selectedStringContent = [_selectedStringContent substringWithRange:NSMakeRange(1, _selectedStringContent.length - 2)];
227-
self.selectedStringRange = NSMakeRange(stringRange.location + lineRange.location, stringRange.length);
227+
self.selectedStringRange = NSMakeRange(stringRange.location + lineRange.location, stringRange.length);
228228
if(oldLocation != _selectedStringRange.location) {
229229
[self dismissPopover];
230230
}
231231

232232
// Color calculations based ion Xcode theme
233-
NSColor *backgroundColor = [self.textView.backgroundColor colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
234-
CGFloat r = 1.0; CGFloat g = 1.0; CGFloat b = 1.0;
235-
[backgroundColor getRed:&r green:&g blue:&b alpha:NULL];
236-
CGFloat backgroundLuminance = (r + g + b) / 3.0;
237-
NSColor *strokeColor = (backgroundLuminance > 0.5) ? [NSColor colorWithCalibratedWhite:0.5 alpha:0.4] : [NSColor colorWithCalibratedWhite:1.000 alpha:0.4];
238-
239-
// Button's label
240-
NSString * aString = [NSString stringWithFormat:@"%d", (int)[[self unescapeString:_selectedStringContent] length]];
241-
NSMutableDictionary * aAttributes = [NSMutableDictionary dictionary];
242-
NSMutableParagraphStyle * aStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
243-
aStyle.alignment = NSCenterTextAlignment;
244-
[aAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
245-
[aAttributes setValue:[NSFont boldSystemFontOfSize:11] forKey:NSFontAttributeName];
246-
[aAttributes setValue:aStyle forKey:NSParagraphStyleAttributeName];
247-
NSAttributedString * aAttributedString = [[NSAttributedString alloc] initWithString:aString attributes:aAttributes];
248-
self.stringButton.attributedTitle = aAttributedString;
249-
self.stringButton.strokeColor = strokeColor;
250-
233+
NSColor *backgroundColor = [self.textView.backgroundColor colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
234+
CGFloat r = 1.0; CGFloat g = 1.0; CGFloat b = 1.0;
235+
[backgroundColor getRed:&r green:&g blue:&b alpha:NULL];
236+
CGFloat backgroundLuminance = (r + g + b) / 3.0;
237+
NSColor *strokeColor = (backgroundLuminance > 0.5) ? [NSColor colorWithCalibratedWhite:0.5 alpha:1] : [NSColor colorWithCalibratedWhite:1.000 alpha:1];
238+
239+
// Button's label
240+
NSString * aString = [NSString stringWithFormat:@"%d", (int)[[self unescapeString:_selectedStringContent] length]];
241+
NSMutableDictionary * aAttributes = [NSMutableDictionary dictionary];
242+
NSMutableParagraphStyle * aStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
243+
aStyle.alignment = NSCenterTextAlignment;
244+
[aAttributes setValue:(backgroundLuminance > 0.5) ? [NSColor whiteColor] : [NSColor blackColor]
245+
forKey:NSForegroundColorAttributeName];
246+
[aAttributes setValue:[NSFont boldSystemFontOfSize:11] forKey:NSFontAttributeName];
247+
[aAttributes setValue:aStyle forKey:NSParagraphStyleAttributeName];
248+
NSAttributedString * aAttributedString = [[NSAttributedString alloc] initWithString:aString attributes:aAttributes];
249+
self.stringButton.attributedTitle = aAttributedString;
250+
self.stringButton.strokeColor = strokeColor;
251+
251252
// Place button
252-
NSRect selectionRectOnScreen = [self.textView firstRectForCharacterRange:self.selectedStringRange];
253-
NSRect selectionRectInWindow = [self.textView.window convertRectFromScreen:selectionRectOnScreen];
254-
NSRect selectionRectInView = [self.textView convertRect:selectionRectInWindow fromView:nil];
255-
256-
CGFloat Width = [aAttributedString size].width + 14;
257-
NSRect buttonRect = NSMakeRect(NSMinX(selectionRectInView), NSMinY(selectionRectInView) - selectionRectInView.size.height - 2, Width, selectionRectInView.size.height);
258-
self.stringButton.frame = NSIntegralRect(buttonRect);
253+
NSRect selectionRectOnScreen = [self.textView firstRectForCharacterRange:self.selectedStringRange];
254+
NSRect selectionRectInWindow = [self.textView.window convertRectFromScreen:selectionRectOnScreen];
255+
NSRect selectionRectInView = [self.textView convertRect:selectionRectInWindow fromView:nil];
259256

257+
CGFloat Width = [aAttributedString size].width + 14;
258+
NSRect buttonRect = NSMakeRect(NSMinX(selectionRectInView), NSMinY(selectionRectInView) - selectionRectInView.size.height - 2, Width, selectionRectInView.size.height);
259+
self.stringButton.frame = NSIntegralRect(buttonRect);
260260

261-
[self.textView addSubview:self.stringButton];
261+
262+
[self.textView addSubview:self.stringButton];
262263

263264
// Draw the frame around the string
264-
self.stringFrameView.frame = NSInsetRect(NSIntegralRect(selectionRectInView), -1, -1);
265-
self.stringFrameView.color = strokeColor;
266-
[self.textView addSubview:self.stringFrameView];
265+
self.stringFrameView.frame = NSInsetRect(NSIntegralRect(selectionRectInView), -1, -1);
266+
self.stringFrameView.color = strokeColor;
267+
[self.textView addSubview:self.stringFrameView];
267268

268269
return;
269-
}
270-
}
270+
}
271+
}
271272
[self removeSelection];
272-
}
273+
}
273274
}
274275

275276
- (void)dismissPopover {
@@ -282,17 +283,17 @@ - (void)dismissPopover {
282283
- (void)removeSelection {
283284
// NSLog(@"%s", __PRETTY_FUNCTION__);
284285
[self dismissPopover];
285-
[self.stringButton removeFromSuperview];
286-
[self.stringFrameView removeFromSuperview];
287-
self.selectedStringRange = NSNullRange;
288-
self.selectedStringContent = nil;
286+
[self.stringButton removeFromSuperview];
287+
[self.stringFrameView removeFromSuperview];
288+
self.selectedStringRange = NSNullRange;
289+
self.selectedStringContent = nil;
289290
}
290291

291292
- (void)stringDidChange:(id)sender {
292293
// NSLog(@"%s", __PRETTY_FUNCTION__);
293-
if (self.selectedStringRange.location == NSNotFound) {
294-
return;
295-
}
294+
if (self.selectedStringRange.location == NSNotFound) {
295+
return;
296+
}
296297
NSTextField *textfield = (id)_stringPopoverViewController.view;
297298
NSString *result = textfield.stringValue;
298299
if(result) {
@@ -339,49 +340,49 @@ - (void)showPopover:(id)sender {
339340
#pragma mark - View Initialization
340341

341342
- (HOStringInfoButton *)stringButton {
342-
if (!_stringButton) {
343-
_stringButton = [[HOStringInfoButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 30)];
344-
[_stringButton setTarget:self];
345-
[_stringButton setAction:@selector(showPopover:)];
346-
}
347-
return _stringButton;
343+
if (!_stringButton) {
344+
_stringButton = [[HOStringInfoButton alloc] initWithFrame:NSMakeRect(0, 0, 100, 30)];
345+
[_stringButton setTarget:self];
346+
[_stringButton setAction:@selector(showPopover:)];
347+
}
348+
return _stringButton;
348349
}
349350

350351
- (HOStringFrameView *)stringFrameView {
351-
if (!_stringFrameView) {
352-
_stringFrameView = [[HOStringFrameView alloc] initWithFrame:NSZeroRect];
353-
}
354-
return _stringFrameView;
352+
if (!_stringFrameView) {
353+
_stringFrameView = [[HOStringFrameView alloc] initWithFrame:NSZeroRect];
354+
}
355+
return _stringFrameView;
355356
}
356357

357358
#pragma mark - Color String Parsing
358359

359360
- (NSString *)stringInText:(NSString *)text selectedRange:(NSRange)selectedRange matchedRange:(NSRangePointer)matchedRange {
360-
__block NSString *foundStringContent = nil;
361-
__block NSRange foundColorRange = NSMakeRange(NSNotFound, 0);
362-
[_stringRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
363-
NSRange colorRange = [result range];
364-
if (selectedRange.location >= colorRange.location + 1 && NSMaxRange(selectedRange) <= NSMaxRange(colorRange) - 1) {
365-
foundStringContent = [text substringWithRange:[result rangeAtIndex:0]];
366-
colorRange.location++;
367-
colorRange.length -= 2;
368-
foundColorRange = colorRange;
369-
*stop = YES;
370-
}
371-
}];
361+
__block NSString *foundStringContent = nil;
362+
__block NSRange foundColorRange = NSMakeRange(NSNotFound, 0);
363+
[_stringRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
364+
NSRange colorRange = [result range];
365+
if (selectedRange.location >= colorRange.location + 1 && NSMaxRange(selectedRange) <= NSMaxRange(colorRange) - 1) {
366+
foundStringContent = [text substringWithRange:[result rangeAtIndex:0]];
367+
colorRange.location++;
368+
colorRange.length -= 2;
369+
foundColorRange = colorRange;
370+
*stop = YES;
371+
}
372+
}];
372373
if (foundStringContent) {
373-
if (matchedRange != NULL) {
374-
*matchedRange = foundColorRange;
375-
}
376-
return foundStringContent;
377-
}
374+
if (matchedRange != NULL) {
375+
*matchedRange = foundColorRange;
376+
}
377+
return foundStringContent;
378+
}
378379
return nil;
379380
}
380381

381382
#pragma mark -
382383

383384
- (void)dealloc {
384-
[[NSNotificationCenter defaultCenter] removeObserver:self];
385+
[[NSNotificationCenter defaultCenter] removeObserver:self];
385386
[self dismissPopover];
386387
}
387388

HOStringSense.xcodeproj/project.pbxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
name = HOStringSenseUnitTests;
200200
productName = HOStringSenseUnitTests;
201201
productReference = 435286B41677D87200A7E837 /* HOStringSenseUnitTests.octest */;
202-
productType = "com.apple.product-type.bundle";
202+
productType = "com.apple.product-type.bundle.ocunit-test";
203203
};
204204
8D5B49AC048680CD000E48DA /* HOStringSense */ = {
205205
isa = PBXNativeTarget;
@@ -226,6 +226,7 @@
226226
089C1669FE841209C02AAC07 /* Project object */ = {
227227
isa = PBXProject;
228228
attributes = {
229+
LastTestingUpgradeCheck = 0610;
229230
LastUpgradeCheck = 0510;
230231
};
231232
buildConfigurationList = 1DEB913E08733D840010E9CD /* Build configuration list for PBXProject "HOStringSense" */;

0 commit comments

Comments
 (0)