|
| 1 | +// |
| 2 | +// UXToolbarStatusView.m |
| 3 | +// swift-progress |
| 4 | +// |
| 5 | +// Created by Helge Hess on 01.03.18. |
| 6 | +// Copyright © 2016-2018 ZeeZide. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +@import QuartzCore; |
| 10 | + |
| 11 | +#import "UXToolbarStatusView.h" |
| 12 | +#import "UXViewFactory.h" |
| 13 | + |
| 14 | +@implementation UXToolbarStatusView |
| 15 | +{ |
| 16 | + NSTextField *_tf; |
| 17 | + NSImageView *_iv; |
| 18 | + NSProgressIndicator *_pi; |
| 19 | + |
| 20 | + NSUInteger spinnerCount; |
| 21 | +} |
| 22 | + |
| 23 | +const CGFloat ZZToolbarCornerRadius = 4.0; |
| 24 | + |
| 25 | ++ (BOOL)requiresConstraintBasedLayout { |
| 26 | + return YES; |
| 27 | +} |
| 28 | + |
| 29 | +- (NSColor *)bottomColor { |
| 30 | + return [NSColor colorWithCalibratedRed:0.929 green:0.929 blue:0.929 alpha:1]; |
| 31 | +} |
| 32 | +- (NSColor *)topColor { |
| 33 | + return [NSColor colorWithCalibratedRed:1 green:1 blue:1 alpha:1]; |
| 34 | +} |
| 35 | + |
| 36 | +- (id)initWithFrame:(NSRect)frameRect { |
| 37 | + if ((self = [super initWithFrame:frameRect])) { |
| 38 | + self.wantsLayer = YES; |
| 39 | + self.layer.masksToBounds = YES; |
| 40 | + self.layer.cornerRadius = ZZToolbarCornerRadius; |
| 41 | + self.layer.borderWidth = 0.5; |
| 42 | + self.layer.borderColor = [NSColor lightGrayColor].CGColor; |
| 43 | + |
| 44 | + CAGradientLayer *gradient = [CAGradientLayer layer]; |
| 45 | + gradient.frame = self.bounds; |
| 46 | + gradient.colors = @[(id)self.topColor.CGColor,(id)self.bottomColor.CGColor]; |
| 47 | + gradient.autoresizingMask = (NSViewWidthSizable | NSViewHeightSizable); |
| 48 | + [self.layer addSublayer:gradient]; |
| 49 | + |
| 50 | + [self _zzCreateSubviews]; |
| 51 | + [self _zzCreateConstraints]; |
| 52 | + } |
| 53 | + return self; |
| 54 | +} |
| 55 | +- (id)init { |
| 56 | + // 23 seems to be right, not sure. 26 is better for small spinner |
| 57 | + // return [self initWithFrame:NSMakeRect(0, 0, 320, 26)]; |
| 58 | + // Xcode 8b size is 24px - also, buttons are always 24px high! |
| 59 | + // 25 seems to align properly with buttons. |
| 60 | + return [self initWithFrame:NSMakeRect(0, 0, 320, 25)]; |
| 61 | +} |
| 62 | + |
| 63 | +- (NSProgressIndicator *)makeProgressIndicator { |
| 64 | + NSProgressIndicator *v; |
| 65 | + |
| 66 | + v = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0,0,42,42)]; |
| 67 | + v.translatesAutoresizingMaskIntoConstraints = NO; |
| 68 | + |
| 69 | + v.indeterminate = YES; |
| 70 | + v.displayedWhenStopped = NO; |
| 71 | + v.bezeled = NO; |
| 72 | + v.controlSize = NSControlSizeSmall; // NSMiniControlSize; |
| 73 | + v.style = NSProgressIndicatorBarStyle; |
| 74 | + |
| 75 | + return v; |
| 76 | +} |
| 77 | + |
| 78 | +- (void)_zzCreateSubviews { |
| 79 | + #if 0 |
| 80 | + self->_iv = [[NSImageView alloc] initWithFrame:ZZInitialSize]; |
| 81 | + self->_iv.translatesAutoresizingMaskIntoConstraints = NO; |
| 82 | + #endif |
| 83 | + |
| 84 | + // @"ZeeCore | Build ZeeCore: Succeeded" |
| 85 | + UXViewFactory *vf = [UXViewFactory defaultViewFactory]; |
| 86 | + self->_tf = [vf makeLabel:@""]; |
| 87 | + |
| 88 | + self->_tf.cell.lineBreakMode = NSLineBreakByTruncatingTail; |
| 89 | + self->_tf.cell.wraps = NO; |
| 90 | + self->_tf.cell.usesSingleLineMode = YES; |
| 91 | + |
| 92 | + self->_tf.alignment = NSTextAlignmentLeft; |
| 93 | + self->_tf.translatesAutoresizingMaskIntoConstraints = NO; |
| 94 | + self->_tf.font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]]; |
| 95 | + self->_tf.textColor = [NSColor blackColor]; |
| 96 | + |
| 97 | + self->_pi = [self makeProgressIndicator]; |
| 98 | + |
| 99 | + if (self->_iv) [self addSubview:self->_iv]; |
| 100 | + if (self->_tf) [self addSubview:self->_tf]; |
| 101 | + if (self->_pi) [self addSubview:self->_pi]; |
| 102 | + |
| 103 | +#if 0 // TESTING |
| 104 | + [self->_pi startAnimation:nil]; |
| 105 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), |
| 106 | + dispatch_get_main_queue(), ^{ |
| 107 | + [self->_pi stopAnimation:nil]; |
| 108 | + }); |
| 109 | +#endif |
| 110 | +} |
| 111 | + |
| 112 | +static inline NSArray *vfl(NSString *_vfl, NSDictionary *_views) { |
| 113 | + return [NSLayoutConstraint constraintsWithVisualFormat:_vfl |
| 114 | + options:0 metrics:nil views:_views]; |
| 115 | +} |
| 116 | + |
| 117 | +- (void)_zzCreateConstraints { |
| 118 | + NSDictionary *nv = self->_iv |
| 119 | + ? @{ @"image": self->_iv, @"title": self->_tf, @"progress": self->_pi } |
| 120 | + : @{ @"title": self->_tf, @"progress": self->_pi }; |
| 121 | + |
| 122 | + NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:8]; |
| 123 | + |
| 124 | + /* horizontal */ |
| 125 | + |
| 126 | + if (self->_iv) |
| 127 | + [constraints addObjectsFromArray:vfl(@"H:|-[image]-[title]-|", nv)]; |
| 128 | + else |
| 129 | + [constraints addObjectsFromArray:vfl(@"H:|-10-[title]-|", nv)]; |
| 130 | + |
| 131 | + [constraints addObjectsFromArray:vfl(@"H:|[progress]|", nv)]; |
| 132 | + |
| 133 | + [_tf setContentHuggingPriority:230 |
| 134 | + forOrientation:NSLayoutConstraintOrientationHorizontal]; |
| 135 | + |
| 136 | + /* vertical */ |
| 137 | + // TODO: bulk activate |
| 138 | + if (self->_iv) |
| 139 | + [constraints addObject:[_iv.centerYAnchor constraintEqualToAnchor:self.centerYAnchor]]; |
| 140 | + |
| 141 | + [constraints addObject:[_tf.centerYAnchor constraintEqualToAnchor:self.centerYAnchor]]; |
| 142 | + |
| 143 | + [constraints addObjectsFromArray:vfl(@"V:[progress(==3)]|", nv)]; |
| 144 | + |
| 145 | + [NSLayoutConstraint activateConstraints:constraints]; |
| 146 | +} |
| 147 | + |
| 148 | +#pragma mark Children |
| 149 | + |
| 150 | +- (NSTextField *)textField { |
| 151 | + return self->_tf; |
| 152 | +} |
| 153 | + |
| 154 | +- (NSImageView *)imageView { |
| 155 | + return self->_iv; |
| 156 | +} |
| 157 | + |
| 158 | +- (NSProgressIndicator *)spinner { |
| 159 | + return self->_pi; |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +#pragma mark Spinner |
| 164 | + |
| 165 | +- (void)startSpinner { |
| 166 | + if (self->spinnerCount == 0) { |
| 167 | + [self->_pi startAnimation:self]; |
| 168 | + //self->_refreshButton.hidden = YES; |
| 169 | + //self->_tbView.imageView.hidden = YES; |
| 170 | + } |
| 171 | + self->spinnerCount++; |
| 172 | +} |
| 173 | +- (void)stopSpinner { |
| 174 | + if (self->spinnerCount == 0) return; |
| 175 | + |
| 176 | + self->spinnerCount--; |
| 177 | + if (self->spinnerCount == 0) { |
| 178 | + [self->_pi stopAnimation:self]; |
| 179 | + //self->_refreshButton.hidden = NO; |
| 180 | + } |
| 181 | +} |
| 182 | +- (void)fullstopSpinner { |
| 183 | + self->spinnerCount = 0; |
| 184 | + [self->_pi stopAnimation:self]; |
| 185 | +} |
| 186 | + |
| 187 | +#pragma mark Title |
| 188 | + |
| 189 | +- (void)setStringValue:(NSString *)_value { |
| 190 | + self->_tf.stringValue = _value; |
| 191 | +} |
| 192 | +- (NSString *)stringValue { |
| 193 | + return self->_tf.stringValue; |
| 194 | +} |
| 195 | + |
| 196 | +@end |
| 197 | + |
0 commit comments