Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(803)

Unified Diff: docs/cocoa_tips_and_tricks.md

Issue 1309473002: WIP: Migrate Wiki content over to src/docs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « docs/closure_compilation.md ('k') | docs/code_coverage.md » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: docs/cocoa_tips_and_tricks.md
diff --git a/docs/cocoa_tips_and_tricks.md b/docs/cocoa_tips_and_tricks.md
new file mode 100644
index 0000000000000000000000000000000000000000..d13ba68f6e16b18d74a0b2df792066f2bc24e881
--- /dev/null
+++ b/docs/cocoa_tips_and_tricks.md
@@ -0,0 +1,69 @@
+# Introduction
+
+A collection of idioms that we use when writing the Cocoa views and controllers for Chromium.
+
+## 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:
+
+```
+- (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:
+
+```
+@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:
+```
+ EXPECT_NSEQ(@"foo", aString);
+```
+Instead of this:
+```
+ EXPECT_TRUE([aString isEqualToString:@"foo"]);
+```
« no previous file with comments | « docs/closure_compilation.md ('k') | docs/code_coverage.md » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698