forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiTermMinimalComposerViewController.m
151 lines (122 loc) · 5.14 KB
/
iTermMinimalComposerViewController.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
//
// iTermMinimalComposerViewController.m
// iTerm2
//
// Created by George Nachman on 3/31/20.
//
#import "iTermMinimalComposerViewController.h"
#import "iTermDragHandleView.h"
#import "iTermStatusBarLargeComposerViewController.h"
#import "iTerm2SharedARC-Swift.h"
static float kAnimationDuration = 0.25;
static NSString *const iTermMinimalComposerViewHeightUserDefaultsKey = @"ComposerHeight";
@interface iTermMinimalComposerViewController ()<iTermComposerTextViewDelegate, iTermDragHandleViewDelegate>
@end
@implementation iTermMinimalComposerViewController {
IBOutlet iTermStatusBarLargeComposerViewController *_largeComposerViewController;
IBOutlet NSView *_containerView;
IBOutlet NSVisualEffectView *_vev;
CGFloat _desiredHeight;
}
- (instancetype)init {
self = [super initWithNibName:NSStringFromClass(self.class) bundle:[NSBundle bundleForClass:self.class]];
if (self) {
[[NSUserDefaults standardUserDefaults] registerDefaults:@{ iTermMinimalComposerViewHeightUserDefaultsKey: @135 }];
_desiredHeight = [[NSUserDefaults standardUserDefaults] doubleForKey:iTermMinimalComposerViewHeightUserDefaultsKey];
}
return self;
}
- (void)awakeFromNib {
[_containerView addSubview:_largeComposerViewController.view];
_containerView.autoresizesSubviews = YES;
_largeComposerViewController.view.frame = _containerView.bounds;
_largeComposerViewController.textView.composerDelegate = self;
_largeComposerViewController.view.autoresizingMask = (NSViewWidthSizable | NSViewHeightSizable);
_vev.layer.cornerRadius = 6;
_vev.layer.borderColor = [[NSColor grayColor] CGColor];
_vev.layer.borderWidth = 1;
}
- (void)setFont:(NSFont *)font {
_largeComposerViewController.textView.font = font;
}
- (void)setHost:(id<VT100RemoteHostReading>)host
workingDirectory:(NSString *)pwd
scope:(iTermVariableScope *)scope
tmuxController:(nonnull TmuxController *)tmuxController {
[self view];
_largeComposerViewController.host = host;
_largeComposerViewController.workingDirectory = pwd;
_largeComposerViewController.scope = scope;
_largeComposerViewController.tmuxController = tmuxController;
}
- (NSRect)frameForHeight:(CGFloat)desiredHeight {
NSRect newFrame = self.view.frame;
newFrame.origin.y = self.view.superview.frame.size.height;
newFrame.origin.y += newFrame.size.height;
const CGFloat maxWidth = self.view.superview.bounds.size.width - newFrame.origin.x - 19;
newFrame = NSMakeRect(newFrame.origin.x,
self.view.superview.frame.size.height - desiredHeight,
MAX(217, maxWidth),
desiredHeight);
return newFrame;
}
- (CGFloat)minHeight {
return 62;
}
- (CGFloat)maxHeight {
return MAX(self.minHeight, NSHeight(self.view.superview.bounds) - 8);
}
- (void)updateFrame {
self.view.frame = [self frameForHeight:MAX(MIN(self.maxHeight, _desiredHeight), self.minHeight)];
[[NSAnimationContext currentContext] setDuration:kAnimationDuration];
self.view.animator.alphaValue = 1;
}
- (void)makeFirstResponder {
[_largeComposerViewController.textView.window makeFirstResponder:_largeComposerViewController.textView];
}
- (IBAction)performClose:(id)sender {
[self.delegate minimalComposer:self sendCommand:@"" dismiss:YES];
}
- (NSString *)stringValue {
return _largeComposerViewController.textView.string;
}
- (void)setStringValue:(NSString *)stringValue {
_largeComposerViewController.textView.string = stringValue;
[_largeComposerViewController.textView it_scrollCursorToVisible];
}
#pragma mark - iTermComposerTextViewDelegate
- (void)composerTextViewDidFinishWithCancel:(BOOL)cancel {
NSString *string = cancel ? @"" : _largeComposerViewController.textView.string;
[self.delegate minimalComposer:self sendCommand:string ?: @"" dismiss:YES];
}
- (void)composerTextViewDidResignFirstResponder {
}
- (void)composerTextViewSendToAdvancedPaste:(NSString *)content {
[self.delegate minimalComposer:self sendToAdvancedPaste:content];
}
- (void)composerTextViewSend:(NSString *)string {
[self.delegate minimalComposer:self sendCommand:string dismiss:NO];
}
- (void)composerTextViewEnqueue:(NSString *)string {
[self.delegate minimalComposer:self enqueueCommand:string dismiss:NO];
}
#pragma mark - iTermDragHandleViewDelegate
- (CGFloat)dragHandleView:(iTermDragHandleView *)dragHandle didMoveBy:(CGFloat)delta {
const CGFloat originalHeight = NSHeight(self.view.frame);
_desiredHeight -= delta;
const CGFloat proposedHeight = NSHeight([self frameForHeight:_desiredHeight]);
if (proposedHeight < self.minHeight) {
const CGFloat error = self.minHeight - proposedHeight;
delta -= error;
_desiredHeight += error;
} else if (proposedHeight > self.maxHeight) {
const CGFloat error = proposedHeight - self.maxHeight;
delta += error;
_desiredHeight -= error;
}
[[NSUserDefaults standardUserDefaults] setDouble:_desiredHeight
forKey:iTermMinimalComposerViewHeightUserDefaultsKey];
[self updateFrame];
return originalHeight - NSHeight(self.view.frame);
}
@end