forked from kgn/LinenClipView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LNClipView.m
252 lines (211 loc) · 7.4 KB
/
LNClipView.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
//
// LNClipView.m
// LinenClipView
//
// Created by David Keegan on 10/22/11.
// Copyright (c) 2011 David Keegan. All rights reserved.
//
#import "LNClipView.h"
#define LN_RUNNING_LION (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
@interface LNClipView : NSClipView
@property (nonatomic, strong) NSColor *pattern;
@end
@implementation LNClipView
@synthesize pattern = _pattern;
- (void)drawRect:(NSRect)dirtyRect{
[super drawRect:dirtyRect];
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setPatternPhase:
NSMakePoint(0.0f, NSHeight(self.bounds))];
// pattern
[self.pattern set];
NSRectFill(self.bounds);
// shadow
static NSGradient *gradient = nil;
if(gradient == nil){
gradient = [[NSGradient alloc] initWithStartingColor:
[NSColor colorWithDeviceWhite:0.0f alpha:0.2f]
endingColor:[NSColor clearColor]];
}
NSRect gradientRect = self.bounds;
gradientRect.size.height = 8.0f;
if(NSMinY(self.bounds) < 0.0f){
gradientRect.origin.y += -NSMinY(self.bounds)-NSHeight(gradientRect);
[gradient drawInRect:gradientRect angle:-90.0f];
}else if(NSMinY(self.bounds) > 1.0f){
NSRect docRect = [[[self enclosingScrollView] documentView] frame];
CGFloat yOffset = NSHeight(self.bounds)-(NSHeight(docRect)-NSMinY(self.bounds));
gradientRect.origin.y += NSHeight(self.bounds)-yOffset;
[gradient drawInRect:gradientRect angle:90.0f];
}
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
@end
@implementation LNScrollView{
LNClipView *_clipView;
}
- (void)setup{
if(LN_RUNNING_LION){
id docView = [self documentView];
_clipView = [[LNClipView alloc] initWithFrame:
[[self contentView] frame]];
[self setContentView:_clipView];
[self setDocumentView:docView];
}
}
- (id)initWithFrame:(NSRect)frameRect{
if((self = [super initWithFrame:frameRect])){
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder{
if((self = [super initWithCoder:coder])){
[self setup];
}
return self;
}
// this is the cure to fix the scrollers
// From https://gist.github.com/1608395
- (NSView *)hitTest:(NSPoint)aPoint{
if(!LN_RUNNING_LION){
return [super hitTest:aPoint];
}
NSEvent * currentEvent = [NSApp currentEvent];
if([currentEvent type] == NSLeftMouseDown){
// if we have a vertical scroller and it accepts the current hit
if([self hasVerticalScroller] && [[self verticalScroller] hitTest:aPoint] != nil){
[[self verticalScroller] mouseDown:currentEvent];
return nil;
}
// if we have a horizontal scroller and it accepts the current hit
if([self hasVerticalScroller] && [[self horizontalScroller] hitTest:aPoint] != nil){
[[self horizontalScroller] mouseDown:currentEvent];
return nil;
}
}else if([currentEvent type] == NSLeftMouseUp){
// if mouse up, just tell both our scrollers we have moused up
if([self hasVerticalScroller]){
[[self verticalScroller] mouseUp:currentEvent];
}
if([self hasHorizontalScroller]){
[[self horizontalScroller] mouseUp:currentEvent];
}
return self;
}
return [super hitTest:aPoint];
}
- (void)setPattern:(NSImage *)pattern{
_clipView.pattern = [NSColor colorWithPatternImage:pattern];
[_clipView setNeedsDisplay:YES];
}
@end
// From http://www.koders.com/objectivec/fid22DEE7EA2343C20D0FEEC2C079245069DF3E32A5.aspx
@interface LNWebClipView : LNClipView
- (void)setAdditionalClip:(NSRect)additionalClip;
- (void)resetAdditionalClip;
- (BOOL)hasAdditionalClip;
- (NSRect)additionalClip;
@end
// From http://www.koders.com/objectivec/fidD68502CAF940A73CC1E990AF8A2E3D17ACFCD647.aspx
@implementation LNWebClipView{
BOOL _haveAdditionalClip;
NSRect _additionalClip;
}
- (id)initWithFrame:(NSRect)frame{
if(!(self = [super initWithFrame:frame])){
return nil;
}
// In WebHTMLView, we set a clip. This is not typical to do in an
// NSView, and while correct for any one invocation of drawRect:,
// it causes some bad problems if that clip is cached between calls.
// The cached graphics state, which clip views keep around, does
// cache the clip in this undesirable way. Consequently, we want to
// release the GState for all clip views for all views contained in
// a WebHTMLView. Here we do it for subframes, which use WebClipView.
// See these bugs for more information:
// <rdar://problem/3409315>: REGRESSSION (7B58-7B60)?: Safari draws blank frames on macosx.apple.com perf page
[self releaseGState];
return self;
}
- (void)resetAdditionalClip{
_haveAdditionalClip = NO;
}
- (void)setAdditionalClip:(NSRect)additionalClip{
_haveAdditionalClip = YES;
_additionalClip = additionalClip;
}
- (BOOL)hasAdditionalClip{
return _haveAdditionalClip;
}
- (NSRect)additionalClip{
return _additionalClip;
}
//- (NSRect)_focusRingVisibleRect{
// NSRect rect = [self visibleRect];
// if (_haveAdditionalClip) {
// rect = NSIntersectionRect(rect, _additionalClip);
// }
// return rect;
//}
//
//- (void)scrollWheel:(NSEvent *)event{
// NSView *docView = [self documentView];
// if ([docView respondsToSelector:@selector(_webView)]) {
// WebView *wv = [docView _webView];
// if ([wv _dashboardBehavior:WebDashboardBehaviorAllowWheelScrolling]) {
// [super scrollWheel:event];
// }
// return;
// }
// [super scrollWheel:event];
//}
// From https://gist.github.com/b6bcb09a9fc0e9557c27
- (NSView *)hitTest:(NSPoint)aPoint{
if(!LN_RUNNING_LION){
return [super hitTest:aPoint];
}
NSEvent *currentEvent = [NSApp currentEvent];
NSScrollView *scrollView = [self enclosingScrollView];
if([currentEvent type] == NSLeftMouseDown){
// if we have a vertical scroller and it accepts the current hit
if([scrollView hasVerticalScroller] && [[scrollView verticalScroller] hitTest:aPoint] != nil){
[[scrollView verticalScroller] mouseDown:currentEvent];
}
// if we have a horizontal scroller and it accepts the current hit
if([scrollView hasVerticalScroller] && [[scrollView horizontalScroller] hitTest:aPoint] != nil){
[[scrollView horizontalScroller] mouseDown:currentEvent];
}
}
return [super hitTest:aPoint];
}
@end
@implementation LNWebView{
LNWebClipView *_clipView;
}
- (void)setup{
if(LN_RUNNING_LION){
id docView = [[[self mainFrame] frameView] documentView];
NSScrollView *scrollView = [docView enclosingScrollView];
_clipView = [[LNWebClipView alloc] initWithFrame:[[scrollView contentView] frame]];
[scrollView setContentView:_clipView];
[scrollView setDocumentView:docView];
}
}
- (id)initWithFrame:(NSRect)frameRect{
if((self = [super initWithFrame:frameRect])){
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder{
if((self = [super initWithCoder:coder])){
[self setup];
}
return self;
}
- (void)setPattern:(NSImage *)pattern{
_clipView.pattern = [NSColor colorWithPatternImage:pattern];
[_clipView setNeedsDisplay:YES];
}
@end