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

ash / system / power / power_button_menu_screen_view.h [blame]

// Copyright 2018 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_SYSTEM_POWER_POWER_BUTTON_MENU_SCREEN_VIEW_H_
#define ASH_SYSTEM_POWER_POWER_BUTTON_MENU_SCREEN_VIEW_H_

#include <unordered_map>

#include "ash/ash_export.h"
#include "ash/display/screen_orientation_controller.h"
#include "ash/system/power/power_button_controller.h"
#include "base/memory/raw_ptr.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/display/display_observer.h"
#include "ui/views/view.h"

namespace ash {
enum class ShutdownReason;
class PowerButtonMenuView;
class PowerButtonMenuCurtainView;

// PowerButtonMenuScreenView is the top-level view of power button menu UI. It
// creates a PowerButtonMenuBackgroundView to display the fullscreen background
// and a PowerButtonMenuView to display the menu.
class ASH_EXPORT PowerButtonMenuScreenView : public views::View,
                                             public display::DisplayObserver {
  METADATA_HEADER(PowerButtonMenuScreenView, views::View)

 public:
  // |show_animation_done| is a callback for when the animation that shows the
  // power menu has finished.
  PowerButtonMenuScreenView(
      ShutdownReason shutdown_reason,
      PowerButtonController::PowerButtonPosition power_button_position,
      double power_button_offset,
      base::RepeatingClosure show_animation_done);
  PowerButtonMenuScreenView(const PowerButtonMenuScreenView&) = delete;
  PowerButtonMenuScreenView& operator=(const PowerButtonMenuScreenView&) =
      delete;
  ~PowerButtonMenuScreenView() override;

  PowerButtonMenuView* power_button_menu_view() const {
    return power_button_menu_view_;
  }

  PowerButtonMenuCurtainView* power_button_menu_curtain_view() const {
    return power_button_menu_curtain_view_;
  }

  // Schedules an animation to show or hide the view.
  void ScheduleShowHideAnimation(bool show);

  // Resets the shield and menu's opacity to 0. Used when dismissing the menu
  // without animation to prepare for the next fade in animation.
  void ResetOpacity();

  // Called when the associated widget is shown. Updates power button related
  // info and calculates |menu_bounds_origins_| if needed. Recreates menu items.
  void OnWidgetShown(PowerButtonController::PowerButtonPosition position,
                     double offset_percentage);

 private:
  class PowerButtonMenuBackgroundView;
  friend class PowerButtonControllerTestApi;

  // views::View:
  void Layout(PassKey) override;
  bool OnMousePressed(const ui::MouseEvent& event) override;
  void OnMouseReleased(const ui::MouseEvent& event) override;
  bool AcceleratorPressed(const ui::Accelerator& accelerator) override;

  // ui::EventHandler:
  void OnGestureEvent(ui::GestureEvent* event) override;

  // display::DisplayObserver:
  void OnDisplayMetricsChanged(const display::Display& display,
                               uint32_t changed_metrics) override;

  // Lays out the view without animation transform.
  void LayoutWithoutTransform();

  // Helper methods for performing layout.
  void LayoutMenuView();
  void LayoutMenuCurtainView();

  // Updates |menu_bounds_origins_| according to power button position info.
  void UpdateMenuBoundsOrigins();

  // Gets the bounds of power button menu.
  gfx::Rect GetMenuBounds();

  gfx::Size GetMenuViewPreferredSize();

  PowerButtonMenuCurtainView* GetOrCreateCurtainView();

  ui::Layer* GetPowerButtonScreenBackgroundShieldLayerForTest() const;

  // Created by PowerButtonMenuScreenView. Owned by views hierarchy. Only
  // power_button_menu_view_ or power_button_menu_curtain_view_ will be
  // available at a time.
  raw_ptr<PowerButtonMenuView> power_button_menu_view_ = nullptr;
  raw_ptr<PowerButtonMenuCurtainView> power_button_menu_curtain_view_ = nullptr;

  raw_ptr<PowerButtonMenuBackgroundView>
      power_button_screen_background_shield_ = nullptr;

  // The physical display side of power button in landscape primary.
  PowerButtonController::PowerButtonPosition power_button_position_;

  // The center of the power button's offset from the top of the screen (for
  // left/right) or left side of the screen (for top/bottom) in
  // landscape_primary. Values are in [0.0, 1.0] and express a fraction of the
  // display's height or width, respectively.
  double power_button_offset_percentage_ = 0.f;

  // The origin of the menu bounds in different screen orientations.
  std::unordered_map<chromeos::OrientationType, gfx::Point>
      menu_bounds_origins_;

  display::ScopedDisplayObserver display_observer_{this};
};

}  // namespace ash

#endif  // ASH_SYSTEM_POWER_POWER_BUTTON_MENU_SCREEN_VIEW_H_