blob: 3c902f96a6c9b1ecb66ef823180db0bc6f9a6b58 [file] [log] [blame]
// Copyright 2017 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 "base/gtest_prod_util.h"
#include "base/optional.h"
#include "base/strings/string16.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/button/menu_button_listener.h"
namespace gfx {
enum ElideBehavior;
class ImageSkia;
} // namespace gfx
namespace media_router {
FORWARD_DECLARE_TEST(CastDialogSinkButtonTest, SetTitleLabel);
FORWARD_DECLARE_TEST(CastDialogSinkButtonTest, SetStatusLabel);
} // namespace media_router
namespace views {
class ButtonListener;
class Label;
class StyledLabel;
class View;
} // namespace views
// A button taking the full width of its parent that shows a background color
// when hovered over.
class HoverButton : public views::MenuButton, public views::MenuButtonListener {
// Creates a single line hover button with no icon.
HoverButton(views::ButtonListener* button_listener,
const base::string16& text);
// Creates a single line hover button with an icon.
HoverButton(views::ButtonListener* button_listener,
const gfx::ImageSkia& icon,
const base::string16& text);
// Creates a HoverButton with custom subviews. |icon_view| replaces the
// LabelButton icon, and titles appear on separate rows. An empty |subtitle|
// will vertically center |title|. |secondary_view|, when set, is shown
// on the opposite side of the button from |icon_view|.
// When |resize_row_for_secondary_icon| is false, the button tries to
// accommodate the view's preferred size by reducing the top and bottom
// insets appropriately up to a value of 0.
HoverButton(views::ButtonListener* button_listener,
std::unique_ptr<views::View> icon_view,
const base::string16& title,
const base::string16& subtitle,
std::unique_ptr<views::View> secondary_view = nullptr,
bool resize_row_for_secondary_view = true,
bool secondary_view_can_process_events = false);
~HoverButton() override;
// views::MenuButton:
bool OnKeyPressed(const ui::KeyEvent& event) override;
void SetBorder(std::unique_ptr<views::Border> b) override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
bool IsTriggerableEventType(const ui::Event& event) override;
gfx::Insets GetInsets() const override;
// Updates the title text, and applies the secondary style to the text
// specified by |range|. If |range| is invalid, no style is applied. This
// method is only supported for |HoverButton|s created with a title and
// subtitle.
void SetTitleTextWithHintRange(const base::string16& title_text,
const gfx::Range& range);
// This method is only supported for |HoverButton|s created with a title and
// non-empty subtitle.
void SetSubtitleElideBehavior(gfx::ElideBehavior elide_behavior);
// Adjusts the background and the text color according to |style|.
void SetStyle(Style style);
// Sets the text style of the title considering the color of the background.
// Passing |background_color| makes sure that the text color will not be
// changed to a color that is not readable on the specified background.
void SetTitleTextStyle(views::style::TextStyle text_style,
SkColor background_color);
void SetSubtitleColor(SkColor color);
void set_auto_compute_tooltip(bool auto_compute_tooltip) {
auto_compute_tooltip_ = auto_compute_tooltip;
// views::MenuButtonListener:
void OnMenuButtonClicked(MenuButton* source,
const gfx::Point& point,
const ui::Event* event) override;
// views::MenuButton:
KeyClickAction GetKeyClickActionForEvent(const ui::KeyEvent& event) override;
void StateChanged(ButtonState old_state) override;
SkColor GetInkDropBaseColor() const override;
std::unique_ptr<views::InkDrop> CreateInkDrop() override;
void Layout() override;
views::View* GetTooltipHandlerForPoint(const gfx::Point& point) override;
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
views::StyledLabel* title() const { return title_; }
views::Label* subtitle() const { return subtitle_; }
views::View* icon_view() const { return icon_view_; }
views::StyledLabel* title_;
views::Label* subtitle_;
views::View* icon_view_;
views::View* secondary_view_;
// The horizontal space the padding and icon take up. Used for calculating the
// available space for |title_|, if it exists.
int taken_width_ = 0;
// Custom insets, when secondary_view_ is larger than the rest of the row.
base::Optional<gfx::Insets> insets_;
// Whether this |HoverButton|'s accessible name and tooltip should be computed
// from the |title_| and |subtitle_| text.
bool auto_compute_tooltip_ = true;
// Listener to be called when button is clicked.
views::ButtonListener* listener_;