Redrawing overlay views
Redrawing overlay views
- Subject: Redrawing overlay views
- From: Erik Stainsby <email@hidden>
- Date: Sat, 21 Apr 2012 17:25:53 -0700
I have a window which owns a webview over which I place buttons (in their own subviews) where the user has clicked. However, because they are not part of the webview's hierarchy of objects, when the window is resized the webview content will likely reflow. I therefore need to reposition the buttons. I have methods which annotate the DOM and which cache references to the nodes the user has selected. I am thinking to walk the cache of nodes and redraw the button's views at the new location of the corresponding DOM element. I am halfway through implementing this strategy but would be interested to hear other opinions of how this might be achieved.
Below is some of the code I have in the works which responds to the window's windowDidResizeNotification in the windowController and instructs the webView to walk the cache of user-selected DOM nodes. At present I cannot seem to recall the DOM nodes from the dictionary in which the code caches them.
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import "RSWebView.h"
//
// this controller is both windowController and webview frameLoadDelegate
@interface RSTrixieController : NSWindowController < NSComboBoxDataSource, NSComboBoxDelegate >
@property (retain) IBOutlet RSWebView * webview;
- (void) refreshWebviewOverlay:(NSNotification*)nota;
@end
@implementation
@synthesize webview;
- (void)windowDidLoad
{
[super windowDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshWebviewOverlay:) name:NSWindowDidResizeNotification object:[webview window]];
}
- (void) refreshWebviewOverlay:(NSNotification*)nota {
[[self webview] repositionLocatorViews];
}
[…]
@end
Then from the webView which responds:
@implementation
@synthesize boundingBox; // trivial view which draws it's outline
@synthesize locators; // mutable dict - button-hosting views - keyed-alike to taggedNodes
@synthesize taggedNodes; // mutable dict - DOMElements adjacent - keyed-alike to locators
- (void) repositionLocatorViews {
// clean up
[self removeBoundingBox];
for(NSString * idtag in [taggedNodes allKeys])
{
DOMElement * node = [taggedNodes objectForKey:idtag];
RSBoundingBox * bbox = [[RSBoundingBox alloc] initWithFrame:[node boundingBox]];
NSRect newBox = [self flipBoundingBox:[bbox frame] fromWebView:self];
[bbox setFrame:newBox];
RSLocatorViewController * lv = [locators objectForKey:idtag];
[[lv view]setFrame: [self frameRelativeTo:bbox]];
}
}
- (NSRect) flipBoundingBox:(NSRect)htmlBox fromWebView:(WebView*)webView {
NSRect bounds = [[[webView window] contentView] bounds];
float newY = bounds.size.height - htmlBox.size.height - htmlBox.origin.y;
return NSMakeRect(htmlBox.origin.x,newY,htmlBox.size.width, htmlBox.size.height);
}
- (NSRect) frameRelativeTo:(RSBoundingBox*)bbox {
return NSMakeRect(bbox.frame.origin.x-20, bbox.frame.origin.y + bbox.frame.size.height - 20, 20, 20);
}
- (void) removeBoundingBox {
if(nil != boundingBox)
{
[boundingBox removeFromSuperview];
boundingBox = nil;
}
}
[ … ]
Any thoughts and suggestions welcomed.
Erik Stainsby
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden