Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: ash/wm/overview/window_selector.cc

Issue 2918403006: CrOS Tablet Window management - Split Screen part I (Closed)
Patch Set: Add unittests. Will split the CL into two CLs. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/wm/overview/window_selector.h ('k') | ash/wm/overview/window_selector_controller.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "ash/wm/overview/window_selector.h" 5 #include "ash/wm/overview/window_selector.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "ash/accessibility_delegate.h" 13 #include "ash/accessibility_delegate.h"
14 #include "ash/accessibility_types.h" 14 #include "ash/accessibility_types.h"
15 #include "ash/metrics/user_metrics_action.h" 15 #include "ash/metrics/user_metrics_action.h"
16 #include "ash/metrics/user_metrics_recorder.h" 16 #include "ash/metrics/user_metrics_recorder.h"
17 #include "ash/public/cpp/shell_window_ids.h" 17 #include "ash/public/cpp/shell_window_ids.h"
18 #include "ash/root_window_controller.h" 18 #include "ash/root_window_controller.h"
19 #include "ash/screen_util.h" 19 #include "ash/screen_util.h"
20 #include "ash/shelf/shelf.h" 20 #include "ash/shelf/shelf.h"
21 #include "ash/shell.h" 21 #include "ash/shell.h"
22 #include "ash/wm/mru_window_tracker.h" 22 #include "ash/wm/mru_window_tracker.h"
23 #include "ash/wm/overview/overview_window_drag_controller.h"
23 #include "ash/wm/overview/window_grid.h" 24 #include "ash/wm/overview/window_grid.h"
24 #include "ash/wm/overview/window_selector_delegate.h" 25 #include "ash/wm/overview/window_selector_delegate.h"
25 #include "ash/wm/overview/window_selector_item.h" 26 #include "ash/wm/overview/window_selector_item.h"
26 #include "ash/wm/panels/panel_layout_manager.h" 27 #include "ash/wm/panels/panel_layout_manager.h"
27 #include "ash/wm/switchable_windows.h" 28 #include "ash/wm/switchable_windows.h"
28 #include "ash/wm/window_state.h" 29 #include "ash/wm/window_state.h"
29 #include "ash/wm/window_util.h" 30 #include "ash/wm/window_util.h"
30 #include "base/auto_reset.h" 31 #include "base/auto_reset.h"
31 #include "base/command_line.h" 32 #include "base/command_line.h"
32 #include "base/metrics/histogram_macros.h" 33 #include "base/metrics/histogram_macros.h"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 132
132 DISALLOW_COPY_AND_ASSIGN(RoundedContainerView); 133 DISALLOW_COPY_AND_ASSIGN(RoundedContainerView);
133 }; 134 };
134 135
135 // Triggers a shelf visibility update on all root window controllers. 136 // Triggers a shelf visibility update on all root window controllers.
136 void UpdateShelfVisibility() { 137 void UpdateShelfVisibility() {
137 for (aura::Window* root : Shell::GetAllRootWindows()) 138 for (aura::Window* root : Shell::GetAllRootWindows())
138 Shelf::ForWindow(root)->UpdateVisibilityState(); 139 Shelf::ForWindow(root)->UpdateVisibilityState();
139 } 140 }
140 141
142 // Returns the bounds for the overview window grid according to the split view
143 // state. If split view mode is active, the overview window should open on the
144 // opposite side of the default snap window.
145 gfx::Rect GetGridBoundsInScreen(aura::Window* root_window) {
146 SplitViewController* split_view_controller =
147 Shell::Get()->split_view_controller();
148 if (Shell::Get()->IsSplitViewModeActive()) {
149 SplitViewController::State state = split_view_controller->state();
150 SplitViewController::State oppsite_state =
151 (state == SplitViewController::LEFT_SNAPPED)
152 ? SplitViewController::RIGHT_SNAPPED
153 : SplitViewController::LEFT_SNAPPED;
154 return split_view_controller->GetSnappedWindowBoundsInScreen(root_window,
155 oppsite_state);
156 } else {
157 return split_view_controller->GetDisplayWorkAreaBoundsInScreen(root_window);
158 }
159 }
160
141 gfx::Rect GetTextFilterPosition(aura::Window* root_window) { 161 gfx::Rect GetTextFilterPosition(aura::Window* root_window) {
142 gfx::Rect total_bounds = ScreenUtil::GetDisplayWorkAreaBoundsInParent( 162 const gfx::Rect total_bounds = GetGridBoundsInScreen(root_window);
143 root_window->GetChildById(kShellWindowId_DefaultContainer));
144 ::wm::ConvertRectToScreen(root_window, &total_bounds);
145 return gfx::Rect( 163 return gfx::Rect(
146 0.5 * (total_bounds.width() - 164 total_bounds.x() +
147 std::min(kTextFilterWidth, total_bounds.width())), 165 0.5 * (total_bounds.width() -
166 std::min(kTextFilterWidth, total_bounds.width())),
148 total_bounds.y() + total_bounds.height() * kTextFilterTopScreenProportion, 167 total_bounds.y() + total_bounds.height() * kTextFilterTopScreenProportion,
149 std::min(kTextFilterWidth, total_bounds.width()), kTextFilterHeight); 168 std::min(kTextFilterWidth, total_bounds.width()), kTextFilterHeight);
150 } 169 }
151 170
152 // Initializes the text filter on the top of the main root window and requests 171 // Initializes the text filter on the top of the main root window and requests
153 // focus on its textfield. Uses |image| to place an icon to the left of the text 172 // focus on its textfield. Uses |image| to place an icon to the left of the text
154 // field. 173 // field.
155 views::Widget* CreateTextFilter(views::TextfieldController* controller, 174 views::Widget* CreateTextFilter(views::TextfieldController* controller,
156 aura::Window* root_window, 175 aura::Window* root_window,
157 const gfx::ImageSkia& image, 176 const gfx::ImageSkia& image,
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 aura::Window* container = 283 aura::Window* container =
265 root->GetChildById(wm::kSwitchableWindowContainerIds[i]); 284 root->GetChildById(wm::kSwitchableWindowContainerIds[i]);
266 container->AddObserver(this); 285 container->AddObserver(this);
267 observed_windows_.insert(container); 286 observed_windows_.insert(container);
268 } 287 }
269 288
270 // Hide the callout widgets for panels. It is safe to call this for 289 // Hide the callout widgets for panels. It is safe to call this for
271 // root windows that don't contain any panel windows. 290 // root windows that don't contain any panel windows.
272 PanelLayoutManager::Get(root)->SetShowCalloutWidgets(false); 291 PanelLayoutManager::Get(root)->SetShowCalloutWidgets(false);
273 292
274 std::unique_ptr<WindowGrid> grid(new WindowGrid(root, windows, this)); 293 std::unique_ptr<WindowGrid> grid(
294 new WindowGrid(root, windows, this, GetGridBoundsInScreen(root)));
275 if (grid->empty()) 295 if (grid->empty())
276 continue; 296 continue;
277 num_items_ += grid->size(); 297 num_items_ += grid->size();
278 grid_list_.push_back(std::move(grid)); 298 grid_list_.push_back(std::move(grid));
279 } 299 }
280 300
281 { 301 {
282 // The calls to WindowGrid::PrepareForOverview() and CreateTextFilter(...) 302 // The calls to WindowGrid::PrepareForOverview() and CreateTextFilter(...)
283 // requires some LayoutManagers (ie PanelLayoutManager) to perform layouts 303 // requires some LayoutManagers (ie PanelLayoutManager) to perform layouts
284 // so that windows are correctly visible and properly animated in overview 304 // so that windows are correctly visible and properly animated in overview
(...skipping 16 matching lines...) Expand all
301 kTextFilterIconColor); 321 kTextFilterIconColor);
302 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 322 aura::Window* root_window = Shell::GetPrimaryRootWindow();
303 text_filter_widget_.reset(CreateTextFilter(this, root_window, search_image_, 323 text_filter_widget_.reset(CreateTextFilter(this, root_window, search_image_,
304 &text_filter_bottom_)); 324 &text_filter_bottom_));
305 } 325 }
306 326
307 DCHECK(!grid_list_.empty()); 327 DCHECK(!grid_list_.empty());
308 UMA_HISTOGRAM_COUNTS_100("Ash.WindowSelector.Items", num_items_); 328 UMA_HISTOGRAM_COUNTS_100("Ash.WindowSelector.Items", num_items_);
309 329
310 Shell::Get()->activation_client()->AddObserver(this); 330 Shell::Get()->activation_client()->AddObserver(this);
331 Shell::Get()->split_view_controller()->AddObserver(this);
311 332
312 display::Screen::GetScreen()->AddObserver(this); 333 display::Screen::GetScreen()->AddObserver(this);
313 Shell::Get()->metrics()->RecordUserMetricsAction(UMA_WINDOW_OVERVIEW); 334 Shell::Get()->metrics()->RecordUserMetricsAction(UMA_WINDOW_OVERVIEW);
314 // Send an a11y alert. 335 // Send an a11y alert.
315 Shell::Get()->accessibility_delegate()->TriggerAccessibilityAlert( 336 Shell::Get()->accessibility_delegate()->TriggerAccessibilityAlert(
316 A11Y_ALERT_WINDOW_OVERVIEW_MODE_ENTERED); 337 A11Y_ALERT_WINDOW_OVERVIEW_MODE_ENTERED);
317 338
318 UpdateShelfVisibility(); 339 UpdateShelfVisibility();
319 } 340 }
320 341
321 // NOTE: The work done in Shutdown() is not done in the destructor because it 342 // NOTE: The work done in Shutdown() is not done in the destructor because it
322 // may cause other, unrelated classes, (ie PanelLayoutManager) to make indirect 343 // may cause other, unrelated classes, (ie PanelLayoutManager) to make indirect
323 // calls to restoring_minimized_windows() on a partially destructed object. 344 // calls to restoring_minimized_windows() on a partially destructed object.
324 void WindowSelector::Shutdown() { 345 void WindowSelector::Shutdown() {
325 is_shut_down_ = true; 346 is_shut_down_ = true;
326 // Stop observing screen metrics changes first to avoid auto-positioning 347 // Stop observing screen metrics changes first to avoid auto-positioning
327 // windows in response to work area changes from window activation. 348 // windows in response to work area changes from window activation.
328 display::Screen::GetScreen()->RemoveObserver(this); 349 display::Screen::GetScreen()->RemoveObserver(this);
329 350
351 // Stop observing split view state changes before restoring window focus.
352 // Otherwise the activation of the window triggers OnSplitViewStateChanged()
353 // that will call into this function again.
354 Shell::Get()->split_view_controller()->RemoveObserver(this);
355
330 size_t remaining_items = 0; 356 size_t remaining_items = 0;
331 for (std::unique_ptr<WindowGrid>& window_grid : grid_list_) { 357 for (std::unique_ptr<WindowGrid>& window_grid : grid_list_) {
332 for (const auto& window_selector_item : window_grid->window_list()) 358 for (const auto& window_selector_item : window_grid->window_list())
333 window_selector_item->RestoreWindow(); 359 window_selector_item->RestoreWindow();
334 remaining_items += window_grid->size(); 360 remaining_items += window_grid->size();
335 } 361 }
336 362
337 // Setting focus after restoring windows' state avoids unnecessary animations. 363 // Setting focus after restoring windows' state avoids unnecessary animations.
338 ResetFocusRestoreWindow(true); 364 ResetFocusRestoreWindow(true);
339 RemoveAllObservers(); 365 RemoveAllObservers();
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 } 465 }
440 } 466 }
441 item->EnsureVisible(); 467 item->EnsureVisible();
442 wm::GetWindowState(window)->Activate(); 468 wm::GetWindowState(window)->Activate();
443 } 469 }
444 470
445 void WindowSelector::WindowClosing(WindowSelectorItem* window) { 471 void WindowSelector::WindowClosing(WindowSelectorItem* window) {
446 grid_list_[selected_grid_index_]->WindowClosing(window); 472 grid_list_[selected_grid_index_]->WindowClosing(window);
447 } 473 }
448 474
475 void WindowSelector::SetBoundsForWindowGridsInScreen(const gfx::Rect& bounds) {
476 for (std::unique_ptr<WindowGrid>& grid : grid_list_)
477 grid->SetBoundsAndUpdatePositions(bounds);
478 }
479
480 void WindowSelector::RemoveWindowSelectorItem(WindowSelectorItem* item) {
481 if (item->GetWindow()->HasObserver(this)) {
482 item->GetWindow()->RemoveObserver(this);
483 observed_windows_.erase(item->GetWindow());
484 if (item->GetWindow() == restore_focus_window_)
485 restore_focus_window_ = nullptr;
486 }
487
488 // Remove |item| from the corresponding grid.
489 for (std::unique_ptr<WindowGrid>& grid : grid_list_) {
490 if (grid->Contains(item->GetWindow())) {
491 grid->RemoveItem(item);
492 break;
493 }
494 }
495 }
496
497 void WindowSelector::InitiateDrag(WindowSelectorItem* item,
498 const gfx::Point& location_in_screen) {
499 window_drag_controller_.reset(new OverviewWindowDragController(this));
500 window_drag_controller_->InitiateDrag(item, location_in_screen);
501 }
502
503 void WindowSelector::Drag(WindowSelectorItem* item,
504 const gfx::Point& location_in_screen) {
505 DCHECK(window_drag_controller_.get());
506 DCHECK_EQ(item, window_drag_controller_->item());
507 window_drag_controller_->Drag(location_in_screen);
508 }
509
510 void WindowSelector::CompleteDrag(WindowSelectorItem* item) {
511 DCHECK(window_drag_controller_.get());
512 DCHECK_EQ(item, window_drag_controller_->item());
513 window_drag_controller_->CompleteDrag();
514 }
515
516 void WindowSelector::PositionWindows(bool animate) {
517 for (std::unique_ptr<WindowGrid>& grid : grid_list_)
518 grid->PositionWindows(animate);
519 }
520
449 bool WindowSelector::HandleKeyEvent(views::Textfield* sender, 521 bool WindowSelector::HandleKeyEvent(views::Textfield* sender,
450 const ui::KeyEvent& key_event) { 522 const ui::KeyEvent& key_event) {
451 if (key_event.type() != ui::ET_KEY_PRESSED) 523 if (key_event.type() != ui::ET_KEY_PRESSED)
452 return false; 524 return false;
453 525
454 switch (key_event.key_code()) { 526 switch (key_event.key_code()) {
455 case ui::VKEY_ESCAPE: 527 case ui::VKEY_ESCAPE:
456 CancelSelection(); 528 CancelSelection();
457 break; 529 break;
458 case ui::VKEY_UP: 530 case ui::VKEY_UP:
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 579
508 void WindowSelector::OnDisplayAdded(const display::Display& display) {} 580 void WindowSelector::OnDisplayAdded(const display::Display& display) {}
509 581
510 void WindowSelector::OnDisplayRemoved(const display::Display& display) { 582 void WindowSelector::OnDisplayRemoved(const display::Display& display) {
511 // TODO(flackr): Keep window selection active on remaining displays. 583 // TODO(flackr): Keep window selection active on remaining displays.
512 CancelSelection(); 584 CancelSelection();
513 } 585 }
514 586
515 void WindowSelector::OnDisplayMetricsChanged(const display::Display& display, 587 void WindowSelector::OnDisplayMetricsChanged(const display::Display& display,
516 uint32_t metrics) { 588 uint32_t metrics) {
589 // Re-calculate the bounds for the window grids and position all the windows.
590 for (std::unique_ptr<WindowGrid>& grid : grid_list_) {
591 SetBoundsForWindowGridsInScreen(
592 GetGridBoundsInScreen(const_cast<aura::Window*>(grid->root_window())));
593 }
517 PositionWindows(/* animate */ false); 594 PositionWindows(/* animate */ false);
518 RepositionTextFilterOnDisplayMetricsChange(); 595 RepositionTextFilterOnDisplayMetricsChange();
519 } 596 }
520 597
521 void WindowSelector::OnWindowHierarchyChanged( 598 void WindowSelector::OnWindowHierarchyChanged(
522 const HierarchyChangeParams& params) { 599 const HierarchyChangeParams& params) {
523 // Only care about newly added children of |observed_windows_|. 600 // Only care about newly added children of |observed_windows_|.
524 if (!observed_windows_.count(params.receiver) || 601 if (!observed_windows_.count(params.receiver) ||
525 !observed_windows_.count(params.new_parent)) { 602 !observed_windows_.count(params.new_parent)) {
526 return; 603 return;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 windows.begin(), windows.end(), 646 windows.begin(), windows.end(),
570 [gained_active](const std::unique_ptr<WindowSelectorItem>& window) { 647 [gained_active](const std::unique_ptr<WindowSelectorItem>& window) {
571 return window->Contains(gained_active); 648 return window->Contains(gained_active);
572 }); 649 });
573 650
574 if (iter == windows.end() && showing_text_filter_ && 651 if (iter == windows.end() && showing_text_filter_ &&
575 lost_active == GetTextFilterWidgetWindow()) { 652 lost_active == GetTextFilterWidgetWindow()) {
576 return; 653 return;
577 } 654 }
578 655
656 // Do not cancel the overview mode if the window activation was caused by
657 // snapping window to one side of the screen.
658 if (Shell::Get()->IsSplitViewModeActive())
659 return;
660
579 // Don't restore focus on exit if a window was just activated. 661 // Don't restore focus on exit if a window was just activated.
580 ResetFocusRestoreWindow(false); 662 ResetFocusRestoreWindow(false);
581 CancelSelection(); 663 CancelSelection();
582 } 664 }
583 665
584 void WindowSelector::OnAttemptToReactivateWindow(aura::Window* request_active, 666 void WindowSelector::OnAttemptToReactivateWindow(aura::Window* request_active,
585 aura::Window* actual_active) { 667 aura::Window* actual_active) {
586 OnWindowActivated(ActivationReason::ACTIVATION_CLIENT, request_active, 668 OnWindowActivated(ActivationReason::ACTIVATION_CLIENT, request_active,
587 actual_active); 669 actual_active);
588 } 670 }
(...skipping 30 matching lines...) Expand all
619 for (auto iter = grid_list_.begin(); iter != grid_list_.end(); iter++) 701 for (auto iter = grid_list_.begin(); iter != grid_list_.end(); iter++)
620 (*iter)->FilterItems(new_contents); 702 (*iter)->FilterItems(new_contents);
621 703
622 // If the selection widget is not active, execute a Move() command so that it 704 // If the selection widget is not active, execute a Move() command so that it
623 // shows up on the first undimmed item. 705 // shows up on the first undimmed item.
624 if (grid_list_[selected_grid_index_]->is_selecting()) 706 if (grid_list_[selected_grid_index_]->is_selecting())
625 return; 707 return;
626 Move(WindowSelector::RIGHT, false); 708 Move(WindowSelector::RIGHT, false);
627 } 709 }
628 710
711 void WindowSelector::OnSplitViewStateChanged(
712 SplitViewController::State previous_state,
713 SplitViewController::State state) {
714 if (state != SplitViewController::NOSNAP) {
715 // Do not restore focus if a window was just snapped and activated.
716 ResetFocusRestoreWindow(false);
717 }
718
719 if (state == SplitViewController::LEFT_SNAPPED) {
720 aura::Window* snapped_window =
721 Shell::Get()->split_view_controller()->left_window();
722 const gfx::Rect bounds_in_screen =
723 Shell::Get()->split_view_controller()->GetSnappedWindowBoundsInScreen(
724 snapped_window, SplitViewController::RIGHT_SNAPPED);
725 SetBoundsForWindowGridsInScreen(bounds_in_screen);
726 } else if (state == SplitViewController::RIGHT_SNAPPED) {
727 aura::Window* snapped_window =
728 Shell::Get()->split_view_controller()->right_window();
729 const gfx::Rect bounds_in_screen =
730 Shell::Get()->split_view_controller()->GetSnappedWindowBoundsInScreen(
731 snapped_window, SplitViewController::LEFT_SNAPPED);
732 SetBoundsForWindowGridsInScreen(bounds_in_screen);
733 } else if (state == SplitViewController::BOTH_SNAPPED) {
734 CancelSelection();
735 } else {
736 DCHECK(state == SplitViewController::BOTH_SNAPPED ||
737 state == SplitViewController::NOSNAP);
738 // If two windows were snapped to both sides of the screen, end overview
739 // mode. If split view mode was ended (e.g., one of the snapped window was
740 // closed or minimized / fullscreened / maximized), also end overview mode
741 // if overview mode is active.
742 CancelSelection();
743 }
744 }
745
629 aura::Window* WindowSelector::GetTextFilterWidgetWindow() { 746 aura::Window* WindowSelector::GetTextFilterWidgetWindow() {
630 return text_filter_widget_->GetNativeWindow(); 747 return text_filter_widget_->GetNativeWindow();
631 } 748 }
632 749
633 void WindowSelector::PositionWindows(bool animate) {
634 for (std::unique_ptr<WindowGrid>& grid : grid_list_)
635 grid->PositionWindows(animate);
636 }
637
638 void WindowSelector::RepositionTextFilterOnDisplayMetricsChange() { 750 void WindowSelector::RepositionTextFilterOnDisplayMetricsChange() {
639 const gfx::Rect rect = GetTextFilterPosition(Shell::GetPrimaryRootWindow()); 751 const gfx::Rect rect = GetTextFilterPosition(Shell::GetPrimaryRootWindow());
640 text_filter_bottom_ = rect.bottom() + kTextFieldBottomMargin; 752 text_filter_bottom_ = rect.bottom() + kTextFieldBottomMargin;
641 text_filter_widget_->SetBounds(rect); 753 text_filter_widget_->SetBounds(rect);
642 754
643 gfx::Transform transform; 755 gfx::Transform transform;
644 transform.Translate( 756 transform.Translate(
645 0, text_filter_string_length_ == 0 ? -text_filter_bottom_ : 0); 757 0, text_filter_string_length_ == 0 ? -text_filter_bottom_ : 0);
646 aura::Window* text_filter_window = GetTextFilterWidgetWindow(); 758 aura::Window* text_filter_window = GetTextFilterWidgetWindow();
647 text_filter_window->layer()->SetOpacity(text_filter_string_length_ == 0 ? 0 759 text_filter_window->layer()->SetOpacity(text_filter_string_length_ == 0 ? 0
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 for (size_t i = 0; i <= grid_list_.size() && 793 for (size_t i = 0; i <= grid_list_.size() &&
682 grid_list_[selected_grid_index_]->Move(direction, animate); 794 grid_list_[selected_grid_index_]->Move(direction, animate);
683 i++) { 795 i++) {
684 selected_grid_index_ = 796 selected_grid_index_ =
685 (selected_grid_index_ + display_direction + grid_list_.size()) % 797 (selected_grid_index_ + display_direction + grid_list_.size()) %
686 grid_list_.size(); 798 grid_list_.size();
687 } 799 }
688 } 800 }
689 801
690 } // namespace ash 802 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/overview/window_selector.h ('k') | ash/wm/overview/window_selector_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698