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" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 if (work_area == work_area_) | 73 if (work_area == work_area_) |
74 return; | 74 return; |
75 work_area_ = work_area; | 75 work_area_ = work_area; |
76 | 76 |
77 auto_hiding_desktop_bar_->UpdateWorkArea(work_area_); | 77 auto_hiding_desktop_bar_->UpdateWorkArea(work_area_); |
78 AdjustWorkAreaForAutoHidingDesktopBars(); | 78 AdjustWorkAreaForAutoHidingDesktopBars(); |
79 | 79 |
80 Rearrange(panels_.begin(), adjusted_work_area_.right()); | 80 Rearrange(panels_.begin(), adjusted_work_area_.right()); |
81 } | 81 } |
82 | 82 |
83 void PanelManager::FindAndClosePanelOnOverflow(const Extension* extension) { | |
84 Panel* panel_to_close = NULL; | |
85 | |
86 // Try to find the left-most panel invoked from the same extension and close | |
87 // it. | |
88 for (Panels::reverse_iterator iter = panels_.rbegin(); | |
89 iter != panels_.rend(); ++iter) { | |
90 if (extension == (*iter)->GetExtension()) { | |
91 panel_to_close = *iter; | |
92 break; | |
93 } | |
94 } | |
95 | |
96 // If none is found, just pick the left-most panel. | |
97 if (!panel_to_close) | |
98 panel_to_close = panels_.back(); | |
99 | |
100 panel_to_close->Close(); | |
101 } | |
102 | |
103 Panel* PanelManager::CreatePanel(Browser* browser) { | 83 Panel* PanelManager::CreatePanel(Browser* browser) { |
104 // Adjust the width and height to fit into our constraint. | 84 // Adjust the width and height to fit into our constraint. |
105 int width = browser->override_bounds().width(); | 85 int width = browser->override_bounds().width(); |
106 int height = browser->override_bounds().height(); | 86 int height = browser->override_bounds().height(); |
107 | 87 |
108 if (width == 0 && height == 0) { | 88 if (width == 0 && height == 0) { |
109 width = kPanelDefaultWidth; | 89 width = kPanelDefaultWidth; |
110 height = kPanelDefaultHeight; | 90 height = kPanelDefaultHeight; |
111 } | 91 } |
112 | 92 |
113 int max_panel_width = GetMaxPanelWidth(); | 93 int max_panel_width = GetMaxPanelWidth(); |
114 int max_panel_height = GetMaxPanelHeight(); | 94 int max_panel_height = GetMaxPanelHeight(); |
115 | 95 |
116 if (width < kPanelMinWidth) | 96 if (width < kPanelMinWidth) |
117 width = kPanelMinWidth; | 97 width = kPanelMinWidth; |
118 else if (width > max_panel_width) | 98 else if (width > max_panel_width) |
119 width = max_panel_width; | 99 width = max_panel_width; |
120 | 100 |
121 if (height < kPanelMinHeight) | 101 if (height < kPanelMinHeight) |
122 height = kPanelMinHeight; | 102 height = kPanelMinHeight; |
123 else if (height > max_panel_height) | 103 else if (height > max_panel_height) |
124 height = max_panel_height; | 104 height = max_panel_height; |
125 | 105 |
126 // Compute the origin. Ensure that it falls within the adjusted work area by | |
127 // closing other panels if needed. | |
128 int y = adjusted_work_area_.bottom() - height; | 106 int y = adjusted_work_area_.bottom() - height; |
129 | 107 int x = GetRightMostAvailablePosition() - width; |
130 const Extension* extension = NULL; | |
131 int x; | |
132 while ((x = GetRightMostAvailablePosition() - width) < | |
133 adjusted_work_area_.x() ) { | |
134 if (!extension) | |
135 extension = Panel::GetExtensionFromBrowser(browser); | |
136 FindAndClosePanelOnOverflow(extension); | |
137 } | |
138 | 108 |
139 // Now create the panel with the computed bounds. | 109 // Now create the panel with the computed bounds. |
140 Panel* panel = new Panel(browser, gfx::Rect(x, y, width, height)); | 110 Panel* panel = new Panel(browser, gfx::Rect(x, y, width, height)); |
111 panel->SetMaxSize(gfx::Size(max_panel_width, max_panel_height)); | |
141 panels_.push_back(panel); | 112 panels_.push_back(panel); |
142 | |
143 UpdateMaxSizeForAllPanels(); | |
144 | |
145 return panel; | 113 return panel; |
146 } | 114 } |
147 | 115 |
148 int PanelManager::GetMaxPanelWidth() const { | 116 int PanelManager::GetMaxPanelWidth() const { |
149 return static_cast<int>(adjusted_work_area_.width() * kPanelMaxWidthFactor); | 117 return static_cast<int>(adjusted_work_area_.width() * kPanelMaxWidthFactor); |
150 } | 118 } |
151 | 119 |
152 int PanelManager::GetMaxPanelHeight() const { | 120 int PanelManager::GetMaxPanelHeight() const { |
153 return static_cast<int>(adjusted_work_area_.height() * kPanelMaxHeightFactor); | 121 return static_cast<int>(adjusted_work_area_.height() * kPanelMaxHeightFactor); |
154 } | 122 } |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
350 void PanelManager::OnPreferredWindowSizeChanged( | 318 void PanelManager::OnPreferredWindowSizeChanged( |
351 Panel* panel, const gfx::Size& preferred_window_size) { | 319 Panel* panel, const gfx::Size& preferred_window_size) { |
352 if (!auto_sizing_enabled_) | 320 if (!auto_sizing_enabled_) |
353 return; | 321 return; |
354 | 322 |
355 gfx::Rect bounds = panel->GetBounds(); | 323 gfx::Rect bounds = panel->GetBounds(); |
356 int restored_height = panel->GetRestoredHeight(); | 324 int restored_height = panel->GetRestoredHeight(); |
357 | 325 |
358 // The panel width: | 326 // The panel width: |
359 // * cannot grow or shrink to go beyond [min_width, max_width] | 327 // * cannot grow or shrink to go beyond [min_width, max_width] |
360 // * cannot grow to take more than the available space and go beyond the left | 328 // * cannot grow to take more than the available space and go beyond the left |
jianli
2011/10/21 22:37:15
Please update the comment here.
| |
361 // of the work area. | 329 // of the work area. |
362 int new_width = preferred_window_size.width(); | 330 int new_width = preferred_window_size.width(); |
363 if (new_width > panel->max_size().width()) | 331 if (new_width > panel->max_size().width()) |
364 new_width = panel->max_size().width(); | 332 new_width = panel->max_size().width(); |
365 if (new_width < panel->min_size().width()) | 333 if (new_width < panel->min_size().width()) |
366 new_width = panel->min_size().width(); | 334 new_width = panel->min_size().width(); |
367 | 335 |
368 int right_most_available_position = GetRightMostAvailablePosition(); | |
369 if (new_width - bounds.width() > right_most_available_position) | |
370 new_width = bounds.width() + right_most_available_position; | |
371 | |
372 if (new_width != bounds.width()) { | 336 if (new_width != bounds.width()) { |
373 int delta = bounds.width() - new_width; | 337 int delta = bounds.width() - new_width; |
374 bounds.set_x(bounds.x() + delta); | 338 bounds.set_x(bounds.x() + delta); |
375 bounds.set_width(new_width); | 339 bounds.set_width(new_width); |
376 | 340 |
377 // Reposition all the panels on the left. | 341 // Reposition all the panels on the left. |
378 int panel_index = -1; | 342 int panel_index = -1; |
379 for (int i = 0; i < static_cast<int>(panels_.size()); ++i) { | 343 for (int i = 0; i < static_cast<int>(panels_.size()); ++i) { |
380 if (panels_[i] == panel) { | 344 if (panels_[i] == panel) { |
381 panel_index = i; | 345 panel_index = i; |
(...skipping 20 matching lines...) Expand all Loading... | |
402 if (new_height != restored_height) { | 366 if (new_height != restored_height) { |
403 panel->SetRestoredHeight(new_height); | 367 panel->SetRestoredHeight(new_height); |
404 // Only need to adjust bounds height when panel is expanded. | 368 // Only need to adjust bounds height when panel is expanded. |
405 if (panel->expansion_state() == Panel::EXPANDED) { | 369 if (panel->expansion_state() == Panel::EXPANDED) { |
406 bounds.set_y(bounds.y() - new_height + bounds.height()); | 370 bounds.set_y(bounds.y() - new_height + bounds.height()); |
407 bounds.set_height(new_height); | 371 bounds.set_height(new_height); |
408 } | 372 } |
409 } | 373 } |
410 | 374 |
411 panel->SetPanelBounds(bounds); | 375 panel->SetPanelBounds(bounds); |
412 | |
413 UpdateMaxSizeForAllPanels(); | |
414 } | 376 } |
415 | 377 |
416 bool PanelManager::ShouldBringUpTitlebars(int mouse_x, int mouse_y) const { | 378 bool PanelManager::ShouldBringUpTitlebars(int mouse_x, int mouse_y) const { |
417 // We should always bring up the titlebar if the mouse is over the | 379 // We should always bring up the titlebar if the mouse is over the |
418 // visible auto-hiding bottom bar. | 380 // visible auto-hiding bottom bar. |
419 if (auto_hiding_desktop_bar_->IsEnabled(AutoHidingDesktopBar::ALIGN_BOTTOM) && | 381 if (auto_hiding_desktop_bar_->IsEnabled(AutoHidingDesktopBar::ALIGN_BOTTOM) && |
420 auto_hiding_desktop_bar_->GetVisibility( | 382 auto_hiding_desktop_bar_->GetVisibility( |
421 AutoHidingDesktopBar::ALIGN_BOTTOM) == | 383 AutoHidingDesktopBar::ALIGN_BOTTOM) == |
422 AutoHidingDesktopBar::VISIBLE && | 384 AutoHidingDesktopBar::VISIBLE && |
423 mouse_y >= adjusted_work_area_.bottom()) | 385 mouse_y >= adjusted_work_area_.bottom()) |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
563 : AutoHidingDesktopBar::HIDDEN; | 525 : AutoHidingDesktopBar::HIDDEN; |
564 if (visibility != expected_visibility) | 526 if (visibility != expected_visibility) |
565 return; | 527 return; |
566 | 528 |
567 DoBringUpOrDownTitlebars(delayed_titlebar_action_ == BRING_UP); | 529 DoBringUpOrDownTitlebars(delayed_titlebar_action_ == BRING_UP); |
568 delayed_titlebar_action_ = NO_ACTION; | 530 delayed_titlebar_action_ = NO_ACTION; |
569 } | 531 } |
570 | 532 |
571 void PanelManager::Rearrange(Panels::iterator iter_to_start, | 533 void PanelManager::Rearrange(Panels::iterator iter_to_start, |
572 int rightmost_position) { | 534 int rightmost_position) { |
573 if (iter_to_start != panels_.end()) { | 535 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { |
574 for (Panels::iterator iter = iter_to_start; iter != panels_.end(); ++iter) { | 536 Panel* panel = *iter; |
575 Panel* panel = *iter; | 537 gfx::Rect new_bounds(panel->GetBounds()); |
576 gfx::Rect new_bounds(panel->GetBounds()); | 538 new_bounds.set_x(rightmost_position - new_bounds.width()); |
577 new_bounds.set_x(rightmost_position - new_bounds.width()); | 539 new_bounds.set_y(adjusted_work_area_.bottom() - new_bounds.height()); |
578 new_bounds.set_y(adjusted_work_area_.bottom() - new_bounds.height()); | 540 if (new_bounds != panel->GetBounds()) |
579 if (new_bounds != panel->GetBounds()) | 541 panel->SetPanelBounds(new_bounds); |
580 panel->SetPanelBounds(new_bounds); | |
581 | 542 |
582 rightmost_position = new_bounds.x() - kPanelsHorizontalSpacing; | 543 rightmost_position = new_bounds.x() - kPanelsHorizontalSpacing; |
583 } | |
584 } | 544 } |
585 | |
586 UpdateMaxSizeForAllPanels(); | |
587 } | 545 } |
588 | 546 |
589 void PanelManager::RemoveAll() { | 547 void PanelManager::RemoveAll() { |
590 // This should not be called when we're in the process of dragging. | 548 // This should not be called when we're in the process of dragging. |
591 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); | 549 DCHECK(dragging_panel_index_ == kInvalidPanelIndex); |
592 | 550 |
593 // Make a copy of the iterator as closing panels can modify the vector. | 551 // Make a copy of the iterator as closing panels can modify the vector. |
594 Panels panels_copy = panels_; | 552 Panels panels_copy = panels_; |
595 | 553 |
596 // Start from the bottom to avoid reshuffling. | 554 // Start from the bottom to avoid reshuffling. |
597 for (Panels::reverse_iterator iter = panels_copy.rbegin(); | 555 for (Panels::reverse_iterator iter = panels_copy.rbegin(); |
598 iter != panels_copy.rend(); ++iter) | 556 iter != panels_copy.rend(); ++iter) |
599 (*iter)->Close(); | 557 (*iter)->Close(); |
600 } | 558 } |
601 | 559 |
602 bool PanelManager::is_dragging_panel() const { | 560 bool PanelManager::is_dragging_panel() const { |
603 return dragging_panel_index_ != kInvalidPanelIndex; | 561 return dragging_panel_index_ != kInvalidPanelIndex; |
604 } | 562 } |
605 | 563 |
606 void PanelManager::UpdateMaxSizeForAllPanels() { | |
607 if (!auto_sizing_enabled_) | |
608 return; | |
609 | |
610 for (Panels::const_iterator iter = panels_.begin(); | |
611 iter != panels_.end(); ++iter) { | |
612 Panel* panel = *iter; | |
613 // A panel can at most grow to take over all the available space that is | |
614 // returned by GetRightMostAvailablePosition. | |
615 int width_can_grow_to = | |
616 panel->GetBounds().width() + GetRightMostAvailablePosition(); | |
617 panel->SetMaxSize(gfx::Size( | |
618 std::min(width_can_grow_to, GetMaxPanelWidth()), | |
619 GetMaxPanelHeight())); | |
620 } | |
621 } | |
OLD | NEW |