Permalink
Browse files

Now highlighting results on the scrollbar. Still need to find a way o…

…f drawing on top of the scroll knob.
  • Loading branch information...
Kapeli committed May 9, 2012
1 parent 783491c commit e4dfd05dfc1368be07455bac1aa44a62d9aa238c
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 433DA6B4155A95340023C7D7 /* DHScrollbarHighlighter.m in Sources */ = {isa = PBXBuildFile; fileRef = 433DA6B3155A95340023C7D7 /* DHScrollbarHighlighter.m */; };
43F1FF7D1558185F0043B796 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43F1FF7C1558185F0043B796 /* WebKit.framework */; };
43F7F3821554F75D00879D86 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43F7F3811554F75D00879D86 /* Cocoa.framework */; };
43F7F38C1554F75D00879D86 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 43F7F38A1554F75D00879D86 /* InfoPlist.strings */; };
@@ -32,6 +33,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
+ 433DA6B2155A95340023C7D7 /* DHScrollbarHighlighter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DHScrollbarHighlighter.h; sourceTree = "<group>"; };
+ 433DA6B3155A95340023C7D7 /* DHScrollbarHighlighter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DHScrollbarHighlighter.m; sourceTree = "<group>"; };
43F1FF7C1558185F0043B796 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
43F7F37D1554F75D00879D86 /* HighlightedWebView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HighlightedWebView.app; sourceTree = BUILT_PRODUCTS_DIR; };
43F7F3811554F75D00879D86 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
@@ -111,6 +114,8 @@
43F7F3941554F75D00879D86 /* DHAppDelegate.m */,
43F7F39E1554F77000879D86 /* DHWebView.h */,
43F7F39F1554F77000879D86 /* DHWebView.m */,
+ 433DA6B2155A95340023C7D7 /* DHScrollbarHighlighter.h */,
+ 433DA6B3155A95340023C7D7 /* DHScrollbarHighlighter.m */,
43F7F3AE155508BB00879D86 /* DHSearchQuery.h */,
43F7F3AF155508BB00879D86 /* DHSearchQuery.m */,
43F7F3B8155566D700879D86 /* DHMatchedText.h */,
@@ -203,6 +208,7 @@
43F7F3A01554F77000879D86 /* DHWebView.m in Sources */,
43F7F3B0155508BB00879D86 /* DHSearchQuery.m in Sources */,
43F7F3BA155566D700879D86 /* DHMatchedText.m in Sources */,
+ 433DA6B4155A95340023C7D7 /* DHScrollbarHighlighter.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -4,6 +4,10 @@
<dict>
<key>HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
<true/>
+ <key>IDEWorkspaceUserSettings_HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
+ <true/>
+ <key>IDEWorkspaceUserSettings_SnapshotAutomaticallyBeforeSignificantChanges</key>
+ <false/>
<key>SnapshotAutomaticallyBeforeSignificantChanges</key>
<false/>
</dict>
@@ -28,7 +28,7 @@
// This is the "alternate" style (the one in the screenshot), abandoned because it resizes the elements and also looks weird
// with elements that span across multiple nodes.
-// static NSString *DHHighlightSpan = @"-webkit-border-radius: 7px; -webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.6); background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(244, 234, 38, 0.7)), to(rgba(237, 206, 0, 0.7)) ); border: 1px solid rgba(244, 234, 38, 0.8);display:inline;position:static;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;";
+ static NSString *DHHighlightSpan = @"-webkit-border-radius: 7px; -webkit-box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.6); background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(244, 234, 38, 0.7)), to(rgba(237, 206, 0, 0.7)) ); border: 1px solid rgba(244, 234, 38, 0.8);display:inline;position:static;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;";
-static NSString *DHHighlightSpan = @"background-color:rgba(251,244,201,0.8);display:inline;position:static;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;opacity:1.0;";
+//static NSString *DHHighlightSpan = @"background-color:rgba(251,244,201,0.8);display:inline;position:static;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;opacity:1.0;";
static NSString *DHSpanWrap = @"display:inline;position:static;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;opacity:1.0;";
@@ -0,0 +1,18 @@
+#import <Cocoa/Cocoa.h>
+#import <WebKit/WebKit.h>
+
+@interface DHScrollbarHighlighter : NSView {
+ WebView *parentView;
+ NSArray *matches;
+ NSMutableArray *highlightRects;
+}
+
+@property (retain) WebView *parentView;
+@property (retain) NSArray *matches;
+@property (retain) NSMutableArray *highlightRects;
+
++ (DHScrollbarHighlighter *)highlighterWithWebView:(WebView *)aWebView andMatches:(NSArray *)matches;
+- (id)initWithWebView:(WebView *)aWebView andMatches:(NSArray *)matches;
+- (void)calculatePositions;
+
+@end
@@ -0,0 +1,99 @@
+#import "DHScrollbarHighlighter.h"
+#import "DHMatchedText.h"
+
+@implementation DHScrollbarHighlighter
+
+@synthesize parentView;
+@synthesize matches;
+@synthesize highlightRects;
+
++ (DHScrollbarHighlighter *)highlighterWithWebView:(WebView *)aWebView andMatches:(NSArray *)someMatches
+{
+ return [[[self alloc] initWithWebView:aWebView andMatches:someMatches] autorelease];
+}
+
+- (id)initWithWebView:(WebView *)aWebView andMatches:(NSArray *)someMatches
+{
+ NSScrollView *scrollView = [[[[aWebView mainFrame] frameView] documentView] enclosingScrollView];
+ if(scrollView && scrollView.verticalScroller && !NSEqualRects(scrollView.verticalScroller.frame, NSZeroRect))
+ {
+ NSRect scrollerFrame = [scrollView verticalScroller].frame;
+ NSRect knobRect = [[scrollView verticalScroller] rectForPart:NSScrollerKnob];
+ [self initWithFrame:NSMakeRect(scrollerFrame.origin.x+knobRect.origin.x, scrollerFrame.origin.y, knobRect.size.width, scrollerFrame.size.height)];
+ if(self)
+ {
+ self.parentView = aWebView;
+ self.matches = someMatches;
+ self.highlightRects = [NSMutableArray array];
+ [self setAutoresizingMask:NSViewMinXMargin | NSViewHeightSizable];
+ [self calculatePositions];
+ [parentView addSubview:self];
+ }
+ }
+ return self;
+}
+
+- (void)calculatePositions
+{
+ NSInteger scrollHeight = [[[parentView mainFrameDocument] body] scrollHeight];
+ float ownHeight = self.frame.size.height;
+ float ownWidth = self.frame.size.width;
+ NSMutableArray *rects = [NSMutableArray array];
+ for(DHMatchedText *matchedText in matches)
+ {
+ DOMHTMLElement *wrapperSpan = (DOMHTMLElement*)[matchedText highlightedSpan];
+ for(int i = 0; i < wrapperSpan.children.length; i++)
+ {
+ DOMHTMLElement *highlightedSpan = (DOMHTMLElement*)[[wrapperSpan children] item:i];
+ if(highlightedSpan.nodeType == DOM_ELEMENT_NODE)
+ {
+ float flippedY = [highlightedSpan offsetTop] / (float)scrollHeight * ownHeight;
+ float actualY = ownHeight-flippedY;
+ int roundY = (int)actualY;
+ [rects addObject:[NSValue valueWithRect:NSMakeRect(0, roundY, ownWidth, 3)]];
+ }
+ }
+ }
+ for(NSValue *rectValue in rects)
+ {
+ NSRect rect = [rectValue rectValue];
+ BOOL didIntersect = NO;
+ for(NSValue *otherRectValue in highlightRects)
+ {
+ NSRect otherRect = [otherRectValue rectValue];
+ if(NSIntersectsRect(rect, otherRect))
+ {
+ didIntersect = YES;
+ break;
+ }
+ }
+ if(!didIntersect)
+ {
+ [highlightRects addObject:rectValue];
+ }
+ }
+}
+
+- (void)drawRect:(NSRect)dirtyRect
+{
+ NSColor *inner = [NSColor colorWithCalibratedRed:0.984f green:0.956f blue:0.788f alpha:1.0f];
+ NSColor *outer = [NSColor colorWithCalibratedRed:0.941f green:0.8f blue:0.203f alpha:1.0f];
+ for(NSValue *highlightRect in highlightRects)
+ {
+ NSRect rect = [highlightRect rectValue];
+ [inner set];
+ NSRectFill(rect);
+ [outer set];
+ [NSBezierPath strokeRect:rect];
+ }
+}
+
+- (void)dealloc
+{
+ [parentView release];
+ [matches release];
+ [highlightRects release];
+ [super dealloc];
+}
+
+@end
@@ -1,20 +1,23 @@
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import "DHSearchQuery.h"
+#import "DHScrollbarHighlighter.h"
@interface DHWebView : WebView {
NSTimer *workerTimer;
DHSearchQuery *currentQuery;
NSMutableArray *highlightedMatches;
NSMutableArray *matchedTexts;
NSMutableString *entirePageContent;
+ DHScrollbarHighlighter *scrollHighlighter;
}
@property (retain) NSTimer *workerTimer;
@property (retain) DHSearchQuery *currentQuery;
@property (retain) NSMutableArray *highlightedMatches;
@property (retain) NSMutableArray *matchedTexts;
@property (retain) NSMutableString *entirePageContent;
+@property (retain) DHScrollbarHighlighter *scrollHighlighter;
- (void)highlightQuery:(NSString *)aQuery caseSensitive:(BOOL)isCaseSensitive;
- (void)highlightQuery:(DHSearchQuery *)query;
@@ -8,6 +8,7 @@ @implementation DHWebView
@synthesize highlightedMatches;
@synthesize matchedTexts;
@synthesize entirePageContent;
+@synthesize scrollHighlighter;
- (BOOL)searchFor:(NSString *)string direction:(BOOL)forward caseSensitive:(BOOL)caseFlag wrap:(BOOL)wrapFlag
{
@@ -62,6 +63,8 @@ - (void)clearHighlights
self.highlightedMatches = [NSMutableArray array];
self.matchedTexts = [NSMutableArray array];
self.entirePageContent = [NSMutableString string];
+ [self.scrollHighlighter removeFromSuperview];
+ self.scrollHighlighter = nil;
if(!currentQuery.query.length)
{
return;
@@ -284,6 +287,7 @@ - (void)timeredHighlightOfMatches:(NSMutableArray *)matches
if(!matches.count)
{
[self tryToGuessSelection:currentQuery.selectionAfterHighlight];
+ self.scrollHighlighter = [DHScrollbarHighlighter highlighterWithWebView:self andMatches:highlightedMatches];
return;
}
DHMatchedText *last = [matches lastObject];
@@ -472,6 +476,7 @@ - (void)dealloc
[highlightedMatches release];
[matchedTexts release];
[entirePageContent release];
+ [scrollHighlighter release];
[super dealloc];
}
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -8,7 +8,7 @@ Drop-in WebView subclass that adds Safari-style in-page search-result highlighti
Features:
=========
* Highlight search results (Safari-style).
-* NOT YET: Show position of search results by highlighting the scrollbar (Google Chrome-style).
+* Show position of search results by highlighting the scrollbar (Google Chrome-style).
Project Goals:
==============

0 comments on commit e4dfd05

Please sign in to comment.