1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/panels/panel_manager.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/ui/panels/detached_panel_collection.h"
#include "chrome/browser/ui/panels/docked_panel_collection.h"
#include "chrome/browser/ui/panels/panel_drag_controller.h"
#include "chrome/browser/ui/panels/panel_mouse_watcher.h"
#include "chrome/browser/ui/panels/panel_resize_controller.h"
#include "chrome/browser/ui/panels/stacked_panel_collection.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "ui/base/hit_test.h"
#if defined(USE_X11) && !defined(OS_CHROMEOS)
#include "base/environment.h"
#include "base/nix/xdg_util.h"
#include "ui/base/x/x11_util.h"
#endif
namespace {
// Maxmium width of a panel is based on a factor of the working area.
#if defined(OS_CHROMEOS)
// ChromeOS device screens are relatively small and limiting the width
// interferes with some apps (e.g. http://crbug.com/111121).
const double kPanelMaxWidthFactor = 0.80;
#else
const double kPanelMaxWidthFactor = 0.35;
#endif
// Maxmium height of a panel is based on a factor of the working area.
const double kPanelMaxHeightFactor = 0.5;
// Width to height ratio is used to compute the default width or height
// when only one value is provided.
const double kPanelDefaultWidthToHeightRatio = 1.62; // golden ratio
// The test code could call PanelManager::SetDisplaySettingsProviderForTesting
// to set this for testing purpose.
DisplaySettingsProvider* display_settings_provider_for_testing;
// The following comparers are used by std::list<>::sort to determine which
// stack or panel we want to seacrh first for adding new panel.
bool ComparePanelsByPosition(Panel* panel1, Panel* panel2) {
gfx::Rect bounds1 = panel1->GetBounds();
gfx::Rect bounds2 = panel2->GetBounds();
// When there're ties, the right-most stack will appear first.
if (bounds1.x() > bounds2.x())
return true;
if (bounds1.x() < bounds2.x())
return false;
// In the event of another draw, the top-most stack will appear first.
return bounds1.y() < bounds2.y();
}
bool ComparerNumberOfPanelsInStack(StackedPanelCollection* stack1,
StackedPanelCollection* stack2) {
// The stack with more panels will appear first.
int num_panels_in_stack1 = stack1->num_panels();
int num_panels_in_stack2 = stack2->num_panels();
if (num_panels_in_stack1 > num_panels_in_stack2)
return true;
if (num_panels_in_stack1 < num_panels_in_stack2)
return false;
DCHECK(num_panels_in_stack1);
return ComparePanelsByPosition(stack1->top_panel(), stack2->top_panel());
}
bool CompareDetachedPanels(Panel* panel1, Panel* panel2) {
return ComparePanelsByPosition(panel1, panel2);
}
} // namespace
// static
bool PanelManager::shorten_time_intervals_ = false;
// static
PanelManager* PanelManager::GetInstance() {
static base::LazyInstance<PanelManager> instance = LAZY_INSTANCE_INITIALIZER;
return instance.Pointer();
}
// static
void PanelManager::SetDisplaySettingsProviderForTesting(
DisplaySettingsProvider* provider) {
display_settings_provider_for_testing = provider;
}
// static
bool PanelManager::ShouldUsePanels(const std::string& extension_id) {
#if defined(USE_X11) && !defined(OS_CHROMEOS)
// If --enable-panels is on, always use panels on Linux.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnablePanels))
return true;
// Otherwise, panels are only supported on tested window managers.
ui::WindowManagerName wm_type = ui::GuessWindowManager();
if (wm_type != ui::WM_COMPIZ &&
wm_type != ui::WM_ICE_WM &&
wm_type != ui::WM_KWIN &&
wm_type != ui::WM_METACITY &&
wm_type != ui::WM_MUFFIN &&
wm_type != ui::WM_MUTTER &&
wm_type != ui::WM_XFWM4) {
return false;
}
#endif // USE_X11 && !OS_CHROMEOS
chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
if (channel == chrome::VersionInfo::CHANNEL_STABLE ||
channel == chrome::VersionInfo::CHANNEL_BETA) {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnablePanels) ||
extension_id == std::string("nckgahadagoaajjgafhacjanaoiihapd") ||
extension_id == std::string("ljclpkphhpbpinifbeabbhlfddcpfdde") ||
extension_id == std::string("ppleadejekpmccmnpjdimmlfljlkdfej") ||
extension_id == std::string("eggnbpckecmjlblplehfpjjdhhidfdoj");
}
return true;
}
// static
bool PanelManager::IsPanelStackingEnabled() {
// Stacked panel mode is not supported in linux-aura.
#if defined(OS_LINUX)
return false;
#else
return true;
#endif
}
// static
bool PanelManager::CanUseSystemMinimize() {
#if defined(USE_X11) && !defined(OS_CHROMEOS)
static base::nix::DesktopEnvironment desktop_env =
base::nix::DESKTOP_ENVIRONMENT_OTHER;
if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_OTHER) {
scoped_ptr<base::Environment> env(base::Environment::Create());
desktop_env = base::nix::GetDesktopEnvironment(env.get());
}
return desktop_env != base::nix::DESKTOP_ENVIRONMENT_UNITY;
#else
return true;
#endif
}
PanelManager::PanelManager()
: panel_mouse_watcher_(PanelMouseWatcher::Create()),
auto_sizing_enabled_(true) {
// DisplaySettingsProvider should be created before the creation of
// collections since some collection might depend on it.
if (display_settings_provider_for_testing)
display_settings_provider_.reset(display_settings_provider_for_testing);
else
display_settings_provider_.reset(DisplaySettingsProvider::Create());
display_settings_provider_->AddDisplayObserver(this);
detached_collection_.reset(new DetachedPanelCollection(this));
docked_collection_.reset(new DockedPanelCollection(this));
drag_controller_.reset(new PanelDragController(this));
resize_controller_.reset(new PanelResizeController(this));
}
PanelManager::~PanelManager() {
display_settings_provider_->RemoveDisplayObserver(this);
// Docked collection should be disposed explicitly before
// DisplaySettingsProvider is gone since docked collection needs to remove
// the observer from DisplaySettingsProvider.
docked_collection_.reset();
}
gfx::Point PanelManager::GetDefaultDetachedPanelOrigin() {
return detached_collection_->GetDefaultPanelOrigin();
}
void PanelManager::OnDisplayChanged() {
docked_collection_->OnDisplayChanged();
detached_collection_->OnDisplayChanged();
for (Stacks::const_iterator iter = stacks_.begin();
iter != stacks_.end(); iter++)
(*iter)->OnDisplayChanged();
}
void PanelManager::OnFullScreenModeChanged(bool is_full_screen) {
std::vector<Panel*> all_panels = panels();
for (std::vector<Panel*>::const_iterator iter = all_panels.begin();
iter != all_panels.end(); ++iter) {
(*iter)->FullScreenModeChanged(is_full_screen);
}
}
int PanelManager::GetMaxPanelWidth(const gfx::Rect& work_area) const {
return static_cast<int>(work_area.width() * kPanelMaxWidthFactor);
}
int PanelManager::GetMaxPanelHeight(const gfx::Rect& work_area) const {
return static_cast<int>(work_area.height() * kPanelMaxHeightFactor);
}
Panel* PanelManager::CreatePanel(const std::string& app_name,
Profile* profile,
const GURL& url,
const gfx::Rect& requested_bounds,
CreateMode mode) {
// Need to sync the display area if no panel is present. This is because:
// 1) Display area is not initialized until first panel is created.
// 2) On windows, display settings notification is tied to a window. When
// display settings are changed at the time that no panel exists, we do
// not receive any notification.
if (num_panels() == 0) {
display_settings_provider_->OnDisplaySettingsChanged();
display_settings_provider_->AddFullScreenObserver(this);
}
// Compute initial bounds for the panel.
int width = requested_bounds.width();
int height = requested_bounds.height();
if (width == 0)
width = height * kPanelDefaultWidthToHeightRatio;
else if (height == 0)
height = width / kPanelDefaultWidthToHeightRatio;
gfx::Rect work_area =
display_settings_provider_->GetWorkAreaMatching(requested_bounds);
gfx::Size min_size(panel::kPanelMinWidth, panel::kPanelMinHeight);
gfx::Size max_size(GetMaxPanelWidth(work_area), GetMaxPanelHeight(work_area));
if (width < min_size.width())
width = min_size.width();
else if (width > max_size.width())
width = max_size.width();
if (height < min_size.height())
height = min_size.height();
else if (height > max_size.height())
height = max_size.height();
// Create the panel.
Panel* panel = new Panel(profile, app_name, min_size, max_size);
// Find the appropriate panel collection to hold the new panel.
gfx::Rect adjusted_requested_bounds(
requested_bounds.x(), requested_bounds.y(), width, height);
PanelCollection::PositioningMask positioning_mask;
PanelCollection* collection = GetCollectionForNewPanel(
panel, adjusted_requested_bounds, mode, &positioning_mask);
// Let the panel collection decide the initial bounds.
gfx::Rect bounds = collection->GetInitialPanelBounds(
adjusted_requested_bounds);
bounds.AdjustToFit(work_area);
panel->Initialize(url, bounds, collection->UsesAlwaysOnTopPanels());
// Auto resizable feature is enabled only if no initial size is requested.
if (auto_sizing_enabled() && requested_bounds.width() == 0 &&
requested_bounds.height() == 0) {
panel->SetAutoResizable(true);
}
// Add the panel to the panel collection.
collection->AddPanel(panel, positioning_mask);
collection->UpdatePanelOnCollectionChange(panel);
return panel;
}
PanelCollection* PanelManager::GetCollectionForNewPanel(
Panel* new_panel,
const gfx::Rect& bounds,
CreateMode mode,
PanelCollection::PositioningMask* positioning_mask) {
if (mode == CREATE_AS_DOCKED) {
// Delay layout refreshes in case multiple panels are created within
// a short time of one another or the focus changes shortly after panel
// is created to avoid excessive screen redraws.
*positioning_mask = PanelCollection::DELAY_LAYOUT_REFRESH;
return docked_collection_.get();
}
DCHECK_EQ(CREATE_AS_DETACHED, mode);
*positioning_mask = PanelCollection::DEFAULT_POSITION;
// If the stacking support is not enabled, new panel will still be created as
// detached.
if (!IsPanelStackingEnabled())
return detached_collection_.get();
// If there're stacks, try to find a stack that can fit new panel.
if (!stacks_.empty()) {
// Perform the search as:
// 1) Search from the stack with more panels to the stack with least panels.
// 2) Amongs the stacks with same number of panels, search from the right-
// most stack to the left-most stack.
// 3) Among the stack with same number of panels and same x position,
// search from the top-most stack to the bottom-most stack.
// 4) If there is not enough space to fit new panel even with all inactive
// panels being collapsed, move to next stack.
stacks_.sort(ComparerNumberOfPanelsInStack);
for (Stacks::const_iterator iter = stacks_.begin();
iter != stacks_.end(); iter++) {
StackedPanelCollection* stack = *iter;
// Do not add to other stack that is from differnt extension or profile.
// Note that the check is based on bottom panel.
Panel* panel = stack->bottom_panel();
if (panel->profile() != new_panel->profile() ||
panel->extension_id() != new_panel->extension_id())
continue;
// Do not add to the stack that is minimized by the system.
if (stack->IsMinimized())
continue;
// Do not stack with the panel that is not shown in current virtual
// desktop.
if (!panel->IsShownOnActiveDesktop())
continue;
if (bounds.height() <= stack->GetMaximiumAvailableBottomSpace()) {
*positioning_mask = static_cast<PanelCollection::PositioningMask>(
*positioning_mask | PanelCollection::COLLAPSE_TO_FIT);
return stack;
}
}
}
// Then try to find a detached panel to which new panel can stack.
if (detached_collection_->num_panels()) {
// Perform the search as:
// 1) Search from the right-most detached panel to the left-most detached
// panel.
// 2) Among the detached panels with same x position, search from the
// top-most detached panel to the bottom-most deatched panel.
// 3) If there is not enough space beneath the detached panel, even by
// collapsing it if it is inactive, to fit new panel, move to next
// detached panel.
detached_collection_->SortPanels(CompareDetachedPanels);
for (DetachedPanelCollection::Panels::const_iterator iter =
detached_collection_->panels().begin();
iter != detached_collection_->panels().end(); ++iter) {
Panel* panel = *iter;
// Do not stack with other panel that is from differnt extension or
// profile.
if (panel->profile() != new_panel->profile() ||
panel->extension_id() != new_panel->extension_id())
continue;
// Do not stack with the panel that is minimized by the system.
if (panel->IsMinimizedBySystem())
continue;
// Do not stack with the panel that is not shown in the active desktop.
if (!panel->IsShownOnActiveDesktop())
continue;
gfx::Rect work_area =
display_settings_provider_->GetWorkAreaMatching(panel->GetBounds());
int max_available_space =
work_area.bottom() - panel->GetBounds().y() -
(panel->IsActive() ? panel->GetBounds().height()
: panel::kTitlebarHeight);
if (bounds.height() <= max_available_space) {
StackedPanelCollection* new_stack = CreateStack();
MovePanelToCollection(panel,
new_stack,
PanelCollection::DEFAULT_POSITION);
*positioning_mask = static_cast<PanelCollection::PositioningMask>(
*positioning_mask | PanelCollection::COLLAPSE_TO_FIT);
return new_stack;
}
}
}
return detached_collection_.get();
}
void PanelManager::OnPanelClosed(Panel* panel) {
if (num_panels() == 1) {
display_settings_provider_->RemoveFullScreenObserver(this);
}
drag_controller_->OnPanelClosed(panel);
resize_controller_->OnPanelClosed(panel);
// Note that we need to keep track of panel's collection since it will be
// gone once RemovePanel is called.
PanelCollection* collection = panel->collection();
collection->RemovePanel(panel, PanelCollection::PANEL_CLOSED);
// If only one panel is left in the stack, move it out of the stack.
// Also make sure that this detached panel will be expanded if not yet.
if (collection->type() == PanelCollection::STACKED) {
StackedPanelCollection* stack =
static_cast<StackedPanelCollection*>(collection);
DCHECK_GE(stack->num_panels(), 1);
if (stack->num_panels() == 1) {
Panel* top_panel = stack->top_panel();
MovePanelToCollection(top_panel,
detached_collection(),
PanelCollection::DEFAULT_POSITION);
if (top_panel->expansion_state() != Panel::EXPANDED)
top_panel->SetExpansionState(Panel::EXPANDED);
RemoveStack(stack);
}
}
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PANEL_CLOSED,
content::Source<Panel>(panel),
content::NotificationService::NoDetails());
}
StackedPanelCollection* PanelManager::CreateStack() {
StackedPanelCollection* stack = new StackedPanelCollection(this);
stacks_.push_back(stack);
return stack;
}
void PanelManager::RemoveStack(StackedPanelCollection* stack) {
DCHECK_EQ(0, stack->num_panels());
stacks_.remove(stack);
stack->CloseAll();
delete stack;
}
void PanelManager::StartDragging(Panel* panel,
const gfx::Point& mouse_location) {
drag_controller_->StartDragging(panel, mouse_location);
}
void PanelManager::Drag(const gfx::Point& mouse_location) {
drag_controller_->Drag(mouse_location);
}
void PanelManager::EndDragging(bool cancelled) {
drag_controller_->EndDragging(cancelled);
}
void PanelManager::StartResizingByMouse(Panel* panel,
const gfx::Point& mouse_location,
int component) {
if (panel->CanResizeByMouse() != panel::NOT_RESIZABLE &&
component != HTNOWHERE) {
resize_controller_->StartResizing(panel, mouse_location, component);
}
}
void PanelManager::ResizeByMouse(const gfx::Point& mouse_location) {
if (resize_controller_->IsResizing())
resize_controller_->Resize(mouse_location);
}
void PanelManager::EndResizingByMouse(bool cancelled) {
if (resize_controller_->IsResizing()) {
Panel* resized_panel = resize_controller_->EndResizing(cancelled);
if (!cancelled && resized_panel->collection())
resized_panel->collection()->RefreshLayout();
}
}
void PanelManager::OnPanelExpansionStateChanged(Panel* panel) {
panel->collection()->OnPanelExpansionStateChanged(panel);
}
void PanelManager::MovePanelToCollection(
Panel* panel,
PanelCollection* target_collection,
PanelCollection::PositioningMask positioning_mask) {
DCHECK(panel);
PanelCollection* current_collection = panel->collection();
DCHECK(current_collection);
DCHECK_NE(current_collection, target_collection);
current_collection->RemovePanel(panel,
PanelCollection::PANEL_CHANGED_COLLECTION);
target_collection->AddPanel(panel, positioning_mask);
target_collection->UpdatePanelOnCollectionChange(panel);
panel->SetAlwaysOnTop(target_collection->UsesAlwaysOnTopPanels());
}
bool PanelManager::ShouldBringUpTitlebars(int mouse_x, int mouse_y) const {
return docked_collection_->ShouldBringUpTitlebars(mouse_x, mouse_y);
}
void PanelManager::BringUpOrDownTitlebars(bool bring_up) {
docked_collection_->BringUpOrDownTitlebars(bring_up);
}
void PanelManager::CloseAll() {
DCHECK(!drag_controller_->is_dragging());
detached_collection_->CloseAll();
docked_collection_->CloseAll();
}
int PanelManager::num_panels() const {
int count = detached_collection_->num_panels() +
docked_collection_->num_panels();
for (Stacks::const_iterator iter = stacks_.begin();
iter != stacks_.end(); iter++)
count += (*iter)->num_panels();
return count;
}
std::vector<Panel*> PanelManager::panels() const {
std::vector<Panel*> panels;
for (DetachedPanelCollection::Panels::const_iterator iter =
detached_collection_->panels().begin();
iter != detached_collection_->panels().end(); ++iter)
panels.push_back(*iter);
for (DockedPanelCollection::Panels::const_iterator iter =
docked_collection_->panels().begin();
iter != docked_collection_->panels().end(); ++iter)
panels.push_back(*iter);
for (Stacks::const_iterator stack_iter = stacks_.begin();
stack_iter != stacks_.end(); stack_iter++) {
for (StackedPanelCollection::Panels::const_iterator iter =
(*stack_iter)->panels().begin();
iter != (*stack_iter)->panels().end(); ++iter) {
panels.push_back(*iter);
}
}
return panels;
}
std::vector<Panel*> PanelManager::GetDetachedAndStackedPanels() const {
std::vector<Panel*> panels;
for (DetachedPanelCollection::Panels::const_iterator iter =
detached_collection_->panels().begin();
iter != detached_collection_->panels().end(); ++iter)
panels.push_back(*iter);
for (Stacks::const_iterator stack_iter = stacks_.begin();
stack_iter != stacks_.end(); stack_iter++) {
for (StackedPanelCollection::Panels::const_iterator iter =
(*stack_iter)->panels().begin();
iter != (*stack_iter)->panels().end(); ++iter) {
panels.push_back(*iter);
}
}
return panels;
}
void PanelManager::SetMouseWatcher(PanelMouseWatcher* watcher) {
panel_mouse_watcher_.reset(watcher);
}
void PanelManager::OnPanelAnimationEnded(Panel* panel) {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PANEL_BOUNDS_ANIMATIONS_FINISHED,
content::Source<Panel>(panel),
content::NotificationService::NoDetails());
}
|