-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathiTermInstantReplayWindowController.m
More file actions
370 lines (322 loc) · 12.4 KB
/
Copy pathiTermInstantReplayWindowController.m
File metadata and controls
370 lines (322 loc) · 12.4 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// iTermInstantReplayWindowController.m
// iTerm
//
// Created by George Nachman on 3/15/14.
//
//
#import "iTermInstantReplayWindowController.h"
#import "DebugLogging.h"
#import "iTermController.h"
#import "NSAppearance+iTerm.h"
#import "PTYSession.h"
#import "PseudoTerminal.h"
static const float kAlphaValue = 0.9;
typedef NS_ENUM(NSUInteger, iTermInstantReplayState) {
iTermInstantReplayStateNormal,
iTermInstantReplayStateSetStart,
iTermInstantReplayStateSetEnd
};
@interface iTermInstantReplayEventsView : NSView
@property (nonatomic, readonly) NSMutableArray<NSNumber *> *fractions;
@property (nonatomic) CGFloat startFraction;
@property (nonatomic) CGFloat endFraction;
@end
@implementation iTermInstantReplayEventsView
- (void)awakeFromNib {
_fractions = [[NSMutableArray alloc] init];
}
- (CGFloat)xFromFraction:(CGFloat)fraction width:(CGFloat)width {
return round(fraction * (width - 1));
}
- (void)drawRect:(NSRect)dirtyRect {
dirtyRect = NSIntersectionRect(dirtyRect, self.bounds);
[[NSColor clearColor] set];
NSRectFill(dirtyRect);
__block CGFloat lastX = -1;
const CGFloat width = self.frame.size.width;
const CGFloat height = self.frame.size.height;
if (_endFraction > _startFraction) {
CGFloat minX = [self xFromFraction:_startFraction width:width];
CGFloat maxX = [self xFromFraction:_endFraction width:width];
NSRect rect = NSMakeRect(minX, 0, maxX - minX, height);
[[[NSColor redColor] colorWithAlphaComponent:0.5] set];
NSRectFill(rect);
}
NSAppearanceName bestMatch = [self.effectiveAppearance bestMatchFromAppearancesWithNames:@[ NSAppearanceNameDarkAqua,
NSAppearanceNameVibrantDark,
NSAppearanceNameAqua,
NSAppearanceNameVibrantLight ]];
if ([bestMatch isEqualToString:NSAppearanceNameDarkAqua] ||
[bestMatch isEqualToString:NSAppearanceNameVibrantDark]) {
[[[NSColor whiteColor] colorWithAlphaComponent:0.5] set];
} else {
[[[NSColor blackColor] colorWithAlphaComponent:0.5] set];
}
[_fractions enumerateObjectsUsingBlock:^(NSNumber * _Nonnull fractionNumber, NSUInteger idx, BOOL * _Nonnull stop) {
const double fraction = fractionNumber.doubleValue;
const CGFloat x = [self xFromFraction:fraction width:width];
if (x < NSMinX(dirtyRect) || x > NSMaxX(dirtyRect)) {
return;
}
if (x == lastX) {
return;
}
lastX = x;
NSRectFill(NSMakeRect(x, 0, 1, height));
}];
}
@end
@implementation iTermInstantReplayPanel
- (BOOL)canBecomeKeyWindow {
return NO;
}
@end
@implementation iTermInstantReplayView {
NSTrackingArea *_trackingArea;
}
- (void)updateTrackingAreas {
if ([self window]) {
if (_trackingArea) {
[self removeTrackingArea:_trackingArea];
}
_trackingArea = [[NSTrackingArea alloc] initWithRect:[self visibleRect]
options:NSTrackingMouseMoved |NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
owner:self
userInfo:nil];
[self addTrackingArea:_trackingArea];
}
}
- (void)mouseEntered:(NSEvent *)theEvent {
[[NSCursor arrowCursor] set];
[self.window.animator setAlphaValue:1];
}
- (void)mouseExited:(NSEvent *)theEvent {
[self.window.animator setAlphaValue:kAlphaValue];
}
@end
@implementation iTermInstantReplayWindowController {
IBOutlet NSSlider *_slider;
IBOutlet NSTextField *_currentTimeLabel;
IBOutlet NSTextField *_earliestTimeLabel;
IBOutlet NSTextField *_latestTimeLabel;
IBOutlet NSButton *_firstButton;
IBOutlet NSButton *_secondButton;
IBOutlet iTermInstantReplayEventsView *_eventsView;
iTermInstantReplayState _state;
long long _start;
double _span;
long long _firstTimestamp;
}
- (instancetype)init {
return [super initWithWindowNibName:@"InstantReplay"];
}
- (void)windowDidLoad
{
[super windowDidLoad];
self.window.level = NSFloatingWindowLevel;
self.window.alphaValue = kAlphaValue;
PTYSession *session = [[iTermController sharedInstance] currentTerminal].currentSession;
self.window.appearance = [NSAppearance it_appearanceForCurrentSessionWithBackgroundColor:session.effectiveUnprocessedBackgroundColor];
}
- (void)windowWillClose:(NSNotification *)notification {
[_delegate replaceSyntheticActiveSessionWithLiveSessionIfNeeded];
}
- (void)setDelegate:(id<iTermInstantReplayDelegate>)delegate {
_delegate = delegate;
if (delegate && [self isWindowLoaded]) {
[self updateEvents];
}
}
- (void)awakeFromNib {
if (_delegate) {
[self updateEvents];
}
}
- (void)updateEvents {
assert(_eventsView);
assert(_delegate);
_firstTimestamp = [_delegate instantReplayFirstTimestamp];
long long lastTimestamp = [_delegate instantReplayLastTimestamp];
_span = lastTimestamp - _firstTimestamp;
[_eventsView.fractions removeAllObjects];
for (long long i = _firstTimestamp; i > 0 && i <= lastTimestamp; i = [_delegate instantReplayTimestampAfter:i]) {
NSNumber *fraction = @((i - _firstTimestamp) / _span);
[_eventsView.fractions addObject:fraction];
}
[_eventsView setNeedsDisplay:YES];
}
- (IBAction)sliderMoved:(id)sender {
__typeof(self) me = self;
[_delegate instantReplaySeekTo:[sender floatValue]];
[me updateInstantReplayView];
}
- (void)keyDown:(NSEvent *)theEvent {
NSString *characters = [theEvent characters];
__typeof(self) me = self;
if ([characters length]) {
unichar code = [characters characterAtIndex:0];
switch (code) {
case NSLeftArrowFunctionKey:
[_delegate instantReplayStep:-1];
[me updateInstantReplayView];
break;
case NSRightArrowFunctionKey:
[_delegate instantReplayStep:1];
[me updateInstantReplayView];
break;
case 27:
[_delegate replaceSyntheticActiveSessionWithLiveSessionIfNeeded];
break;
}
}
}
- (IBAction)exportButton:(id)sender {
switch (_state) {
case iTermInstantReplayStateNormal:
[self setState:iTermInstantReplayStateSetStart byCancelling:NO];
break;
case iTermInstantReplayStateSetStart:
[self setState:iTermInstantReplayStateSetEnd byCancelling:NO];
break;
case iTermInstantReplayStateSetEnd:
[self setState:iTermInstantReplayStateNormal byCancelling:NO];
}
}
- (IBAction)cancelButton:(id)sender {
[self setState:iTermInstantReplayStateNormal byCancelling:YES];
}
- (void)setState:(iTermInstantReplayState)destinationState byCancelling:(BOOL)cancel {
if (_state == iTermInstantReplayStateSetStart &&
destinationState == iTermInstantReplayStateSetEnd) {
_start = [_delegate instantReplayCurrentTimestamp];
_eventsView.startFraction = (_start - _firstTimestamp) / _span;
_eventsView.endFraction = _eventsView.startFraction;
} else if (destinationState == iTermInstantReplayStateNormal &&
_state == iTermInstantReplayStateSetEnd &&
!cancel) {
long long end = [_delegate instantReplayCurrentTimestamp];
if (end < _start) {
DLog(@"Beep: end is before start");
NSBeep();
return;
}
[_delegate instantReplayExportFrom:_start to:end];
}
_state = destinationState;
switch (_state) {
case iTermInstantReplayStateNormal:
_firstButton.title = @"Export…";
_eventsView.startFraction = 0;
_eventsView.endFraction = 0;
[_eventsView setNeedsDisplay:YES];
_secondButton.hidden = YES;
break;
case iTermInstantReplayStateSetStart:
_firstButton.title = @"Set Start";
_slider.floatValue = 0;
[_delegate instantReplaySeekTo:0];
[self updateInstantReplayView];
_secondButton.title = @"Cancel";
_secondButton.hidden = NO;
break;
case iTermInstantReplayStateSetEnd:
_firstButton.title = @"Set End";
_slider.floatValue = 1;
[_delegate instantReplaySeekTo:1];
[self updateInstantReplayView];
_secondButton.title = @"Cancel";
_secondButton.hidden = NO;
}
}
#pragma mark - NSWindowController
- (void)windowDidBecomeKey:(NSNotification *)notification {
[self updateInstantReplayView];
}
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize {
static const CGFloat kMinWidth = 270;
return NSMakeSize(MAX(kMinWidth, frameSize.width), self.window.frame.size.height);
}
- (void)windowDidResize:(NSNotification *)notification {
[self updateInstantReplayView];
}
#pragma mark - Private
- (NSString*)stringForTimestamp:(long long)timestamp
{
time_t startTime = timestamp / 1000000;
time_t now = time(NULL);
struct tm startTimeParts;
struct tm nowParts;
localtime_r(&startTime, &startTimeParts);
localtime_r(&now, &nowParts);
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
[fmt setDateStyle:NSDateFormatterShortStyle];
if (startTimeParts.tm_year != nowParts.tm_year ||
startTimeParts.tm_yday != nowParts.tm_yday) {
[fmt setDateStyle:NSDateFormatterShortStyle];
} else {
[fmt setDateStyle:NSDateFormatterNoStyle];
}
[fmt setTimeStyle:NSDateFormatterMediumStyle];
NSDate* date = [NSDate dateWithTimeIntervalSince1970:startTime];
NSString* result = [fmt stringFromDate:date];
return result;
}
- (void)updateInstantReplayView {
if (![self.window isVisible]) {
return;
}
long long timestamp = [_delegate instantReplayCurrentTimestamp];
long long firstTimestamp = [_delegate instantReplayFirstTimestamp];
long long lastTimestamp = [_delegate instantReplayLastTimestamp];
if (timestamp >= 0) {
[_currentTimeLabel setStringValue:[self stringForTimestamp:timestamp]];
[_currentTimeLabel sizeToFit];
float range = ((float)(lastTimestamp - firstTimestamp)) / 1000000.0;
if (range > 0) {
float offset = ((float)(timestamp - firstTimestamp)) / 1000000.0;
float frac = offset / range;
[_slider setFloatValue:frac];
}
} else {
// Live view
[_slider setFloatValue:1.0];
[_currentTimeLabel setStringValue:@"Live View"];
[_currentTimeLabel sizeToFit];
}
[_earliestTimeLabel setStringValue:[self stringForTimestamp:firstTimestamp]];
[_latestTimeLabel setStringValue:@"Now"];
// Adjust the width of the "earliest time" label, and keep the margin between it and the
// slider the same.
NSRect labelFrame = _earliestTimeLabel.frame;
CGFloat margin = _slider.frame.origin.x - labelFrame.origin.x - labelFrame.size.width;
[_earliestTimeLabel sizeToFit];
[_latestTimeLabel sizeToFit];
labelFrame = _earliestTimeLabel.frame;
CGFloat newXOrigin = labelFrame.origin.x + labelFrame.size.width + margin;
_slider.frame = NSMakeRect(newXOrigin,
_slider.frame.origin.y,
_slider.frame.origin.x + _slider.frame.size.width - newXOrigin,
_slider.frame.size.height);
NSRect frame = _eventsView.frame;
frame.origin.x = newXOrigin + 5;
frame.size.width = _slider.frame.size.width - 10;
_eventsView.frame = frame;
// Align the currentTime with the slider
NSRect f = [_currentTimeLabel frame];
NSRect sf = [_slider frame];
float newX = [_slider floatValue] * sf.size.width + sf.origin.x - f.size.width / 2;
if (newX + f.size.width > sf.origin.x + sf.size.width) {
newX = sf.origin.x + sf.size.width - f.size.width;
}
if (newX < sf.origin.x) {
newX = sf.origin.x;
}
[_currentTimeLabel setFrameOrigin:NSMakePoint(newX, f.origin.y)];
[self.window.contentView setNeedsDisplay:YES];
if (_state == iTermInstantReplayStateSetEnd) {
_eventsView.endFraction = (timestamp - _firstTimestamp) / _span;
[_eventsView setNeedsDisplay:YES];
}
}
@end