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

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

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

Powered by Google App Engine
This is Rietveld 408576698