Commit d65f1e79 authored by Christopher Grant's avatar Christopher Grant Committed by Commit Bot

VR: Make sounds consistent across all buttons

Have the button class itself assign default sounds.  This ensures they
all sound the same, in both enabled and disabled states.

BUG=823413

Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_vr;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: Id254c67f702d7cd7ccd169b55f68f9b79cf4eb49
Reviewed-on: https://chromium-review.googlesource.com/978631
Commit-Queue: Christopher Grant <cjgrant@chromium.org>
Reviewed-by: default avatarIan Vollick <vollick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#545645}
parent 5736637e
......@@ -23,7 +23,8 @@ constexpr float kDefaultHoverOffsetDMM = 0.048f;
} // namespace
Button::Button(base::RepeatingCallback<void()> click_handler)
Button::Button(base::RepeatingCallback<void()> click_handler,
AudioDelegate* audio_delegate)
: click_handler_(click_handler), hover_offset_(kDefaultHoverOffsetDMM) {
auto background = std::make_unique<Rect>();
background->SetType(kTypeButtonBackground);
......@@ -53,6 +54,14 @@ Button::Button(base::RepeatingCallback<void()> click_handler)
event_handlers.button_up =
base::BindRepeating(&Button::HandleButtonUp, base::Unretained(this));
set_event_handlers(event_handlers);
Sounds sounds;
sounds.hover_enter = kSoundButtonHover;
sounds.button_down = kSoundButtonClick;
SetSounds(sounds, audio_delegate);
disabled_sounds_.hover_enter = kSoundNone;
disabled_sounds_.button_down = kSoundInactiveButtonClick;
}
Button::~Button() = default;
......@@ -146,4 +155,11 @@ void Button::NotifyClientSizeAnimated(const gfx::SizeF& size,
UiElement::NotifyClientSizeAnimated(size, target_property_id, animation);
}
const Sounds& Button::GetSounds() const {
if (!enabled()) {
return disabled_sounds_;
}
return UiElement::GetSounds();
}
} // namespace vr
......@@ -28,7 +28,8 @@ class Rect;
// desired.
class Button : public UiElement {
public:
explicit Button(base::RepeatingCallback<void()> click_handler);
explicit Button(base::RepeatingCallback<void()> click_handler,
AudioDelegate* audio_delegate);
~Button() override;
void Render(UiElementRenderer* renderer,
......@@ -48,6 +49,8 @@ class Button : public UiElement {
// method and the associated field can be removed.
void set_hover_offset(float hover_offset) { hover_offset_ = hover_offset; }
void set_disabled_sounds(const Sounds& sounds) { disabled_sounds_ = sounds; }
bool hovered() const { return hovered_; }
bool down() const { return down_; }
bool pressed() const { return pressed_; }
......@@ -73,6 +76,8 @@ class Button : public UiElement {
void HandleButtonDown();
void HandleButtonUp();
const Sounds& GetSounds() const override;
bool down_ = false;
bool hovered_ = false;
bool pressed_ = false;
......@@ -80,9 +85,9 @@ class Button : public UiElement {
base::RepeatingCallback<void()> click_handler_;
ButtonColors colors_;
float hover_offset_;
Rect* background_;
UiElement* hit_plane_;
Sounds disabled_sounds_;
DISALLOW_COPY_AND_ASSIGN(Button);
};
......
......@@ -10,7 +10,7 @@ namespace vr {
TEST(Button, Hover) {
base::RepeatingCallback<void()> callback;
Button button(callback);
Button button(callback, nullptr);
button.set_hover_offset(0.0f);
button.SetSize(1.0f, 1.0f);
......
......@@ -17,7 +17,13 @@ namespace vr {
DiscButton::DiscButton(base::RepeatingCallback<void()> click_handler,
const gfx::VectorIcon& icon,
AudioDelegate* audio_delegate)
: VectorIconButton(click_handler, icon, audio_delegate) {}
: VectorIconButton(click_handler, icon, audio_delegate) {
// By default, DiscButton is a mode exit button, and gets the 'back' sound.
Sounds sounds;
sounds.hover_enter = kSoundButtonHover;
sounds.button_down = kSoundBackButtonClick;
SetSounds(sounds, audio_delegate);
}
DiscButton::~DiscButton() = default;
......
......@@ -140,9 +140,10 @@ void UiElement::Render(UiElementRenderer* renderer,
void UiElement::Initialize(SkiaSurfaceProvider* provider) {}
void UiElement::OnHoverEnter(const gfx::PointF& position) {
if (sounds_.hover_enter != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(sounds_.hover_enter);
if (GetSounds().hover_enter != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(GetSounds().hover_enter);
}
if (event_handlers_.hover_enter) {
event_handlers_.hover_enter.Run();
} else if (parent() && bubble_events()) {
......@@ -151,8 +152,8 @@ void UiElement::OnHoverEnter(const gfx::PointF& position) {
}
void UiElement::OnHoverLeave() {
if (sounds_.hover_leave != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(sounds_.hover_leave);
if (GetSounds().hover_leave != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(GetSounds().hover_leave);
}
if (event_handlers_.hover_leave) {
event_handlers_.hover_leave.Run();
......@@ -162,8 +163,8 @@ void UiElement::OnHoverLeave() {
}
void UiElement::OnMove(const gfx::PointF& position) {
if (sounds_.move != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(sounds_.move);
if (GetSounds().move != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(GetSounds().move);
}
if (event_handlers_.hover_move) {
event_handlers_.hover_move.Run(position);
......@@ -173,8 +174,8 @@ void UiElement::OnMove(const gfx::PointF& position) {
}
void UiElement::OnButtonDown(const gfx::PointF& position) {
if (sounds_.button_down != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(sounds_.button_down);
if (GetSounds().button_down != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(GetSounds().button_down);
}
if (event_handlers_.button_down) {
event_handlers_.button_down.Run();
......@@ -184,8 +185,8 @@ void UiElement::OnButtonDown(const gfx::PointF& position) {
}
void UiElement::OnButtonUp(const gfx::PointF& position) {
if (sounds_.button_up != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(sounds_.button_up);
if (GetSounds().button_up != kSoundNone && audio_delegate_) {
audio_delegate_->PlaySound(GetSounds().button_up);
}
if (event_handlers_.button_up) {
event_handlers_.button_up.Run();
......@@ -827,4 +828,8 @@ gfx::Transform UiElement::GetTargetLocalTransform() const {
return layout_offset_.Apply() * GetTargetTransform().Apply();
}
const Sounds& UiElement::GetSounds() const {
return sounds_;
}
} // namespace vr
......@@ -489,6 +489,8 @@ class UiElement : public cc::AnimationTarget {
base::TimeTicks last_frame_time() const { return last_frame_time_; }
virtual const Sounds& GetSounds() const;
EventHandlers event_handlers_;
private:
......
......@@ -24,7 +24,8 @@ VectorIconButton::VectorIconButton(
base::RepeatingCallback<void()> click_handler,
const gfx::VectorIcon& icon,
AudioDelegate* audio_delegate)
: Button(click_handler), icon_scale_factor_(kDefaultIconScaleFactor) {
: Button(click_handler, audio_delegate),
icon_scale_factor_(kDefaultIconScaleFactor) {
auto vector_icon = std::make_unique<VectorIcon>(512);
vector_icon->SetType(kTypeButtonForeground);
vector_icon->SetIcon(icon);
......@@ -32,11 +33,6 @@ VectorIconButton::VectorIconButton(
foreground_ = vector_icon.get();
background()->AddChild(std::move(vector_icon));
Sounds sounds;
sounds.hover_enter = kSoundButtonHover;
sounds.button_down = kSoundBackButtonClick;
SetSounds(sounds, audio_delegate);
}
VectorIconButton::~VectorIconButton() = default;
......
......@@ -133,13 +133,6 @@ std::unique_ptr<T> Create(UiElementName name, DrawPhase phase, Args&&... args) {
return element;
}
Sounds CreateButtonSounds() {
Sounds sounds;
sounds.hover_enter = kSoundButtonHover;
sounds.button_down = kSoundButtonClick;
return sounds;
}
typedef VectorBinding<OmniboxSuggestion, Button> SuggestionSetBinding;
typedef typename SuggestionSetBinding::ElementBinding SuggestionBinding;
......@@ -207,14 +200,14 @@ void OnSuggestionModelAdded(UiScene* scene,
ui->OnUiRequestedNavigation();
},
base::Unretained(browser), base::Unretained(ui),
base::Unretained(model), base::Unretained(element_binding)));
base::Unretained(model), base::Unretained(element_binding)),
audio_delegate);
background->SetType(kTypeOmniboxSuggestionBackground);
background->set_hit_testable(true);
background->set_bubble_events(true);
background->set_bounds_contain_children(true);
background->set_hover_offset(0.0);
background->SetSounds(CreateButtonSounds(), audio_delegate);
VR_BIND_BUTTON_COLORS(model, background.get(),
&ColorScheme::suggestion_button_colors,
&Button::SetButtonColors);
......@@ -340,7 +333,8 @@ std::unique_ptr<UiElement> CreateSnackbar(
const gfx::VectorIcon& vector_icon,
const base::string16& label,
const base::string16& button_label,
base::RepeatingCallback<void()> callback) {
base::RepeatingCallback<void()> callback,
AudioDelegate* audio_delegate) {
auto scaler = std::make_unique<ScaledDepthAdjuster>(kSnackbarDistance);
auto snackbar_layout =
......@@ -378,7 +372,8 @@ std::unique_ptr<UiElement> CreateSnackbar(
VR_BIND_COLOR(model, text.get(), &ColorScheme::snackbar_foreground,
&Text::SetColor);
auto button = Create<Button>(kNone, kPhaseForeground, callback);
auto button =
Create<Button>(kNone, kPhaseForeground, callback, audio_delegate);
button->SetType(kTypeSnackbarButton);
VR_BIND_BUTTON_COLORS(model, button.get(),
&ColorScheme::snackbar_button_colors,
......@@ -1958,8 +1953,8 @@ void UiSceneCreator::CreateUrlBar() {
url_click_callback = base::BindRepeating([] {});
}
auto origin_region =
Create<Button>(kUrlBarOriginRegion, kPhaseForeground, url_click_callback);
auto origin_region = Create<Button>(kUrlBarOriginRegion, kPhaseForeground,
url_click_callback, audio_delegate_);
origin_region->set_hit_testable(true);
origin_region->set_bounds_contain_children(true);
origin_region->set_hover_offset(0);
......@@ -1987,7 +1982,6 @@ void UiSceneCreator::CreateUrlBar() {
security_button->SetSize(kUrlBarButtonSizeDMM, kUrlBarButtonSizeDMM);
security_button->set_corner_radius(kUrlBarItemCornerRadiusDMM);
security_button->set_hover_offset(kOmniboxTextFieldIconButtonHoverOffsetDMM);
security_button->SetSounds(CreateButtonSounds(), audio_delegate_);
VR_BIND_VISIBILITY(security_button, model->toolbar_state.should_display_url);
VR_BIND_BUTTON_COLORS(model_, security_button.get(),
&ColorScheme::url_bar_button, &Button::SetButtonColors);
......@@ -2203,8 +2197,8 @@ void UiSceneCreator::CreateOverflowMenu() {
spacer->set_resizable_by_layout(true);
layout->AddChild(std::move(spacer));
auto background =
Create<Button>(std::get<0>(item), kPhaseForeground, base::DoNothing());
auto background = Create<Button>(std::get<0>(item), kPhaseForeground,
base::DoNothing(), audio_delegate_);
background->set_hit_testable(true);
background->set_bounds_contain_children(true);
background->set_hover_offset(0);
......@@ -2253,7 +2247,7 @@ void UiSceneCreator::CreateSnackbars() {
kDownloadedSnackbar, model_, kFileDownloadDoneIcon,
l10n_util::GetStringUTF16(IDS_VR_COMPONENT_UPDATE_READY),
base::i18n::ToUpper(l10n_util::GetStringUTF16(IDS_VR_COMPONENT_APPLY)),
base::DoNothing());
base::DoNothing(), audio_delegate_);
snackbar->SetVisible(false);
snackbar->SetRotate(1, 0, 0, kSnackbarMoveInAngle);
snackbar->SetTransitionedProperties({OPACITY, TRANSFORM});
......@@ -2478,7 +2472,6 @@ void UiSceneCreator::CreateOmnibox() {
kOmniboxTextFieldIconButtonSizeDMM);
mic_button->set_hover_offset(kOmniboxTextFieldIconButtonHoverOffsetDMM);
mic_button->set_corner_radius(kUrlBarItemCornerRadiusDMM);
mic_button->SetSounds(CreateButtonSounds(), audio_delegate_);
VR_BIND_VISIBILITY(mic_button,
model->speech.has_or_can_request_audio_permission &&
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment