OLD | NEW |
(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/sidebar/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 // Get the active sidebar content. |
| 70 if (SidebarManager::GetInstance() == NULL) // Happens in tests. |
| 71 return; |
| 72 |
| 73 content::WebContents* sidebarContents = NULL; |
| 74 if (contents) { |
| 75 SidebarContainer* activeSidebar = |
| 76 SidebarManager::GetInstance()->GetActiveSidebarContainerFor(contents); |
| 77 if (!activeSidebar) |
| 78 activeSidebar = SidebarManager::GetInstance()->MigrateSidebarTo(contents); |
| 79 if (activeSidebar) |
| 80 sidebarContents = activeSidebar->host_contents(); |
| 81 } |
| 82 |
| 83 if (!contentsController_.get()) |
| 84 contentsController_.reset( |
| 85 [[TabContentsController alloc] initWithContents:contents]); |
| 86 |
| 87 content::WebContents* oldSidebarContents = |
| 88 static_cast<content::WebContents*>([contentsController_ webContents]); |
| 89 if (oldSidebarContents == sidebarContents) |
| 90 return; |
| 91 |
| 92 // Adjust sidebar view. |
| 93 [self showSidebarContents:sidebarContents]; |
| 94 |
| 95 // Notify extensions. |
| 96 SidebarManager::GetInstance()->NotifyStateChanges(oldSidebarContents, |
| 97 sidebarContents); |
| 98 } |
| 99 |
| 100 - (void)ensureContentsVisible { |
| 101 [contentsController_ ensureContentsVisible]; |
| 102 } |
| 103 |
| 104 - (void)showSidebarContents:(content::WebContents*)sidebarContents { |
| 105 [contentsController_ ensureContentsSizeDoesNotChange]; |
| 106 |
| 107 NSArray* subviews = [splitView_ subviews]; |
| 108 if (sidebarContents) { |
| 109 DCHECK_GE([subviews count], 1u); |
| 110 |
| 111 // Native view is a TabContentsViewCocoa object, whose ViewID was |
| 112 // set to VIEW_ID_TAB_CONTAINER initially, so change it to |
| 113 // VIEW_ID_SIDE_BAR_CONTAINER here. |
| 114 view_id_util::SetID(sidebarContents->GetNativeView(), |
| 115 VIEW_ID_SIDE_BAR_CONTAINER); |
| 116 |
| 117 CGFloat sidebarWidth = 0; |
| 118 if ([subviews count] == 1) { |
| 119 // Load the default split offset. |
| 120 sidebarWidth = g_browser_process->local_state()->GetInteger( |
| 121 prefs::kExtensionSidebarWidth); |
| 122 if (sidebarWidth < 0) { |
| 123 // Initial load, set to default value. |
| 124 sidebarWidth = NSWidth([splitView_ frame]) * kDefaultSidebarWidthRatio; |
| 125 } |
| 126 |
| 127 [splitView_ addSubview:[contentsController_ view]]; |
| 128 } else { |
| 129 DCHECK_EQ([subviews count], 2u); |
| 130 sidebarWidth = NSWidth([[subviews objectAtIndex:1] frame]); |
| 131 } |
| 132 |
| 133 sidebarWidth = std::max(static_cast<CGFloat>(0), sidebarWidth); |
| 134 |
| 135 [self resizeSidebarToNewWidth:sidebarWidth]; |
| 136 } else { |
| 137 if ([subviews count] > 1) { |
| 138 NSView* oldSidebarContentsView = [subviews objectAtIndex:1]; |
| 139 // Store split offset when hiding sidebar window only. |
| 140 int sidebarWidth = NSWidth([oldSidebarContentsView frame]); |
| 141 g_browser_process->local_state()->SetInteger( |
| 142 prefs::kExtensionSidebarWidth, sidebarWidth); |
| 143 [oldSidebarContentsView removeFromSuperview]; |
| 144 [splitView_ adjustSubviews]; |
| 145 } |
| 146 } |
| 147 |
| 148 [contentsController_ changeWebContents:sidebarContents]; |
| 149 } |
| 150 |
| 151 - (void)resizeSidebarToNewWidth:(CGFloat)width { |
| 152 NSArray* subviews = [splitView_ subviews]; |
| 153 |
| 154 NSView* sidebarView = [subviews objectAtIndex:1]; |
| 155 NSRect sidebarFrame = [sidebarView frame]; |
| 156 sidebarFrame.size.width = width; |
| 157 [sidebarView setFrame:sidebarFrame]; |
| 158 |
| 159 NSView* webView = [subviews objectAtIndex:0]; |
| 160 NSRect webFrame = [webView frame]; |
| 161 webFrame.size.width = |
| 162 NSWidth([splitView_ frame]) - ([splitView_ dividerThickness] + width); |
| 163 [webView setFrame:webFrame]; |
| 164 |
| 165 [splitView_ adjustSubviews]; |
| 166 } |
| 167 |
| 168 /* NSSplitViewDelegate Support |
| 169 * |
| 170 * Sidebar behavior: |
| 171 * - initial sidebar is kDefaultSidebarWidthRatio * width |
| 172 * of the split-view's frame |
| 173 * - sidebar width is not allowed to be greater than 50% of width of the |
| 174 * the split-view's frame |
| 175 * |
| 176 */ |
| 177 |
| 178 - (BOOL)splitView:(NSSplitView*)splitView |
| 179 shouldHideDividerAtIndex:(NSInteger)dividerIndex { |
| 180 return NO; |
| 181 } |
| 182 |
| 183 - (BOOL)splitView:(NSSplitView*)splitView canCollapseSubview:(NSView*)subview { |
| 184 return NO; |
| 185 } |
| 186 |
| 187 - (BOOL)splitView:(NSSplitView*)splitView |
| 188 shouldCollapseSubview:(NSView*)subview |
| 189 forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex { |
| 190 return NO; |
| 191 } |
| 192 |
| 193 - (CGFloat)splitView:(NSSplitView*)splitView |
| 194 constrainMinCoordinate:(CGFloat)proposedMinimumPosition |
| 195 ofSubviewAt:(NSInteger)dividerIndex { |
| 196 return std::max(proposedMinimumPosition, |
| 197 kMaximumSidebarWidthRatio * NSWidth([splitView_ frame])); |
| 198 } |
| 199 |
| 200 - (CGFloat)splitView:(NSSplitView*)splitView |
| 201 constrainMaxCoordinate:(CGFloat)proposedMaximumPosition |
| 202 ofSubviewAt:(NSInteger)dividerIndex { |
| 203 return std::min(proposedMaximumPosition, |
| 204 NSWidth([splitView_ frame]) - [splitView_ dividerThickness]); |
| 205 } |
| 206 |
| 207 @end |
OLD | NEW |