-
-
Save mmower/7aba5c24046462284c95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Application Delegate | |
@interface XKAppDelegate : NSObject <NSApplicationDelegate> { | |
NSInteger _previousSelectedRow; | |
} | |
@property (assign) IBOutlet NSWindow *window; | |
@property (assign) IBOutlet NSTableView *tableView; | |
@end | |
@implementation XKAppDelegate | |
@synthesize window = _window; | |
@synthesize tableView = _tableView; | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]]; | |
} | |
- (void)dealloc { | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { | |
return 100; | |
} | |
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { | |
XKMessageView *view = [tableView makeViewWithIdentifier:@"MessageCell" owner:self]; | |
NSInteger paras = 1 + random() % 32; | |
NSMutableString *markup = [NSMutableString stringWithFormat:@"<html>\n<body>\n<h1>Paras = %ld</h1>\n",paras]; | |
while( paras-- > 0 ) { | |
[markup appendFormat:@"<p>Hello from para %ld</p>\n",paras]; | |
} | |
[markup appendFormat:@"</body>\n</html>\n"]; | |
[[[view webView] mainFrame] loadHTMLString:markup baseURL:[NSURL URLWithString:@"http://127.0.0.1/"]]; | |
return view; | |
} | |
- (void)tableViewSelectionDidChange:(NSNotification *)notification { | |
NSTableView *tableView = [notification object]; | |
NSInteger selectedRow = [tableView selectedRow]; | |
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; | |
[indexes addIndex:_previousSelectedRow]; | |
[indexes addIndex:selectedRow]; | |
[tableView noteHeightOfRowsWithIndexesChanged:indexes]; | |
_previousSelectedRow = selectedRow; | |
} | |
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row { | |
CGFloat height = 128.0; | |
if( row == [tableView selectedRow] ) { | |
XKMessageView *messageView = [tableView viewAtColumn:0 row:row makeIfNecessary:NO]; | |
if( messageView ) { | |
height = MAX( height, [messageView desiredHeight] ); | |
} else { | |
NSLog( @"Cannot obtain webframe to calculate document height" ); | |
} | |
} | |
NSLog( @"Height = %g", height ); | |
return height; | |
} | |
- (void)windowResized:(NSNotification *)notification { | |
if( [notification object] == [self window] ) { | |
XKMessageView *messageView = [[self tableView] viewAtColumn:0 row:[[self tableView] selectedRow] makeIfNecessary:NO]; | |
[messageView updateDesiredHeightOfWebView]; | |
[[self tableView] noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:[[self tableView] selectedRow]]]; | |
} | |
} | |
// Custom view object for table rows | |
@interface XKMessageView : NSView | |
@property (assign) IBOutlet NSView *controlView; | |
@property (assign) IBOutlet WebView *webView; | |
@property (assign) CGFloat desiredHeight; | |
- (void)updateDesiredHeightOfWebView; | |
@end | |
@implementation XKMessageView | |
@synthesize controlView = _controlView; | |
@synthesize webView = _webView; | |
@synthesize desiredHeight = _desiredHeight; | |
- (void)awakeFromNib { | |
[[self webView] setFrameLoadDelegate:self]; | |
[[[[self webView] mainFrame] frameView] setAllowsScrolling:NO]; | |
} | |
- (void)webView:(WebView *)webView didFinishLoadForFrame:(WebFrame *)webFrame { | |
if( webFrame == [[self webView] mainFrame] ) { | |
[self updateDesiredHeightOfWebView]; | |
} | |
} | |
- (void)updateDesiredHeightOfWebView { | |
WebFrame *mainFrame = [[self webView] mainFrame]; | |
DOMDocument *document = [mainFrame DOMDocument]; | |
DOMNodeList *nodes = [document getElementsByTagName:@"body"]; | |
DOMNode *body = [nodes item:0]; | |
DOMHTMLBodyElement *bodyElement = (DOMHTMLBodyElement *)body; | |
CGFloat contentHeight = [bodyElement offsetHeight]; | |
NSLog( @"contentHeight = %g", contentHeight ); | |
[self setDesiredHeight:contentHeight+NSHeight( [[self controlView] frame] )]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment