legend.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/legend.h"
  5. #include "ash/hud_display/graph.h"
  6. #include "ash/hud_display/hud_constants.h"
  7. #include "ash/hud_display/solid_source_background.h"
  8. #include "cc/paint/paint_flags.h"
  9. #include "third_party/skia/include/core/SkBlendMode.h"
  10. #include "third_party/skia/include/core/SkPaint.h"
  11. #include "third_party/skia/include/core/SkPath.h"
  12. #include "ui/base/metadata/metadata_impl_macros.h"
  13. #include "ui/gfx/canvas.h"
  14. #include "ui/gfx/text_constants.h"
  15. #include "ui/views/border.h"
  16. #include "ui/views/controls/label.h"
  17. #include "ui/views/layout/box_layout.h"
  18. namespace ash {
  19. namespace hud_display {
  20. namespace {
  21. class LegendEntry : public views::View {
  22. public:
  23. METADATA_HEADER(LegendEntry);
  24. explicit LegendEntry(const Legend::Entry& data);
  25. LegendEntry(const LegendEntry&) = delete;
  26. LegendEntry& operator=(const LegendEntry&) = delete;
  27. ~LegendEntry() override;
  28. // views::View:
  29. void OnPaint(gfx::Canvas* canvas) override;
  30. void SetValueIndex(size_t index);
  31. void RefreshValue();
  32. // This is used by parent to match sizes.
  33. views::View* value() { return value_; }
  34. private:
  35. const SkColor color_;
  36. const Graph& graph_;
  37. size_t value_index_ = 0;
  38. Legend::Formatter formatter_;
  39. views::Label* value_ = nullptr;
  40. };
  41. BEGIN_METADATA(LegendEntry, views::View)
  42. END_METADATA
  43. LegendEntry::LegendEntry(const Legend::Entry& data)
  44. : color_(data.graph.color()),
  45. graph_(data.graph),
  46. formatter_(data.formatter) {
  47. views::BoxLayout* layout_manager =
  48. SetLayoutManager(std::make_unique<views::BoxLayout>(
  49. views::BoxLayout::Orientation::kHorizontal));
  50. layout_manager->set_cross_axis_alignment(
  51. views::BoxLayout::CrossAxisAlignment::kCenter);
  52. // We need to allocate space for the colorpicker. It should be square with
  53. // edge size matching default views::Label height. This is not known until
  54. // layout runs, so just hard code it.
  55. constexpr int kColorpickerAreaWidth = 20;
  56. SetBorder(views::CreateEmptyBorder(
  57. gfx::Insets::TLBR(0, kColorpickerAreaWidth, 0, 0)));
  58. views::Label* label = AddChildView(
  59. std::make_unique<views::Label>(data.label, views::style::CONTEXT_LABEL));
  60. label->SetEnabledColor(kHUDDefaultColor);
  61. if (!data.tooltip.empty())
  62. label->SetTooltipText(data.tooltip);
  63. constexpr int kLabelToValueSpece = 5;
  64. value_ = AddChildView(std::make_unique<views::Label>(
  65. std::u16string(), views::style::CONTEXT_LABEL));
  66. layout_manager->SetFlexForView(value_, /*flex=*/1);
  67. value_->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_RIGHT);
  68. value_->SetBorder(
  69. views::CreateEmptyBorder(gfx::Insets::TLBR(0, kLabelToValueSpece, 0, 0)));
  70. value_->SetEnabledColor(kHUDDefaultColor);
  71. }
  72. LegendEntry::~LegendEntry() = default;
  73. void LegendEntry::OnPaint(gfx::Canvas* canvas) {
  74. // Draw 10x10 sold color rectangle in the middle of the left border.
  75. // (We used border to allocate space for the colorpicker above.)
  76. constexpr int kBoxSize = 10;
  77. const gfx::Rect bounds(GetInsets().left(), height());
  78. constexpr int kBoxBorderWidth = 1;
  79. gfx::Rect box = bounds;
  80. box.ClampToCenteredSize(gfx::Size(kBoxSize, kBoxSize));
  81. const SkRect border =
  82. SkRect::MakeXYWH(box.x(), box.y(), box.width(), box.height());
  83. SkPath box_border;
  84. box_border.addRect(border);
  85. SkPath box_filled;
  86. box_filled.addRect(border.makeInset(kBoxBorderWidth, kBoxBorderWidth));
  87. cc::PaintFlags flags;
  88. flags.setAntiAlias(true);
  89. flags.setBlendMode(SkBlendMode::kSrc);
  90. flags.setStyle(cc::PaintFlags::kFill_Style);
  91. flags.setColor(color_);
  92. canvas->DrawPath(box_filled, flags);
  93. flags.setStyle(cc::PaintFlags::kStroke_Style);
  94. flags.setStrokeWidth(kBoxBorderWidth);
  95. flags.setColor(kHUDDefaultColor);
  96. canvas->DrawPath(box_border, flags);
  97. views::View::OnPaint(canvas);
  98. }
  99. void LegendEntry::SetValueIndex(size_t index) {
  100. if (index == value_index_)
  101. return;
  102. value_index_ = index;
  103. RefreshValue();
  104. }
  105. void LegendEntry::RefreshValue() {
  106. if (graph_.IsFilledIndex(value_index_)) {
  107. value_->SetText(formatter_.Run(graph_.GetUnscaledValueAt(value_index_)));
  108. } else {
  109. value_->SetText(std::u16string());
  110. }
  111. }
  112. } // namespace
  113. Legend::Entry::Entry(const Graph& graph,
  114. std::u16string label,
  115. std::u16string tooltip,
  116. Formatter formatter)
  117. : graph(graph), label(label), tooltip(tooltip), formatter(formatter) {}
  118. Legend::Entry::Entry(const Entry&) = default;
  119. Legend::Entry::~Entry() = default;
  120. BEGIN_METADATA(Legend, views::View)
  121. END_METADATA
  122. Legend::Legend(const std::vector<Legend::Entry>& contents) {
  123. views::BoxLayout* layout_manager =
  124. SetLayoutManager(std::make_unique<views::BoxLayout>(
  125. views::BoxLayout::Orientation::kVertical));
  126. layout_manager->set_cross_axis_alignment(
  127. views::BoxLayout::CrossAxisAlignment::kStretch);
  128. SetBackground(std::make_unique<SolidSourceBackground>(kHUDLegendBackground,
  129. /*radius=*/0));
  130. SetBorder(views::CreateEmptyBorder(kHUDInset));
  131. for (const auto& entry : contents)
  132. AddChildView(std::make_unique<LegendEntry>(entry));
  133. }
  134. Legend::~Legend() = default;
  135. void Legend::Layout() {
  136. views::View::Layout();
  137. gfx::Size max_size;
  138. bool updated = false;
  139. for (auto* view : children()) {
  140. if (view->GetClassName() != LegendEntry::kViewClassName)
  141. continue;
  142. views::View* value = static_cast<LegendEntry*>(view)->value();
  143. max_size.SetToMax(value->GetPreferredSize());
  144. updated |= max_size != value->GetPreferredSize();
  145. }
  146. if (updated) {
  147. for (auto* view : children()) {
  148. if (view->GetClassName() != LegendEntry::kViewClassName)
  149. continue;
  150. static_cast<LegendEntry*>(view)->value()->SetPreferredSize(max_size);
  151. }
  152. views::View::Layout();
  153. }
  154. }
  155. void Legend::SetValuesIndex(size_t index) {
  156. for (auto* view : children()) {
  157. if (view->GetClassName() != LegendEntry::kViewClassName)
  158. continue;
  159. static_cast<LegendEntry*>(view)->SetValueIndex(index);
  160. }
  161. }
  162. void Legend::RefreshValues() {
  163. for (auto* view : children()) {
  164. if (view->GetClassName() != LegendEntry::kViewClassName)
  165. continue;
  166. static_cast<LegendEntry*>(view)->RefreshValue();
  167. }
  168. }
  169. } // namespace hud_display
  170. } // namespace ash