| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/ui/panels/panel_manager.h" | 5 #include "chrome/browser/ui/panels/panel_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/window_sizer.h" | 12 #include "chrome/browser/ui/window_sizer.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 // Invalid panel index. | 15 // Invalid panel index. |
| 16 const size_t kInvalidPanelIndex = static_cast<size_t>(-1); | 16 const size_t kInvalidPanelIndex = static_cast<size_t>(-1); |
| 17 | 17 |
| 18 // Minimum width and height of a panel. | 18 // Minimum width and height of a panel. |
| 19 const int kPanelMinWidthPixels = 64; | 19 // Note: The minimum size of a widget (see widget.cc) is fixed to 100x100. |
| 20 const int kPanelMinHeightPixels = 24; | 20 // TODO(jianli): Need to fix this to support smaller panel. |
| 21 const int kPanelMinWidthPixels = 100; |
| 22 const int kPanelMinHeightPixels = 100; |
| 21 | 23 |
| 22 // Default width and height of a panel. | 24 // Default width and height of a panel. |
| 23 const int kPanelDefaultWidthPixels = 240; | 25 const int kPanelDefaultWidthPixels = 240; |
| 24 const int kPanelDefaultHeightPixels = 290; | 26 const int kPanelDefaultHeightPixels = 290; |
| 25 | 27 |
| 26 // Maxmium width and height of a panel based on the factor of the working | 28 // Maxmium width and height of a panel based on the factor of the working |
| 27 // area. | 29 // area. |
| 28 const double kPanelMaxWidthFactor = 1.0; | 30 const double kPanelMaxWidthFactor = 1.0; |
| 29 const double kPanelMaxHeightFactor = 0.5; | 31 const double kPanelMaxHeightFactor = 0.5; |
| 30 | 32 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 const Extension* extension = NULL; | 105 const Extension* extension = NULL; |
| 104 gfx::Rect bounds = browser->override_bounds(); | 106 gfx::Rect bounds = browser->override_bounds(); |
| 105 while (!ComputeBoundsForNextPanel(&bounds, true)) { | 107 while (!ComputeBoundsForNextPanel(&bounds, true)) { |
| 106 if (!extension) | 108 if (!extension) |
| 107 extension = Panel::GetExtension(browser); | 109 extension = Panel::GetExtension(browser); |
| 108 FindAndClosePanelOnOverflow(extension); | 110 FindAndClosePanelOnOverflow(extension); |
| 109 } | 111 } |
| 110 | 112 |
| 111 Panel* panel = new Panel(browser, bounds); | 113 Panel* panel = new Panel(browser, bounds); |
| 112 panels_.push_back(panel); | 114 panels_.push_back(panel); |
| 115 UpdateMaxSizeForAllPanels(); |
| 113 | 116 |
| 114 return panel; | 117 return panel; |
| 115 } | 118 } |
| 116 | 119 |
| 117 void PanelManager::Remove(Panel* panel) { | 120 void PanelManager::Remove(Panel* panel) { |
| 118 // If we're in the process of dragging, delay the removal. | 121 // If we're in the process of dragging, delay the removal. |
| 119 if (dragging_panel_index_ != kInvalidPanelIndex) { | 122 if (dragging_panel_index_ != kInvalidPanelIndex) { |
| 120 panels_pending_to_remove_.push_back(panel); | 123 panels_pending_to_remove_.push_back(panel); |
| 121 return; | 124 return; |
| 122 } | 125 } |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 } else { | 267 } else { |
| 265 panels_[dragging_panel_index_]->SetPanelBounds( | 268 panels_[dragging_panel_index_]->SetPanelBounds( |
| 266 dragging_panel_bounds_); | 269 dragging_panel_bounds_); |
| 267 } | 270 } |
| 268 | 271 |
| 269 dragging_panel_index_ = kInvalidPanelIndex; | 272 dragging_panel_index_ = kInvalidPanelIndex; |
| 270 | 273 |
| 271 DelayedRemove(); | 274 DelayedRemove(); |
| 272 } | 275 } |
| 273 | 276 |
| 277 void PanelManager::OnPreferredWindowSizeChanged( |
| 278 Panel* panel, const gfx::Size& preferred_window_size) { |
| 279 gfx::Rect bounds = panel->GetBounds(); |
| 280 int restored_height = panel->GetRestoredHeight(); |
| 281 |
| 282 // The panel width: |
| 283 // * cannot grow or shrink to go beyond [min_width, max_width] |
| 284 // * cannot grow to take more than the available space and go beyond the left |
| 285 // of the work area. |
| 286 int new_width = preferred_window_size.width(); |
| 287 int max_width = panel->max_size().width(); |
| 288 if (new_width > max_width) |
| 289 new_width = max_width; |
| 290 if (new_width - bounds.width() > current_x_) |
| 291 new_width = bounds.width() + current_x_; |
| 292 |
| 293 if (new_width > bounds.width() || |
| 294 (new_width < bounds.width() && |
| 295 new_width > panel->min_size().width())) { |
| 296 int delta = bounds.width() - new_width; |
| 297 bounds.set_x(bounds.x() + delta); |
| 298 bounds.set_width(new_width); |
| 299 |
| 300 // Reposition all the panels on the left. |
| 301 int panel_index = -1; |
| 302 for (int i = 0; i < static_cast<int>(panels_.size()); ++i) { |
| 303 if (panels_[i] == panel) { |
| 304 panel_index = i; |
| 305 break; |
| 306 } |
| 307 } |
| 308 DCHECK(panel_index >= 0); |
| 309 for (int i = static_cast<int>(panels_.size()) -1; i > panel_index; |
| 310 --i) { |
| 311 gfx::Rect this_bounds = panels_[i]->GetBounds(); |
| 312 this_bounds.set_x(this_bounds.x() + delta); |
| 313 panels_[i]->SetPanelBounds(this_bounds); |
| 314 } |
| 315 } |
| 316 |
| 317 // The panel height: |
| 318 // * cannot grow or shrink to go beyond [min_height, max_height] |
| 319 int new_height = preferred_window_size.height(); |
| 320 if (new_height > max_height_) |
| 321 new_height = max_height_; |
| 322 |
| 323 if (new_height > restored_height || |
| 324 (new_height < restored_height && |
| 325 new_height > panel->min_size().height())) { |
| 326 // If the panel is not expanded, we only need to save the new restored |
| 327 // height. |
| 328 if (panel->expansion_state() == Panel::EXPANDED) { |
| 329 bounds.set_y(bounds.y() - new_height + bounds.height()); |
| 330 bounds.set_height(new_height); |
| 331 } else { |
| 332 panel->SetRestoredHeight(new_height); |
| 333 } |
| 334 } |
| 335 |
| 336 panel->SetPanelBounds(bounds); |
| 337 current_x_ = |
| 338 panels_.back()->GetBounds().x() - kPanelsHorizontalSpacing; |
| 339 |
| 340 UpdateMaxSizeForAllPanels(); |
| 341 } |
| 342 |
| 274 bool PanelManager::ShouldBringUpTitleBarForAllMinimizedPanels( | 343 bool PanelManager::ShouldBringUpTitleBarForAllMinimizedPanels( |
| 275 int mouse_x, int mouse_y) const { | 344 int mouse_x, int mouse_y) const { |
| 276 for (Panels::const_iterator iter = panels_.begin(); | 345 for (Panels::const_iterator iter = panels_.begin(); |
| 277 iter != panels_.end(); ++iter) { | 346 iter != panels_.end(); ++iter) { |
| 278 if ((*iter)->ShouldBringUpTitleBar(mouse_x, mouse_y)) | 347 if ((*iter)->ShouldBringUpTitleBar(mouse_x, mouse_y)) |
| 279 return true; | 348 return true; |
| 280 } | 349 } |
| 281 return false; | 350 return false; |
| 282 } | 351 } |
| 283 | 352 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 303 void PanelManager::Rearrange(Panels::iterator iter_to_start) { | 372 void PanelManager::Rearrange(Panels::iterator iter_to_start) { |
| 304 if (iter_to_start == panels_.end()) | 373 if (iter_to_start == panels_.end()) |
| 305 return; | 374 return; |
| 306 | 375 |
| 307 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { | 376 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { |
| 308 gfx::Rect new_bounds((*iter)->GetBounds()); | 377 gfx::Rect new_bounds((*iter)->GetBounds()); |
| 309 ComputeBoundsForNextPanel(&new_bounds, false); | 378 ComputeBoundsForNextPanel(&new_bounds, false); |
| 310 if (new_bounds != (*iter)->GetBounds()) | 379 if (new_bounds != (*iter)->GetBounds()) |
| 311 (*iter)->SetPanelBounds(new_bounds); | 380 (*iter)->SetPanelBounds(new_bounds); |
| 312 } | 381 } |
| 382 |
| 383 UpdateMaxSizeForAllPanels(); |
| 313 } | 384 } |
| 314 | 385 |
| 315 bool PanelManager::ComputeBoundsForNextPanel(gfx::Rect* bounds, | 386 bool PanelManager::ComputeBoundsForNextPanel(gfx::Rect* bounds, |
| 316 bool allow_size_change) { | 387 bool allow_size_change) { |
| 317 int width = bounds->width(); | 388 int width = bounds->width(); |
| 318 int height = bounds->height(); | 389 int height = bounds->height(); |
| 319 | 390 |
| 320 // Update the width and/or height to fit into our constraint. | 391 // Update the width and/or height to fit into our constraint. |
| 321 if (allow_size_change) { | 392 if (allow_size_change) { |
| 322 if (width == 0 && height == 0) { | 393 if (width == 0 && height == 0) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 349 | 420 |
| 350 void PanelManager::RemoveAll() { | 421 void PanelManager::RemoveAll() { |
| 351 // This should not be called when we're in the process of dragging. | 422 // This should not be called when we're in the process of dragging. |
| 352 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); | 423 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); |
| 353 | 424 |
| 354 // Start from the bottom to avoid reshuffling. | 425 // Start from the bottom to avoid reshuffling. |
| 355 for (Panels::reverse_iterator iter = panels_.rbegin(); | 426 for (Panels::reverse_iterator iter = panels_.rbegin(); |
| 356 iter != panels_.rend(); ++iter) | 427 iter != panels_.rend(); ++iter) |
| 357 (*iter)->Close(); | 428 (*iter)->Close(); |
| 358 } | 429 } |
| 430 |
| 431 void PanelManager::UpdateMaxSizeForAllPanels() { |
| 432 for (Panels::const_iterator iter = panels_.begin(); |
| 433 iter != panels_.end(); ++iter) { |
| 434 Panel* panel = *iter; |
| 435 // We arrange the panels from right to left on the screen. |current_x_| is |
| 436 // the left-most point that panels occupy to. So we add |current_x_| for all |
| 437 // panels to mean each of them can try to take over this available space. |
| 438 int width_can_grow_to = panel->GetBounds().width() + current_x_; |
| 439 panel->SetMaxSize( |
| 440 gfx::Size(std::min(width_can_grow_to, max_width_), max_height_)); |
| 441 } |
| 442 } |
| OLD | NEW |