Skip to content

Commit

Permalink
misc progress on the ever-evolving refactoring process
Browse files Browse the repository at this point in the history
  • Loading branch information
rsms committed Oct 9, 2010
1 parent 092c9de commit 8816a96
Show file tree
Hide file tree
Showing 25 changed files with 360 additions and 195 deletions.
9 changes: 8 additions & 1 deletion examples/simple-app/AppDelegate.mm
Expand Up @@ -5,12 +5,19 @@ @implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
// Create a new browser & window when we start
[MyBrowser openEmptyWindow];
CTBrowserWindowController* windowController =
[[CTBrowserWindowController alloc] initWithBrowser:[MyBrowser browser]];
[windowController.browser addBlankTabInForeground:YES];
[windowController showWindow:self];
// Because window controller are owned by the app, we need to release our
// reference.
//[windowController autorelease];
}

// When there are no windows in our application, this class (AppDelegate) will
// become the first responder. We forward the command to the browser class.
- (void)commandDispatch:(id)sender {
NSLog(@"commandDispatch %d", [sender tag]);
[MyBrowser executeCommand:[sender tag]];
}

Expand Down
3 changes: 2 additions & 1 deletion examples/simple-app/MyBrowser.mm
Expand Up @@ -7,7 +7,8 @@ @implementation MyBrowser
// new CTTabContents object which will represent the contents of the new tab.
-(CTTabContents*)createBlankTabBasedOn:(CTTabContents*)baseContents {
// Create a new instance of our tab type
return [[MyTabContents alloc] initWithBaseTabContents:baseContents];
return [[[MyTabContents alloc]
initWithBaseTabContents:baseContents] autorelease];
}

@end
2 changes: 1 addition & 1 deletion examples/simple-app/MyTabContents.mm
Expand Up @@ -4,7 +4,7 @@
@implementation MyTabContents

-(id)initWithBaseTabContents:(CTTabContents*)baseContents {
if (!(self = [super init])) return nil;
if (!(self = [super initWithBaseTabContents:baseContents])) return nil;

// Setup our contents -- a scrolling text view

Expand Down
30 changes: 30 additions & 0 deletions ownership-and-relations.txt
@@ -0,0 +1,30 @@

CTBrowserWindowController
CTTabStripController
CTTabStripView
TabContentsController[] # NSViewController for the contents of a tab
weak:CTTabContents contents_
TabController[] # NSViewController for a tab in the tab strip
weak:id<CTTabControllerTarget> target_
GTMWindowSheetController sheetController_
weak:CTTabContents currentTab_
weak:CTBrowser browser_
weak:CTTabStripModel tabStripModel_
CTTabStripModelObserverBridge
weak:id controller_ # owns this
weak:CTTabStripModel model_
CTToolbarController
weak:CTBrowser browser_
CTBrowser browser_
CTTabStripModel
TabContentsData[]
CTTabContents
NSView view_
weak: CTBrowser browser_
weak: CTTabContents parentOpener_
CTTabStripModelOrderController
weak:CTTabStripModel tabStripModel_
CTTabStripModelObserver[]
weak:NSObject<CTTabStripModelDelegate> delegate_
weak:CTBrowserWindowController *windowController_

33 changes: 11 additions & 22 deletions src/CTBrowser.h
Expand Up @@ -20,7 +20,7 @@ class CTTabStripModel;

@interface CTBrowser : NSObject <CTTabStripModelDelegate> {
CTTabStripModel *tabStripModel_;
CTBrowserWindowController *windowController_;
__weak CTBrowserWindowController *windowController_;
}

// The tab strip model
Expand All @@ -32,43 +32,34 @@ class CTTabStripModel;
// The window. Convenience for [windowController window]
@property(readonly, nonatomic) NSWindow* window;

// Create a new browser with a window. (autoreleased)
// Create a new browser with a window.
// @autoreleased
+(CTBrowser*)browser;
+(CTBrowser*)browserWithWindowFrame:(const NSRect)frame;

// Returns the current "main" browser instance, or nil if none. "main" means the
// browser's window(Controller) is the main window. Useful when there's a need
// to e.g. add contents to the "best browser from the users perspective".
+ (CTBrowser*)mainBrowser;
// Initialize a new browser as the child of windowController
-(id)initWithWindowController:(CTBrowserWindowController*)windowController;

// Creates and opens a new window. (retained)
+(CTBrowser*)openEmptyWindow;

// Create a new window controller. The default implementation will create a
// controller loaded with a nib called "BrowserWindow". If the nib can't be
// found in the main bundle, a fallback nib will be loaded from the framework.
// This is usually enough since all UI which normally is customized is comprised
// within each tab (CTTabContents view).
-(CTBrowserWindowController *)createWindowController;

// This should normally _not_ be overridden
-(void)createWindowControllerInstance;
// alias for [initWithWindowController:[self createWindowController]]
-(id)init;

// Create a new toolbar controller. The default implementation will create a
// controller loaded with a nib called "Toolbar". If the nib can't be found in
// the main bundle, a fallback nib will be loaded from the framework.
// Returning nil means there is no toolbar.
// @autoreleased
-(CTToolbarController *)createToolbarController;

// Create a new tab contents controller. Override this to provide a custom
// CTTabContentsController subclass.
// @autoreleased
-(CTTabContentsController*)createTabContentsControllerWithContents:
(CTTabContents*)contents;

// Create a new default/blank CTTabContents.
// |baseContents| represents the CTTabContents which is currently in the
// foreground. It might be nil.
// Subclasses could override this to provide a custom CTTabContents type.
// @autoreleased
-(CTTabContents*)createBlankTabBasedOn:(CTTabContents*)baseContents;

// Add blank tab
Expand All @@ -82,7 +73,7 @@ class CTTabStripModel;
inForeground:(BOOL)foreground;
-(CTTabContents*)addTabContents:(CTTabContents*)contents; // inForeground:YES

// Commands
// Commands -- TODO: move to CTBrowserWindowController
-(void)newWindow;
-(void)closeWindow;
-(void)closeTab;
Expand Down Expand Up @@ -111,8 +102,6 @@ class CTTabStripModel;
// callbacks
-(void)loadingStateDidChange:(CTTabContents*)contents;
-(void)windowDidBeginToClose;
-(void)windowDidBecomeMain:(NSNotification*)notification;
-(void)windowDidResignMain:(NSNotification*)notification;

// Convenience helpers (proxy for TabStripModel)
-(int)tabCount;
Expand Down
120 changes: 40 additions & 80 deletions src/CTBrowser.mm
Expand Up @@ -9,114 +9,72 @@

@implementation CTBrowser

static CTBrowser* _currentMain = nil; // weak

@synthesize windowController = windowController_;
@synthesize tabStripModel = tabStripModel_;


+(CTBrowser*)browser {
CTBrowser *browser = [[[self alloc] init] autorelease];
[browser createWindowControllerInstance];
// TODO: post notification browserReady:self ?
return browser;
/*- (id)retain {
self = [super retain];
NSLog(@"%@ did retain (retainCount: %u)", self, [self retainCount]);
NSLog(@"%@", [NSThread callStackSymbols]);
return self;
}

+(CTBrowser*)browserWithWindowFrame:(const NSRect)frame {
CTBrowser* browser = [self browser];
[browser.window setFrame:frame display:NO];
return browser;
}
- (void)release {
NSLog(@"%@ will release (retainCount: %u)", self, [self retainCount]);
NSLog(@"%@", [NSThread callStackSymbols]);
[super release];
}*/


+(CTBrowser*)openEmptyWindow {
CTBrowser *browser = [self browser];
// reference will live as long as the window lives (until closed)
[browser addBlankTabInForeground:YES];
[browser.windowController showWindow:self];
return browser;
+ (CTBrowser*)browser {
return [[[self alloc] init] autorelease];
}


+ (CTBrowser*)mainBrowser {
// TODO: synchronization
return _currentMain;
- (id)init {
if (!(self = [super init])) return nil;
tabStripModel_ = new CTTabStripModel(self);
return self;
}


-(id)init {
if (!(self = [super init])) return nil;
tabStripModel_ = new CTTabStripModel(self);
if (!_currentMain) {
// TODO: synchronization
_currentMain = self;
- (id)initWithWindowController:(CTBrowserWindowController*)windowController {
if ((self = [self init])) {
windowController_ = windowController; // weak since it own us
}
return self;
}


-(void)dealloc {
DLOG("deallocing browser %@", self);
if (_currentMain == self)
_currentMain = nil;
//[self finalize];
delete tabStripModel_;
[windowController_ release];
[super dealloc];
}


-(void)finalize {
if (_currentMain == self) {
// TODO: synchronization
_currentMain = nil;
}
delete tabStripModel_;
[super finalize];
}


- (void)windowDidBecomeMain:(NSNotification*)notification {
assert([NSThread isMainThread]); // since we don't lock
_currentMain = self;
}

- (void)windowDidResignMain:(NSNotification*)notification {
if (_currentMain == self) {
assert([NSThread isMainThread]); // since we don't lock
_currentMain = nil;
}
}


// private
-(void)createWindowControllerInstance {
assert(!windowController_);
windowController_ = [self createWindowController];
assert(windowController_);
}


-(CTBrowserWindowController *)createWindowController {
// subclasses could override this
NSString *windowNibPath = [CTUtil pathForResource:@"BrowserWindow"
ofType:@"nib"];
return [[CTBrowserWindowController alloc] initWithWindowNibPath:windowNibPath
browser:self];
}

-(CTToolbarController *)createToolbarController {
// subclasses could override this -- returning nil means no toolbar
NSBundle *bundle = [CTUtil bundleForResource:@"Toolbar" ofType:@"nib"];
return [[CTToolbarController alloc] initWithNibName:@"Toolbar"
return [[[CTToolbarController alloc] initWithNibName:@"Toolbar"
bundle:bundle
browser:self];
browser:self] autorelease];
}

-(CTTabContentsController*)createTabContentsControllerWithContents:
(CTTabContents*)contents {
// subclasses could override this
return [[[CTTabContentsController alloc] initWithContents:contents] autorelease];
return [[[CTTabContentsController alloc]
initWithContents:contents] autorelease];
}


Expand Down Expand Up @@ -186,8 +144,6 @@ -(void)loadingStateDidChange:(CTTabContents*)contents {
}

-(void)windowDidBeginToClose {
if (_currentMain == self)
_currentMain = nil;
tabStripModel_->CloseAllTabs();
}

Expand All @@ -209,12 +165,19 @@ -(void)windowDidBeginToClose {
#pragma mark Commands

-(void)newWindow {
[isa openEmptyWindow];
// Create a new browser & window when we start
Class cls = self.windowController ? [self.windowController class] :
[CTBrowserWindowController class];
CTBrowser *browser = [isa browser];
CTBrowserWindowController* windowController =
[[cls alloc] initWithBrowser:browser];
[browser addBlankTabInForeground:YES];
[windowController showWindow:self];
[windowController autorelease];
}

-(void)closeWindow {
[self.window orderOut:self];
[self.window performClose:self]; // Autoreleases the controller.
[self.windowController close];
}

-(CTTabContents*)addTabContents:(CTTabContents*)contents
Expand All @@ -240,7 +203,8 @@ -(CTTabContents*)addTabContents:(CTTabContents*)contents {
-(CTTabContents*)createBlankTabBasedOn:(CTTabContents*)baseContents {
// subclasses should override this to provide a custom CTTabContents type
// and/or initialization
return [[CTTabContents alloc] initWithBaseTabContents:baseContents];
return [[[CTTabContents alloc]
initWithBaseTabContents:baseContents] autorelease];
}

// implementation conforms to CTTabStripModelDelegate
Expand Down Expand Up @@ -363,8 +327,6 @@ -(void)executeCommand:(int)cmd {

+(void)executeCommand:(int)cmd {
switch (cmd) {
case CTBrowserCommandNewWindow:
case CTBrowserCommandNewTab: [self openEmptyWindow]; break;
case CTBrowserCommandExit: [NSApp terminate:self]; break;
}
}
Expand All @@ -374,20 +336,17 @@ +(void)executeCommand:(int)cmd {
#pragma mark CTTabStripModelDelegate protocol implementation


-(CTBrowser*)createNewStripWithContents:(CTTabContents*)contents
windowBounds:(const NSRect)windowBounds
maximize:(BOOL)maximize {
-(CTBrowser*)createNewStripWithContents:(CTTabContents*)contents {
//assert(CanSupportWindowFeature(FEATURE_TABSTRIP));

//gfx::Rect new_window_bounds = window_bounds;
//if (dock_info.GetNewWindowBounds(&new_window_bounds, &maximize))
// dock_info.AdjustOtherWindowBounds();

// Create an empty new browser window the same size as the old one.
CTBrowser* browser = [isa browserWithWindowFrame:windowBounds];
CTBrowser* browser = [isa browser];
browser.tabStripModel->AppendTabContents(contents, true);
[browser loadingStateDidChange:contents];
[browser.windowController showWindow:self];

// Orig impl:
//browser->set_override_bounds(new_window_bounds);
Expand Down Expand Up @@ -418,12 +377,13 @@ -(void)continueDraggingDetachedTab:(CTTabContents*)contents

// Returns whether some contents can be duplicated.
-(BOOL)canDuplicateContentsAt:(int)index {
return false;
return NO;
}

// Duplicates the contents at the provided index and places it into its own
// window.
-(void)duplicateContentsAt:(int)index {
NOTIMPLEMENTED();
}

// Called when a drag session has completed and the frame that initiated the
Expand Down

0 comments on commit 8816a96

Please sign in to comment.