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

ash / wm / splitview / auto_snap_controller.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_WM_SPLITVIEW_AUTO_SNAP_CONTROLLER_H_
#define ASH_WM_SPLITVIEW_AUTO_SNAP_CONTROLLER_H_

#include "base/scoped_multi_source_observation.h"
#include "ui/aura/window_observer.h"
#include "ui/wm/public/activation_change_observer.h"

namespace ash {

// The controller that observes the window state and performs auto snapping
// for the window if needed. When it's created, it observes the root window
// and all windows in a current active desk. When 1) an observed window is
// activated or 2) changed to visible from minimized, this class performs
// auto snapping for the window if it's possible.
class AutoSnapController : public wm::ActivationChangeObserver,
                           public aura::WindowObserver {
 public:
  explicit AutoSnapController(aura::Window* root_window);

  AutoSnapController(const AutoSnapController&) = delete;
  AutoSnapController& operator=(const AutoSnapController&) = delete;

  ~AutoSnapController() override;

  // Called by `OverviewSession` when the `gained_active` window is being
  // activated. Returns true if `gained_active` was snapped, false otherwise.
  bool OnWindowActivatingFromOverview(ActivationReason reason,
                                      aura::Window* gained_active);

  // wm::ActivationChangeObserver:
  void OnWindowActivating(ActivationReason reason,
                          aura::Window* gained_active,
                          aura::Window* lost_active) override;
  void OnWindowActivated(ActivationReason reason,
                         aura::Window* gained_active,
                         aura::Window* lost_active) override {}

  // aura::WindowObserver:
  void OnWindowVisibilityChanging(aura::Window* window, bool visible) override;
  void OnWindowAddedToRootWindow(aura::Window* window) override;
  void OnWindowRemovingFromRootWindow(aura::Window* window,
                                      aura::Window* new_root) override;
  void OnWindowDestroying(aura::Window* window) override;

 private:
  // Auto-snaps `window` in split view upon gaining active or becoming visible.
  // Returns true if `window` was snapped, false otherwise.
  bool AutoSnapWindowIfNeeded(aura::Window* window);

  void AddWindow(aura::Window* window);
  void RemoveWindow(aura::Window* window);

  raw_ptr<aura::Window> root_window_;

  // Tracks observed windows.
  base::ScopedMultiSourceObservation<aura::Window, aura::WindowObserver>
      window_observations_{this};
};

}  // namespace ash

#endif  // ASH_WM_SPLITVIEW_AUTO_SNAP_CONTROLLER_H_