forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTermIndicatorsHelper.m
277 lines (241 loc) · 10.9 KB
/
iTermIndicatorsHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// iTermIndicatorsHelper.m
// iTerm2
//
// Created by George Nachman on 11/23/14.
//
//
#import "iTermIndicatorsHelper.h"
#import "DebugLogging.h"
#import "iTermAdvancedSettingsModel.h"
#import "NSImage+iTerm.h"
static NSDictionary *gIndicatorImages;
NSString *const kiTermIndicatorBell = @"kiTermIndicatorBell";
NSString *const kiTermIndicatorWrapToTop = @"kiTermIndicatorWrapToTop";
NSString *const kiTermIndicatorWrapToBottom = @"kiTermIndicatorWrapToBottom";
NSString *const kItermIndicatorBroadcastInput = @"kItermIndicatorBroadcastInput";
NSString *const kiTermIndicatorMaximized = @"kiTermIndicatorMaximized";
NSString *const kiTermIndicatorCoprocess = @"kiTermIndicatorCoprocess";
NSString *const kiTermIndicatorAlert = @"kiTermIndicatorAlert";
NSString *const kiTermIndicatorAllOutputSuppressed = @"kiTermIndicatorAllOutputSuppressed";
NSString *const kiTermIndicatorZoomedIn = @"kiTermIndicatorZoomedIn";
NSString *const kiTermIndicatorCopyMode = @"kiTermIndicatorCopyMode";
static const NSTimeInterval kFullScreenFlashDuration = 0.3;
static const NSTimeInterval kFlashDuration = 0.3;
CGFloat kiTermIndicatorStandardHeight = 20;
@interface iTermIndicator : NSObject
@property(nonatomic, retain) NSImage *image;
@property(nonatomic, readonly) CGFloat alpha;
- (void)startFlash;
@end
@implementation iTermIndicator {
NSTimeInterval _flashStartTime;
}
- (void)dealloc {
[_image release];
[super dealloc];
}
- (void)startFlash {
_flashStartTime = [NSDate timeIntervalSinceReferenceDate];
}
- (CGFloat)alpha {
NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] - _flashStartTime;
return MAX(0, 1.0 - elapsed / kFlashDuration);
}
@end
@implementation iTermIndicatorsHelper {
// Maps an identifier to a NSNumber in [0, 1]
NSMutableDictionary *_visibleIndicators;
NSTimeInterval _fullScreenFlashStartTime;
// Rate limits calls to setNeedsDisplay: to not be faster than drawRect can be called.
BOOL _haveSetNeedsDisplay;
}
+ (NSDictionary *)indicatorImages {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
gIndicatorImages = @{ kiTermIndicatorBell: [NSImage it_imageNamed:@"bell" forClass:self.class],
kiTermIndicatorWrapToTop: [NSImage it_imageNamed:@"wrap_to_top" forClass:self.class],
kiTermIndicatorWrapToBottom: [NSImage it_imageNamed:@"wrap_to_bottom" forClass:self.class],
kItermIndicatorBroadcastInput: [NSImage it_imageNamed:@"BroadcastInput" forClass:self.class],
kiTermIndicatorMaximized: [NSImage it_imageNamed:@"Maximized" forClass:self.class],
kiTermIndicatorCoprocess: [NSImage it_imageNamed:@"Coprocess" forClass:self.class],
kiTermIndicatorAlert: [NSImage it_imageNamed:@"Alert" forClass:self.class],
kiTermIndicatorAllOutputSuppressed: [NSImage it_imageNamed:@"SuppressAllOutput" forClass:self.class],
kiTermIndicatorZoomedIn: [NSImage it_imageNamed:@"Zoomed" forClass:self.class],
kiTermIndicatorCopyMode: [NSImage it_imageNamed:@"CopyMode" forClass:self.class] };
[gIndicatorImages retain];
});
return gIndicatorImages;
}
- (instancetype)init {
self = [super init];
if (self) {
_visibleIndicators = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
[_visibleIndicators release];
[super dealloc];
}
- (void)setIndicator:(NSString *)identifier visible:(BOOL)visible {
if (visible && !_visibleIndicators[identifier]) {
iTermIndicator *indicator = [[[iTermIndicator alloc] init] autorelease];
indicator.image = [[self class] indicatorImages][identifier];
_visibleIndicators[identifier] = indicator;;
[_delegate setNeedsDisplay:YES];
} else if (!visible && _visibleIndicators[identifier]) {
[_visibleIndicators removeObjectForKey:identifier];
[_delegate setNeedsDisplay:YES];
}
}
+ (NSArray *)flashingIndicatorIdentifiers {
return @[ kiTermIndicatorBell,
kiTermIndicatorWrapToTop,
kiTermIndicatorWrapToBottom ];
}
+ (NSArray *)sequentialIndicatorIdentifiers {
return @[ kiTermIndicatorMaximized,
kItermIndicatorBroadcastInput,
kiTermIndicatorCoprocess,
kiTermIndicatorAlert,
kiTermIndicatorAllOutputSuppressed,
kiTermIndicatorZoomedIn,
kiTermIndicatorCopyMode ];
}
- (void)enumerateTopRightIndicatorsInFrame:(NSRect)frame andDraw:(BOOL)shouldDraw block:(void (^)(NSString *, NSImage *, NSRect))block {
NSArray *sequentialIdentifiers = [iTermIndicatorsHelper sequentialIndicatorIdentifiers];
const CGFloat vmargin = [iTermAdvancedSettingsModel terminalVMargin];
const CGFloat kIndicatorTopMargin = MAX(5, vmargin);
NSPoint point = NSMakePoint(frame.origin.x + frame.size.width,
frame.origin.y + kIndicatorTopMargin);
for (NSString *identifier in sequentialIdentifiers) {
iTermIndicator *indicator = _visibleIndicators[identifier];
if (indicator) {
static const CGFloat kInterIndicatorHorizontalMargin = 4;
point.x -= indicator.image.size.width;
point.x -= kInterIndicatorHorizontalMargin;
NSImage *image = indicator.image;
block(identifier, image, NSMakeRect(point.x, point.y, image.size.width, image.size.height));
if (shouldDraw) {
[image drawInRect:NSMakeRect(point.x, point.y, image.size.width, image.size.height)
fromRect:NSMakeRect(0, 0, image.size.width, image.size.height)
operation:NSCompositingOperationSourceOver
fraction:0.5
respectFlipped:YES
hints:nil];
}
}
}
}
- (void)enumerateCenterIndicatorsInFrame:(NSRect)frame block:(void (^)(NSString *, NSImage *, NSRect, CGFloat))block {
NSArray *centeredIdentifiers = [iTermIndicatorsHelper flashingIndicatorIdentifiers];
for (NSString *identifier in centeredIdentifiers) {
iTermIndicator *indicator = _visibleIndicators[identifier];
CGFloat alpha = indicator.alpha;
if (alpha > 0) {
NSImage *image = indicator.image;
NSSize size = [image size];
NSRect destinationRect = NSMakeRect(frame.origin.x + frame.size.width / 2 - size.width / 2,
frame.origin.y + frame.size.height / 2 - size.height / 2,
size.width,
size.height);
block(identifier, image, destinationRect, alpha);
}
}
}
- (void)drawInFrame:(NSRect)frame {
DLog(@"drawInFrame %@", NSStringFromRect(frame));
// Draw top-right indicators.
[self enumerateTopRightIndicatorsInFrame:frame andDraw:YES block:^(NSString *identifier, NSImage *image, NSRect frame) {
[image drawInRect:frame
fromRect:NSMakeRect(0, 0, image.size.width, image.size.height)
operation:NSCompositingOperationSourceOver
fraction:0.5
respectFlipped:YES
hints:nil];
}];
// Draw centered flashing indicators.
[self enumerateCenterIndicatorsInFrame:frame block:^(NSString *identifier, NSImage *image, NSRect destinationRect, CGFloat alpha) {
[image drawInRect:destinationRect
fromRect:NSMakeRect(0, 0, image.size.width, image.size.height)
operation:NSCompositingOperationSourceOver
fraction:alpha
respectFlipped:YES
hints:nil];
}];
// Draw full screen flash.
if (_fullScreenAlpha > 0) {
DLog(@"Drawing full screen flash overlay");
[[[_delegate indicatorFullScreenFlashColor] colorWithAlphaComponent:_fullScreenAlpha] set];
NSRectFillUsingOperation(frame, NSCompositingOperationSourceOver);
} else if (_fullScreenFlashStartTime > 0 && _fullScreenAlpha == 0) {
DLog(@"Not drawing full screen flash overlay");
}
[self didDraw];
}
- (void)didDraw {
NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] - _fullScreenFlashStartTime;
DLog(@"elapsed=%@, fullScreenAlpha=%@", @(elapsed), @(_fullScreenAlpha));
DLog(@"Set haveSetNeedsDisplay=NO");
_haveSetNeedsDisplay = NO;
}
- (void)checkForFlashUpdate {
DLog(@"Check for flash update. full screen flash start time is %@, haveSetNeedsDisplay=%@",
@(_fullScreenFlashStartTime), @(_haveSetNeedsDisplay));
NSTimeInterval elapsed = [NSDate timeIntervalSinceReferenceDate] - _fullScreenFlashStartTime;
if (_fullScreenFlashStartTime > 0 || [self haveFlashingIndicator]) {
const CGFloat kMaxFullScreenFlashAlpha = [iTermAdvancedSettingsModel indicatorFlashInitialAlpha];
_fullScreenAlpha = MAX(0, 1.0 - elapsed / kFullScreenFlashDuration) * kMaxFullScreenFlashAlpha;
DLog(@"Set fullScreenAlpha=%@", @(_fullScreenAlpha));
if (!_haveSetNeedsDisplay) {
DLog(@"Tell delegate %@ setNeedsDisplay", _delegate);
[_delegate setNeedsDisplay:YES];
}
DLog(@"Set haveSetNeedsDisplay=YES");
_haveSetNeedsDisplay = YES;
// Ensure that the screen gets redrawn with alpha = 0.
if (_fullScreenAlpha == 0) {
DLog(@"Reset fullScreenFlashStartTime");
_fullScreenFlashStartTime = 0;
}
}
// Remove any indicators that became invisible since the last check.
NSArray *visibleIdentifiers = [[_visibleIndicators.allKeys copy] autorelease];
for (NSString *identifier in visibleIdentifiers) {
if ([_visibleIndicators[identifier] alpha] == 0) {
[_visibleIndicators removeObjectForKey:identifier];
}
}
// Request another update if needed.
if (_fullScreenFlashStartTime > 0 || [self haveFlashingIndicator]) {
DLog(@"Schedule another call to checkForFlashUpdate");
[self performSelector:@selector(checkForFlashUpdate) withObject:nil afterDelay:1 / 60.0];
}
}
- (void)beginFlashingIndicator:(NSString *)identifier {
assert([[iTermIndicatorsHelper flashingIndicatorIdentifiers] containsObject:identifier]);
if (_visibleIndicators[identifier]) {
return;
}
[self setIndicator:identifier visible:YES];
[_visibleIndicators[identifier] startFlash];
[self checkForFlashUpdate];
}
- (BOOL)haveFlashingIndicator {
for (NSString *identifier in [iTermIndicatorsHelper flashingIndicatorIdentifiers]) {
if (_visibleIndicators[identifier]) {
return YES;
}
}
return NO;
}
- (NSInteger)numberOfVisibleIndicators {
return _visibleIndicators.count;
}
- (void)beginFlashingFullScreen {
_fullScreenFlashStartTime = [NSDate timeIntervalSinceReferenceDate];
[_delegate setNeedsDisplay:YES];
[self checkForFlashUpdate];
}
@end