blob: 6516133260c3ca416084b4004f422cfbb919badb [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/common/system/tray/system_menu_button.h"
#include "ash/common/ash_constants.h"
#include "ash/common/system/tray/system_tray.h"
#include "ash/common/system/tray/tray_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/animation/flood_fill_ink_drop_ripple.h"
#include "ui/views/animation/ink_drop_highlight.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/animation/square_ink_drop_ripple.h"
#include "ui/views/border.h"
#include "ui/views/painter.h"
namespace ash {
SystemMenuButton::SystemMenuButton(views::ButtonListener* listener,
InkDropStyle ink_drop_style,
gfx::ImageSkia normal_icon,
gfx::ImageSkia disabled_icon,
int accessible_name_id)
: views::ImageButton(listener), ink_drop_style_(ink_drop_style) {
DCHECK_EQ(normal_icon.width(), disabled_icon.width());
DCHECK_EQ(normal_icon.height(), disabled_icon.height());
SetImage(views::Button::STATE_NORMAL, &normal_icon);
SetImage(views::Button::STATE_DISABLED, &disabled_icon);
const int horizontal_padding = (kMenuButtonSize - normal_icon.width()) / 2;
const int vertical_padding = (kMenuButtonSize - normal_icon.height()) / 2;
SetBorder(views::CreateEmptyBorder(vertical_padding, horizontal_padding,
vertical_padding, horizontal_padding));
SetTooltipText(l10n_util::GetStringUTF16(accessible_name_id));
// TODO(tdanderson): Update the focus rect color, border thickness, and
// location for material design.
SetFocusForPlatform();
SetFocusPainter(views::Painter::CreateSolidFocusPainter(
kFocusBorderColor, gfx::Insets(1, 1, 1, 1)));
SetInkDropMode(InkDropMode::ON);
set_has_ink_drop_action_on_click(true);
set_ink_drop_base_color(kTrayPopupInkDropBaseColor);
set_ink_drop_visible_opacity(kTrayPopupInkDropRippleOpacity);
}
SystemMenuButton::SystemMenuButton(views::ButtonListener* listener,
InkDropStyle ink_drop_style,
const gfx::VectorIcon& icon,
int accessible_name_id)
: SystemMenuButton(listener,
ink_drop_style,
gfx::CreateVectorIcon(icon, kMenuIconColor),
gfx::CreateVectorIcon(icon, kMenuIconColorDisabled),
accessible_name_id) {}
SystemMenuButton::~SystemMenuButton() {}
std::unique_ptr<views::InkDrop> SystemMenuButton::CreateInkDrop() {
std::unique_ptr<views::InkDropImpl> ink_drop =
CreateDefaultFloodFillInkDropImpl();
ink_drop->SetShowHighlightOnHover(false);
return std::move(ink_drop);
}
std::unique_ptr<views::InkDropRipple> SystemMenuButton::CreateInkDropRipple()
const {
const gfx::Size size = GetInkDropSize();
switch (ink_drop_style_) {
case InkDropStyle::SQUARE:
return base::MakeUnique<views::SquareInkDropRipple>(
size, size.width() / 2, size, size.width() / 2,
GetInkDropCenterBasedOnLastEvent(), GetLocalBounds().CenterPoint(),
GetInkDropBaseColor(), ink_drop_visible_opacity());
case InkDropStyle::FLOOD_FILL:
gfx::Rect bounds = GetLocalBounds();
bounds.Inset(kTrayPopupInkDropInset, kTrayPopupInkDropInset);
return base::MakeUnique<views::FloodFillInkDropRipple>(
bounds, GetInkDropCenterBasedOnLastEvent(), GetInkDropBaseColor(),
ink_drop_visible_opacity());
}
// Required for some compilers.
NOTREACHED();
return nullptr;
}
std::unique_ptr<views::InkDropHighlight>
SystemMenuButton::CreateInkDropHighlight() const {
int highlight_radius = 0;
switch (ink_drop_style_) {
case InkDropStyle::SQUARE:
highlight_radius = GetInkDropSize().width() / 2;
break;
case InkDropStyle::FLOOD_FILL:
highlight_radius = 0;
break;
}
std::unique_ptr<views::InkDropHighlight> highlight(
new views::InkDropHighlight(GetInkDropSize(), highlight_radius,
gfx::RectF(GetLocalBounds()).CenterPoint(),
GetInkDropBaseColor()));
highlight->set_visible_opacity(kTrayPopupInkDropHighlightOpacity);
return highlight;
}
gfx::Size SystemMenuButton::GetInkDropSize() const {
gfx::Rect bounds = GetLocalBounds();
bounds.Inset(kTrayPopupInkDropInset, kTrayPopupInkDropInset);
return bounds.size();
}
} // namespace ash