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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
// 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.
#include "ash/system/power/power_button_menu_screen_view.h"
#include <utility>
#include "ash/curtain/security_curtain_controller.h"
#include "ash/shell.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/power/power_button_menu_curtain_view.h"
#include "ash/system/power/power_button_menu_metrics_type.h"
#include "ash/system/power/power_button_menu_view.h"
#include "ash/system/power/power_button_menu_view_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
// Opacity of the power button menu fullscreen background shield.
constexpr float kPowerButtonMenuOpacity = 0.4f;
// TODO(minch): Get the internal display size instead if needed.
// Gets the landscape size of the primary display. For landscape orientation,
// the width is always larger than height.
gfx::Size GetPrimaryDisplayLandscapeSize() {
gfx::Rect bounds = display::Screen::GetScreen()->GetPrimaryDisplay().bounds();
return gfx::Size(std::max(bounds.width(), bounds.height()),
std::min(bounds.width(), bounds.height()));
}
// Adjust the menu's |actual_position| to be at least kMenuTransformDistanceDp
// from the edge of the display. |menu_size| means the width or height of the
// menu and |actual_position| is x-coordinate or y-coordinate of the menu.
// |display_edge| is the width or height of the display in landscape_primary
// orientation depending on the power button's posotion.
int AdjustMenuEdgeForDisplaySize(int actual_position,
int display_edge,
int menu_size) {
return std::min(
display_edge - kPowerButtonMenuTransformDistanceDp - menu_size,
std::max(kPowerButtonMenuTransformDistanceDp, actual_position));
}
bool IsCurtainModeEnabled() {
return ash::Shell::Get()->security_curtain_controller().IsEnabled();
}
} // namespace
using PowerButtonPosition = PowerButtonController::PowerButtonPosition;
using TransformDirection = PowerButtonMenuView::TransformDirection;
class PowerButtonMenuScreenView::PowerButtonMenuBackgroundView
: public views::View,
public ui::ImplicitAnimationObserver {
METADATA_HEADER(PowerButtonMenuBackgroundView, views::View)
public:
explicit PowerButtonMenuBackgroundView(
base::RepeatingClosure show_animation_done)
: show_animation_done_(show_animation_done) {
SetPaintToLayer(ui::LAYER_SOLID_COLOR);
layer()->SetOpacity(0.f);
}
PowerButtonMenuBackgroundView(const PowerButtonMenuBackgroundView&) = delete;
PowerButtonMenuBackgroundView& operator=(
const PowerButtonMenuBackgroundView&) = delete;
~PowerButtonMenuBackgroundView() override = default;
void OnImplicitAnimationsCompleted() override {
// If animation was aborted and opacity is currently 0, we could get left
// in an inconsistent state where we're animating to nonzero opacity but
// the layer has been set invisible. Only act on completed animations.
if (!WasAnimationCompletedForProperty(
ui::LayerAnimationElement::AnimatableProperty::OPACITY)) {
return;
}
PowerButtonController* power_button_controller =
Shell::Get()->power_button_controller();
if (layer()->opacity() == 0.f) {
SetVisible(false);
power_button_controller->DismissMenu();
}
if (layer()->opacity() == kPowerButtonMenuOpacity) {
CHECK(layer()->GetTargetVisibility())
<< "layer is invisible but we animated it to visible";
show_animation_done_.Run();
}
}
void ScheduleShowHideAnimation(bool show) {
SetVisible(true);
layer()->GetAnimator()->AbortAllAnimations();
ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
animation.AddObserver(this);
animation.SetTweenType(show ? gfx::Tween::EASE_IN_2
: gfx::Tween::FAST_OUT_LINEAR_IN);
animation.SetTransitionDuration(kPowerButtonMenuAnimationDuration);
animation.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
layer()->SetOpacity(show ? kPowerButtonMenuOpacity : 0.f);
}
private:
// views::View:
void OnThemeChanged() override {
views::View::OnThemeChanged();
layer()->SetColor(
GetColorProvider()->GetColor(kColorAshShieldAndBaseOpaque));
}
// A callback for when the animation that shows the power menu has finished.
base::RepeatingClosure show_animation_done_;
};
BEGIN_METADATA(PowerButtonMenuScreenView, PowerButtonMenuBackgroundView)
END_METADATA
PowerButtonMenuScreenView::PowerButtonMenuScreenView(
ShutdownReason shutdown_reason,
PowerButtonPosition power_button_position,
double power_button_offset_percentage,
base::RepeatingClosure show_animation_done)
: power_button_position_(power_button_position),
power_button_offset_percentage_(power_button_offset_percentage) {
power_button_screen_background_shield_ =
new PowerButtonMenuBackgroundView(show_animation_done);
AddChildViewRaw(power_button_screen_background_shield_.get());
power_button_menu_view_ =
new PowerButtonMenuView(shutdown_reason, power_button_position_);
AddChildViewRaw(power_button_menu_view_.get());
AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
}
PowerButtonMenuScreenView::~PowerButtonMenuScreenView() = default;
void PowerButtonMenuScreenView::ScheduleShowHideAnimation(bool show) {
power_button_screen_background_shield_->ScheduleShowHideAnimation(show);
if (IsCurtainModeEnabled()) {
GetOrCreateCurtainView()->ScheduleShowHideAnimation(show);
} else {
power_button_menu_view_->ScheduleShowHideAnimation(show);
}
}
void PowerButtonMenuScreenView::ResetOpacity() {
if (IsCurtainModeEnabled()) {
for (ui::Layer* layer : {power_button_screen_background_shield_->layer(),
GetOrCreateCurtainView()->layer()}) {
DCHECK(layer);
layer->SetOpacity(0.f);
}
} else {
for (ui::Layer* layer : {power_button_screen_background_shield_->layer(),
power_button_menu_view_->layer()}) {
DCHECK(layer);
layer->SetOpacity(0.f);
}
}
}
void PowerButtonMenuScreenView::OnWidgetShown(
PowerButtonController::PowerButtonPosition position,
double offset_percentage) {
power_button_position_ = position;
power_button_offset_percentage_ = offset_percentage;
// The order here matters. RecreateItems() must be called before calling
// UpdateMenuBoundsOrigins(), since the latter relies on the
// power_button_menu_view_'s preferred size, which depends on the items added
// to the view.
if (!IsCurtainModeEnabled()) {
power_button_menu_view_->RecreateItems();
}
if (power_button_position_ != PowerButtonPosition::NONE) {
UpdateMenuBoundsOrigins();
}
DeprecatedLayoutImmediately();
}
PowerButtonMenuCurtainView*
PowerButtonMenuScreenView::GetOrCreateCurtainView() {
if (!power_button_menu_curtain_view_) {
power_button_menu_curtain_view_ =
AddChildView(std::make_unique<PowerButtonMenuCurtainView>());
}
return power_button_menu_curtain_view_;
}
void PowerButtonMenuScreenView::Layout(PassKey) {
power_button_screen_background_shield_->SetBoundsRect(GetContentsBounds());
if (IsCurtainModeEnabled()) {
LayoutMenuCurtainView();
} else {
LayoutMenuView();
}
}
void PowerButtonMenuScreenView::LayoutMenuView() {
gfx::Rect menu_bounds = GetMenuBounds();
PowerButtonMenuView::TransformDisplacement transform_displacement =
power_button_menu_view_->GetTransformDisplacement();
if (transform_displacement.direction == TransformDirection::X) {
menu_bounds.set_x(menu_bounds.x() - transform_displacement.distance);
} else if (transform_displacement.direction == TransformDirection::Y) {
menu_bounds.set_y(menu_bounds.y() - transform_displacement.distance);
}
power_button_menu_view_->SetBoundsRect(menu_bounds);
}
void PowerButtonMenuScreenView::LayoutMenuCurtainView() {
gfx::Rect menu_bounds = GetMenuBounds();
menu_bounds.set_y(menu_bounds.y() - kPowerButtonMenuTransformDistanceDp);
GetOrCreateCurtainView()->SetBoundsRect(menu_bounds);
}
bool PowerButtonMenuScreenView::OnMousePressed(const ui::MouseEvent& event) {
return true;
}
void PowerButtonMenuScreenView::OnMouseReleased(const ui::MouseEvent& event) {
ScheduleShowHideAnimation(false);
RecordMenuActionHistogram(PowerButtonMenuActionType::kDismissByMouse);
}
bool PowerButtonMenuScreenView::AcceleratorPressed(
const ui::Accelerator& accelerator) {
DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code());
Shell::Get()->power_button_controller()->DismissMenu();
RecordMenuActionHistogram(PowerButtonMenuActionType::kDismissByEsc);
return true;
}
void PowerButtonMenuScreenView::OnGestureEvent(ui::GestureEvent* event) {
if (event->type() != ui::EventType::kGestureTapDown) {
return;
}
// Dismisses the menu if tap anywhere on the background shield.
ScheduleShowHideAnimation(false);
RecordMenuActionHistogram(PowerButtonMenuActionType::kDismissByTouch);
}
void PowerButtonMenuScreenView::OnDisplayMetricsChanged(
const display::Display& display,
uint32_t changed_metrics) {
GetWidget()->SetBounds(
display::Screen::GetScreen()->GetPrimaryDisplay().bounds());
LayoutWithoutTransform();
}
void PowerButtonMenuScreenView::LayoutWithoutTransform() {
power_button_screen_background_shield_->SetBoundsRect(GetContentsBounds());
if (IsCurtainModeEnabled()) {
GetOrCreateCurtainView()->SetBoundsRect(GetMenuBounds());
} else {
power_button_menu_view_->SetTransform(gfx::Transform());
power_button_menu_view_->SetBoundsRect(GetMenuBounds());
}
}
void PowerButtonMenuScreenView::UpdateMenuBoundsOrigins() {
// Power button position offset in pixels from the top when the button is at
// the left/right of the screen after rotation.
int left_power_button_y = 0, right_power_button_y = 0;
// Power button position offset in pixels from the left when the button is at
// the top/bottom of the screen after rotation.
int top_power_button_x = 0, bottom_power_button_x = 0;
// The screen orientation when the power button is at the
// left/right/top/bottom of the screen after rotation.
chromeos::OrientationType left_screen_orientation, right_screen_orientation,
top_screen_orientation, bottom_screen_orientation;
const gfx::Size landscape_size = GetPrimaryDisplayLandscapeSize();
int display_width = landscape_size.width();
int display_height = landscape_size.height();
int display_edge_for_adjust = landscape_size.height();
if (power_button_position_ == PowerButtonPosition::TOP ||
power_button_position_ == PowerButtonPosition::BOTTOM) {
std::swap(display_width, display_height);
display_edge_for_adjust = landscape_size.width();
}
int power_button_offset = display_height * power_button_offset_percentage_;
switch (power_button_position_) {
case PowerButtonPosition::LEFT:
case PowerButtonPosition::BOTTOM:
left_power_button_y = bottom_power_button_x = power_button_offset;
right_power_button_y = top_power_button_x =
display_height - power_button_offset;
break;
case PowerButtonPosition::RIGHT:
case PowerButtonPosition::TOP:
left_power_button_y = bottom_power_button_x =
display_height - power_button_offset;
right_power_button_y = top_power_button_x = power_button_offset;
break;
default:
NOTREACHED();
}
switch (power_button_position_) {
case PowerButtonPosition::LEFT:
left_screen_orientation = chromeos::OrientationType::kLandscapePrimary;
right_screen_orientation = chromeos::OrientationType::kLandscapeSecondary;
top_screen_orientation = chromeos::OrientationType::kPortraitPrimary;
bottom_screen_orientation = chromeos::OrientationType::kPortraitSecondary;
break;
case PowerButtonPosition::RIGHT:
left_screen_orientation = chromeos::OrientationType::kLandscapeSecondary;
right_screen_orientation = chromeos::OrientationType::kLandscapePrimary;
top_screen_orientation = chromeos::OrientationType::kPortraitSecondary;
bottom_screen_orientation = chromeos::OrientationType::kPortraitPrimary;
break;
case PowerButtonPosition::TOP:
left_screen_orientation = chromeos::OrientationType::kPortraitSecondary;
right_screen_orientation = chromeos::OrientationType::kPortraitPrimary;
top_screen_orientation = chromeos::OrientationType::kLandscapePrimary;
bottom_screen_orientation =
chromeos::OrientationType::kLandscapeSecondary;
break;
case PowerButtonPosition::BOTTOM:
left_screen_orientation = chromeos::OrientationType::kPortraitPrimary;
right_screen_orientation = chromeos::OrientationType::kPortraitSecondary;
top_screen_orientation = chromeos::OrientationType::kLandscapeSecondary;
bottom_screen_orientation = chromeos::OrientationType::kLandscapePrimary;
break;
default:
NOTREACHED();
}
menu_bounds_origins_.clear();
const gfx::Size menu_size = GetMenuViewPreferredSize();
// Power button position offset from the left when the button is at the left
// is always zero.
menu_bounds_origins_.insert(std::make_pair(
left_screen_orientation,
gfx::Point(kPowerButtonMenuTransformDistanceDp,
AdjustMenuEdgeForDisplaySize(
left_power_button_y - menu_size.height() / 2,
display_edge_for_adjust, menu_size.height()))));
menu_bounds_origins_.insert(std::make_pair(
right_screen_orientation,
gfx::Point(display_width - kPowerButtonMenuTransformDistanceDp -
menu_size.width(),
AdjustMenuEdgeForDisplaySize(
right_power_button_y - menu_size.height() / 2,
display_edge_for_adjust, menu_size.height()))));
// Power button position offset from the top when the button is at the top
// is always zero.
menu_bounds_origins_.insert(
std::make_pair(top_screen_orientation,
gfx::Point(AdjustMenuEdgeForDisplaySize(
top_power_button_x - menu_size.width() / 2,
display_edge_for_adjust, menu_size.width()),
kPowerButtonMenuTransformDistanceDp)));
menu_bounds_origins_.insert(std::make_pair(
bottom_screen_orientation,
gfx::Point(AdjustMenuEdgeForDisplaySize(
bottom_power_button_x - menu_size.width() / 2,
display_edge_for_adjust, menu_size.width()),
display_width - kPowerButtonMenuTransformDistanceDp -
menu_size.height())));
}
gfx::Rect PowerButtonMenuScreenView::GetMenuBounds() {
gfx::Rect menu_bounds;
if (power_button_position_ == PowerButtonPosition::NONE ||
!display::Screen::GetScreen()->InTabletMode()) {
menu_bounds = GetContentsBounds();
menu_bounds.ClampToCenteredSize(GetMenuViewPreferredSize());
} else {
menu_bounds.set_origin(
menu_bounds_origins_[Shell::Get()
->screen_orientation_controller()
->GetCurrentOrientation()]);
menu_bounds.set_size(GetMenuViewPreferredSize());
}
return menu_bounds;
}
gfx::Size PowerButtonMenuScreenView::GetMenuViewPreferredSize() {
if (IsCurtainModeEnabled()) {
return GetOrCreateCurtainView()->GetPreferredSize();
} else {
return power_button_menu_view_->GetPreferredSize();
}
}
ui::Layer*
PowerButtonMenuScreenView::GetPowerButtonScreenBackgroundShieldLayerForTest()
const {
return power_button_screen_background_shield_->layer();
}
BEGIN_METADATA(PowerButtonMenuScreenView)
END_METADATA
} // namespace ash
|