Chromium Code Reviews| 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 "base/message_loop.h" | |
| 11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/window_sizer.h" | 13 #include "chrome/browser/ui/window_sizer.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 // Invalid panel index. | 16 // Invalid panel index. |
| 16 const size_t kInvalidPanelIndex = static_cast<size_t>(-1); | 17 const size_t kInvalidPanelIndex = static_cast<size_t>(-1); |
| 17 | 18 |
| 18 // Minimum width and height of a panel. | 19 // Minimum width and height of a panel. |
| 19 const int kPanelMinWidthPixels = 64; | 20 const int kPanelMinWidthPixels = 64; |
| 20 const int kPanelMinHeightPixels = 24; | 21 const int kPanelMinHeightPixels = 24; |
| 21 | 22 |
| 22 // Default width and height of a panel. | 23 // Default width and height of a panel. |
| 23 const int kPanelDefaultWidthPixels = 240; | 24 const int kPanelDefaultWidthPixels = 240; |
| 24 const int kPanelDefaultHeightPixels = 290; | 25 const int kPanelDefaultHeightPixels = 290; |
| 25 | 26 |
| 26 // Maxmium width and height of a panel based on the factor of the working | 27 // Maxmium width and height of a panel based on the factor of the working |
| 27 // area. | 28 // area. |
| 28 const double kPanelMaxWidthFactor = 1.0; | 29 const double kPanelMaxWidthFactor = 1.0; |
| 29 const double kPanelMaxHeightFactor = 0.5; | 30 const double kPanelMaxHeightFactor = 0.5; |
| 30 | 31 |
| 31 // Horizontal spacing between two panels. | 32 // Horizontal spacing between two panels. |
| 32 const int kPanelsHorizontalSpacing = 4; | 33 const int kPanelsHorizontalSpacing = 4; |
| 33 | 34 |
| 35 // The delayed interval to do the check to ensure we bring up or down titlebars. | |
|
Dmitry Titov
2011/08/22 22:35:53
It seems some explanation has to be here. I think
jianli
2011/08/22 23:28:10
The reason for adding this additional logic is tha
| |
| 36 const int kDelayedBringUpOrDownTitlebarsCheckMs = 1000; | |
| 37 | |
| 34 // Single instance of PanelManager. | 38 // Single instance of PanelManager. |
| 35 scoped_ptr<PanelManager> panel_instance; | 39 scoped_refptr<PanelManager> panel_instance; |
| 36 } // namespace | 40 } // namespace |
| 37 | 41 |
| 38 // static | 42 // static |
| 39 PanelManager* PanelManager::GetInstance() { | 43 PanelManager* PanelManager::GetInstance() { |
| 40 if (!panel_instance.get()) { | 44 if (!panel_instance.get()) { |
| 41 panel_instance.reset(new PanelManager()); | 45 panel_instance = new PanelManager(); |
| 46 panel_instance->Init(); | |
| 42 } | 47 } |
| 43 return panel_instance.get(); | 48 return panel_instance.get(); |
| 44 } | 49 } |
| 45 | 50 |
| 51 // static | |
| 52 void PanelManager::CreateForTesting(const gfx::Rect& work_area, | |
| 53 AutoHideBottomBar* auto_hide_bottom_bar) { | |
| 54 DCHECK(!panel_instance.get()); | |
| 55 panel_instance = new PanelManager(); | |
| 56 panel_instance->auto_hide_bottom_bar_ = auto_hide_bottom_bar; | |
| 57 panel_instance->SetWorkArea(work_area); | |
| 58 } | |
| 59 | |
| 46 PanelManager::PanelManager() | 60 PanelManager::PanelManager() |
| 47 : max_width_(0), | 61 : max_width_(0), |
| 48 max_height_(0), | 62 max_height_(0), |
| 49 min_x_(0), | |
| 50 current_x_(0), | 63 current_x_(0), |
| 51 bottom_edge_y_(0), | |
| 52 dragging_panel_index_(kInvalidPanelIndex), | 64 dragging_panel_index_(kInvalidPanelIndex), |
| 53 dragging_panel_original_x_(0) { | 65 dragging_panel_original_x_(0), |
| 54 OnDisplayChanged(); | 66 delayed_titlebar_action_(NO_ACTION), |
| 67 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
| 55 } | 68 } |
| 56 | 69 |
| 57 PanelManager::~PanelManager() { | 70 PanelManager::~PanelManager() { |
| 58 DCHECK(panels_.empty()); | 71 DCHECK(panels_.empty()); |
| 59 DCHECK(panels_pending_to_remove_.empty()); | 72 DCHECK(panels_pending_to_remove_.empty()); |
| 60 } | 73 } |
| 61 | 74 |
| 75 void PanelManager::Init() { | |
| 76 auto_hide_bottom_bar_ = AutoHideBottomBar::Create(this); | |
| 77 OnDisplayChanged(); | |
| 78 } | |
| 79 | |
| 62 void PanelManager::OnDisplayChanged() { | 80 void PanelManager::OnDisplayChanged() { |
| 63 scoped_ptr<WindowSizer::MonitorInfoProvider> info_provider( | 81 scoped_ptr<WindowSizer::MonitorInfoProvider> info_provider( |
| 64 WindowSizer::CreateDefaultMonitorInfoProvider()); | 82 WindowSizer::CreateDefaultMonitorInfoProvider()); |
| 65 SetWorkArea(info_provider->GetPrimaryMonitorWorkArea()); | 83 SetWorkArea(info_provider->GetPrimaryMonitorWorkArea()); |
| 66 } | 84 } |
| 67 | 85 |
| 68 void PanelManager::SetWorkArea(const gfx::Rect& work_area) { | 86 void PanelManager::SetWorkArea(const gfx::Rect& work_area) { |
| 69 if (work_area == work_area_) | 87 if (work_area == work_area_) |
| 70 return; | 88 return; |
| 71 work_area_ = work_area; | 89 work_area_ = work_area; |
| 72 | 90 |
| 73 min_x_ = work_area.x(); | 91 auto_hide_bottom_bar_->UpdateWorkArea(work_area); |
| 92 | |
| 74 current_x_ = work_area.right(); | 93 current_x_ = work_area.right(); |
| 75 bottom_edge_y_ = work_area.bottom(); | |
| 76 max_width_ = static_cast<int>(work_area.width() * kPanelMaxWidthFactor); | 94 max_width_ = static_cast<int>(work_area.width() * kPanelMaxWidthFactor); |
| 77 max_height_ = static_cast<int>(work_area.height() * kPanelMaxHeightFactor); | 95 max_height_ = static_cast<int>(work_area.height() * kPanelMaxHeightFactor); |
| 78 | 96 |
| 79 Rearrange(panels_.begin()); | 97 Rearrange(panels_.begin()); |
| 80 } | 98 } |
| 81 | 99 |
| 82 void PanelManager::FindAndClosePanelOnOverflow(const Extension* extension) { | 100 void PanelManager::FindAndClosePanelOnOverflow(const Extension* extension) { |
| 83 Panel* panel_to_close = NULL; | 101 Panel* panel_to_close = NULL; |
| 84 | 102 |
| 85 // Try to find the left-most panel invoked from the same extension and close | 103 // Try to find the left-most panel invoked from the same extension and close |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 264 } else { | 282 } else { |
| 265 panels_[dragging_panel_index_]->SetPanelBounds( | 283 panels_[dragging_panel_index_]->SetPanelBounds( |
| 266 dragging_panel_bounds_); | 284 dragging_panel_bounds_); |
| 267 } | 285 } |
| 268 | 286 |
| 269 dragging_panel_index_ = kInvalidPanelIndex; | 287 dragging_panel_index_ = kInvalidPanelIndex; |
| 270 | 288 |
| 271 DelayedRemove(); | 289 DelayedRemove(); |
| 272 } | 290 } |
| 273 | 291 |
| 274 bool PanelManager::ShouldBringUpTitlebarForAllMinimizedPanels( | 292 bool PanelManager::ShouldBringUpTitlebars(int mouse_x, int mouse_y) const { |
| 275 int mouse_x, int mouse_y) const { | 293 // We should always bring up the title-bar if the mouse is over the auto-hide |
| 294 // task-bar that becomes visible. | |
| 295 if (auto_hide_bottom_bar_->Exists() && | |
| 296 auto_hide_bottom_bar_->GetVisibility() == AutoHideBottomBar::VISIBLE && | |
| 297 mouse_y >= work_area_.bottom() - auto_hide_bottom_bar_->GetHeight()) | |
| 298 return true; | |
| 299 | |
| 276 for (Panels::const_iterator iter = panels_.begin(); | 300 for (Panels::const_iterator iter = panels_.begin(); |
| 277 iter != panels_.end(); ++iter) { | 301 iter != panels_.end(); ++iter) { |
| 278 if ((*iter)->ShouldBringUpTitlebar(mouse_x, mouse_y)) | 302 if ((*iter)->ShouldBringUpTitlebar(mouse_x, mouse_y)) |
| 279 return true; | 303 return true; |
| 280 } | 304 } |
| 281 return false; | 305 return false; |
| 282 } | 306 } |
| 283 | 307 |
| 284 void PanelManager::BringUpOrDownTitlebarForAllMinimizedPanels(bool bring_up) { | 308 void PanelManager::BringUpOrDownTitlebars(bool bring_up) { |
| 309 // If the auto-hide bottom bar exists, delay the action until the bottom | |
| 310 // bar is fully visible or hidden. We do not want both bottom bar and panel | |
| 311 // titlebar to move at the same time but with different speeds. | |
| 312 if (auto_hide_bottom_bar_->Exists()) { | |
| 313 AutoHideBottomBar::Visibility visibility = | |
| 314 auto_hide_bottom_bar_->GetVisibility(); | |
| 315 if (visibility != (bring_up ? AutoHideBottomBar::VISIBLE | |
| 316 : AutoHideBottomBar::HIDDEN)) { | |
| 317 // OnAutoHideBottomBarVisibilityChanged will handle this. | |
| 318 delayed_titlebar_action_ = bring_up ? BRING_UP : BRING_DOWN; | |
| 319 | |
| 320 // Sometimes, the bottom bar does not appear or hide promptly. Schedule | |
| 321 // a delayed task to make sure our delayed action is performed. | |
| 322 MessageLoop::current()->PostDelayedTask( | |
| 323 FROM_HERE, | |
| 324 method_factory_.NewRunnableMethod( | |
| 325 &PanelManager::DelayedBringUpOrDownTitlebarsCheck), | |
| 326 kDelayedBringUpOrDownTitlebarsCheckMs); | |
| 327 | |
| 328 return; | |
| 329 } | |
| 330 } | |
| 331 | |
| 332 DoBringUpOrDownTitlebars(bring_up); | |
| 333 } | |
| 334 | |
| 335 void PanelManager::DelayedBringUpOrDownTitlebarsCheck() { | |
| 336 switch (delayed_titlebar_action_) { | |
| 337 case NO_ACTION: | |
| 338 break; | |
| 339 case BRING_UP: | |
| 340 delayed_titlebar_action_ = NO_ACTION; | |
| 341 DoBringUpOrDownTitlebars(true); | |
| 342 break; | |
| 343 case BRING_DOWN: | |
| 344 delayed_titlebar_action_ = NO_ACTION; | |
| 345 DoBringUpOrDownTitlebars(false); | |
| 346 break; | |
| 347 default: | |
| 348 NOTREACHED(); | |
| 349 break; | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 void PanelManager::DoBringUpOrDownTitlebars(bool bring_up) { | |
| 285 for (Panels::const_iterator iter = panels_.begin(); | 354 for (Panels::const_iterator iter = panels_.begin(); |
| 286 iter != panels_.end(); ++iter) { | 355 iter != panels_.end(); ++iter) { |
| 287 Panel* panel = *iter; | 356 Panel* panel = *iter; |
| 288 | 357 |
| 289 // Skip any panel that is drawing the attention. | 358 // Skip any panel that is drawing the attention. |
| 290 if (panel->IsDrawingAttention()) | 359 if (panel->IsDrawingAttention()) |
| 291 continue; | 360 continue; |
| 292 | 361 |
| 293 if (bring_up) { | 362 if (bring_up) { |
| 294 if (panel->expansion_state() == Panel::MINIMIZED) | 363 if (panel->expansion_state() == Panel::MINIMIZED) |
| 295 panel->SetExpansionState(Panel::TITLE_ONLY); | 364 panel->SetExpansionState(Panel::TITLE_ONLY); |
| 296 } else { | 365 } else { |
| 297 if (panel->expansion_state() == Panel::TITLE_ONLY) | 366 if (panel->expansion_state() == Panel::TITLE_ONLY) |
| 298 panel->SetExpansionState(Panel::MINIMIZED); | 367 panel->SetExpansionState(Panel::MINIMIZED); |
| 299 } | 368 } |
| 300 } | 369 } |
| 301 } | 370 } |
| 302 | 371 |
| 372 int PanelManager::GetBottomPositionForExpansionState( | |
| 373 Panel::ExpansionState expansion_state) const { | |
| 374 int bottom = work_area_.bottom(); | |
| 375 if (expansion_state != Panel::MINIMIZED) | |
| 376 bottom -= auto_hide_bottom_bar_->GetHeight(); | |
| 377 return bottom; | |
| 378 } | |
| 379 | |
| 380 void PanelManager::OnAutoHideBottomBarVisibilityChanged( | |
| 381 AutoHideBottomBar::Visibility visibility) { | |
| 382 switch (delayed_titlebar_action_) { | |
| 383 case NO_ACTION: | |
| 384 break; | |
| 385 case BRING_UP: | |
| 386 if (visibility == AutoHideBottomBar::VISIBLE) { | |
| 387 delayed_titlebar_action_ = NO_ACTION; | |
| 388 DoBringUpOrDownTitlebars(true); | |
| 389 } | |
| 390 break; | |
| 391 case BRING_DOWN: | |
| 392 if (visibility == AutoHideBottomBar::HIDDEN) { | |
| 393 delayed_titlebar_action_ = NO_ACTION; | |
| 394 DoBringUpOrDownTitlebars(false); | |
| 395 } | |
| 396 break; | |
| 397 default: | |
| 398 NOTREACHED(); | |
| 399 break; | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 void PanelManager::OnAutoHideBottomBarHeightChanged(int height) { | |
| 404 current_x_ = work_area_.right(); | |
| 405 Rearrange(panels_.begin()); | |
| 406 } | |
| 407 | |
| 303 void PanelManager::Rearrange(Panels::iterator iter_to_start) { | 408 void PanelManager::Rearrange(Panels::iterator iter_to_start) { |
| 304 if (iter_to_start == panels_.end()) | 409 if (iter_to_start == panels_.end()) |
| 305 return; | 410 return; |
| 306 | 411 |
| 307 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { | 412 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { |
| 308 gfx::Rect new_bounds((*iter)->GetBounds()); | 413 gfx::Rect new_bounds((*iter)->GetBounds()); |
| 309 ComputeBoundsForNextPanel(&new_bounds, false); | 414 ComputeBoundsForNextPanel(&new_bounds, false); |
| 310 if (new_bounds != (*iter)->GetBounds()) | 415 if (new_bounds != (*iter)->GetBounds()) |
| 311 (*iter)->SetPanelBounds(new_bounds); | 416 (*iter)->SetPanelBounds(new_bounds); |
| 312 } | 417 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 329 else if (width > max_width_) | 434 else if (width > max_width_) |
| 330 width = max_width_; | 435 width = max_width_; |
| 331 | 436 |
| 332 if (height < kPanelMinHeightPixels) | 437 if (height < kPanelMinHeightPixels) |
| 333 height = kPanelMinHeightPixels; | 438 height = kPanelMinHeightPixels; |
| 334 else if (height > max_height_) | 439 else if (height > max_height_) |
| 335 height = max_height_; | 440 height = max_height_; |
| 336 } | 441 } |
| 337 | 442 |
| 338 int x = current_x_ - width; | 443 int x = current_x_ - width; |
| 339 int y = bottom_edge_y_ - height; | 444 int y = work_area_.bottom() - auto_hide_bottom_bar_->GetHeight() - height; |
| 340 | 445 |
| 341 if (x < min_x_) | 446 if (x < work_area_.x()) |
| 342 return false; | 447 return false; |
| 343 | 448 |
| 344 current_x_ -= width + kPanelsHorizontalSpacing; | 449 current_x_ -= width + kPanelsHorizontalSpacing; |
| 345 | 450 |
| 346 bounds->SetRect(x, y, width, height); | 451 bounds->SetRect(x, y, width, height); |
| 347 return true; | 452 return true; |
| 348 } | 453 } |
| 349 | 454 |
| 350 void PanelManager::RemoveAll() { | 455 void PanelManager::RemoveAll() { |
| 351 // This should not be called when we're in the process of dragging. | 456 // This should not be called when we're in the process of dragging. |
| 352 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); | 457 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); |
| 353 | 458 |
| 354 // Start from the bottom to avoid reshuffling. | 459 // Start from the bottom to avoid reshuffling. |
| 355 for (Panels::reverse_iterator iter = panels_.rbegin(); | 460 for (Panels::reverse_iterator iter = panels_.rbegin(); |
| 356 iter != panels_.rend(); ++iter) | 461 iter != panels_.rend(); ++iter) |
| 357 (*iter)->Close(); | 462 (*iter)->Close(); |
| 358 } | 463 } |
| 359 | 464 |
| 360 bool PanelManager::is_dragging_panel() const { | 465 bool PanelManager::is_dragging_panel() const { |
| 361 return dragging_panel_index_ != kInvalidPanelIndex; | 466 return dragging_panel_index_ != kInvalidPanelIndex; |
| 362 } | 467 } |
| OLD | NEW |