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

ash / wm / desks / desk_preview_view.h [blame]

// Copyright 2019 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_DESKS_DESK_PREVIEW_VIEW_H_
#define ASH_WM_DESKS_DESK_PREVIEW_VIEW_H_

#include <memory>
#include <optional>

#include "ash/ash_export.h"
#include "ash/style/system_shadow.h"
#include "ash/wm/desks/window_occlusion_calculator.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "ui/aura/window_occlusion_tracker.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/controls/button/button.h"

namespace ui {
class LayerTreeOwner;
}  // namespace ui

namespace ash {

class DeskMiniView;
class WallpaperBaseView;

// A view that shows the contents of the corresponding desk in its mini_view.
// This view has the following layer hierarchy:
//
//                +---------------------------+
//                |             <-------------+------  This view's layer.
//                +---------------------------+
//                       |               \  ----->>>>> Higher in Z-order.
//                       |                \
//                    +-----+               +-----+
//                    |     |               |     |
//                    +-----+               +-----+
//                       ^    \                ^
//                       |     \ +-----+       |
//                       |       |     |       |
//                       |       +-----+       |
//                       |          ^          |
//                       |          |   `highlight_overlay_`'s layer:
//                       |          |   A solid color layer that is
//                       |          |   visible when `mini_view_`'s
//                       |          |   `DeskActionContextMenu` is open.
//                       |          |
//                       |          |
//                       |    The root layer of the desk's mirrored
//                       |    contents layer tree. This tree is owned by
//                       |    `desk_mirrored_contents_layer_tree_owner_`.
//                       |
//                       |
//             `desk_mirrored_contents_view_`'s layer: Will be the
//              parent layer of the desk's contents mirrored layer tree.
//
// Note that `desk_mirrored_contents_view_` and `highlight_overlay_` paint to
// layers with rounded corners. In order to use the fast rounded corners
// implementation we must make them sibling layers, rather than one being a
// descendant of the other. Otherwise, this will trigger a render surface.
class ASH_EXPORT DeskPreviewView : public views::Button,
                                   public WindowOcclusionCalculator::Observer {
  METADATA_HEADER(DeskPreviewView, views::Button)

 public:
  DeskPreviewView(
      PressedCallback callback,
      DeskMiniView* mini_view,
      base::WeakPtr<WindowOcclusionCalculator> window_occlusion_calculator);

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

  ~DeskPreviewView() override;

  static constexpr SystemShadow::Type kDefaultShadowType =
      SystemShadow::Type::kElevation4;
  static constexpr SystemShadow::Type kDraggedShadowType =
      SystemShadow::Type::kElevation12;

  // Returns the height of the DeskPreviewView, which is a function of the
  // |root| window's height.
  static int GetHeight(aura::Window* root);

  SystemShadow* shadow() const { return shadow_.get(); }

  std::optional<ui::ColorId> focus_color_id() { return focus_color_id_; }
  void set_focus_color_id(std::optional<ui::ColorId> focus_color_id) {
    focus_color_id_ = focus_color_id;
  }

  // Sets the visibility of `highlight_overlay_` to `visible`. If `visible` is
  // true, this `DeskPreviewView` becomes highlighted.
  void SetHighlightOverlayVisibility(bool visible);

  // This should be called when there is a change in the desk contents so that
  // we can recreate the mirrored layer tree.
  void RecreateDeskContentsMirrorLayers();

  // Performs close action for this preview. when `primary_action` is true, it's
  // merge-desk action; otherwise it's close-all action.
  void Close(bool primary_action);

  // Performs swap action for this preview. When `right` is true, it swaps with
  // its right preview; otherwise it swaps with its left preview.
  void Swap(bool right);

  // Updates accessible name for this desk preview.
  void UpdateAccessibleName();

  // Called when the user exits overview by using 3-finger vertical trackpad
  // swipes.
  void AcceptSelection();

  // views::Button:
  void Layout(PassKey) override;
  bool OnMousePressed(const ui::MouseEvent& event) override;
  bool OnMouseDragged(const ui::MouseEvent& event) override;
  void OnMouseReleased(const ui::MouseEvent& event) override;
  void OnGestureEvent(ui::GestureEvent* event) override;
  void OnThemeChanged() override;
  void OnFocus() override;
  void OnBlur() override;
  void AboutToRequestFocusFromTabTraversal(bool reverse) override;
  bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
  bool CanHandleAccelerators() const override;

  // WindowOcclusionCalculator::Observer:
  void OnWindowOcclusionChanged(aura::Window* window) override;

 private:
  friend class DesksTestApi;

  const raw_ptr<DeskMiniView, LeakedDanglingUntriaged> mini_view_;
  const base::WeakPtr<WindowOcclusionCalculator> window_occlusion_calculator_;

  // A view that paints the wallpaper in the mini_view. It avoids the dimming
  // and blur overview mode adds to the original wallpaper. Owned by the views
  // hierarchy.
  using DeskWallpaperPreview = WallpaperBaseView;
  raw_ptr<DeskWallpaperPreview> wallpaper_preview_;

  // A view whose layer will act as the parent of desk's mirrored contents layer
  // tree. Owned by the views hierarchy.
  raw_ptr<views::View> desk_mirrored_contents_view_;

  // An overlay that becomes visible on top of the
  // `desk_mirrored_contents_view_` when the `mini_view_`'s
  // `DeskActionContextMenu` is active. Owned by the views hierarchy.
  raw_ptr<views::View> highlight_overlay_ = nullptr;

  // Owns the layer tree of the desk's contents mirrored layers.
  std::unique_ptr<ui::LayerTreeOwner> desk_mirrored_contents_layer_tree_owner_;

  // Forces the occlusion tracker to treat the associated desk's container
  // window to be visible (even though in reality it may not be when the desk is
  // inactive). This is needed since we may be mirroring the contents of an
  // inactive desk which contains a playing video, which would not show at all
  // in the mirrored contents if we didn't ask the occlusion tracker to consider
  // the desk container to be visible. We need a separate scoped force on the
  // floated window, if there is one, as it isn't parented to the desk
  // container.
  aura::WindowOcclusionTracker::ScopedForceVisible
      force_desk_occlusion_tracker_visible_;
  std::optional<aura::WindowOcclusionTracker::ScopedForceVisible>
      force_float_occlusion_tracker_visible_;

  std::unique_ptr<SystemShadow> shadow_;

  std::optional<ui::ColorId> focus_color_id_;

  base::WeakPtrFactory<DeskPreviewView> recreate_mirror_layers_weak_factory_{
      this};
};

}  // namespace ash

#endif  // ASH_WM_DESKS_DESK_PREVIEW_VIEW_H_