forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolProfiles.m
241 lines (216 loc) · 9.21 KB
/
ToolProfiles.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
//
// ToolProfiles.m
// iTerm
//
// Created by George Nachman on 9/5/11.
// Copyright 2011 Georgetech. All rights reserved.
//
#import "ToolProfiles.h"
#import "DebugLogging.h"
#import "iTermController.h"
#import "iTermSessionLauncher.h"
#import "NSEvent+iTerm.h"
#import "NSImage+iTerm.h"
#import "ProfileModel.h"
#import "PseudoTerminal.h"
static const int kVerticalMargin = 5;
static const int kMargin = 0;
static const int kPopupHeight = 26;
static const CGFloat kButtonHeight = 23;
static const CGFloat kInnerMargin = 5;
static NSString *const iTermToolProfilesProfileListViewState = @"iTermToolProfilesProfileListViewState";
@implementation ToolProfiles {
ProfileListView *listView_;
NSPopUpButton *popup_;
NSButton *_openButton;
}
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
listView_ = [[ProfileListView alloc] initWithFrame:NSMakeRect(kMargin, 0, frame.size.width - kMargin * 2, frame.size.height - kPopupHeight - kVerticalMargin)
model:[ProfileModel sharedInstance]
font:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
[listView_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[listView_ setDelegate:self];
[listView_ disableArrowHandler];
[listView_ allowMultipleSelections];
[listView_.tableView setHeaderView:nil];
listView_.tableView.enclosingScrollView.drawsBackground = NO;
if (@available(macOS 10.16, *)) {
[listView_ forceOverlayScroller];
}
if (@available(macOS 10.14, *)) {
listView_.tableView.backgroundColor = [NSColor clearColor];
}
[self addSubview:listView_];
_openButton = [[NSButton alloc] initWithFrame:NSMakeRect(0, frame.size.height - kButtonHeight, frame.size.width, kButtonHeight)];
if (@available(macOS 10.16, *)) {
_openButton.bezelStyle = NSBezelStyleRegularSquare;
_openButton.bordered = NO;
_openButton.image = [NSImage it_imageForSymbolName:@"play" accessibilityDescription:@"Open Profile"];
_openButton.imageScaling = NSImageScaleProportionallyUpOrDown;
_openButton.imagePosition = NSImageOnly;
} else {
[_openButton setButtonType:NSButtonTypeMomentaryPushIn];
[_openButton setTitle:@"Open"];
[_openButton setBezelStyle:NSBezelStyleSmallSquare];
}
[_openButton setTarget:self];
[_openButton setAction:@selector(open:)];
[_openButton sizeToFit];
[_openButton setAutoresizingMask:NSViewMinYMargin];
[self addSubview:_openButton];
[_openButton bind:@"enabled" toObject:listView_ withKeyPath:@"hasSelection" options:nil];
popup_ = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, frame.size.height - kPopupHeight, frame.size.width - _openButton.frame.size.width - kInnerMargin, kPopupHeight)];
[[popup_ cell] setControlSize:NSControlSizeSmall];
[[popup_ cell] setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
[[popup_ menu] addItemWithTitle:@"New Tab"
action:@selector(toolProfilesNewTab:)
keyEquivalent:@""];
[[popup_ menu] addItemWithTitle:@"New Window"
action:@selector(toolProfilesNewWindow:)
keyEquivalent:@""];
[[popup_ menu] addItemWithTitle:@"New Horizontal Split"
action:@selector(toolProfilesNewHorizontalSplit:)
keyEquivalent:@""];
[[popup_ menu] addItemWithTitle:@"New Vertical Split"
action:@selector(toolProfilesNewVerticalSplit:)
keyEquivalent:@""];
for (NSMenuItem *i in [[popup_ menu] itemArray]) {
[i setTarget:self];
}
[self addSubview:popup_];
[popup_ setAutoresizingMask:NSViewMinYMargin | NSViewWidthSizable];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(refreshTerminal:)
name:kRefreshTerminalNotification
object:nil];
[popup_ bind:@"enabled" toObject:listView_ withKeyPath:@"hasSelection" options:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[popup_ unbind:@"enabled"];
[_openButton unbind:@"enabled"];
}
- (void)refreshTerminal:(NSNotification *)notification {
[listView_ reloadData];
}
- (void)windowBackgroundColorDidChange {
[listView_ reloadData];
}
- (void)relayout {
NSRect frame = self.frame;
listView_.frame = NSMakeRect(kMargin, 0, frame.size.width - kMargin * 2, frame.size.height - kPopupHeight - kVerticalMargin);
if (@available(macOS 10.16, *)) {
const CGFloat margin = 0;
popup_.frame = NSMakeRect(0,
frame.size.height - kPopupHeight,
frame.size.width - NSWidth(_openButton.frame) - margin,
kPopupHeight);
NSRect rect = _openButton.frame;
const CGFloat inset = (NSHeight(popup_.frame) - NSHeight(rect)) / 2.0;
rect.origin.x = NSMaxX(popup_.frame) + margin;
const CGFloat fudgeFactor = 1;
rect.origin.y = inset + NSMinY(popup_.frame) - fudgeFactor;
_openButton.frame = rect;
} else {
popup_.frame = NSMakeRect(0,
frame.size.height - kPopupHeight,
frame.size.width - _openButton.frame.size.width - kInnerMargin,
kPopupHeight);
_openButton.frame = NSMakeRect(frame.size.width - _openButton.frame.size.width,
frame.size.height - kPopupHeight,
_openButton.frame.size.width,
_openButton.frame.size.height);
}
}
- (BOOL)isFlipped
{
return YES;
}
- (void)toolProfilesNewTab:(id)sender
{
PseudoTerminal* terminal = [[iTermController sharedInstance] currentTerminal];
for (NSString* guid in [listView_ selectedGuids]) {
Profile* bookmark = [[ProfileModel sharedInstance] bookmarkWithGuid:guid];
[iTermSessionLauncher launchBookmark:bookmark
inTerminal:terminal
respectTabbingMode:NO
completion:nil];
}
}
- (void)toolProfilesNewWindow:(id)sender
{
for (NSString* guid in [listView_ selectedGuids]) {
Profile* bookmark = [[ProfileModel sharedInstance] bookmarkWithGuid:guid];
[iTermSessionLauncher launchBookmark:bookmark
inTerminal:nil
respectTabbingMode:NO
completion:nil];
}
}
- (void)toolProfilesNewHorizontalSplit:(id)sender
{
PseudoTerminal* terminal = [[iTermController sharedInstance] currentTerminal];
for (NSString* guid in [listView_ selectedGuids]) {
Profile *profile = [[ProfileModel sharedInstance] bookmarkWithGuid:guid];
if (!profile) {
continue;
}
[terminal asyncSplitVertically:NO
before:NO
profile:profile
targetSession:[terminal currentSession]
completion:nil
ready:nil];
}
}
- (void)toolProfilesNewVerticalSplit:(id)sender
{
PseudoTerminal* terminal = [[iTermController sharedInstance] currentTerminal];
for (NSString* guid in [listView_ selectedGuids]) {
Profile *profile = [[ProfileModel sharedInstance] bookmarkWithGuid:guid];
if (!profile) {
continue;
}
[terminal asyncSplitVertically:YES
before:NO
profile:profile
targetSession:[terminal currentSession]
completion:nil
ready:nil];
}
}
- (void)profileTableRowSelected:(id)profileTable
{
NSEvent *event = [[NSApplication sharedApplication] currentEvent];
if ([event it_modifierFlags] & (NSEventModifierFlagControl)) {
[self toolProfilesNewHorizontalSplit:nil];
} else if ([event it_modifierFlags] & (NSEventModifierFlagOption)) {
[self toolProfilesNewVerticalSplit:nil];
} else if ([event it_modifierFlags] & (NSEventModifierFlagShift)) {
[self toolProfilesNewWindow:nil];
} else {
[self toolProfilesNewTab:nil];
}
}
- (void)shutdown
{
}
- (CGFloat)minimumHeight
{
return 88;
}
- (void)open:(id)sender {
[self it_performNonObjectReturningSelector:[[popup_ selectedItem] action]
withObject:nil];
}
- (NSDictionary *)restorableState {
return @{ iTermToolProfilesProfileListViewState: listView_.restorableState };
}
- (void)restoreFromState:(NSDictionary *)state {
[listView_ restoreFromState:state[iTermToolProfilesProfileListViewState]];
}
@end