OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/cocoa/tab_contents_controller.h" | 5 #import "chrome/browser/cocoa/tab_contents_controller.h" |
6 | 6 |
7 #include "base/mac_util.h" | 7 #include "base/mac_util.h" |
8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/bookmarks/bookmark_model.h" | 10 #include "chrome/browser/bookmarks/bookmark_model.h" |
10 #include "chrome/browser/tab_contents/tab_contents.h" | 11 #include "chrome/browser/tab_contents/tab_contents.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/pref_service.h" |
| 14 |
| 15 // Default offset of the contents splitter in pixels. |
| 16 static const int kDefaultContentsSplitOffset = 400; |
| 17 |
| 18 // Never make the web part of the tab contents smaller than this (needed if the |
| 19 // window is only a few pixels high). |
| 20 static const int kMinWebHeight = 50; |
| 21 |
11 | 22 |
12 @implementation TabContentsController | 23 @implementation TabContentsController |
13 | 24 |
14 - (id)initWithNibName:(NSString*)name | 25 - (id)initWithNibName:(NSString*)name |
15 contents:(TabContents*)contents { | 26 contents:(TabContents*)contents { |
16 if ((self = [super initWithNibName:name | 27 if ((self = [super initWithNibName:name |
17 bundle:mac_util::MainAppBundle()])) { | 28 bundle:mac_util::MainAppBundle()])) { |
18 contents_ = contents; | 29 contents_ = contents; |
19 } | 30 } |
20 return self; | 31 return self; |
21 } | 32 } |
22 | 33 |
23 - (void)dealloc { | 34 - (void)dealloc { |
24 // make sure our contents have been removed from the window | 35 // make sure our contents have been removed from the window |
25 [[self view] removeFromSuperview]; | 36 [[self view] removeFromSuperview]; |
26 [super dealloc]; | 37 [super dealloc]; |
27 } | 38 } |
28 | 39 |
29 // Call when the tab view is properly sized and the render widget host view | 40 // Call when the tab view is properly sized and the render widget host view |
30 // should be put into the view hierarchy. | 41 // should be put into the view hierarchy. |
31 - (void)ensureContentsVisible { | 42 - (void)ensureContentsVisible { |
32 [contentsBox_ setContentView:contents_->GetNativeView()]; | 43 NSArray* subviews = [contentsContainer_ subviews]; |
| 44 if ([subviews count] == 0) |
| 45 [contentsContainer_ addSubview:contents_->GetNativeView()]; |
| 46 else if ([subviews objectAtIndex:0] != contents_->GetNativeView()) |
| 47 [contentsContainer_ replaceSubview:[subviews objectAtIndex:0] |
| 48 with:contents_->GetNativeView()]; |
33 } | 49 } |
34 | 50 |
35 // Returns YES if the tab represented by this controller is the front-most. | 51 // Returns YES if the tab represented by this controller is the front-most. |
36 - (BOOL)isCurrentTab { | 52 - (BOOL)isCurrentTab { |
37 // We're the current tab if we're in the view hierarchy, otherwise some other | 53 // We're the current tab if we're in the view hierarchy, otherwise some other |
38 // tab is. | 54 // tab is. |
39 return [[self view] superview] ? YES : NO; | 55 return [[self view] superview] ? YES : NO; |
40 } | 56 } |
41 | 57 |
42 - (void)willBecomeUnselectedTab { | 58 - (void)willBecomeUnselectedTab { |
43 RenderViewHost* rvh = contents_->render_view_host(); | 59 RenderViewHost* rvh = contents_->render_view_host(); |
44 if (rvh) | 60 if (rvh) |
45 rvh->Blur(); | 61 rvh->Blur(); |
46 } | 62 } |
47 | 63 |
48 - (void)willBecomeSelectedTab { | 64 - (void)willBecomeSelectedTab { |
49 RenderViewHost* rvh = contents_->render_view_host(); | 65 RenderViewHost* rvh = contents_->render_view_host(); |
50 if (rvh) | 66 if (rvh) |
51 rvh->Focus(); | 67 rvh->Focus(); |
52 } | 68 } |
53 | 69 |
54 - (void)tabDidChange:(TabContents*)updatedContents { | 70 - (void)tabDidChange:(TabContents*)updatedContents { |
55 // Calling setContentView: here removes any first responder status | 71 // Calling setContentView: here removes any first responder status |
56 // the view may have, so avoid changing the view hierarchy unless | 72 // the view may have, so avoid changing the view hierarchy unless |
57 // the view is different. | 73 // the view is different. |
58 if (contents_ != updatedContents) { | 74 if (contents_ != updatedContents) { |
59 contents_ = updatedContents; | 75 contents_ = updatedContents; |
60 [contentsBox_ setContentView:contents_->GetNativeView()]; | 76 [self ensureContentsVisible]; |
61 } | 77 } |
62 } | 78 } |
63 | 79 |
| 80 - (void)showDevToolsContents:(TabContents*)devToolsContents { |
| 81 NSArray* subviews = [contentsContainer_ subviews]; |
| 82 if (devToolsContents) { |
| 83 DCHECK_GE([subviews count], 1u); |
| 84 if ([subviews count] == 1) { |
| 85 [contentsContainer_ addSubview:devToolsContents->GetNativeView()]; |
| 86 } else { |
| 87 DCHECK_EQ([subviews count], 2u); |
| 88 [contentsContainer_ replaceSubview:[subviews objectAtIndex:1] |
| 89 with:devToolsContents->GetNativeView()]; |
| 90 } |
| 91 // Restore split offset. |
| 92 CGFloat splitOffset = g_browser_process->local_state()->GetInteger( |
| 93 prefs::kDevToolsSplitLocation); |
| 94 if (splitOffset == -1) { |
| 95 // Initial load, set to default value. |
| 96 splitOffset = kDefaultContentsSplitOffset; |
| 97 } |
| 98 splitOffset = MIN(splitOffset, |
| 99 NSHeight([contentsContainer_ frame]) - kMinWebHeight); |
| 100 DCHECK_GE(splitOffset, 0) << "kMinWebHeight needs to be smaller than " |
| 101 << "smallest available tab contents space."; |
| 102 splitOffset = MAX(0, splitOffset); |
| 103 |
| 104 // It seems as if |-setPosition:ofDividerAtIndex:| should do what's needed, |
| 105 // but I can't figure out how to use it. Manually resize web and devtools. |
| 106 NSRect devtoolsFrame = [devToolsContents->GetNativeView() frame]; |
| 107 devtoolsFrame.size.height = splitOffset; |
| 108 [devToolsContents->GetNativeView() setFrame:devtoolsFrame]; |
| 109 |
| 110 NSRect webFrame = [[subviews objectAtIndex:0] frame]; |
| 111 webFrame.size.height = NSHeight([contentsContainer_ frame]) - |
| 112 [self devToolsHeight]; |
| 113 [[subviews objectAtIndex:0] setFrame:webFrame]; |
| 114 |
| 115 [contentsContainer_ adjustSubviews]; |
| 116 } else { |
| 117 if ([subviews count] > 1) { |
| 118 NSView* oldDevToolsContentsView = [subviews objectAtIndex:1]; |
| 119 // Store split offset when hiding devtools window only. |
| 120 int splitOffset = NSHeight([oldDevToolsContentsView frame]); |
| 121 g_browser_process->local_state()->SetInteger( |
| 122 prefs::kDevToolsSplitLocation, splitOffset); |
| 123 [oldDevToolsContentsView removeFromSuperview]; |
| 124 } |
| 125 } |
| 126 } |
| 127 |
| 128 - (CGFloat)devToolsHeight { |
| 129 NSArray* subviews = [contentsContainer_ subviews]; |
| 130 if ([subviews count] < 2) |
| 131 return 0; |
| 132 return NSHeight([[subviews objectAtIndex:1] frame]) + |
| 133 [contentsContainer_ dividerThickness]; |
| 134 } |
| 135 |
64 @end | 136 @end |
OLD | NEW |