|
| 1 | +/* Copyright © 2007-2009, The Sequential Project |
| 2 | +All rights reserved. |
| 3 | +
|
| 4 | +Redistribution and use in source and binary forms, with or without |
| 5 | +modification, are permitted provided that the following conditions are met: |
| 6 | + * Redistributions of source code must retain the above copyright |
| 7 | + notice, this list of conditions and the following disclaimer. |
| 8 | + * Redistributions in binary form must reproduce the above copyright |
| 9 | + notice, this list of conditions and the following disclaimer in the |
| 10 | + documentation and/or other materials provided with the distribution. |
| 11 | + * Neither the name of the the Sequential Project nor the |
| 12 | + names of its contributors may be used to endorse or promote products |
| 13 | + derived from this software without specific prior written permission. |
| 14 | +
|
| 15 | +THIS SOFTWARE IS PROVIDED BY THE SEQUENTIAL PROJECT ''AS IS'' AND ANY |
| 16 | +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | +DISCLAIMED. IN NO EVENT SHALL THE SEQUENTIAL PROJECT BE LIABLE FOR ANY |
| 19 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
| 25 | +#import "PGThumbnailInfoView.h" |
| 26 | + |
| 27 | +// Other Sources |
| 28 | +#import "PGAppKitAdditions.h" |
| 29 | +#import "PGFoundationAdditions.h" |
| 30 | +#import "PGDocumentController.h" // for thumbnail userDefault keys |
| 31 | + |
| 32 | +// 2023/10/02 for _infoWindow: |
| 33 | +typedef enum SizeFormat { SizeFormatNone, SizeFormatBase10, SizeFormatBase2, SizeFormatBytes } SizeFormat; |
| 34 | +extern NSString* StringForByteSizeWithFormat(SizeFormat format, uint64_t bytes, int nDecimalDigits); |
| 35 | +extern NSString* StringForImageCount(NSUInteger const imageCount); |
| 36 | +extern NSInteger GetThumbnailSizeFormat(void); |
| 37 | + |
| 38 | +static |
| 39 | +NSString* |
| 40 | +StringForDisplay(NSUInteger imageCount, uint64_t byteSizeTotal) { |
| 41 | + enum { DECIMAL_DIGITS = 2 }; |
| 42 | + return [NSString stringWithFormat:@"%@ %@", StringForImageCount(imageCount), |
| 43 | + StringForByteSizeWithFormat((SizeFormat) (1 + GetThumbnailSizeFormat()), |
| 44 | + byteSizeTotal, DECIMAL_DIGITS)]; |
| 45 | +} |
| 46 | + |
| 47 | +@implementation PGThumbnailInfoView |
| 48 | + |
| 49 | +- (id)initWithFrame:(NSRect)frameRect { |
| 50 | + if((self = [super initWithFrame:frameRect])) { |
| 51 | + NSUserDefaults *sud = NSUserDefaults.standardUserDefaults; |
| 52 | + [sud addObserver:self |
| 53 | + forKeyPath:PGThumbnailSizeFormatKey |
| 54 | + options:kNilOptions |
| 55 | + context:NULL]; |
| 56 | + } |
| 57 | + return self; |
| 58 | +} |
| 59 | + |
| 60 | +- (NSAttributedString *)attributedStringValue |
| 61 | +{ |
| 62 | + NSMutableParagraphStyle *const style = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease]; |
| 63 | + [style setAlignment:NSTextAlignmentCenter]; |
| 64 | + [style setLineBreakMode:NSLineBreakByTruncatingMiddle]; |
| 65 | + return [[[NSAttributedString alloc] initWithString:StringForDisplay(_imageCount, _byteSizeTotal) |
| 66 | + attributes:@{ |
| 67 | + NSFontAttributeName: [NSFont labelFontOfSize:0.0f], |
| 68 | + NSForegroundColorAttributeName: NSColor.whiteColor, |
| 69 | + NSParagraphStyleAttributeName: style, |
| 70 | + }] autorelease]; |
| 71 | +} |
| 72 | +- (void)setImageCount:(NSUInteger)imageCount byteSizeTotal:(uint64_t)byteSizeTotal { |
| 73 | + _imageCount = imageCount; |
| 74 | + _byteSizeTotal = byteSizeTotal; |
| 75 | + |
| 76 | + self.needsDisplay = YES; |
| 77 | +} |
| 78 | + |
| 79 | +#pragma mark - NSView |
| 80 | + |
| 81 | +- (BOOL)isFlipped |
| 82 | +{ |
| 83 | + return NO; |
| 84 | +} |
| 85 | + |
| 86 | +#define PGMarginSize 4.0f // Outside the window. |
| 87 | +#define PGPaddingSize 2.0f // Inside the window. |
| 88 | +#define PGTotalPaddingSize (PGPaddingSize * 2.0f) |
| 89 | +#define PGTextBottomPadding (PGPaddingSize - 1.0f) |
| 90 | +#define PGTextTotalVertPadding (PGPaddingSize + PGTextBottomPadding) |
| 91 | +#define PGTextHorzPadding 4.0f |
| 92 | +#define PGTextTotalHorzPadding (PGTextHorzPadding * 2.0f) |
| 93 | + |
| 94 | +- (void)drawRect:(NSRect)aRect |
| 95 | +{ |
| 96 | + NSRect const b = [self bounds]; |
| 97 | + |
| 98 | + [[NSColor PG_bezelBackgroundColor] set]; |
| 99 | + [[NSBezierPath bezierPathWithRect:b] fill]; |
| 100 | + |
| 101 | + [[NSColor PG_bezelForegroundColor] set]; |
| 102 | + [[self attributedStringValue] drawInRect:NSMakeRect(NSMinX(b) + PGPaddingSize + PGTextHorzPadding, |
| 103 | + NSMinY(b) + PGTextBottomPadding, |
| 104 | + NSWidth(b) - PGTotalPaddingSize - PGTextTotalHorzPadding, |
| 105 | + NSHeight(b) - PGTextTotalVertPadding)]; |
| 106 | +} |
| 107 | + |
| 108 | +#pragma mark - NSObject |
| 109 | + |
| 110 | +- (void)dealloc |
| 111 | +{ |
| 112 | + NSUserDefaults *sud = NSUserDefaults.standardUserDefaults; |
| 113 | + [sud removeObserver:self forKeyPath:PGThumbnailSizeFormatKey]; |
| 114 | + |
| 115 | + [super dealloc]; |
| 116 | +} |
| 117 | + |
| 118 | +#pragma mark - NSObject(NSKeyValueObserving) |
| 119 | + |
| 120 | +- (void)observeValueForKeyPath:(NSString *)keyPath |
| 121 | + ofObject:(id)object |
| 122 | + change:(NSDictionary *)change |
| 123 | + context:(void *)context |
| 124 | +{ |
| 125 | + if(PGEqualObjects(keyPath, PGThumbnailSizeFormatKey)) |
| 126 | + self.needsDisplay = YES; |
| 127 | + else |
| 128 | + [super observeValueForKeyPath:keyPath |
| 129 | + ofObject:object |
| 130 | + change:change |
| 131 | + context:context]; |
| 132 | +} |
| 133 | + |
| 134 | +#pragma mark - <PGBezelPanelContentView> |
| 135 | + |
| 136 | +- (NSRect)bezelPanel:(PGBezelPanel *)sender frameForContentRect:(NSRect)aRect scale:(CGFloat)scaleFactor |
| 137 | +{ |
| 138 | + NSSize const messageSize = [[self attributedStringValue] size]; |
| 139 | + CGFloat const scaledMarginSize = PGMarginSize * scaleFactor; |
| 140 | + NSRect const frame = NSIntersectionRect( |
| 141 | + NSMakeRect( |
| 142 | + NSMinX(aRect) + scaledMarginSize, |
| 143 | + NSMinY(aRect) + scaledMarginSize, |
| 144 | + ceilf((messageSize.width + PGTextTotalHorzPadding + messageSize.width + PGTotalPaddingSize) * scaleFactor), |
| 145 | + ceilf(MAX(messageSize.height + PGTextTotalVertPadding, messageSize.height) * scaleFactor)), |
| 146 | + NSInsetRect(aRect, scaledMarginSize, scaledMarginSize)); |
| 147 | + return frame; |
| 148 | +} |
| 149 | + |
| 150 | +@end |
0 commit comments