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

Side by Side Diff: chrome/browser/ui/cocoa/sidebar_controller.mm

Issue 1152613003: Implement sidebar support for extension action popups (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move SidebarManager to ExtensionSystem and remove notifications Created 5 years, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/sidebar_controller.h"
6
7 #include <Cocoa/Cocoa.h>
8
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/sidebar_manager.h"
12 #import "chrome/browser/ui/cocoa/view_id_util.h"
13 #include "chrome/common/pref_names.h"
14 #include "content/public/browser/web_contents.h"
15
16 namespace {
17
18 // By default sidebar width is 1/7th of the current page content width.
19 const CGFloat kDefaultSidebarWidthRatio = 1.0f / 7.0f;
20 const CGFloat kMaximumSidebarWidthRatio = 1.0f / 2.0f;
21
22 } // end namespace
23
24 @interface SidebarController (Private)
25 - (void)showSidebarContents:(content::WebContents*)sidebarContents;
26 - (void)resizeSidebarToNewWidth:(CGFloat)width;
27 @end
28
29 @interface SidebarSplitView : NSSplitView
30 @end
31
32 @implementation SidebarSplitView
33 - (NSColor*)dividerColor {
34 return [NSColor controlColor];
35 }
36 @end
37
38 @implementation SidebarController
39
40 - (id)initWithParentViewController:(id)parentController
41 andContentsController:(id)contentsController {
42 DCHECK(parentController);
43
44 if (self = [super init]) {
45 splitView_.reset([[SidebarSplitView alloc]
46 initWithFrame:[[parentController view] bounds]]);
47 [splitView_ setDelegate:self];
48 [splitView_ setVertical:YES];
49 [splitView_ setDividerStyle:NSSplitViewDividerStyleThin];
50 [splitView_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
51 [[parentController view] addSubview:splitView_];
52
53 [splitView_ addSubview:[contentsController view]];
54 [splitView_ adjustSubviews];
55 }
56 return self;
57 }
58
59 - (void)dealloc {
60 [splitView_ setDelegate:nil];
61 [super dealloc];
62 }
63
64 - (NSSplitView*)view {
65 return splitView_.get();
66 }
67
68 - (void)updateSidebarForTabContents:(content::WebContents*)contents
69 withContext:(content::BrowserContext*)context {
70 // Get the active sidebar content.
71 extensions::SidebarManager* sidebarManager =
72 extensions::SidebarManager::GetFromContext(context);
73
74 if (sidebarManager == NULL) // Happens in tests.
75 return;
76
77 content::WebContents* sidebarContents = NULL;
78 if (contents) {
79 SidebarContainer* activeSidebar =
80 sidebarManager->GetActiveSidebarContainerFor(contents);
81 if (!activeSidebar)
82 activeSidebar = sidebarManager->MigrateSidebarTo(contents);
83 if (activeSidebar)
84 sidebarContents = activeSidebar->host_contents();
85 }
86
87 if (!contentsController_.get())
88 contentsController_.reset(
89 [[TabContentsController alloc] initWithContents:contents]);
90
91 content::WebContents* oldSidebarContents =
92 static_cast<content::WebContents*>([contentsController_ webContents]);
93 if (oldSidebarContents == sidebarContents)
94 return;
95
96 // Adjust sidebar view.
97 [self showSidebarContents:sidebarContents];
98
99 // Notify extensions.
100 sidebarManager->NotifyStateChanges(oldSidebarContents, sidebarContents);
101 }
102
103 - (void)ensureContentsVisible {
104 [contentsController_ ensureContentsVisible];
105 }
106
107 - (void)showSidebarContents:(content::WebContents*)sidebarContents {
108 [contentsController_ ensureContentsSizeDoesNotChange];
109
110 NSArray* subviews = [splitView_ subviews];
111 if (sidebarContents) {
112 DCHECK_GE([subviews count], 1u);
113
114 // Native view is a TabContentsViewCocoa object, whose ViewID was
115 // set to VIEW_ID_TAB_CONTAINER initially, so change it to
116 // VIEW_ID_SIDE_BAR_CONTAINER here.
117 view_id_util::SetID(sidebarContents->GetNativeView(),
118 VIEW_ID_SIDE_BAR_CONTAINER);
119
120 CGFloat sidebarWidth = 0;
121 if ([subviews count] == 1) {
122 // Load the default split offset.
123 sidebarWidth = g_browser_process->local_state()->GetInteger(
124 prefs::kExtensionSidebarWidth);
125 if (sidebarWidth < 0) {
126 // Initial load, set to default value.
127 sidebarWidth = NSWidth([splitView_ frame]) * kDefaultSidebarWidthRatio;
128 }
129
130 [splitView_ addSubview:[contentsController_ view]];
131 } else {
132 DCHECK_EQ([subviews count], 2u);
133 sidebarWidth = NSWidth([[subviews objectAtIndex:1] frame]);
134 }
135
136 sidebarWidth = std::max(static_cast<CGFloat>(0), sidebarWidth);
137
138 [self resizeSidebarToNewWidth:sidebarWidth];
139 } else {
140 if ([subviews count] > 1) {
141 NSView* oldSidebarContentsView = [subviews objectAtIndex:1];
142 // Store split offset when hiding sidebar window only.
143 int sidebarWidth = NSWidth([oldSidebarContentsView frame]);
144 g_browser_process->local_state()->SetInteger(
145 prefs::kExtensionSidebarWidth, sidebarWidth);
146 [oldSidebarContentsView removeFromSuperview];
147 [splitView_ adjustSubviews];
148 }
149 }
150
151 [contentsController_ changeWebContents:sidebarContents];
152 }
153
154 - (void)resizeSidebarToNewWidth:(CGFloat)width {
155 NSArray* subviews = [splitView_ subviews];
156
157 NSView* sidebarView = [subviews objectAtIndex:1];
158 NSRect sidebarFrame = [sidebarView frame];
159 sidebarFrame.size.width = width;
160 [sidebarView setFrame:sidebarFrame];
161
162 NSView* webView = [subviews objectAtIndex:0];
163 NSRect webFrame = [webView frame];
164 webFrame.size.width =
165 NSWidth([splitView_ frame]) - ([splitView_ dividerThickness] + width);
166 [webView setFrame:webFrame];
167
168 [splitView_ adjustSubviews];
169 }
170
171 /* NSSplitViewDelegate Support
172 *
173 * Sidebar behavior:
174 * - initial sidebar is kDefaultSidebarWidthRatio * width
175 * of the split-view's frame
176 * - sidebar width is not allowed to be greater than 50% of width of the
177 * the split-view's frame
178 *
179 */
180
181 - (BOOL)splitView:(NSSplitView*)splitView
182 shouldHideDividerAtIndex:(NSInteger)dividerIndex {
183 return NO;
184 }
185
186 - (BOOL)splitView:(NSSplitView*)splitView canCollapseSubview:(NSView*)subview {
187 return NO;
188 }
189
190 - (BOOL)splitView:(NSSplitView*)splitView
191 shouldCollapseSubview:(NSView*)subview
192 forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex {
193 return NO;
194 }
195
196 - (CGFloat)splitView:(NSSplitView*)splitView
197 constrainMinCoordinate:(CGFloat)proposedMinimumPosition
198 ofSubviewAt:(NSInteger)dividerIndex {
199 return std::max(proposedMinimumPosition,
200 kMaximumSidebarWidthRatio * NSWidth([splitView_ frame]));
201 }
202
203 - (CGFloat)splitView:(NSSplitView*)splitView
204 constrainMaxCoordinate:(CGFloat)proposedMaximumPosition
205 ofSubviewAt:(NSInteger)dividerIndex {
206 return std::min(proposedMaximumPosition,
207 NSWidth([splitView_ frame]) - [splitView_ dividerThickness]);
208 }
209
210 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698