window_cycle_list.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_WM_WINDOW_CYCLE_WINDOW_CYCLE_LIST_H_
  5. #define ASH_WM_WINDOW_CYCLE_WINDOW_CYCLE_LIST_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "ash/ash_export.h"
  9. #include "ash/wm/window_cycle/window_cycle_controller.h"
  10. #include "ash/wm/window_cycle/window_cycle_tab_slider.h"
  11. #include "ash/wm/window_cycle/window_cycle_view.h"
  12. #include "base/timer/timer.h"
  13. #include "ui/aura/window_observer.h"
  14. #include "ui/display/display_observer.h"
  15. #include "ui/display/screen.h"
  16. #include "ui/views/controls/label.h"
  17. #include "ui/views/view.h"
  18. namespace aura {
  19. class ScopedWindowTargeter;
  20. class Window;
  21. } // namespace aura
  22. namespace views {
  23. class Widget;
  24. }
  25. namespace ash {
  26. // Tracks a set of Windows that can be stepped through. This class is used by
  27. // the WindowCycleController.
  28. class ASH_EXPORT WindowCycleList : public aura::WindowObserver,
  29. public display::DisplayObserver {
  30. public:
  31. using WindowList = std::vector<aura::Window*>;
  32. explicit WindowCycleList(const WindowList& windows);
  33. WindowCycleList(const WindowCycleList&) = delete;
  34. WindowCycleList& operator=(const WindowCycleList&) = delete;
  35. ~WindowCycleList() override;
  36. const WindowCycleView* cycle_view() const { return cycle_view_; }
  37. // Returns the |target_window_| from |cycle_view_|.
  38. aura::Window* GetTargetWindow();
  39. // Removes the existing windows and replaces them with |windows|. If
  40. // |windows| is empty, cancels cycling.
  41. void ReplaceWindows(const WindowList& windows);
  42. // Cycles to the next or previous window based on |direction| or to the
  43. // default position if |starting_alt_tab_or_switching_mode| is true.
  44. // This moves the focus ring and also scrolls the list.
  45. // If |starting_alt_tab_or_switching_mode| is true and |direction| is
  46. // forward, the highlight moves to the first non-active window in MRU list:
  47. // the second window by default or the first window if it is not active.
  48. void Step(WindowCycleController::WindowCyclingDirection direction,
  49. bool starting_alt_tab_or_switching_mode);
  50. // Should be called when a user drags their finger on the touch screen.
  51. // Translates the mirror container by |delta_x|.
  52. void Drag(float delta_x);
  53. // Beings a fling with initial velocity of |velocity_x|.
  54. void StartFling(float velocity_x);
  55. // Moves the focus ring to the respective preview for |window|. Does not
  56. // scroll the window cycle list.
  57. void SetFocusedWindow(aura::Window* window);
  58. // Moves the focus to the tab slider or the window cycle list based on
  59. // |focus| value during keyboard navigation.
  60. void SetFocusTabSlider(bool focus);
  61. // Returns true if during keyboard navigation, alt-tab focuses the tab slider
  62. // instead of cycle window.
  63. bool IsTabSliderFocused();
  64. // Checks whether |event| occurs within the cycle view. Returns false if
  65. // |cycle_view_| does not exist.
  66. bool IsEventInCycleView(const ui::LocatedEvent* event);
  67. // Returns the window for the preview item located at |event|. Returns nullptr
  68. // if |event| not in cycle view or if |cycle_view_| does not exist.
  69. aura::Window* GetWindowAtPoint(const ui::LocatedEvent* event);
  70. // Returns whether or not the event is located in tab slider container.
  71. bool IsEventInTabSliderContainer(const ui::LocatedEvent* event);
  72. // Returns true if the window list overlay should be shown.
  73. bool ShouldShowUi();
  74. // Updates the tab slider mode UI when alt-tab mode in user prefs changes.
  75. void OnModePrefsChanged();
  76. void set_user_did_accept(bool user_did_accept) {
  77. user_did_accept_ = user_did_accept;
  78. }
  79. static void SetDisableInitialDelayForTesting(bool disabled);
  80. private:
  81. friend class ModeSelectionWindowCycleControllerTest;
  82. friend class MultiUserWindowCycleControllerTest;
  83. friend class WindowCycleListTestApi;
  84. friend class WindowCycleControllerTest;
  85. // aura::WindowObserver:
  86. // There is a chance a window is destroyed, for example by JS code. We need to
  87. // take care of that even if it is not intended for the user to close a window
  88. // while window cycling.
  89. void OnWindowDestroying(aura::Window* window) override;
  90. // display::DisplayObserver:
  91. void OnDisplayMetricsChanged(const display::Display& display,
  92. uint32_t changed_metrics) override;
  93. // Removes all windows from the window list. Also removes the windows from
  94. // |cycle_view_| if |cycle_view_| exists.
  95. void RemoveAllWindows();
  96. // Initializes and shows |cycle_view_|.
  97. void InitWindowCycleView();
  98. // Selects a window, which either activates it or expands it in the case of
  99. // PIP.
  100. void SelectWindow(aura::Window* window);
  101. // Scrolls windows by |offset|. Does not move the focus ring. If you want to
  102. // scroll the list and move the focus ring in one animation, call
  103. // SetFocusedWindow() before this.
  104. void Scroll(int offset);
  105. // Returns the index for the window |offset| away from |current_index_|. Can
  106. // only be called if |windows_| is not empty. Also checks that the window for
  107. // the returned index exists.
  108. int GetOffsettedWindowIndex(int offset) const;
  109. // Returns the index for |window| in |windows_|. |window| must be in
  110. // |windows_|.
  111. int GetIndexOfWindow(aura::Window* window) const;
  112. // Returns the number of windows in the window cycle list for all desks.
  113. int GetNumberOfWindowsAllDesks() const;
  114. // List of weak pointers to windows to use while cycling with the keyboard.
  115. // List is built when the user initiates the gesture (i.e. hits alt-tab the
  116. // first time) and is emptied when the gesture is complete (i.e. releases the
  117. // alt key).
  118. WindowList windows_;
  119. // Current position in the |windows_|. Can be used to query selection depth,
  120. // i.e., the position of an active window in a global MRU ordering.
  121. int current_index_ = 0;
  122. // True if the user accepted the window switch (as opposed to cancelling or
  123. // interrupting the interaction).
  124. bool user_did_accept_ = false;
  125. // True if one of the windows in the list has already been selected.
  126. bool window_selected_ = false;
  127. // The top level View for the window cycle UI. May be null if the UI is not
  128. // showing.
  129. WindowCycleView* cycle_view_ = nullptr;
  130. // The widget that hosts the window cycle UI.
  131. views::Widget* cycle_ui_widget_ = nullptr;
  132. // The window list will dismiss if the display metrics change.
  133. display::ScopedDisplayObserver display_observer_{this};
  134. // A timer to delay showing the UI. Quick Alt+Tab should not flash a UI.
  135. base::OneShotTimer show_ui_timer_;
  136. // This is needed so that it won't leak keyboard events even if the widget is
  137. // not activatable.
  138. std::unique_ptr<aura::ScopedWindowTargeter> window_targeter_;
  139. // Tracks what window was active when starting to cycle and used to determine
  140. // if alt-tab should highlight the first or the second window in the list.
  141. aura::Window* active_window_before_window_cycle_ = nullptr;
  142. };
  143. } // namespace ash
  144. #endif // ASH_WM_WINDOW_CYCLE_WINDOW_CYCLE_LIST_H_