forked from KOed/welly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWLMouseBehaviorManager.m
279 lines (251 loc) · 8.99 KB
/
WLMouseBehaviorManager.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
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
//
// WLMouseBehaviorManager.m
// Welly
//
// Created by K.O.ed on 09-1-31.
// Copyright 2009 Welly Group. All rights reserved.
//
#import "WLMouseBehaviorManager.h"
#import "WLIPAddrHotspotHandler.h"
#import "WLMovingAreaHotspotHandler.h"
#import "WLClickEntryHotspotHandler.h"
#import "WLButtonAreaHotspotHandler.h"
#import "WLEditingCursorMoveHotspotHandler.h"
#import "WLAuthorAreaHotspotHandler.h"
#import "WLURLManager.h"
#import "WLTerminalView.h"
#import "WLTerminal.h"
#import "WLConnection.h"
#import "WLSite.h"
#import "WLEffectView.h"
NSString * const WLMouseHandlerUserInfoName = @"Handler";
NSString * const WLMouseRowUserInfoName = @"Row";
NSString * const WLMouseCommandSequenceUserInfoName = @"Command Sequence";
NSString * const WLMouseButtonTypeUserInfoName = @"Button Type";
NSString * const WLMouseButtonTextUserInfoName = @"Button Text";
NSString * const WLMouseCursorUserInfoName = @"Cursor";
NSString * const WLMouseAuthorUserInfoName = @"Author";
NSString * const WLURLUserInfoName = @"URL";
NSString * const WLRangeLocationUserInfoName = @"RangeLocation";
NSString * const WLRangeLengthUserInfoName = @"RangeLength";
const float WLHorizontalScrollReactivateTimeInteval = 1.0;
@implementation WLMouseBehaviorManager
@synthesize activeTrackingAreaUserInfo = _activeTrackingAreaUserInfo;
@synthesize backgroundTrackingAreaUserInfo = _backgroundTrackingAreaUserInfo;
@synthesize normalCursor = _normalCursor;
@synthesize lastBBSState = _lastBBSState;
@synthesize lastCursorRow = _lastCursorRow;
@synthesize view = _view;
#pragma mark -
#pragma mark Initialization
- (id)initWithView:(WLTerminalView *)view {
[self init];
_view = view;
_handlers = [[NSMutableArray alloc] initWithObjects:
[[[WLIPAddrHotspotHandler alloc] initWithManager:self] autorelease],
[[[WLClickEntryHotspotHandler alloc] initWithManager:self] autorelease],
[[[WLButtonAreaHotspotHandler alloc] initWithManager:self] autorelease],
[[[WLMovingAreaHotspotHandler alloc] initWithManager:self] autorelease],
[[[WLEditingCursorMoveHotspotHandler alloc] initWithManager:self] autorelease],
[[[WLAuthorAreaHotspotHandler alloc] initWithManager:self] autorelease],
nil];
_horizontalScrollReactivateTimer = [NSTimer scheduledTimerWithTimeInterval:WLHorizontalScrollReactivateTimeInteval
target:self
selector:@selector(reactiveHorizontalScroll:)
userInfo:nil
repeats:YES];
_lastHorizontalScrollDirection = WLHorizontalScrollNone;
_lastBBSState.state = BBSUnknown;
_lastCursorRow = -1;
return self;
}
- (id)init {
[super init];
_normalCursor = [NSCursor arrowCursor];
return self;
}
- (void)dealloc {
for (NSObject *obj in _handlers)
[obj dealloc];
[_handlers dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark Event Handle
- (void)mouseEntered:(NSEvent *)theEvent {
if (![_view isConnected])
return;
if ([theEvent trackingArea]) {
NSDictionary *userInfo = [[theEvent trackingArea] userInfo];
if (!userInfo)
return;
WLMouseHotspotHandler *handler = [userInfo valueForKey:WLMouseHandlerUserInfoName];
[handler mouseEntered:theEvent];
}
}
- (void)mouseExited:(NSEvent *)theEvent {
if (![_view isConnected])
return;
if ([theEvent trackingArea]) {
NSDictionary *userInfo = [[theEvent trackingArea] userInfo];
if (!userInfo)
return;
WLMouseHotspotHandler *handler = [userInfo valueForKey:WLMouseHandlerUserInfoName];
[handler mouseExited:theEvent];
}
}
- (void)mouseMoved:(NSEvent *)theEvent {
if (![_view isConnected])
return;
if (_activeTrackingAreaUserInfo) {
WLMouseHotspotHandler *handler = [_activeTrackingAreaUserInfo valueForKey:WLMouseHandlerUserInfoName];
[handler mouseMoved:theEvent];
} else if (_backgroundTrackingAreaUserInfo) {
WLMouseHotspotHandler *handler = [_backgroundTrackingAreaUserInfo valueForKey:WLMouseHandlerUserInfoName];
[handler mouseMoved:theEvent];
}
}
- (void)mouseUp:(NSEvent *)theEvent {
if (![_view isConnected])
return;
if (_activeTrackingAreaUserInfo) {
WLMouseHotspotHandler *handler = [_activeTrackingAreaUserInfo valueForKey:WLMouseHandlerUserInfoName];
if ([handler conformsToProtocol:@protocol(WLMouseUpHandler)])
[handler mouseUp:theEvent];
return;
}
if (_backgroundTrackingAreaUserInfo) {
WLMouseHotspotHandler *handler = [_backgroundTrackingAreaUserInfo valueForKey:WLMouseHandlerUserInfoName];
if ([handler conformsToProtocol:@protocol(WLMouseUpHandler)])
[handler mouseUp:theEvent];
return;
}
if ([[_view frontMostTerminal] bbsState].state == BBSWaitingEnter) {
[_view sendText:termKeyEnter];
}
}
- (void)scrollWheel:(NSEvent *)theEvent {
const int WLScrollWheelHorizontalThreshold = 3;
if ([[[_view frontMostTerminal] connection] isConnected]) {
// For Y-Axis
if ([theEvent deltaY] < 0)
[_view sendText:termKeyDown];
else if ([theEvent deltaY] > 0)
[_view sendText:termKeyUp];
else if (_lastHorizontalScrollDirection != WLHorizontalScrollLeft && [theEvent deltaX] > WLScrollWheelHorizontalThreshold) {
// Disable horizontal scroll, in order to prevent multiple action
_lastHorizontalScrollDirection = WLHorizontalScrollLeft;
[_view sendText:termKeyLeft];
}
else if (_lastHorizontalScrollDirection != WLHorizontalScrollRight && [theEvent deltaX] < -WLScrollWheelHorizontalThreshold){
// Disable horizontal scroll, in order to prevent multiple action
_lastHorizontalScrollDirection = WLHorizontalScrollRight;
[_view sendText:termKeyRight];
}
}
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
if (_activeTrackingAreaUserInfo) {
WLMouseHotspotHandler *handler = [_activeTrackingAreaUserInfo valueForKey:WLMouseHandlerUserInfoName];
if ([handler conformsToProtocol:@protocol(WLContextualMenuHandler)])
return [(NSObject <WLContextualMenuHandler> *)handler menuForEvent:theEvent];
}
if (_backgroundTrackingAreaUserInfo) {
WLMouseHotspotHandler *handler = [_backgroundTrackingAreaUserInfo valueForKey:WLMouseHandlerUserInfoName];
if ([handler conformsToProtocol:@protocol(WLContextualMenuHandler)])
return [(NSObject <WLContextualMenuHandler> *)handler menuForEvent:theEvent];
}
return nil;
}
#pragma mark -
#pragma mark Add/Remove Tracking Area
- (BOOL)isMouseInsideRect:(NSRect)rect {
NSPoint mousePos = [_view mouseLocationInView];
return [_view mouse:mousePos inRect:rect];
}
- (NSTrackingArea *)addTrackingAreaWithRect:(NSRect)rect
userInfo:(NSDictionary *)userInfo {
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:rect
options:( NSTrackingMouseEnteredAndExited
| NSTrackingMouseMoved
| NSTrackingActiveInActiveApp)
owner:self
userInfo:userInfo];
[_view addTrackingArea:area];
if ([self isMouseInsideRect:rect]) {
NSEvent *event = [NSEvent enterExitEventWithType:NSMouseEntered
location:[NSEvent mouseLocation]
modifierFlags:NSMouseEnteredMask
timestamp:0
windowNumber:[[_view window] windowNumber]
context:nil
eventNumber:0
trackingNumber:(NSInteger)area
userData:nil];
[self mouseEntered:event];
}
return area;
}
- (NSTrackingArea *)addTrackingAreaWithRect:(NSRect)rect
userInfo:(NSDictionary *)userInfo
cursor:(NSCursor *)cursor {
[_view addCursorRect:rect cursor:cursor];
return [self addTrackingAreaWithRect:rect userInfo:userInfo];
}
- (void)removeTrackingArea:(NSTrackingArea *)area {
NSRect rect = [area rect];
if ([self isMouseInsideRect:rect]) {
NSEvent *event = [NSEvent enterExitEventWithType:NSMouseExited
location:[NSEvent mouseLocation]
modifierFlags:NSMouseExitedMask
timestamp:0
windowNumber:[[_view window] windowNumber]
context:nil
eventNumber:0
trackingNumber:(NSInteger)area
userData:nil];
[self mouseExited:event];
}
[_view removeTrackingArea:area];
[area release];
}
#pragma mark -
#pragma mark Update State
- (BOOL)shouldUpdate {
return YES;
}
- (void)update {
for (NSObject *obj in _handlers) {
if ([obj conformsToProtocol:@protocol(WLUpdatable)]) {
NSObject <WLUpdatable> *updater = (NSObject <WLUpdatable> *)obj;
// Ask if should update
if ([updater shouldUpdate])
[updater update];
}
}
_lastBBSState = [[_view frontMostTerminal] bbsState];
_lastCursorRow = [[_view frontMostTerminal] cursorRow];
}
- (void)forceUpdate {
_activeTrackingAreaUserInfo = nil;
_backgroundTrackingAreaUserInfo = nil;
for (NSObject *obj in _handlers) {
if ([obj conformsToProtocol:@protocol(WLUpdatable)])
[(NSObject <WLUpdatable> *)obj update];
}
_lastBBSState = [[_view frontMostTerminal] bbsState];
_lastCursorRow = [[_view frontMostTerminal] cursorRow];
}
#pragma mark -
#pragma mark Accessor
- (void)restoreNormalCursor {
[_normalCursor set];
}
- (void)addHandler:(WLMouseHotspotHandler *)handler {
[_handlers addObject:handler];
[handler setManager:self];
}
- (void)reactiveHorizontalScroll:(id)sender {
_lastHorizontalScrollDirection = WLHorizontalScrollNone;
}
@end