]> granicus.if.org Git - handbrake/commitdiff
MacGui: cache the text attributes in DockTextField to avoid recreating the same ident...
authorDamiano Galassi <damiog@gmail.com>
Fri, 30 Dec 2016 08:01:21 +0000 (09:01 +0100)
committerDamiano Galassi <damiog@gmail.com>
Fri, 30 Dec 2016 08:01:21 +0000 (09:01 +0100)
macosx/DockTextField.h
macosx/DockTextField.m

index ea9342bc49fa3787dff074d581178100f215eb16..cca7bd074337532d86a3bba1c8127c0acfc81b50 100644 (file)
@@ -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
index 5ce10da5e71f4222eec49f5d48f43a95d7df2399..bf6bc544412207c3a73374dcd4d8240fc7a879e2 100644 (file)
@@ -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
 {
     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
     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