Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 231 lines (195 sloc) 6.911 kb
070abf1 Add files missing from last commit
George Nachman authored
1 //
2 // SplitSelectionView.m
3 // iTerm2
4 //
5 // Created by George Nachman on 8/26/11.
6 //
7
8 #import "SplitSelectionView.h"
9
10 @interface SplitSelectionView ()
11
12 - (void)_createTrackingArea;
13
14 @end
15
16 @implementation SplitSelectionView
17
18 @synthesize cancelOnly = cancelOnly_;
19
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
20 - (id)initWithFrame:(NSRect)frameRect
21 {
22 self = [super initWithFrame:frameRect];
23 if (self) {
24 half_ = kNoHalf;
25 [self setAlphaValue:0.9];
26 }
27 return self;
28 }
29
070abf1 Add files missing from last commit
George Nachman authored
30 - (id)initAsCancelOnly:(BOOL)cancelOnly
31 withFrame:(NSRect)frame
32 withSession:(PTYSession *)session
33 delegate:(id<SplitSelectionViewDelegate>)delegate {
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
34 self = [self initWithFrame:frame];
070abf1 Add files missing from last commit
George Nachman authored
35 if (self) {
36 cancelOnly_ = cancelOnly;
37 session_ = session;
38 delegate_ = delegate;
39 [self _createTrackingArea];
40 }
41 return self;
42 }
43
44 - (void)dealloc
45 {
46 [trackingArea_ release];
47 [super dealloc];
48 }
49
50 - (void)_createTrackingArea
51 {
52 NSRect frame = self.frame;
53 trackingArea_ = [[NSTrackingArea alloc] initWithRect:NSMakeRect(0, 0, frame.size.width, frame.size.height)
54 options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInActiveApp
55 owner:self
56 userInfo:nil];
57 [self addTrackingArea:trackingArea_];
58 }
59
60 - (void)setFrameSize:(NSSize)newSize
61 {
62 [super setFrameSize:newSize];
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
63 if (trackingArea_) {
64 [self removeTrackingArea:trackingArea_];
65 [trackingArea_ release];
66 [self _createTrackingArea];
67 }
070abf1 Add files missing from last commit
George Nachman authored
68 }
69
70 - (void)_showMessage:(NSString *)message inRect:(NSRect)frame
71 {
72 [[NSColor whiteColor] set];
73 NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
74 [pStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
75 [pStyle setAlignment:NSCenterTextAlignment];
76
77 CGFloat fontSize = 25;
78 NSMutableAttributedString* attributedString;
79 NSRect rect;
80 do {
81 fontSize--;
82 NSDictionary* attrs =
83 [NSDictionary dictionaryWithObjectsAndKeys:
84 [NSFont systemFontOfSize:fontSize], NSFontAttributeName,
85 [NSColor whiteColor], NSForegroundColorAttributeName,
86 pStyle, NSParagraphStyleAttributeName,
87 nil];
88 attributedString = [[[NSMutableAttributedString alloc] initWithString:message
89 attributes:attrs] autorelease];
90 rect = NSMakeRect(frame.origin.x,
91 frame.origin.y + frame.size.height * 2.0 / 3.0,
92 frame.size.width,
93 attributedString.size.height);
94 if (fontSize < 8) {
95 break;
96 }
97 } while ([attributedString size].width > frame.size.width ||
98 [attributedString size].height > frame.size.height ||
99 rect.size.height + rect.origin.y > frame.origin.y + frame.size.height);
100
101
102 NSShadow *theShadow = [[[NSShadow alloc] init] autorelease];
103 [theShadow setShadowOffset:NSMakeSize(0, 0)];
104 [theShadow setShadowBlurRadius:4.0];
105 [theShadow setShadowColor:[NSColor blackColor]];
106 [theShadow set];
107 [attributedString drawWithRect:rect
108 options:0];
109 }
110
111 - (void)drawRect:(NSRect)dirtyRect {
112 if (cancelOnly_) {
113 [[NSColor colorWithCalibratedRed:0 green:0.5 blue:0 alpha:1] set];
114 NSRectFill(dirtyRect);
115
116 [self _showMessage:@"Select a destination pane" inRect:self.frame];
117 } else {
118 NSRect highlightRect;
119 NSRect clearRect;
120 NSRect rect = [self frame];
121 switch (half_) {
122 case kNoHalf:
123 highlightRect = NSZeroRect;
124 clearRect = rect;
125 break;
126
127 case kSouthHalf:
128 NSDivideRect([self frame], &highlightRect, &clearRect, rect.size.height / 2, NSMinYEdge);
129 break;
130
131 case kNorthHalf:
132 NSDivideRect([self frame], &highlightRect, &clearRect, rect.size.height / 2, NSMaxYEdge);
133 break;
134
135 case kWestHalf:
136 NSDivideRect([self frame], &highlightRect, &clearRect, rect.size.width / 2, NSMinXEdge);
137 break;
138
139 case kEastHalf:
140 NSDivideRect([self frame], &highlightRect, &clearRect, rect.size.width / 2, NSMaxXEdge);
141 break;
142 }
143
144 [[NSColor colorWithCalibratedRed:0.5 green:0 blue:0 alpha:1] set];
145 NSRectFill(highlightRect);
146
147 [[NSColor whiteColor] set];
148 NSFrameRect(highlightRect);
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
149
070abf1 Add files missing from last commit
George Nachman authored
150 highlightRect = NSInsetRect(highlightRect, 1, 1);
151 [[NSColor blackColor] set];
152 NSFrameRect(highlightRect);
153
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
154 if (delegate_ && half_ != kNoHalf) {
155 [self _showMessage:@"Click to move source pane to this split" inRect:highlightRect];
070abf1 Add files missing from last commit
George Nachman authored
156 }
157 }
158 }
159
160 - (void)mouseDown:(NSEvent *)theEvent
161 {
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
162 if (delegate_) {
163 [delegate_ didSelectDestinationSession:session_ half:half_];
164 }
070abf1 Add files missing from last commit
George Nachman authored
165 }
166
167 - (void)mouseEntered:(NSEvent *)theEvent
168 {
169 [self mouseMoved:theEvent];
170 }
171
172 - (void)mouseExited:(NSEvent *)theEvent
173 {
174 half_ = kNoHalf;
175 [self setNeedsDisplay:YES];
176 }
177
178 - (void)mouseMoved:(NSEvent *)theEvent
179 {
180 NSPoint locationInWindow = [theEvent locationInWindow];
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
181 NSPoint point = [self convertPoint:locationInWindow fromView:nil];
182 [self updateAtPoint:point];
183 }
070abf1 Add files missing from last commit
George Nachman authored
184
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
185 - (void)updateAtPoint:(NSPoint)point
186 {
070abf1 Add files missing from last commit
George Nachman authored
187 SplitSessionHalf possibilities[4];
188 CGFloat scores[4];
189 int numPossibilities = 0;
190 if (point.x < self.frame.size.width / 2) {
191 scores[numPossibilities] = point.x / self.frame.size.width;
192 possibilities[numPossibilities++] = kWestHalf;
193 } else {
194 scores[numPossibilities] = (self.frame.size.width - point.x) / self.frame.size.width;
195 possibilities[numPossibilities++] = kEastHalf;
196 }
197 if (point.y < self.frame.size.height / 2) {
198 scores[numPossibilities] = point.y / self.frame.size.height;
199 possibilities[numPossibilities++] = kSouthHalf;
200 } else {
201 scores[numPossibilities] = (self.frame.size.height - point.y) / self.frame.size.height;
202 possibilities[numPossibilities++] = kNorthHalf;
203 }
204
205 CGFloat minScore = INFINITY;
206 int bestIndex = 0;
207 for (int i = 0; i < numPossibilities; i++) {
208 if (scores[i] < minScore) {
209 minScore = scores[i];
210 bestIndex = i;
211 }
212 }
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
213
214 if (half_ != possibilities[bestIndex] && minScore < 0.4) {
070abf1 Add files missing from last commit
George Nachman authored
215 half_ = possibilities[bestIndex];
216 [self setNeedsDisplay:YES];
217 }
218 }
219
220 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
221 {
222 return YES;
223 }
224
b0b4c0b Implement dragging of sessions into split panes using cmd-opt-shift +…
George Nachman authored
225 - (SplitSessionHalf)half
226 {
227 return half_;
228 }
229
070abf1 Add files missing from last commit
George Nachman authored
230 @end
Something went wrong with that request. Please try again.