File: button.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (870 lines) | stat: -rw-r--r-- 27,383 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/button/button.h"

#include <utility>
#include <variant>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/functional/overloaded.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/actions/actions.h"
#include "ui/base/class_property.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom-forward.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/animation/throb_animation.h"
#include "ui/gfx/color_palette.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_highlight.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/controls/button/button_controller.h"
#include "ui/views/controls/button/button_controller_delegate.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/button/radio_button.h"
#include "ui/views/controls/button/toggle_button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/painter.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/view_class_properties.h"

#if defined(USE_AURA)
#include "ui/aura/client/capture_client.h"
#include "ui/aura/window.h"
#endif

namespace views {

namespace {

DEFINE_UI_CLASS_PROPERTY_KEY(bool, kIsButtonProperty, false)

}  // namespace

Button::DefaultButtonControllerDelegate::DefaultButtonControllerDelegate(
    Button* button)
    : ButtonControllerDelegate(button) {}

Button::DefaultButtonControllerDelegate::~DefaultButtonControllerDelegate() =
    default;

void Button::DefaultButtonControllerDelegate::RequestFocusFromEvent() {
  button()->RequestFocusFromEvent();
}

void Button::DefaultButtonControllerDelegate::NotifyClick(
    const ui::Event& event) {
  button()->NotifyClick(event);
}

void Button::DefaultButtonControllerDelegate::OnClickCanceled(
    const ui::Event& event) {
  button()->OnClickCanceled(event);
}

bool Button::DefaultButtonControllerDelegate::IsTriggerableEvent(
    const ui::Event& event) {
  return button()->IsTriggerableEvent(event);
}

bool Button::DefaultButtonControllerDelegate::ShouldEnterPushedState(
    const ui::Event& event) {
  return button()->ShouldEnterPushedState(event);
}

bool Button::DefaultButtonControllerDelegate::ShouldEnterHoveredState() {
  return button()->ShouldEnterHoveredState();
}

InkDrop* Button::DefaultButtonControllerDelegate::GetInkDrop() {
  return InkDrop::Get(button()->ink_drop_view())->GetInkDrop();
}

int Button::DefaultButtonControllerDelegate::GetDragOperations(
    const gfx::Point& press_pt) {
  return button()->GetDragOperations(press_pt);
}

bool Button::DefaultButtonControllerDelegate::InDrag() {
  return button()->InDrag();
}

Button::PressedCallback::PressedCallback(base::OnceClosure closure)
    : callback_(std::move(closure)) {}

Button::PressedCallback::PressedCallback(
    Button::PressedCallback::Callback callback)
    : callback_(std::move(callback)) {}

Button::PressedCallback::PressedCallback(base::RepeatingClosure closure)
    : callback_(std::move(closure)) {}

Button::PressedCallback::PressedCallback(PressedCallback&&) = default;

Button::PressedCallback& Button::PressedCallback::operator=(PressedCallback&&) =
    default;

Button::PressedCallback::~PressedCallback() = default;

Button::PressedCallback::operator bool() const {
  return std::visit([](const auto& callback) { return !callback.is_null(); },
                    callback_);
}

void Button::PressedCallback::Run(const ui::Event& event) {
  return std::visit(
      base::Overloaded{
          [](base::OnceClosure& closure) { std::move(closure).Run(); },
          [](const base::RepeatingClosure& closure) { closure.Run(); },
          [&](const Callback& callback) { callback.Run(event); },
      },
      callback_);
}

Button::ScopedAnchorHighlight::ScopedAnchorHighlight(
    base::WeakPtr<Button> button)
    : button_(std::move(button)) {}
Button::ScopedAnchorHighlight::~ScopedAnchorHighlight() {
  if (button_) {
    button_->ReleaseAnchorHighlight();
  }
}
Button::ScopedAnchorHighlight::ScopedAnchorHighlight(
    Button::ScopedAnchorHighlight&&) = default;

// We need to implement this one manually because the default move assignment
// operator does not call the destructor on `this`. That leads to us failing to
// release our reference on `button_`.
Button::ScopedAnchorHighlight& Button::ScopedAnchorHighlight::operator=(
    Button::ScopedAnchorHighlight&& other) {
  if (button_) {
    button_->ReleaseAnchorHighlight();
  }

  button_ = std::move(other.button_);
  return *this;
}

// static
constexpr Button::ButtonState Button::kButtonStates[STATE_COUNT];

// static
const Button* Button::AsButton(const views::View* view) {
  return AsButton(const_cast<View*>(view));
}

// static
Button* Button::AsButton(views::View* view) {
  if (view && view->GetProperty(kIsButtonProperty)) {
    return static_cast<Button*>(view);
  }
  return nullptr;
}

// static
Button::ButtonState Button::GetButtonStateFrom(ui::NativeTheme::State state) {
  switch (state) {
    case ui::NativeTheme::kDisabled:
      return Button::STATE_DISABLED;
    case ui::NativeTheme::kHovered:
      return Button::STATE_HOVERED;
    case ui::NativeTheme::kNormal:
      return Button::STATE_NORMAL;
    case ui::NativeTheme::kPressed:
      return Button::STATE_PRESSED;
    case ui::NativeTheme::kNumStates:
      NOTREACHED();
  }
  return Button::STATE_NORMAL;
}

Button::~Button() = default;

void Button::OnTooltipTextChanged(const std::u16string& old_tooltip_text) {
  View::OnTooltipTextChanged(old_tooltip_text);
  if ((GetViewAccessibility().GetCachedName().empty() ||
       GetViewAccessibility().GetCachedName() == old_tooltip_text) &&
      !GetTooltipText().empty()) {
    GetViewAccessibility().SetName(GetTooltipText());
  }

  OnSetTooltipText(GetTooltipText());
}

void Button::SetCallback(PressedCallback callback) {
  callback_ = std::move(callback);
}

void Button::AdjustAccessibleName(std::u16string& new_name,
                                  ax::mojom::NameFrom& name_from) {
  if (new_name.empty()) {
    new_name = GetAlternativeAccessibleName();
  }
}

std::u16string Button::GetAlternativeAccessibleName() const {
  return GetTooltipText();
}

Button::ButtonState Button::GetState() const {
  return state_;
}

void Button::SetState(ButtonState state) {
  if (state == state_) {
    return;
  }

  if (animate_on_state_change_) {
    if ((state_ == STATE_HOVERED) && (state == STATE_NORMAL)) {
      // For HOVERED -> NORMAL, animate from hovered (1) to not hovered (0).
      hover_animation_.Hide();
    } else if (state != STATE_HOVERED) {
      // For HOVERED -> PRESSED/DISABLED, or any transition not involving
      // HOVERED at all, simply set the state to not hovered (0).
      hover_animation_.Reset();
    } else if (state_ == STATE_NORMAL) {
      // For NORMAL -> HOVERED, animate from not hovered (0) to hovered (1).
      hover_animation_.Show();
    } else {
      // For PRESSED/DISABLED -> HOVERED, simply set the state to hovered (1).
      hover_animation_.Reset(1);
    }
  }
  // The hover animation affects the highlight state, make sure the highlight
  // state is correct if there are supposed to be anchor highlights.
  if (anchor_count_ > 0) {
    SetHighlighted(true);
  }

  ButtonState old_state = state_;
  state_ = state;

  GetViewAccessibility().SetIsEnabled(state_ != STATE_DISABLED);
  GetViewAccessibility().SetIsHovered(state_ == STATE_HOVERED);
  UpdateAccessibleCheckedState();
  StateChanged(old_state);
  OnPropertyChanged(&state_, kPropertyEffectsPaint);
}

int Button::GetTag() const {
  return tag_;
}

void Button::SetTag(int tag) {
  if (tag_ == tag) {
    return;
  }
  tag_ = tag;
  OnPropertyChanged(&tag_, kPropertyEffectsNone);
}

void Button::SetAnimationDuration(base::TimeDelta duration) {
  hover_animation_.SetSlideDuration(duration);
}

void Button::SetTriggerableEventFlags(int triggerable_event_flags) {
  if (triggerable_event_flags == triggerable_event_flags_) {
    return;
  }
  triggerable_event_flags_ = triggerable_event_flags;
  OnPropertyChanged(&triggerable_event_flags_, kPropertyEffectsNone);
}

int Button::GetTriggerableEventFlags() const {
  return triggerable_event_flags_;
}

void Button::SetRequestFocusOnPress(bool value) {
// On Mac, buttons should not request focus on a mouse press. Hence keep the
// default value i.e. false.
#if !BUILDFLAG(IS_MAC)
  if (request_focus_on_press_ == value) {
    return;
  }
  request_focus_on_press_ = value;
  OnPropertyChanged(&request_focus_on_press_, kPropertyEffectsNone);
#endif
}

bool Button::GetRequestFocusOnPress() const {
  return request_focus_on_press_;
}

void Button::SetAnimateOnStateChange(bool value) {
  if (value == animate_on_state_change_) {
    return;
  }
  animate_on_state_change_ = value;
  OnPropertyChanged(&animate_on_state_change_, kPropertyEffectsNone);
}

bool Button::GetAnimateOnStateChange() const {
  return animate_on_state_change_;
}

void Button::SetHideInkDropWhenShowingContextMenu(bool value) {
  if (value == hide_ink_drop_when_showing_context_menu_) {
    return;
  }
  hide_ink_drop_when_showing_context_menu_ = value;
  OnPropertyChanged(&hide_ink_drop_when_showing_context_menu_,
                    kPropertyEffectsNone);
}

bool Button::GetHideInkDropWhenShowingContextMenu() const {
  return hide_ink_drop_when_showing_context_menu_;
}

void Button::SetShowInkDropWhenHotTracked(bool value) {
  if (value == show_ink_drop_when_hot_tracked_) {
    return;
  }
  show_ink_drop_when_hot_tracked_ = value;
  OnPropertyChanged(&show_ink_drop_when_hot_tracked_, kPropertyEffectsNone);
}

bool Button::GetShowInkDropWhenHotTracked() const {
  return show_ink_drop_when_hot_tracked_;
}

void Button::SetHasInkDropActionOnClick(bool value) {
  if (value == has_ink_drop_action_on_click_) {
    return;
  }
  has_ink_drop_action_on_click_ = value;
  OnPropertyChanged(&has_ink_drop_action_on_click_, kPropertyEffectsNone);
}

bool Button::GetHasInkDropActionOnClick() const {
  return has_ink_drop_action_on_click_;
}

void Button::SetInstallFocusRingOnFocus(bool install) {
  if (install == GetInstallFocusRingOnFocus()) {
    return;
  }
  if (install) {
    FocusRing::Install(this);
  } else {
    FocusRing::Remove(this);
  }
}

bool Button::GetInstallFocusRingOnFocus() const {
  return FocusRing::Get(this) != nullptr;
}

void Button::SetHotTracked(bool is_hot_tracked) {
  if (state_ != STATE_DISABLED) {
    SetState(is_hot_tracked ? STATE_HOVERED : STATE_NORMAL);
    if (show_ink_drop_when_hot_tracked_) {
      InkDrop::Get(ink_drop_view_)
          ->AnimateToState(is_hot_tracked ? views::InkDropState::ACTIVATED
                                          : views::InkDropState::HIDDEN,
                           nullptr);
    }
  }

  if (is_hot_tracked) {
    NotifyAccessibilityEventDeprecated(ax::mojom::Event::kHover, true);
  }
}

bool Button::IsHotTracked() const {
  return state_ == STATE_HOVERED;
}

void Button::SetFocusPainter(std::unique_ptr<Painter> focus_painter) {
  focus_painter_ = std::move(focus_painter);
}

void Button::SetHighlighted(bool highlighted) {
  // Do nothing if the ink drop's target state matches what we are trying to set
  // since same state transitions may restart animations.
  InkDropState state = highlighted ? views::InkDropState::ACTIVATED
                                   : views::InkDropState::DEACTIVATED;
  if (InkDrop::Get(ink_drop_view_)->GetInkDrop()->GetTargetInkDropState() ==
      state) {
    return;
  }
  InkDrop::Get(ink_drop_view_)->AnimateToState(state, nullptr);
}

Button::ScopedAnchorHighlight Button::AddAnchorHighlight() {
  if (0 == anchor_count_++) {
    SetHighlighted(true);
  }
  anchor_count_changed_callbacks_.Notify(anchor_count_);
  return ScopedAnchorHighlight(GetWeakPtr());
}

base::CallbackListSubscription Button::AddStateChangedCallback(
    PropertyChangedCallback callback) {
  return AddPropertyChangedCallback(&state_, std::move(callback));
}

base::CallbackListSubscription Button::AddAnchorCountChangedCallback(
    base::RepeatingCallback<void(size_t)> callback) {
  return anchor_count_changed_callbacks_.Add(std::move(callback));
}

Button::KeyClickAction Button::GetKeyClickActionForEvent(
    const ui::KeyEvent& event) {
  if (event.key_code() == ui::VKEY_SPACE) {
    return PlatformStyle::kKeyClickActionOnSpace;
  }
  // Note that default buttons also have VKEY_RETURN installed as an accelerator
  // in LabelButton::SetIsDefault(). On platforms where
  // PlatformStyle::kReturnClicksFocusedControl, the logic here will take
  // precedence over that.
  if (event.key_code() == ui::VKEY_RETURN &&
      PlatformStyle::kReturnClicksFocusedControl) {
    return KeyClickAction::kOnKeyPress;
  }
  return KeyClickAction::kNone;
}

void Button::SetButtonController(
    std::unique_ptr<ButtonController> button_controller) {
  button_controller_ = std::move(button_controller);
  UpdateAccessibleDefaultActionVerb();
}

gfx::Point Button::GetMenuPosition() const {
  gfx::Rect lb = GetLocalBounds();

  // Offset of the associated menu position.
  constexpr gfx::Vector2d kMenuOffset{-2, -4};

  // The position of the menu depends on whether or not the locale is
  // right-to-left.
  gfx::Point menu_position(lb.right(), lb.bottom());
  if (base::i18n::IsRTL()) {
    menu_position.set_x(lb.x());
  }

  View::ConvertPointToScreen(this, &menu_position);
  if (base::i18n::IsRTL()) {
    menu_position.Offset(-kMenuOffset.x(), kMenuOffset.y());
  } else {
    menu_position += kMenuOffset;
  }

  DCHECK(GetWidget());
  const int max_x_coordinate =
      GetWidget()->GetWorkAreaBoundsInScreen().right() - 1;
  if (max_x_coordinate && max_x_coordinate <= menu_position.x()) {
    menu_position.set_x(max_x_coordinate - 1);
  }
  return menu_position;
}

void Button::SetInkDropView(View* view) {
  if (ink_drop_view_ == view) {
    return;
  }

  InkDrop::Remove(ink_drop_view_);
  ink_drop_view_ = view;
}

bool Button::OnMousePressed(const ui::MouseEvent& event) {
  return button_controller_->OnMousePressed(event);
}

bool Button::OnMouseDragged(const ui::MouseEvent& event) {
  if (state_ != STATE_DISABLED) {
    const bool should_enter_pushed = ShouldEnterPushedState(event);
    const bool should_show_pending =
        should_enter_pushed &&
        button_controller_->notify_action() ==
            ButtonController::NotifyAction::kOnRelease &&
        !InDrag();
    if (HitTestPoint(event.location())) {
      SetState(should_enter_pushed ? STATE_PRESSED : STATE_HOVERED);
      if (should_show_pending &&
          InkDrop::Get(ink_drop_view_)->GetInkDrop()->GetTargetInkDropState() ==
              views::InkDropState::HIDDEN) {
        InkDrop::Get(ink_drop_view_)
            ->AnimateToState(views::InkDropState::ACTION_PENDING, &event);
      }
    } else {
      SetState(STATE_NORMAL);
      if (should_show_pending &&
          InkDrop::Get(ink_drop_view_)->GetInkDrop()->GetTargetInkDropState() ==
              views::InkDropState::ACTION_PENDING) {
        InkDrop::Get(ink_drop_view_)
            ->AnimateToState(views::InkDropState::HIDDEN, &event);
      }
    }
  }
  return true;
}

void Button::OnMouseReleased(const ui::MouseEvent& event) {
  button_controller_->OnMouseReleased(event);
}

void Button::OnMouseCaptureLost() {
  // Starting a drag results in a MouseCaptureLost. Reset button state.
  // TODO(varkha): Reset the state even while in drag. The same logic may
  // applies everywhere so gather any feedback and update.
  if (state_ != STATE_DISABLED) {
    SetState(STATE_NORMAL);
  }
  InkDrop::Get(ink_drop_view_)
      ->AnimateToState(views::InkDropState::HIDDEN, nullptr /* event */);
  InkDrop::Get(ink_drop_view_)->GetInkDrop()->SetHovered(false);
  View::OnMouseCaptureLost();
}

void Button::OnMouseEntered(const ui::MouseEvent& event) {
  button_controller_->OnMouseEntered(event);
}

void Button::OnMouseExited(const ui::MouseEvent& event) {
  button_controller_->OnMouseExited(event);
}

void Button::OnMouseMoved(const ui::MouseEvent& event) {
  button_controller_->OnMouseMoved(event);
}

bool Button::OnKeyPressed(const ui::KeyEvent& event) {
  return button_controller_->OnKeyPressed(event);
}

bool Button::OnKeyReleased(const ui::KeyEvent& event) {
  return button_controller_->OnKeyReleased(event);
}

void Button::OnGestureEvent(ui::GestureEvent* event) {
  button_controller_->OnGestureEvent(event);
}

bool Button::AcceleratorPressed(const ui::Accelerator& accelerator) {
  SetState(STATE_NORMAL);
  NotifyClick(accelerator.ToKeyEvent());
  return true;
}

bool Button::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) {
  // If this button is focused and the user presses space or enter, don't let
  // that be treated as an accelerator if there is a key click action
  // corresponding to it.
  return GetKeyClickActionForEvent(event) != KeyClickAction::kNone;
}

void Button::ShowContextMenu(const gfx::Point& p,
                             ui::mojom::MenuSourceType source_type) {
  if (!context_menu_controller()) {
    return;
  }

  // We're about to show the context menu. Showing the context menu likely means
  // we won't get a mouse exited and reset state. Reset it now to be sure.
  if (state_ != STATE_DISABLED) {
    SetState(STATE_NORMAL);
  }
  if (hide_ink_drop_when_showing_context_menu_) {
    InkDrop::Get(ink_drop_view_)->GetInkDrop()->SetHovered(false);
    InkDrop::Get(ink_drop_view_)
        ->AnimateToState(InkDropState::HIDDEN, nullptr /* event */);
  }
  View::ShowContextMenu(p, source_type);
}

void Button::OnDragDone() {
  // Only reset the state to normal if the button isn't currently disabled
  // (since disabled buttons may still be able to be dragged).
  if (state_ != STATE_DISABLED) {
    SetState(STATE_NORMAL);
  }
  if (anchor_count_ > 0) {
    SetHighlighted(true);
  } else {
    InkDrop::Get(ink_drop_view_)
        ->AnimateToState(InkDropState::HIDDEN, nullptr /* event */);
  }
}

void Button::OnPaint(gfx::Canvas* canvas) {
  View::OnPaint(canvas);
  PaintButtonContents(canvas);
  Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
}

void Button::VisibilityChanged(View* starting_from, bool visible) {
  View::VisibilityChanged(starting_from, visible);
  if (state_ == STATE_DISABLED) {
    return;
  }
  SetState(visible && ShouldEnterHoveredState() ? STATE_HOVERED : STATE_NORMAL);
  if (visible && anchor_count_ > 0) {
    SetHighlighted(true);
  }
}

void Button::ViewHierarchyChanged(const ViewHierarchyChangedDetails& details) {
  if (!details.is_add && state_ != STATE_DISABLED && details.child == this) {
    SetState(STATE_NORMAL);
  }
  View::ViewHierarchyChanged(details);
}

void Button::OnFocus() {
  View::OnFocus();
  if (focus_painter_) {
    SchedulePaint();
  }
}

void Button::OnBlur() {
  View::OnBlur();
  if (IsHotTracked() || state_ == STATE_PRESSED) {
    SetState(STATE_NORMAL);
    if (InkDrop::Get(ink_drop_view_)->GetInkDrop()->GetTargetInkDropState() !=
        views::InkDropState::HIDDEN) {
      InkDrop::Get(ink_drop_view_)
          ->AnimateToState(views::InkDropState::HIDDEN, nullptr /* event */);
    }
    // TODO(bruthig) : Fix Buttons to work well when multiple input
    // methods are interacting with a button. e.g. By animating to HIDDEN here
    // it is possible for a Mouse Release to trigger an action however there
    // would be no visual cue to the user that this will occur.
  }
  if (focus_painter_) {
    SchedulePaint();
  }
}

std::unique_ptr<ActionViewInterface> Button::GetActionViewInterface() {
  return std::make_unique<ButtonActionViewInterface>(this);
}

void Button::AnimationProgressed(const gfx::Animation* animation) {
  SchedulePaint();
}

Button::Button(PressedCallback callback)
    : AnimationDelegateViews(this), callback_(std::move(callback)) {
  InkDrop::Install(this, std::make_unique<InkDropHost>(this));

  SetFocusBehavior(PlatformStyle::kDefaultFocusBehavior);
  SetProperty(kIsButtonProperty, true);
  hover_animation_.SetSlideDuration(base::Milliseconds(150));
  SetInstallFocusRingOnFocus(true);
  button_controller_ = std::make_unique<ButtonController>(
      this, std::make_unique<DefaultButtonControllerDelegate>(this));
  InkDrop::Get(ink_drop_view_)
      ->SetCreateInkDropCallback(base::BindRepeating(
          [](Button* button) {
            std::unique_ptr<InkDrop> ink_drop =
                InkDrop::CreateInkDropForFloodFillRipple(InkDrop::Get(button));
            ink_drop->SetShowHighlightOnFocus(!FocusRing::Get(button));
            return ink_drop;
          },
          base::Unretained(this)));
  // TODO(pbos): Investigate not setting a default color so that we can DCHECK
  // if one hasn't been set.
  InkDrop::Get(ink_drop_view_)->SetBaseColor(gfx::kPlaceholderColor);

  GetViewAccessibility().SetRole(ax::mojom::Role::kButton);
  UpdateAccessibleDefaultActionVerb();
}

void Button::RequestFocusFromEvent() {
  if (request_focus_on_press_) {
    RequestFocus();
  }
}

void Button::NotifyClick(const ui::Event& event) {
  if (has_ink_drop_action_on_click_) {
    InkDrop::Get(ink_drop_view_)
        ->AnimateToState(InkDropState::ACTION_TRIGGERED,
                         ui::LocatedEvent::FromIfValid(&event));
  }

  // If we have an associated help context ID, notify that system that we have
  // been activated.
  const ui::ElementIdentifier element_id = GetProperty(kElementIdentifierKey);
  if (element_id) {
    views::ElementTrackerViews::GetInstance()->NotifyViewActivated(element_id,
                                                                   this);
  }

  if (callback_) {
    callback_.Run(event);
  }
}

void Button::OnClickCanceled(const ui::Event& event) {
  if (ShouldUpdateInkDropOnClickCanceled()) {
    if (InkDrop::Get(ink_drop_view_)->GetInkDrop()->GetTargetInkDropState() ==
            views::InkDropState::ACTION_PENDING ||
        InkDrop::Get(ink_drop_view_)->GetInkDrop()->GetTargetInkDropState() ==
            views::InkDropState::ALTERNATE_ACTION_PENDING) {
      InkDrop::Get(ink_drop_view_)
          ->AnimateToState(views::InkDropState::HIDDEN,
                           ui::LocatedEvent::FromIfValid(&event));
    }
  }
}

void Button::OnSetTooltipText(const std::u16string& tooltip_text) {}

void Button::StateChanged(ButtonState old_state) {}

bool Button::IsTriggerableEvent(const ui::Event& event) {
  return button_controller_->IsTriggerableEvent(event);
}

bool Button::ShouldUpdateInkDropOnClickCanceled() const {
  return true;
}

bool Button::ShouldEnterPushedState(const ui::Event& event) {
  return IsTriggerableEvent(event);
}

void Button::PaintButtonContents(gfx::Canvas* canvas) {}

bool Button::ShouldEnterHoveredState() {
  if (!GetVisible()) {
    return false;
  }

  bool check_mouse_position = true;
#if defined(USE_AURA)
  // If another window has capture, we shouldn't check the current mouse
  // position because the button won't receive any mouse events - so if the
  // mouse was hovered, the button would be stuck in a hovered state (since it
  // would never receive OnMouseExited).
  const Widget* widget = GetWidget();
  if (widget && widget->GetNativeWindow()) {
    aura::Window* root_window = widget->GetNativeWindow()->GetRootWindow();
    aura::client::CaptureClient* capture_client =
        aura::client::GetCaptureClient(root_window);
    aura::Window* capture_window =
        capture_client ? capture_client->GetGlobalCaptureWindow() : nullptr;
    check_mouse_position = !capture_window || capture_window == root_window;
  }
#endif

  return check_mouse_position && IsMouseHovered();
}

base::WeakPtr<Button> Button::GetWeakPtr() {
  return weak_ptr_factory_.GetWeakPtr();
}

void Button::OnEnabledChanged() {
  if (GetEnabled() ? (state_ != STATE_DISABLED) : (state_ == STATE_DISABLED)) {
    return;
  }

  if (GetEnabled()) {
    bool should_enter_hover_state = ShouldEnterHoveredState();
    SetState(should_enter_hover_state ? STATE_HOVERED : STATE_NORMAL);
    InkDrop::Get(ink_drop_view_)
        ->GetInkDrop()
        ->SetHovered(should_enter_hover_state);
  } else {
    SetState(STATE_DISABLED);
    InkDrop::Get(ink_drop_view_)->GetInkDrop()->SetHovered(false);
  }
  UpdateAccessibleDefaultActionVerb();
}

void Button::UpdateAccessibleCheckedState() {
  switch (state_) {
    case STATE_PRESSED:
      GetViewAccessibility().SetCheckedState(ax::mojom::CheckedState::kTrue);
      break;
    default:
      GetViewAccessibility().RemoveCheckedState();
      break;
  }
}

void Button::ReleaseAnchorHighlight() {
  if (0 == --anchor_count_) {
    SetHighlighted(false);
  }
  anchor_count_changed_callbacks_.Notify(anchor_count_);
}

void Button::SetDefaultActionVerb(ax::mojom::DefaultActionVerb verb) {
  default_action_verb_ = verb;
}

void Button::UpdateAccessibleDefaultActionVerb() {
  if (GetEnabled()) {
    GetViewAccessibility().SetDefaultActionVerb(default_action_verb_);
  } else {
    GetViewAccessibility().RemoveDefaultActionVerb();
  }

  if (button_controller_) {
    button_controller_->UpdateButtonAccessibleDefaultActionVerb();
  }
}

ButtonActionViewInterface::ButtonActionViewInterface(Button* action_view)
    : BaseActionViewInterface(action_view), action_view_(action_view) {}

void ButtonActionViewInterface::ActionItemChangedImpl(
    actions::ActionItem* action_item) {
  BaseActionViewInterface::ActionItemChangedImpl(action_item);
  std::u16string tooltip_text(action_item->GetTooltipText());
  if (!tooltip_text.empty()) {
    action_view_->SetTooltipText(std::move(tooltip_text));
  }
}

void ButtonActionViewInterface::LinkActionInvocationToView(
    base::RepeatingClosure invoke_action_callback) {
  if (!action_view_) {
    return;
  }
  action_view_->SetCallback(invoke_action_callback);
}

BEGIN_METADATA(Button)
ADD_PROPERTY_METADATA(PressedCallback, Callback)
ADD_PROPERTY_METADATA(bool, AnimateOnStateChange)
ADD_PROPERTY_METADATA(bool, HasInkDropActionOnClick)
ADD_PROPERTY_METADATA(bool, HideInkDropWhenShowingContextMenu)
ADD_PROPERTY_METADATA(bool, InstallFocusRingOnFocus)
ADD_PROPERTY_METADATA(bool, RequestFocusOnPress)
ADD_PROPERTY_METADATA(ButtonState, State)
ADD_PROPERTY_METADATA(int, Tag)
ADD_PROPERTY_METADATA(int, TriggerableEventFlags)
END_METADATA

}  // namespace views

DEFINE_ENUM_CONVERTERS(views::Button::ButtonState,
                       {views::Button::STATE_NORMAL, u"STATE_NORMAL"},
                       {views::Button::STATE_HOVERED, u"STATE_HOVERED"},
                       {views::Button::STATE_PRESSED, u"STATE_PRESSED"},
                       {views::Button::STATE_DISABLED, u"STATE_DISABLED"})