1 // Copyright 2018 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 
5 #include "chrome/browser/vr/elements/vector_icon_button.h"
6 
7 #include "chrome/browser/vr/elements/rect.h"
8 #include "chrome/browser/vr/elements/ui_element.h"
9 #include "chrome/browser/vr/elements/ui_element_name.h"
10 #include "chrome/browser/vr/elements/vector_icon.h"
11 #include "chrome/browser/vr/ui_scene_constants.h"
12 
13 #include "ui/gfx/geometry/point_f.h"
14 
15 namespace vr {
16 
17 namespace {
18 
19 constexpr float kDefaultIconScaleFactor = 0.5f;
20 
21 }  // namespace
22 
VectorIconButton(base::RepeatingCallback<void ()> click_handler,const gfx::VectorIcon & icon,AudioDelegate * audio_delegate)23 VectorIconButton::VectorIconButton(
24     base::RepeatingCallback<void()> click_handler,
25     const gfx::VectorIcon& icon,
26     AudioDelegate* audio_delegate)
27     : Button(click_handler, audio_delegate),
28       icon_scale_factor_(kDefaultIconScaleFactor) {
29   auto vector_icon = std::make_unique<VectorIcon>(512);
30   vector_icon->SetType(kTypeButtonForeground);
31   vector_icon->SetIcon(icon);
32   vector_icon->set_hit_testable(false);
33   foreground_ = vector_icon.get();
34 
35   background()->AddChild(std::move(vector_icon));
36 }
37 
38 VectorIconButton::~VectorIconButton() = default;
39 
SetIcon(const gfx::VectorIcon & icon)40 void VectorIconButton::SetIcon(const gfx::VectorIcon& icon) {
41   foreground_->SetIcon(icon);
42 }
43 
SetIconScaleFactor(float factor)44 void VectorIconButton::SetIconScaleFactor(float factor) {
45   icon_scale_factor_ = factor;
46   OnSetSize(size());
47 }
48 
SetIconTranslation(float x,float y)49 void VectorIconButton::SetIconTranslation(float x, float y) {
50   foreground_->SetTranslate(x, y, 0);
51 }
52 
OnStateUpdated()53 void VectorIconButton::OnStateUpdated() {
54   Button::OnStateUpdated();
55   foreground_->SetColor(colors().GetForegroundColor(!enabled()));
56 }
57 
OnSetDrawPhase()58 void VectorIconButton::OnSetDrawPhase() {
59   Button::OnSetDrawPhase();
60   foreground_->SetDrawPhase(draw_phase());
61 }
62 
OnSetName()63 void VectorIconButton::OnSetName() {
64   Button::OnSetName();
65   foreground_->set_owner_name_for_test(name());
66 }
67 
OnSetSize(const gfx::SizeF & size)68 void VectorIconButton::OnSetSize(const gfx::SizeF& size) {
69   Button::OnSetSize(size);
70   // Maintain aspect ratio of the icon, even if the button isn't square.
71   float new_size = std::min(size.width(), size.height()) * icon_scale_factor_;
72   foreground()->SetSize(new_size, new_size);
73 }
74 
75 }  // namespace vr
76