diff options
author | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2015-10-13 13:24:50 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> | 2015-10-14 10:57:25 +0000 |
commit | af3d4809763ef308f08ced947a73b624729ac7ea (patch) | |
tree | 4402b911e30383f6c6dace1e8cf3b8e85355db3a /chromium/docs/cocoa_tips_and_tricks.md | |
parent | 0e8ff63a407fe323e215bb1a2c423c09a4747c8a (diff) | |
download | qtwebengine-chromium-af3d4809763ef308f08ced947a73b624729ac7ea.tar.gz |
BASELINE: Update Chromium to 47.0.2526.14
Also adding in sources needed for spellchecking.
Change-Id: Idd44170fa1616f26315188970a8d5ba7d472b18a
Reviewed-by: Michael BrĂ¼ning <michael.bruning@theqtcompany.com>
Diffstat (limited to 'chromium/docs/cocoa_tips_and_tricks.md')
-rw-r--r-- | chromium/docs/cocoa_tips_and_tricks.md | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/chromium/docs/cocoa_tips_and_tricks.md b/chromium/docs/cocoa_tips_and_tricks.md new file mode 100644 index 00000000000..2ab0329af57 --- /dev/null +++ b/chromium/docs/cocoa_tips_and_tricks.md @@ -0,0 +1,85 @@ +# Cocoa Tips and Tricks + +A collection of idioms that we use when writing the Cocoa views and controllers +for Chromium. + +[TOC] + +## NSWindowController Initialization + +To make sure that |window| and |delegate| are wired up correctly in your xib, +it's useful to add this to your window controller: + +```objective-c +- (void)awakeFromNib { + DCHECK([self window]); + DCHECK_EQ(self, [[self window] delegate]); +} +``` + +## NSWindowController Cleanup + +"You want the window controller to release itself it |-windowDidClose:|, because +else it could die while its views are still around. if it (auto)releases itself +in the callback, the window and its views are already gone and they won't send +messages to the released controller." +- Nico Weber (thakis@) + +See +[Window Closing Behavior, ADC Reference](http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Documents/Concepts/WindowClosingBehav.html#//apple_ref/doc/uid/20000027) +for the full story. + +What this means in practice is: + +```objective-c +@interface MyWindowController : NSWindowController<NSWindowDelegate> { + IBOutlet NSButton* closeButton_; +} +- (IBAction)closeButton:(id)sender; +@end + +@implementation MyWindowController +- (id)init { + if ((self = [super initWithWindowNibName:@"MyWindow" ofType:@"nib"])) { + } + return self; +} + +- (void)awakeFromNib { + // Check that we set the window and its delegate in the XIB. + DCHECK([self window]); + DCHECK_EQ(self, [[self window] delegate]); +} + +// NSWindowDelegate notification. +- (void)windowWillClose:(NSNotification*)notif { + [self autorelease]; +} + +// Action for a button that lets the user close the window. +- (IBAction)closeButton:(id)sender { + // We clean ourselves up after the window has closed. + [self close]; +} +@end +``` + +## Unit Tests + +There are four Chromium-specific GTest macros for writing ObjC++ test cases. +These macros are `EXPECT_NSEQ`, `EXPECT_NSNE`, and `ASSERT` variants by the same +names. These test `-[id<NSObject> isEqual:]` and will print the object's +`-description` in GTest-style if the assertion fails. These macros are defined +in `//testing/gtest_mac.h`. Just include that file and you can start using them. + +This allows you to write this: + +```objective-c +EXPECT_NSEQ(@"foo", aString); +``` + +Instead of this: + +```objective-c +EXPECT_TRUE([aString isEqualToString:@"foo"]); +``` |