graph_page_view_base.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/hud_display/graph_page_view_base.h"
  5. #include "ash/hud_display/hud_constants.h"
  6. #include "ash/hud_display/hud_properties.h"
  7. #include "ash/hud_display/legend.h"
  8. #include "ash/hud_display/reference_lines.h"
  9. #include "ash/hud_display/solid_source_background.h"
  10. #include "base/bind.h"
  11. #include "ui/base/metadata/metadata_impl_macros.h"
  12. #include "ui/gfx/canvas.h"
  13. #include "ui/gfx/paint_vector_icon.h"
  14. #include "ui/views/border.h"
  15. #include "ui/views/controls/button/button.h"
  16. #include "ui/views/controls/button/image_button.h"
  17. #include "ui/views/layout/box_layout.h"
  18. #include "ui/views/layout/fill_layout.h"
  19. #include "ui/views/window/vector_icons/vector_icons.h"
  20. namespace ash {
  21. namespace hud_display {
  22. namespace {
  23. constexpr int kMinMaxButtonIconSize = 10;
  24. constexpr int kMinMaxButtonBorder = 5;
  25. // ImageButton with underline
  26. class MinMaxButton : public views::ImageButton {
  27. public:
  28. METADATA_HEADER(MinMaxButton);
  29. explicit MinMaxButton(views::Button::PressedCallback callback)
  30. : views::ImageButton(callback) {
  31. SetBorder(views::CreateEmptyBorder(kMinMaxButtonBorder));
  32. SetBackground(std::make_unique<SolidSourceBackground>(kHUDLegendBackground,
  33. /*radius=*/0));
  34. SetProperty(kHUDClickHandler, HTCLIENT);
  35. SetFocusBehavior(views::View::FocusBehavior::ACCESSIBLE_ONLY);
  36. }
  37. MinMaxButton(const MinMaxButton&) = delete;
  38. MinMaxButton& operator=(const MinMaxButton&) = delete;
  39. ~MinMaxButton() override = default;
  40. protected:
  41. // ImageButton
  42. void PaintButtonContents(gfx::Canvas* canvas) override {
  43. views::ImageButton::PaintButtonContents(canvas);
  44. SkPath path;
  45. path.moveTo(0, height());
  46. path.lineTo(height(), width());
  47. cc::PaintFlags flags;
  48. flags.setAntiAlias(true);
  49. flags.setBlendMode(SkBlendMode::kSrc);
  50. flags.setStyle(cc::PaintFlags::kStroke_Style);
  51. flags.setStrokeWidth(1);
  52. flags.setColor(kHUDDefaultColor);
  53. canvas->DrawPath(path, flags);
  54. }
  55. };
  56. BEGIN_METADATA(MinMaxButton, views::ImageButton)
  57. END_METADATA
  58. void SetMinimizeIconToButton(views::ImageButton* button) {
  59. button->SetImage(
  60. views::Button::ButtonState::STATE_NORMAL,
  61. gfx::CreateVectorIcon(views::kWindowControlMinimizeIcon,
  62. kMinMaxButtonIconSize, kHUDDefaultColor));
  63. }
  64. void SetRestoreIconToButton(views::ImageButton* button) {
  65. button->SetImage(
  66. views::Button::ButtonState::STATE_NORMAL,
  67. gfx::CreateVectorIcon(views::kWindowControlRestoreIcon,
  68. kMinMaxButtonIconSize, kHUDDefaultColor));
  69. }
  70. } // namespace
  71. BEGIN_METADATA(GraphPageViewBase, views::View)
  72. END_METADATA
  73. GraphPageViewBase::GraphPageViewBase() {
  74. DCHECK_CALLED_ON_VALID_SEQUENCE(ui_sequence_checker_);
  75. // There are two overlaid children: reference lines and legend container.
  76. SetLayoutManager(std::make_unique<views::FillLayout>());
  77. // |ReferenceLines| object is added after this object is fully initialized,
  78. // but it should be located under control elements (or they will never receive
  79. // events). This way we need to create a separate container for it.
  80. reference_lines_container_ = AddChildView(std::make_unique<views::View>());
  81. reference_lines_container_->SetLayoutManager(
  82. std::make_unique<views::FillLayout>());
  83. // Legend is floating in its own container. Invisible border of
  84. // kLegendPositionOffset makes it float on top of the graph.
  85. constexpr int kLegendPositionOffset = 20;
  86. legend_container_ = AddChildView(std::make_unique<views::View>());
  87. legend_container_
  88. ->SetLayoutManager(std::make_unique<views::BoxLayout>(
  89. views::BoxLayout::Orientation::kVertical))
  90. ->set_cross_axis_alignment(views::BoxLayout::CrossAxisAlignment::kStart);
  91. legend_container_->SetBorder(views::CreateEmptyBorder(kLegendPositionOffset));
  92. legend_container_->SetVisible(false);
  93. legend_min_max_button_ = legend_container_->AddChildView(
  94. std::make_unique<MinMaxButton>(base::BindRepeating(
  95. &GraphPageViewBase::OnButtonPressed, base::Unretained(this))));
  96. legend_min_max_button_->SetTooltipText(u"Trigger graph legend");
  97. SetMinimizeIconToButton(legend_min_max_button_);
  98. }
  99. GraphPageViewBase::~GraphPageViewBase() {
  100. DCHECK_CALLED_ON_VALID_SEQUENCE(ui_sequence_checker_);
  101. }
  102. void GraphPageViewBase::OnButtonPressed() {
  103. if (legend_->GetVisible()) {
  104. legend_->SetVisible(false);
  105. SetRestoreIconToButton(legend_min_max_button_);
  106. } else {
  107. legend_->SetVisible(true);
  108. SetMinimizeIconToButton(legend_min_max_button_);
  109. }
  110. }
  111. void GraphPageViewBase::CreateLegend(
  112. const std::vector<Legend::Entry>& entries) {
  113. DCHECK(!legend_);
  114. legend_ = legend_container_->AddChildView(std::make_unique<Legend>(entries));
  115. legend_container_->SetVisible(true);
  116. }
  117. ReferenceLines* GraphPageViewBase::CreateReferenceLines(
  118. float left,
  119. float top,
  120. float right,
  121. float bottom,
  122. const std::u16string& x_unit,
  123. const std::u16string& y_unit,
  124. int horizontal_points_number,
  125. int horizontal_ticks_interval,
  126. float vertical_ticks_interval) {
  127. DCHECK(reference_lines_container_->children().empty());
  128. return reference_lines_container_->AddChildView(
  129. std::make_unique<ReferenceLines>(
  130. left, top, right, bottom, x_unit, y_unit, horizontal_points_number,
  131. horizontal_ticks_interval, vertical_ticks_interval));
  132. }
  133. void GraphPageViewBase::RefreshLegendValues() {
  134. if (legend_)
  135. legend_->RefreshValues();
  136. }
  137. } // namespace hud_display
  138. } // namespace ash