forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovePaneController.m
216 lines (191 loc) · 6.86 KB
/
MovePaneController.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
//
// MovePaneController.m
// iTerm2
//
// Created by George Nachman on 8/26/11.
#import "MovePaneController.h"
#import "PTYSession.h"
#import "iTermController.h"
#import "PseudoTerminal.h"
#import "PTYTab.h"
#import "SessionView.h"
#import "TmuxController.h"
@implementation MovePaneController
@synthesize dragFailed = dragFailed_;
@synthesize session = session_;
+ (MovePaneController *)sharedInstance
{
static MovePaneController *inst;
if (!inst) {
inst = [[MovePaneController alloc] init];
}
return inst;
}
- (void)movePane:(PTYSession *)session
{
session_ = [session liveSession];
if (!session_) {
session_ = session;
}
for (PseudoTerminal *term in [[iTermController sharedInstance] terminals]) {
[term setSplitSelectionMode:YES excludingSession:session_];
}
}
- (void)exitMovePaneMode
{
for (PseudoTerminal *term in [[iTermController sharedInstance] terminals]) {
[term setSplitSelectionMode:NO excludingSession:nil];
}
session_ = nil;
}
- (void)moveSessionToNewWindow:(PTYSession *)movingSession atPoint:(NSPoint)point
{
if ([movingSession isTmuxClient]) {
[[movingSession tmuxController] breakOutWindowPane:[movingSession tmuxPane]
toPoint:point];
return;
}
PTYTab *theTab = [movingSession tab];
PseudoTerminal *term = [[theTab realParentWindow] terminalDraggedFromAnotherWindowAtPoint:point];
SessionView *oldView = [movingSession view];
[oldView retain];
[theTab removeSession:movingSession];
if ([[theTab sessions] count] == 0) {
[[theTab realParentWindow] closeTab:theTab];
}
[term insertSession:movingSession atIndex:0];
[oldView autorelease];
[theTab numberOfSessionsDidChange];
[[term currentTab] numberOfSessionsDidChange];
}
- (void)clearSession
{
session_ = nil;
}
- (SessionView *)removeAndClearSession
{
SessionView *oldView = [session_ view];
[oldView retain];
PTYSession *movingSession = session_;
PTYTab *theTab = [movingSession tab];
[[movingSession tab] removeSession:movingSession];
if ([[theTab sessions] count] == 0) {
[[theTab realParentWindow] closeTab:theTab];
}
session_ = nil;
return [oldView autorelease];
}
- (BOOL)dropTab:(PTYTab *)tab
inSession:(PTYSession *)dest
half:(SplitSessionHalf)half
atPoint:(NSPoint)point
{
if ([[tab sessions] count] != 1) {
// This sometimes can't be done at all, and it usually can't be done right if tabs' sizes
// differ, etc. Just wimp out, basically.
return NO;
}
session_ = [[tab sessions] objectAtIndex:0];
return [self dropInSession:dest half:half atPoint:point];
}
// This function is either called once or twice at the end of a drag.
// If only one call is made, then dest will be nil and the session will be moved to a new window.
// If two calls are made, the both have dest non-null but only the first call will perform a split.
// The second call will do nothing.
// It isn't called at all if a session is dragged into an existing tab bar.
- (BOOL)dropInSession:(PTYSession *)dest
half:(SplitSessionHalf)half
atPoint:(NSPoint)point
{
if ((dest && ![session_ isCompatibleWith:dest]) || // Would create hetero-tmuxual splits in tab
dest == session_ || // move to self
!session_) { // no source (?)
didSplit_ = YES;
return NO;
}
if (!dest && !didSplit_) {
// We were not called before, so move the session to a new window.
[self moveSessionToNewWindow:session_ atPoint:point];
return YES;
} else if (!dest) {
// This is the second call and a split has already been performed/aborted.
return NO;
}
didSplit_ = YES;
if ([self.session isTmuxClient]) {
// Do this after setting didSplit becaue a second call to this method
// will happen no matter what and we want it to do nothing if we get
// here.
[[self.session tmuxController] movePane:[self.session tmuxPane]
intoPane:[dest tmuxPane]
isVertical:(half == kEastHalf || half == kWestHalf)
before:(half == kNorthHalf || half == kWestHalf)];
return YES;
}
PTYSession *movingSession = session_;
BOOL isVertical = (half == kWestHalf || half == kEastHalf);
if (![[[dest tab] realParentWindow] canSplitPaneVertically:isVertical
withBookmark:[movingSession addressBookEntry]]) {
return NO;
}
SessionView *oldView = [movingSession view];
[oldView retain];
PTYTab *theTab = [movingSession tab];
[[movingSession tab] removeSession:movingSession];
if ([[theTab sessions] count] == 0) {
[[theTab realParentWindow] closeTab:theTab];
}
[[[dest tab] realParentWindow] splitVertically:isVertical
before:(half == kNorthHalf || half == kWestHalf)
addingSession:movingSession
targetSession:dest
performSetup:NO];
[oldView release];
[[dest tab] fitSessionToCurrentViewSize:movingSession];
return YES;
}
- (BOOL)isMovingSession:(PTYSession *)s
{
return session_ == s;
}
- (void)beginDrag:(PTYSession *)session
{
[self exitMovePaneMode];
session_ = session;
self.dragFailed = NO;
NSPasteboard *pboard;
pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObjects:@"iTermDragPanePBType", nil] owner: nil];
[pboard setString:@"" forType:@"iTermDragPanePBType"];
PTYTab *theTab = [session tab];
NSRect rect = [[[session view] superview] convertRect:[[session view] frame] toView:nil];
SessionView *source = [session view];
[source retain];
didSplit_ = NO;
NSWindow *theWindow = [[theTab realParentWindow] window];
for (PseudoTerminal *term in [[iTermController sharedInstance] terminals]) {
[[term window] disableCursorRects];
}
[[NSCursor closedHandCursor] set];
[theWindow dragImage:[session dragImage]
at:rect.origin
offset:NSZeroSize
event:[NSApp currentEvent]
pasteboard:pboard
source:source
slideBack:NO];
for (PseudoTerminal *term in [[iTermController sharedInstance] terminals]) {
[[term window] enableCursorRects];
}
[[NSCursor openHandCursor] set];
[source autorelease];
session_ = nil;
}
#pragma mark Delegate
- (void)didSelectDestinationSession:(PTYSession *)dest
half:(SplitSessionHalf)half
{
[self dropInSession:dest half:half atPoint:NSZeroPoint];
[self exitMovePaneMode];
}
@end