OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/sidebar_controller.h" | 5 #import "chrome/browser/cocoa/sidebar_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include <Cocoa/Cocoa.h> | 9 #include <Cocoa/Cocoa.h> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 | 30 |
31 @interface SidebarController (Private) | 31 @interface SidebarController (Private) |
32 - (void)showSidebarContents:(TabContents*)sidebarContents; | 32 - (void)showSidebarContents:(TabContents*)sidebarContents; |
33 - (void)resizeSidebarToNewWidth:(CGFloat)width; | 33 - (void)resizeSidebarToNewWidth:(CGFloat)width; |
34 @end | 34 @end |
35 | 35 |
36 | 36 |
37 @implementation SidebarController | 37 @implementation SidebarController |
38 | 38 |
39 - (id)init { | 39 - (id)initWithDelegate:(id<TabContentsControllerDelegate>)delegate { |
40 if ((self = [super init])) { | 40 if ((self = [super init])) { |
41 splitView_.reset([[NSSplitView alloc] initWithFrame:NSZeroRect]); | 41 splitView_.reset([[NSSplitView alloc] initWithFrame:NSZeroRect]); |
42 [splitView_ setDividerStyle:NSSplitViewDividerStyleThin]; | 42 [splitView_ setDividerStyle:NSSplitViewDividerStyleThin]; |
43 [splitView_ setVertical:YES]; | 43 [splitView_ setVertical:YES]; |
44 [splitView_ setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; | 44 [splitView_ setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; |
45 [splitView_ setDelegate:self]; | 45 [splitView_ setDelegate:self]; |
46 sidebarContents_ = NULL; | 46 |
| 47 contentsController_.reset( |
| 48 [[TabContentsController alloc] initWithContents:NULL |
| 49 delegate:delegate]); |
47 } | 50 } |
48 return self; | 51 return self; |
49 } | 52 } |
50 | 53 |
51 - (void)dealloc { | 54 - (void)dealloc { |
52 [splitView_ setDelegate:nil]; | 55 [splitView_ setDelegate:nil]; |
53 [super dealloc]; | 56 [super dealloc]; |
54 } | 57 } |
55 | 58 |
56 - (NSSplitView*)view { | 59 - (NSSplitView*)view { |
57 return splitView_.get(); | 60 return splitView_.get(); |
58 } | 61 } |
59 | 62 |
60 - (NSSplitView*)splitView { | 63 - (NSSplitView*)splitView { |
61 return splitView_.get(); | 64 return splitView_.get(); |
62 } | 65 } |
63 | 66 |
64 - (void)updateSidebarForTabContents:(TabContents*)contents { | 67 - (void)updateSidebarForTabContents:(TabContents*)contents { |
65 // Get the active sidebar content. | 68 // Get the active sidebar content. |
66 if (SidebarManager::GetInstance() == NULL) // Happens in tests. | 69 if (SidebarManager::GetInstance() == NULL) // Happens in tests. |
67 return; | 70 return; |
68 | 71 |
69 TabContents* sidebarContents = NULL; | 72 TabContents* sidebarContents = NULL; |
70 if (contents && SidebarManager::IsSidebarAllowed()) { | 73 if (contents && SidebarManager::IsSidebarAllowed()) { |
71 SidebarContainer* activeSidebar = | 74 SidebarContainer* activeSidebar = |
72 SidebarManager::GetInstance()->GetActiveSidebarContainerFor(contents); | 75 SidebarManager::GetInstance()->GetActiveSidebarContainerFor(contents); |
73 if (activeSidebar) | 76 if (activeSidebar) |
74 sidebarContents = activeSidebar->sidebar_contents(); | 77 sidebarContents = activeSidebar->sidebar_contents(); |
75 } | 78 } |
76 if (sidebarContents_ == sidebarContents) | 79 |
| 80 TabContents* oldSidebarContents = [contentsController_ tabContents]; |
| 81 if (oldSidebarContents == sidebarContents) |
77 return; | 82 return; |
78 | 83 |
79 TabContents* oldSidebarContents = sidebarContents_; | |
80 sidebarContents_ = sidebarContents; | |
81 | |
82 // Adjust sidebar view. | 84 // Adjust sidebar view. |
83 [self showSidebarContents:sidebarContents]; | 85 [self showSidebarContents:sidebarContents]; |
84 | 86 |
85 // Notify extensions. | 87 // Notify extensions. |
86 SidebarManager::GetInstance()->NotifyStateChanges( | 88 SidebarManager::GetInstance()->NotifyStateChanges( |
87 oldSidebarContents, sidebarContents); | 89 oldSidebarContents, sidebarContents); |
88 } | 90 } |
89 | 91 |
| 92 - (void)ensureContentsVisible { |
| 93 [contentsController_ ensureContentsVisible]; |
| 94 } |
| 95 |
90 - (void)showSidebarContents:(TabContents*)sidebarContents { | 96 - (void)showSidebarContents:(TabContents*)sidebarContents { |
| 97 [contentsController_ ensureContentsSizeDoesNotChange]; |
| 98 |
91 NSArray* subviews = [splitView_ subviews]; | 99 NSArray* subviews = [splitView_ subviews]; |
92 if (sidebarContents) { | 100 if (sidebarContents) { |
93 DCHECK_GE([subviews count], 1u); | 101 DCHECK_GE([subviews count], 1u); |
94 | 102 |
95 // |sidebarView| is a TabContentsViewCocoa object, whose ViewID was | 103 // Native view is a TabContentsViewCocoa object, whose ViewID was |
96 // set to VIEW_ID_TAB_CONTAINER initially, so change it to | 104 // set to VIEW_ID_TAB_CONTAINER initially, so change it to |
97 // VIEW_ID_SIDE_BAR_CONTAINER here. | 105 // VIEW_ID_SIDE_BAR_CONTAINER here. |
98 NSView* sidebarView = sidebarContents->GetNativeView(); | 106 view_id_util::SetID( |
99 view_id_util::SetID(sidebarView, VIEW_ID_SIDE_BAR_CONTAINER); | 107 sidebarContents->GetNativeView(), VIEW_ID_SIDE_BAR_CONTAINER); |
100 | 108 |
101 CGFloat sidebarWidth = 0; | 109 CGFloat sidebarWidth = 0; |
102 if ([subviews count] == 1) { | 110 if ([subviews count] == 1) { |
103 // Load the default split offset. | 111 // Load the default split offset. |
104 sidebarWidth = g_browser_process->local_state()->GetInteger( | 112 sidebarWidth = g_browser_process->local_state()->GetInteger( |
105 prefs::kExtensionSidebarWidth); | 113 prefs::kExtensionSidebarWidth); |
106 if (sidebarWidth < 0) { | 114 if (sidebarWidth < 0) { |
107 // Initial load, set to default value. | 115 // Initial load, set to default value. |
108 sidebarWidth = | 116 sidebarWidth = |
109 NSWidth([splitView_ frame]) * kDefaultSidebarWidthRatio; | 117 NSWidth([splitView_ frame]) * kDefaultSidebarWidthRatio; |
110 } | 118 } |
111 [splitView_ addSubview:sidebarView]; | 119 [splitView_ addSubview:[contentsController_ view]]; |
112 } else { | 120 } else { |
113 DCHECK_EQ([subviews count], 2u); | 121 DCHECK_EQ([subviews count], 2u); |
114 sidebarWidth = NSWidth([[subviews objectAtIndex:1] frame]); | 122 sidebarWidth = NSWidth([[subviews objectAtIndex:1] frame]); |
115 [splitView_ replaceSubview:[subviews objectAtIndex:1] | |
116 with:sidebarView]; | |
117 } | 123 } |
118 | 124 |
119 // Make sure |sidebarWidth| isn't too large or too small. | 125 // Make sure |sidebarWidth| isn't too large or too small. |
120 sidebarWidth = std::min(sidebarWidth, | 126 sidebarWidth = std::min(sidebarWidth, |
121 NSWidth([splitView_ frame]) - kMinWebWidth); | 127 NSWidth([splitView_ frame]) - kMinWebWidth); |
122 DCHECK_GE(sidebarWidth, 0) << "kMinWebWidth needs to be smaller than " | 128 DCHECK_GE(sidebarWidth, 0) << "kMinWebWidth needs to be smaller than " |
123 << "smallest available tab contents space."; | 129 << "smallest available tab contents space."; |
124 sidebarWidth = std::max(static_cast<CGFloat>(0), sidebarWidth); | 130 sidebarWidth = std::max(static_cast<CGFloat>(0), sidebarWidth); |
125 | 131 |
126 [self resizeSidebarToNewWidth:sidebarWidth]; | 132 [self resizeSidebarToNewWidth:sidebarWidth]; |
127 } else { | 133 } else { |
128 if ([subviews count] > 1) { | 134 if ([subviews count] > 1) { |
129 NSView* oldSidebarContentsView = [subviews objectAtIndex:1]; | 135 NSView* oldSidebarContentsView = [subviews objectAtIndex:1]; |
130 // Store split offset when hiding sidebar window only. | 136 // Store split offset when hiding sidebar window only. |
131 int sidebarWidth = NSWidth([oldSidebarContentsView frame]); | 137 int sidebarWidth = NSWidth([oldSidebarContentsView frame]); |
132 g_browser_process->local_state()->SetInteger( | 138 g_browser_process->local_state()->SetInteger( |
133 prefs::kExtensionSidebarWidth, sidebarWidth); | 139 prefs::kExtensionSidebarWidth, sidebarWidth); |
134 [oldSidebarContentsView removeFromSuperview]; | 140 [oldSidebarContentsView removeFromSuperview]; |
| 141 [splitView_ adjustSubviews]; |
135 } | 142 } |
136 } | 143 } |
| 144 |
| 145 [contentsController_ changeTabContents:sidebarContents]; |
137 } | 146 } |
138 | 147 |
139 - (void)resizeSidebarToNewWidth:(CGFloat)width { | 148 - (void)resizeSidebarToNewWidth:(CGFloat)width { |
140 NSArray* subviews = [splitView_ subviews]; | 149 NSArray* subviews = [splitView_ subviews]; |
141 | 150 |
142 // It seems as if |-setPosition:ofDividerAtIndex:| should do what's needed, | 151 // It seems as if |-setPosition:ofDividerAtIndex:| should do what's needed, |
143 // but I can't figure out how to use it. Manually resize web and sidebar. | 152 // but I can't figure out how to use it. Manually resize web and sidebar. |
144 // TODO(alekseys): either make setPosition:ofDividerAtIndex: work or to add a | 153 // TODO(alekseys): either make setPosition:ofDividerAtIndex: work or to add a |
145 // category on NSSplitView to handle manual resizing. | 154 // category on NSSplitView to handle manual resizing. |
146 NSView* sidebarView = [subviews objectAtIndex:1]; | 155 NSView* sidebarView = [subviews objectAtIndex:1]; |
(...skipping 14 matching lines...) Expand all Loading... |
161 - (BOOL)splitView:(NSSplitView *)splitView | 170 - (BOOL)splitView:(NSSplitView *)splitView |
162 shouldAdjustSizeOfSubview:(NSView *)subview { | 171 shouldAdjustSizeOfSubview:(NSView *)subview { |
163 // Return NO for the sidebar view to indicate that it should not be resized | 172 // Return NO for the sidebar view to indicate that it should not be resized |
164 // automatically. The sidebar keeps the width set by the user. | 173 // automatically. The sidebar keeps the width set by the user. |
165 if ([[splitView_ subviews] indexOfObject:subview] == 1) | 174 if ([[splitView_ subviews] indexOfObject:subview] == 1) |
166 return NO; | 175 return NO; |
167 return YES; | 176 return YES; |
168 } | 177 } |
169 | 178 |
170 @end | 179 @end |
OLD | NEW |