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 // Occasionally some system, like Windows, might not bring up or down the bottom | |
| 36 // bar when the mouse enters or leaves the bottom screen area. This is the | |
| 37 // maximum time we will wait for the bottom bar visibility change notification. | |
| 38 // After the time expires, we bring up/down the titlebars as planned. | |
| 39 const int kMaxTimeWaitForBottomBarVisibilityChange = 1000; | |
|
jennb
2011/08/23 20:28:34
s/Time/<name of time unit>
jianli
2011/08/26 00:18:16
Done.
| |
| 40 | |
| 34 // Single instance of PanelManager. | 41 // Single instance of PanelManager. |
| 35 scoped_ptr<PanelManager> panel_instance; | 42 scoped_refptr<PanelManager> panel_instance; |
| 36 } // namespace | 43 } // namespace |
| 37 | 44 |
| 38 // static | 45 // static |
| 39 PanelManager* PanelManager::GetInstance() { | 46 PanelManager* PanelManager::GetInstance() { |
| 40 if (!panel_instance.get()) { | 47 if (!panel_instance.get()) { |
| 41 panel_instance.reset(new PanelManager()); | 48 panel_instance = new PanelManager(); |
| 49 panel_instance->Init(); | |
| 42 } | 50 } |
| 43 return panel_instance.get(); | 51 return panel_instance.get(); |
| 44 } | 52 } |
| 45 | 53 |
| 54 // static | |
| 55 void PanelManager::CreateForTestingHelper( | |
| 56 const gfx::Rect& work_area, AutoHideBottomBar* auto_hide_bottom_bar) { | |
| 57 DCHECK(!panel_instance.get()); | |
| 58 panel_instance = new PanelManager(); | |
| 59 panel_instance->auto_hide_bottom_bar_ = auto_hide_bottom_bar; | |
| 60 panel_instance->SetWorkArea(work_area); | |
| 61 } | |
| 62 | |
| 46 PanelManager::PanelManager() | 63 PanelManager::PanelManager() |
| 47 : max_width_(0), | 64 : max_width_(0), |
| 48 max_height_(0), | 65 max_height_(0), |
| 49 min_x_(0), | |
| 50 current_x_(0), | 66 current_x_(0), |
| 51 bottom_edge_y_(0), | |
| 52 dragging_panel_index_(kInvalidPanelIndex), | 67 dragging_panel_index_(kInvalidPanelIndex), |
| 53 dragging_panel_original_x_(0) { | 68 dragging_panel_original_x_(0), |
| 54 OnDisplayChanged(); | 69 delayed_titlebar_action_(NO_ACTION), |
| 70 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
| 55 } | 71 } |
| 56 | 72 |
| 57 PanelManager::~PanelManager() { | 73 PanelManager::~PanelManager() { |
| 58 DCHECK(panels_.empty()); | 74 DCHECK(panels_.empty()); |
| 59 DCHECK(panels_pending_to_remove_.empty()); | 75 DCHECK(panels_pending_to_remove_.empty()); |
| 60 } | 76 } |
| 61 | 77 |
| 78 void PanelManager::Init() { | |
| 79 auto_hide_bottom_bar_ = AutoHideBottomBar::Create(this); | |
| 80 OnDisplayChanged(); | |
| 81 } | |
| 82 | |
| 62 void PanelManager::OnDisplayChanged() { | 83 void PanelManager::OnDisplayChanged() { |
| 63 scoped_ptr<WindowSizer::MonitorInfoProvider> info_provider( | 84 scoped_ptr<WindowSizer::MonitorInfoProvider> info_provider( |
| 64 WindowSizer::CreateDefaultMonitorInfoProvider()); | 85 WindowSizer::CreateDefaultMonitorInfoProvider()); |
| 65 SetWorkArea(info_provider->GetPrimaryMonitorWorkArea()); | 86 SetWorkArea(info_provider->GetPrimaryMonitorWorkArea()); |
| 66 } | 87 } |
| 67 | 88 |
| 68 void PanelManager::SetWorkArea(const gfx::Rect& work_area) { | 89 void PanelManager::SetWorkArea(const gfx::Rect& work_area) { |
| 69 if (work_area == work_area_) | 90 if (work_area == work_area_) |
| 70 return; | 91 return; |
| 71 work_area_ = work_area; | 92 work_area_ = work_area; |
| 72 | 93 |
| 73 min_x_ = work_area.x(); | 94 auto_hide_bottom_bar_->UpdateWorkArea(work_area); |
| 95 | |
| 74 current_x_ = work_area.right(); | 96 current_x_ = work_area.right(); |
| 75 bottom_edge_y_ = work_area.bottom(); | |
| 76 max_width_ = static_cast<int>(work_area.width() * kPanelMaxWidthFactor); | 97 max_width_ = static_cast<int>(work_area.width() * kPanelMaxWidthFactor); |
| 77 max_height_ = static_cast<int>(work_area.height() * kPanelMaxHeightFactor); | 98 max_height_ = static_cast<int>(work_area.height() * kPanelMaxHeightFactor); |
| 78 | 99 |
| 79 Rearrange(panels_.begin()); | 100 Rearrange(panels_.begin()); |
| 80 } | 101 } |
| 81 | 102 |
| 82 void PanelManager::FindAndClosePanelOnOverflow(const Extension* extension) { | 103 void PanelManager::FindAndClosePanelOnOverflow(const Extension* extension) { |
| 83 Panel* panel_to_close = NULL; | 104 Panel* panel_to_close = NULL; |
| 84 | 105 |
| 85 // Try to find the left-most panel invoked from the same extension and close | 106 // 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 { | 285 } else { |
| 265 panels_[dragging_panel_index_]->SetPanelBounds( | 286 panels_[dragging_panel_index_]->SetPanelBounds( |
| 266 dragging_panel_bounds_); | 287 dragging_panel_bounds_); |
| 267 } | 288 } |
| 268 | 289 |
| 269 dragging_panel_index_ = kInvalidPanelIndex; | 290 dragging_panel_index_ = kInvalidPanelIndex; |
| 270 | 291 |
| 271 DelayedRemove(); | 292 DelayedRemove(); |
| 272 } | 293 } |
| 273 | 294 |
| 274 bool PanelManager::ShouldBringUpTitlebarForAllMinimizedPanels( | 295 bool PanelManager::ShouldBringUpTitlebars(int mouse_x, int mouse_y) const { |
| 275 int mouse_x, int mouse_y) const { | 296 // We should always bring up the title-bar if the mouse is over the auto-hide |
| 297 // task-bar that becomes visible. | |
| 298 if (auto_hide_bottom_bar_->IsEnabled() && | |
| 299 auto_hide_bottom_bar_->GetVisibility() == AutoHideBottomBar::VISIBLE && | |
| 300 mouse_y >= work_area_.bottom() - auto_hide_bottom_bar_->GetHeight()) | |
| 301 return true; | |
| 302 | |
| 276 for (Panels::const_iterator iter = panels_.begin(); | 303 for (Panels::const_iterator iter = panels_.begin(); |
| 277 iter != panels_.end(); ++iter) { | 304 iter != panels_.end(); ++iter) { |
| 278 if ((*iter)->ShouldBringUpTitlebar(mouse_x, mouse_y)) | 305 if ((*iter)->ShouldBringUpTitlebar(mouse_x, mouse_y)) |
| 279 return true; | 306 return true; |
| 280 } | 307 } |
| 281 return false; | 308 return false; |
| 282 } | 309 } |
| 283 | 310 |
| 284 void PanelManager::BringUpOrDownTitlebarForAllMinimizedPanels(bool bring_up) { | 311 void PanelManager::BringUpOrDownTitlebars(bool bring_up) { |
| 312 // If the auto-hide bottom bar exists, delay the action until the bottom | |
| 313 // bar is fully visible or hidden. We do not want both bottom bar and panel | |
| 314 // titlebar to move at the same time but with different speeds. | |
| 315 if (auto_hide_bottom_bar_->IsEnabled()) { | |
| 316 AutoHideBottomBar::Visibility visibility = | |
| 317 auto_hide_bottom_bar_->GetVisibility(); | |
| 318 if (visibility != (bring_up ? AutoHideBottomBar::VISIBLE | |
| 319 : AutoHideBottomBar::HIDDEN)) { | |
| 320 // OnAutoHideBottomBarVisibilityChanged will handle this. | |
| 321 delayed_titlebar_action_ = bring_up ? BRING_UP : BRING_DOWN; | |
| 322 | |
| 323 // Occasionally some system, like Windows, might not bring up or down the | |
| 324 // bottom bar when the mouse enters or leaves the bottom screen area. | |
| 325 // Thus, we schedule a delayed task to do the work if we do not receive | |
| 326 // the bottom bar visibility change notification within a certain period | |
| 327 // of time. | |
| 328 MessageLoop::current()->PostDelayedTask( | |
| 329 FROM_HERE, | |
| 330 method_factory_.NewRunnableMethod( | |
| 331 &PanelManager::DelayedBringUpOrDownTitlebarsCheck), | |
| 332 kMaxTimeWaitForBottomBarVisibilityChange); | |
| 333 | |
| 334 return; | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 DoBringUpOrDownTitlebars(bring_up); | |
| 339 } | |
| 340 | |
| 341 void PanelManager::DelayedBringUpOrDownTitlebarsCheck() { | |
|
jennb
2011/08/23 20:28:34
Seems like this could be coded more compactly:
if
jianli
2011/08/26 00:18:16
Done.
| |
| 342 switch (delayed_titlebar_action_) { | |
| 343 case NO_ACTION: | |
| 344 break; | |
| 345 case BRING_UP: | |
| 346 delayed_titlebar_action_ = NO_ACTION; | |
| 347 DoBringUpOrDownTitlebars(true); | |
| 348 break; | |
| 349 case BRING_DOWN: | |
| 350 delayed_titlebar_action_ = NO_ACTION; | |
| 351 DoBringUpOrDownTitlebars(false); | |
| 352 break; | |
| 353 default: | |
| 354 NOTREACHED(); | |
| 355 break; | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 void PanelManager::DoBringUpOrDownTitlebars(bool bring_up) { | |
| 285 for (Panels::const_iterator iter = panels_.begin(); | 360 for (Panels::const_iterator iter = panels_.begin(); |
| 286 iter != panels_.end(); ++iter) { | 361 iter != panels_.end(); ++iter) { |
| 287 Panel* panel = *iter; | 362 Panel* panel = *iter; |
| 288 | 363 |
| 289 // Skip any panel that is drawing the attention. | 364 // Skip any panel that is drawing the attention. |
| 290 if (panel->IsDrawingAttention()) | 365 if (panel->IsDrawingAttention()) |
| 291 continue; | 366 continue; |
| 292 | 367 |
| 293 if (bring_up) { | 368 if (bring_up) { |
| 294 if (panel->expansion_state() == Panel::MINIMIZED) | 369 if (panel->expansion_state() == Panel::MINIMIZED) |
| 295 panel->SetExpansionState(Panel::TITLE_ONLY); | 370 panel->SetExpansionState(Panel::TITLE_ONLY); |
| 296 } else { | 371 } else { |
| 297 if (panel->expansion_state() == Panel::TITLE_ONLY) | 372 if (panel->expansion_state() == Panel::TITLE_ONLY) |
| 298 panel->SetExpansionState(Panel::MINIMIZED); | 373 panel->SetExpansionState(Panel::MINIMIZED); |
| 299 } | 374 } |
| 300 } | 375 } |
| 301 } | 376 } |
| 302 | 377 |
| 378 int PanelManager::GetBottomPositionForExpansionState( | |
|
jennb
2011/08/23 20:28:34
As discussed, some logic error computing bottom he
jianli
2011/08/26 00:18:16
Done.
| |
| 379 Panel::ExpansionState expansion_state) const { | |
| 380 int bottom = work_area_.bottom(); | |
| 381 if (expansion_state != Panel::MINIMIZED) | |
| 382 bottom -= auto_hide_bottom_bar_->GetHeight(); | |
| 383 return bottom; | |
| 384 } | |
| 385 | |
| 386 void PanelManager::OnAutoHideBottomBarVisibilityChanged( | |
| 387 AutoHideBottomBar::Visibility visibility) { | |
| 388 switch (delayed_titlebar_action_) { | |
|
jennb
2011/08/23 20:28:34
Feels like this method can be coded more compactly
jianli
2011/08/26 00:18:16
Done.
| |
| 389 case NO_ACTION: | |
| 390 break; | |
| 391 case BRING_UP: | |
| 392 if (visibility == AutoHideBottomBar::VISIBLE) { | |
| 393 delayed_titlebar_action_ = NO_ACTION; | |
| 394 DoBringUpOrDownTitlebars(true); | |
| 395 } | |
| 396 break; | |
| 397 case BRING_DOWN: | |
| 398 if (visibility == AutoHideBottomBar::HIDDEN) { | |
| 399 delayed_titlebar_action_ = NO_ACTION; | |
| 400 DoBringUpOrDownTitlebars(false); | |
| 401 } | |
| 402 break; | |
| 403 default: | |
| 404 NOTREACHED(); | |
| 405 break; | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 void PanelManager::OnAutoHideBottomBarHeightChanged(int height) { | |
| 410 current_x_ = work_area_.right(); | |
|
jennb
2011/08/23 20:28:34
Why does current_x_ get updated when height change
jianli
2011/08/26 00:18:16
Changed the logic such that current_x_ is removed.
| |
| 411 Rearrange(panels_.begin()); | |
| 412 } | |
| 413 | |
| 303 void PanelManager::Rearrange(Panels::iterator iter_to_start) { | 414 void PanelManager::Rearrange(Panels::iterator iter_to_start) { |
| 304 if (iter_to_start == panels_.end()) | 415 if (iter_to_start == panels_.end()) |
| 305 return; | 416 return; |
| 306 | 417 |
| 307 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { | 418 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { |
| 308 gfx::Rect new_bounds((*iter)->GetBounds()); | 419 gfx::Rect new_bounds((*iter)->GetBounds()); |
| 309 ComputeBoundsForNextPanel(&new_bounds, false); | 420 ComputeBoundsForNextPanel(&new_bounds, false); |
| 310 if (new_bounds != (*iter)->GetBounds()) | 421 if (new_bounds != (*iter)->GetBounds()) |
| 311 (*iter)->SetPanelBounds(new_bounds); | 422 (*iter)->SetPanelBounds(new_bounds); |
| 312 } | 423 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 329 else if (width > max_width_) | 440 else if (width > max_width_) |
| 330 width = max_width_; | 441 width = max_width_; |
| 331 | 442 |
| 332 if (height < kPanelMinHeightPixels) | 443 if (height < kPanelMinHeightPixels) |
| 333 height = kPanelMinHeightPixels; | 444 height = kPanelMinHeightPixels; |
| 334 else if (height > max_height_) | 445 else if (height > max_height_) |
| 335 height = max_height_; | 446 height = max_height_; |
| 336 } | 447 } |
| 337 | 448 |
| 338 int x = current_x_ - width; | 449 int x = current_x_ - width; |
| 339 int y = bottom_edge_y_ - height; | 450 int y = work_area_.bottom() - auto_hide_bottom_bar_->GetHeight() - height; |
| 340 | 451 |
| 341 if (x < min_x_) | 452 if (x < work_area_.x()) |
| 342 return false; | 453 return false; |
| 343 | 454 |
| 344 current_x_ -= width + kPanelsHorizontalSpacing; | 455 current_x_ -= width + kPanelsHorizontalSpacing; |
| 345 | 456 |
| 346 bounds->SetRect(x, y, width, height); | 457 bounds->SetRect(x, y, width, height); |
| 347 return true; | 458 return true; |
| 348 } | 459 } |
| 349 | 460 |
| 350 void PanelManager::RemoveAll() { | 461 void PanelManager::RemoveAll() { |
| 351 // This should not be called when we're in the process of dragging. | 462 // This should not be called when we're in the process of dragging. |
| 352 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); | 463 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); |
| 353 | 464 |
| 354 // Start from the bottom to avoid reshuffling. | 465 // Start from the bottom to avoid reshuffling. |
| 355 for (Panels::reverse_iterator iter = panels_.rbegin(); | 466 for (Panels::reverse_iterator iter = panels_.rbegin(); |
| 356 iter != panels_.rend(); ++iter) | 467 iter != panels_.rend(); ++iter) |
| 357 (*iter)->Close(); | 468 (*iter)->Close(); |
| 358 } | 469 } |
| 359 | 470 |
| 360 bool PanelManager::is_dragging_panel() const { | 471 bool PanelManager::is_dragging_panel() const { |
| 361 return dragging_panel_index_ != kInvalidPanelIndex; | 472 return dragging_panel_index_ != kInvalidPanelIndex; |
| 362 } | 473 } |
| OLD | NEW |