summaryrefslogtreecommitdiffstats
path: root/views/controls/button/custom_button.cc
blob: e507d9373599e8359896b96f098651d31886814f (plain)
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
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

#include "app/throb_animation.h"
#include "base/keyboard_codes.h"

namespace views {

// How long the hover animation takes if uninterrupted.
static const int kHoverFadeDurationMs = 150;

////////////////////////////////////////////////////////////////////////////////
// CustomButton, public:

CustomButton::~CustomButton() {
}

void CustomButton::SetState(ButtonState state) {
  if (state != state_) {
    if (animate_on_state_change_ || !hover_animation_->IsAnimating()) {
      animate_on_state_change_ = true;
      if (state_ == BS_NORMAL && state == BS_HOT) {
        // Button is hovered from a normal state, start hover animation.
        hover_animation_->Show();
      } else if (state_ == BS_HOT && state == BS_NORMAL) {
        // Button is returning to a normal state from hover, start hover
        // fade animation.
        hover_animation_->Hide();
      } else {
        hover_animation_->Stop();
      }
    }

    state_ = state;
    SchedulePaint();
  }
}

void CustomButton::StartThrobbing(int cycles_til_stop) {
  animate_on_state_change_ = false;
  hover_animation_->StartThrobbing(cycles_til_stop);
}

void CustomButton::SetAnimationDuration(int duration) {
  hover_animation_->SetSlideDuration(duration);
}

////////////////////////////////////////////////////////////////////////////////
// CustomButton, View overrides:

void CustomButton::SetEnabled(bool enabled) {
  if (enabled && state_ == BS_DISABLED) {
    SetState(BS_NORMAL);
  } else if (!enabled && state_ != BS_DISABLED) {
    SetState(BS_DISABLED);
  }
}

bool CustomButton::IsEnabled() const {
  return state_ != BS_DISABLED;
}

bool CustomButton::IsFocusable() const {
  return (state_ != BS_DISABLED) && View::IsFocusable();
}

////////////////////////////////////////////////////////////////////////////////
// CustomButton, protected:

CustomButton::CustomButton(ButtonListener* listener)
    : Button(listener),
      state_(BS_NORMAL),
      animate_on_state_change_(true),
      triggerable_event_flags_(MouseEvent::EF_LEFT_BUTTON_DOWN) {
  hover_animation_.reset(new ThrobAnimation(this));
  hover_animation_->SetSlideDuration(kHoverFadeDurationMs);
}

bool CustomButton::IsTriggerableEvent(const MouseEvent& e) {
  return (triggerable_event_flags_ & e.GetFlags()) != 0;
}

////////////////////////////////////////////////////////////////////////////////
// CustomButton, View overrides (protected):

bool CustomButton::AcceleratorPressed(const Accelerator& accelerator) {
  if (enabled_) {
    SetState(BS_NORMAL);
#if defined(OS_WIN)
    KeyEvent key_event(Event::ET_KEY_RELEASED, accelerator.GetKeyCode(), 0, 0);
#elif defined(OS_LINUX)
    GdkEventKey gdk_key;
    memset(&gdk_key, 0, sizeof(GdkEventKey));
    gdk_key.type = GDK_KEY_RELEASE;
    gdk_key.keyval = accelerator.GetKeyCode();
    gdk_key.state = (accelerator.IsAltDown() << 3) +
                    (accelerator.IsCtrlDown() << 2) +
                    accelerator.IsShiftDown();
    KeyEvent key_event(&gdk_key);
#endif
    NotifyClick(key_event);
    return true;
  }
  return false;
}

bool CustomButton::OnMousePressed(const MouseEvent& e) {
  if (state_ != BS_DISABLED) {
    if (ShouldEnterPushedState(e) && HitTest(e.location()))
      SetState(BS_PUSHED);
    RequestFocus();
  }
  return true;
}

bool CustomButton::OnMouseDragged(const MouseEvent& e) {
  if (state_ != BS_DISABLED) {
    if (!HitTest(e.location()))
      SetState(BS_NORMAL);
    else if (ShouldEnterPushedState(e))
      SetState(BS_PUSHED);
    else
      SetState(BS_HOT);
  }
  return true;
}

void CustomButton::OnMouseReleased(const MouseEvent& e, bool canceled) {
  if (InDrag()) {
    // Starting a drag results in a MouseReleased, we need to ignore it.
    return;
  }

  if (state_ != BS_DISABLED) {
    if (canceled || !HitTest(e.location())) {
      SetState(BS_NORMAL);
    } else {
      SetState(BS_HOT);
      if (IsTriggerableEvent(e)) {
        NotifyClick(e);
        // We may be deleted at this point (by the listener's notification
        // handler) so no more doing anything, just return.
        return;
      }
    }
  }
}

void CustomButton::OnMouseEntered(const MouseEvent& e) {
  if (state_ != BS_DISABLED)
    SetState(BS_HOT);
}

void CustomButton::OnMouseMoved(const MouseEvent& e) {
  if (state_ != BS_DISABLED) {
    if (HitTest(e.location())) {
      SetState(BS_HOT);
    } else {
      SetState(BS_NORMAL);
    }
  }
}

void CustomButton::OnMouseExited(const MouseEvent& e) {
  // Starting a drag results in a MouseExited, we need to ignore it.
  if (state_ != BS_DISABLED && !InDrag())
    SetState(BS_NORMAL);
}

bool CustomButton::OnKeyPressed(const KeyEvent& e) {
  if (state_ != BS_DISABLED) {
    // Space sets button state to pushed. Enter clicks the button. This matches
    // the Windows native behavior of buttons, where Space clicks the button
    // on KeyRelease and Enter clicks the button on KeyPressed.
    if (e.GetCharacter() == base::VKEY_SPACE) {
      SetState(BS_PUSHED);
      return true;
    } else if  (e.GetCharacter() == base::VKEY_RETURN) {
      SetState(BS_NORMAL);
      NotifyClick(e);
      return true;
    }
  }
  return false;
}

bool CustomButton::OnKeyReleased(const KeyEvent& e) {
  if (state_ != BS_DISABLED) {
    if (e.GetCharacter() == base::VKEY_SPACE) {
      SetState(BS_NORMAL);
      NotifyClick(e);
      return true;
    }
  }
  return false;
}

void CustomButton::OnDragDone() {
  SetState(BS_NORMAL);
}

void CustomButton::ShowContextMenu(int x, int y, bool is_mouse_gesture) {
  if (GetContextMenuController()) {
    // 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_ != BS_DISABLED)
      SetState(BS_NORMAL);
    View::ShowContextMenu(x, y, is_mouse_gesture);
  }
}

void CustomButton::ViewHierarchyChanged(bool is_add, View *parent,
                                        View *child) {
  if (!is_add && state_ != BS_DISABLED)
    SetState(BS_NORMAL);
}

void CustomButton::SetHotTracked(bool flag) {
  if (state_ != BS_DISABLED)
    SetState(flag ? BS_HOT : BS_NORMAL);
}

bool CustomButton::IsHotTracked() const {
  return state_ == BS_HOT;
}

void CustomButton::WillLoseFocus() {
  if (IsHotTracked())
    SetState(BS_NORMAL);
}

////////////////////////////////////////////////////////////////////////////////
// CustomButton, AnimationDelegate implementation:

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

bool CustomButton::ShouldEnterPushedState(const MouseEvent& e) {
  return IsTriggerableEvent(e);
}

////////////////////////////////////////////////////////////////////////////////
// CustomButton, private:

void CustomButton::SetHighlighted(bool highlighted) {
  if (highlighted && state_ != BS_DISABLED) {
    SetState(BS_HOT);
  } else if (!highlighted && state_ != BS_DISABLED) {
    SetState(BS_NORMAL);
  }
}

bool CustomButton::IsHighlighted() const {
  return state_ == BS_HOT;
}

bool CustomButton::IsPushed() const {
  return state_ == BS_PUSHED;
}

}  // namespace views