summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamiano Galassi <[email protected]>2016-12-30 09:01:21 +0100
committerDamiano Galassi <[email protected]>2016-12-30 09:01:21 +0100
commit6c9569df06635b942248d4a30897bae07280b765 (patch)
tree371073b3b0254d38478b87d42c818218a9dad52e
parent2dd9968af2db70243337085bfa3de46fa580f9b1 (diff)
MacGui: cache the text attributes in DockTextField to avoid recreating the same identical object each time the textfield is drawn.
-rw-r--r--macosx/DockTextField.h8
-rw-r--r--macosx/DockTextField.m84
2 files changed, 54 insertions, 38 deletions
diff --git a/macosx/DockTextField.h b/macosx/DockTextField.h
index ea9342bc4..cca7bd074 100644
--- a/macosx/DockTextField.h
+++ b/macosx/DockTextField.h
@@ -8,10 +8,10 @@
@interface DockTextField : NSTextField
-@property (nonatomic,strong) NSString *textToDisplay;
-@property (nonatomic,strong) NSColor *startColor;
-@property (nonatomic,strong) NSColor *endColor;
+@property (nonatomic, copy) NSString *textToDisplay;
+@property (nonatomic, copy) NSColor *startColor;
+@property (nonatomic, copy) NSColor *endColor;
-- (void)changeGradientColors:(NSColor*)startColor endColor:(NSColor*)endColor;
+- (void)changeGradientColors:(NSColor *)startColor endColor:(NSColor *)endColor;
@end
diff --git a/macosx/DockTextField.m b/macosx/DockTextField.m
index 5ce10da5e..bf6bc5444 100644
--- a/macosx/DockTextField.m
+++ b/macosx/DockTextField.m
@@ -9,11 +9,16 @@
#define DOCK_TEXTFIELD_ALPHA 0.8
#define DOCK_TEXTFIELD_FONTSIZE 28.0
-@implementation DockTextField
+@interface DockTextField ()
+
+@property (nonatomic, readonly) NSDictionary *textAttributes;
+@property (nonatomic, readonly) NSDictionary *smallTextAttributes;
+
+@property (nonatomic, readwrite) NSGradient *gradient;
+
+@end
-@synthesize textToDisplay = _textToDisplay;
-@synthesize startColor = _startColor;
-@synthesize endColor = _endColor;
+@implementation DockTextField
- (instancetype)initWithFrame:(NSRect)frame
{
@@ -21,16 +26,40 @@
if (self) {
[[self cell] setBezelStyle:NSRoundedBezelStyle];
_textToDisplay = @"";
+ _textAttributes = [self textAttributesWithFontSize:DOCK_TEXTFIELD_FONTSIZE];
+ _smallTextAttributes = [self textAttributesWithFontSize:DOCK_TEXTFIELD_FONTSIZE - 2];
[self changeGradientColors:[NSColor grayColor] endColor:[NSColor blackColor]];
}
return self;
}
-- (void)changeGradientColors:(NSColor*)startColor endColor:(NSColor*)endColor
+- (NSDictionary *)textAttributesWithFontSize:(CGFloat)fontSize
+{
+ NSShadow *shadow = [[NSShadow alloc] init];
+ shadow.shadowColor = [NSColor blackColor];
+ shadow.shadowOffset = NSMakeSize(2, -2);
+ shadow.shadowBlurRadius = 6;
+
+ NSFont *font;
+ if ([[NSFont class] respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
+ // On macOS 10.11+ the monospaced digit system is available.
+ font = [NSFont monospacedDigitSystemFontOfSize:fontSize weight:NSFontWeightBold];
+ } else {
+ // macOS 10.10- use the default system font.
+ font = [NSFont boldSystemFontOfSize:fontSize];
+ }
+
+ return @{ NSForegroundColorAttributeName: [NSColor whiteColor],
+ NSFontAttributeName: font,
+ NSShadowAttributeName: shadow};
+}
+
+- (void)changeGradientColors:(NSColor *)startColor endColor:(NSColor *)endColor
{
self.startColor = [startColor colorWithAlphaComponent:DOCK_TEXTFIELD_ALPHA];
self.endColor = [endColor colorWithAlphaComponent:DOCK_TEXTFIELD_ALPHA];
+ self.gradient = [[NSGradient alloc] initWithStartingColor:self.startColor endingColor:self.endColor];
}
- (void)drawRect:(NSRect)dirtyRect
@@ -38,40 +67,27 @@
if (self.isHidden)
return;
- NSFont *font;
- if ([[NSFont class] respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
- // On macOS 10.11+ the monospaced digit system is available.
- font = [NSFont monospacedDigitSystemFontOfSize:DOCK_TEXTFIELD_FONTSIZE weight:NSFontWeightBold];
- } else {
- // macOS 10.10- use the default system font.
- font = [NSFont boldSystemFontOfSize:DOCK_TEXTFIELD_FONTSIZE];
- }
-
- NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0);
+ NSSize size = self.bounds.size;
+ NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, size.width, size.height - 1.0);
double radius = self.bounds.size.height / 2;
- NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:self.startColor endingColor:self.endColor];
- [gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:radius yRadius:radius] angle:90];
-
- NSMutableDictionary *drawStringAttributes = [[NSMutableDictionary alloc] init];
- [drawStringAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
- [drawStringAttributes setValue:font forKey:NSFontAttributeName];
- NSShadow *stringShadow = [[NSShadow alloc] init];
- [stringShadow setShadowColor:[NSColor blackColor]];
- NSSize shadowSize;
- shadowSize.width = 2;
- shadowSize.height = -2;
- [stringShadow setShadowOffset:shadowSize];
- [stringShadow setShadowBlurRadius:6];
- [drawStringAttributes setValue:stringShadow forKey:NSShadowAttributeName];
-
- NSString *MRString = _textToDisplay;
- NSString *budgetString = [NSString stringWithFormat:@"%@", MRString];
- NSSize stringSize = [budgetString sizeWithAttributes:drawStringAttributes];
+ [self.gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:radius yRadius:radius] angle:90];
+
+ NSDictionary *attributes = self.textAttributes;
+ NSString *budgetString = _textToDisplay;
+ NSSize stringSize = [budgetString sizeWithAttributes:attributes];
+
+ if (size.width - 4 < stringSize.width)
+ {
+ attributes = self.smallTextAttributes;
+ stringSize = [budgetString sizeWithAttributes:attributes];
+ }
+
NSPoint centerPoint;
centerPoint.x = (dirtyRect.size.width / 2) - (stringSize.width / 2);
centerPoint.y = dirtyRect.size.height / 2 - (stringSize.height / 2) - 2;
- [budgetString drawAtPoint:centerPoint withAttributes:drawStringAttributes];
+
+ [budgetString drawAtPoint:centerPoint withAttributes:attributes];
}
@end