Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / sticky_keys / sticky_keys_overlay.cc
1 // Copyright 2014 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 "ash/sticky_keys/sticky_keys_overlay.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/sticky_keys/sticky_keys_controller.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/compositor/scoped_layer_animation_settings.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/font_list.h"
18 #include "ui/views/border.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/layout/box_layout.h"
21 #include "ui/views/view.h"
22 #include "ui/views/widget/widget.h"
23 #include "ui/views/widget/widget_delegate.h"
24
25 namespace ash {
26
27 namespace {
28
29 // Horizontal offset of the overlay from the top left of the screen.
30 const int kHorizontalOverlayOffset = 18;
31
32 // Vertical offset of the overlay from the top left of the screen.
33 const int kVerticalOverlayOffset = 18;
34
35 // Font style used for modifier key labels.
36 const ui::ResourceBundle::FontStyle kKeyLabelFontStyle =
37     ui::ResourceBundle::LargeFont;
38
39 // Duration of slide animation when overlay is shown or hidden.
40 const int kSlideAnimationDurationMs = 100;
41
42 }
43
44 ///////////////////////////////////////////////////////////////////////////////
45 //  StickyKeyOverlayLabel
46 class StickyKeyOverlayLabel : public views::Label {
47  public:
48   explicit StickyKeyOverlayLabel(const std::string& key_name);
49
50   virtual ~StickyKeyOverlayLabel();
51
52   StickyKeyState state() const { return state_; }
53
54   void SetKeyState(StickyKeyState state);
55
56  private:
57   // views::Label overrides:
58   virtual void PaintText(gfx::Canvas* canvas,
59                          const base::string16& text,
60                          const gfx::Rect& text_bounds,
61                          int flags) OVERRIDE;
62
63   StickyKeyState state_;
64
65   DISALLOW_COPY_AND_ASSIGN(StickyKeyOverlayLabel);
66 };
67
68 StickyKeyOverlayLabel::StickyKeyOverlayLabel(const std::string& key_name)
69     : state_(STICKY_KEY_STATE_DISABLED) {
70   ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
71
72   SetText(base::UTF8ToUTF16(key_name));
73   SetHorizontalAlignment(gfx::ALIGN_LEFT);
74   SetFontList(rb->GetFontList(kKeyLabelFontStyle));
75   SetAutoColorReadabilityEnabled(false);
76   SetFocusable(false);
77   SetEnabledColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
78   SetDisabledColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
79 }
80
81 StickyKeyOverlayLabel::~StickyKeyOverlayLabel() {
82 }
83
84 void StickyKeyOverlayLabel::SetKeyState(StickyKeyState state) {
85   state_ = state;
86   SkColor label_color;
87   int style;
88   switch (state) {
89     case STICKY_KEY_STATE_ENABLED:
90       style = gfx::Font::NORMAL;
91       label_color = SkColorSetA(enabled_color(), 0xFF);
92       break;
93     case STICKY_KEY_STATE_LOCKED:
94       style = gfx::Font::UNDERLINE;
95       label_color = SkColorSetA(enabled_color(), 0xFF);
96       break;
97     default:
98       style = gfx::Font::NORMAL;
99       label_color = SkColorSetA(enabled_color(), 0x80);
100   }
101
102   SetEnabledColor(label_color);
103   SetDisabledColor(label_color);
104   SetFontList(font_list().DeriveWithStyle(style));
105 }
106
107 void StickyKeyOverlayLabel::PaintText(gfx::Canvas* canvas,
108                                       const base::string16& text,
109                                       const gfx::Rect& text_bounds,
110                                       int flags) {
111   views::Label::PaintText(canvas,
112                           text,
113                           text_bounds,
114                           flags | gfx::Canvas::NO_SUBPIXEL_RENDERING);
115 }
116
117
118 ///////////////////////////////////////////////////////////////////////////////
119 //  StickyKeyOverlayLabel
120 class StickyKeysOverlayView : public views::WidgetDelegateView {
121  public:
122   StickyKeysOverlayView();
123
124   virtual ~StickyKeysOverlayView();
125
126   // views::WidgetDelegateView overrides:
127   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
128
129   void SetKeyState(ui::EventFlags modifier, StickyKeyState state);
130
131   StickyKeyState GetKeyState(ui::EventFlags modifier);
132
133  private:
134   void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label);
135
136   typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap;
137   ModifierLabelMap modifier_label_map_;
138
139   DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView);
140 };
141
142 StickyKeysOverlayView::StickyKeysOverlayView() {
143   const gfx::Font& font =
144       ui::ResourceBundle::GetSharedInstance().GetFont(kKeyLabelFontStyle);
145   int font_size = font.GetFontSize();
146   int font_padding = font.GetHeight() - font.GetBaseline();
147
148   // Text should have a margin of 0.5 times the font size on each side, so
149   // the spacing between two labels will be the same as the font size.
150   int horizontal_spacing = font_size / 2;
151   int vertical_spacing = font_size / 2 - font_padding;
152   int child_spacing = font_size - 2 * font_padding;
153
154   SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
155                                         horizontal_spacing,
156                                         vertical_spacing,
157                                         child_spacing));
158   AddKeyLabel(ui::EF_CONTROL_DOWN,
159               l10n_util::GetStringUTF8(IDS_ASH_CONTROL_KEY));
160   AddKeyLabel(ui::EF_ALT_DOWN,
161               l10n_util::GetStringUTF8(IDS_ASH_ALT_KEY));
162   AddKeyLabel(ui::EF_SHIFT_DOWN,
163               l10n_util::GetStringUTF8(IDS_ASH_SHIFT_KEY));
164 }
165
166 StickyKeysOverlayView::~StickyKeysOverlayView() {}
167
168 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) {
169   SkPaint paint;
170   paint.setStyle(SkPaint::kFill_Style);
171   paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55));
172   canvas->DrawRoundRect(GetLocalBounds(), 2, paint);
173   views::WidgetDelegateView::OnPaint(canvas);
174 }
175
176 void StickyKeysOverlayView::SetKeyState(ui::EventFlags modifier,
177                                         StickyKeyState state) {
178   ModifierLabelMap::iterator it = modifier_label_map_.find(modifier);
179   DCHECK(it != modifier_label_map_.end());
180   if (it != modifier_label_map_.end()) {
181     StickyKeyOverlayLabel* label = it->second;
182     label->SetKeyState(state);
183   }
184 }
185
186 StickyKeyState StickyKeysOverlayView::GetKeyState(ui::EventFlags modifier) {
187   ModifierLabelMap::iterator it = modifier_label_map_.find(modifier);
188   DCHECK(it != modifier_label_map_.end());
189   return it->second->state();
190 }
191
192 void StickyKeysOverlayView::AddKeyLabel(ui::EventFlags modifier,
193                                         const std::string& key_label) {
194   StickyKeyOverlayLabel* label = new StickyKeyOverlayLabel(key_label);
195   AddChildView(label);
196   modifier_label_map_[modifier] = label;
197 }
198
199 ///////////////////////////////////////////////////////////////////////////////
200 //  StickyKeysOverlay
201 StickyKeysOverlay::StickyKeysOverlay()
202     : is_visible_(false),
203       overlay_view_(new StickyKeysOverlayView),
204       widget_size_(overlay_view_->GetPreferredSize()) {
205   views::Widget::InitParams params;
206   params.type = views::Widget::InitParams::TYPE_POPUP;
207   params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
208   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
209   params.accept_events = false;
210   params.can_activate = false;
211   params.keep_on_top = true;
212   params.remove_standard_frame = true;
213   params.delegate = overlay_view_;
214   params.bounds = CalculateOverlayBounds();
215   params.parent = Shell::GetContainer(
216       Shell::GetTargetRootWindow(),
217       internal::kShellWindowId_OverlayContainer);
218   overlay_widget_.reset(new views::Widget);
219   overlay_widget_->Init(params);
220   overlay_widget_->SetVisibilityChangedAnimationsEnabled(false);
221   overlay_widget_->SetContentsView(overlay_view_);
222   overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay");
223 }
224
225 StickyKeysOverlay::~StickyKeysOverlay() {}
226
227 void StickyKeysOverlay::Show(bool visible) {
228   if (is_visible_ == visible)
229     return;
230
231   is_visible_ = visible;
232   if (is_visible_)
233     overlay_widget_->Show();
234   overlay_widget_->SetBounds(CalculateOverlayBounds());
235
236   ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
237   animator->AddObserver(this);
238
239   // Ensure transform is correct before beginning animation.
240   if (!animator->is_animating()) {
241     int sign = is_visible_ ? -1 : 1;
242     gfx::Transform transform;
243     transform.Translate(
244         sign * (widget_size_.width() + kHorizontalOverlayOffset), 0);
245     overlay_widget_->GetLayer()->SetTransform(transform);
246   }
247
248   ui::ScopedLayerAnimationSettings settings(animator);
249   settings.SetPreemptionStrategy(
250       ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
251   settings.SetTweenType(visible ? gfx::Tween::EASE_OUT : gfx::Tween::EASE_IN);
252   settings.SetTransitionDuration(
253       base::TimeDelta::FromMilliseconds(kSlideAnimationDurationMs));
254
255   overlay_widget_->GetLayer()->SetTransform(gfx::Transform());
256 }
257
258 void StickyKeysOverlay::SetModifierKeyState(ui::EventFlags modifier,
259                                             StickyKeyState state) {
260   overlay_view_->SetKeyState(modifier, state);
261 }
262
263 StickyKeyState StickyKeysOverlay::GetModifierKeyState(
264     ui::EventFlags modifier) {
265   return overlay_view_->GetKeyState(modifier);
266 }
267
268 gfx::Rect StickyKeysOverlay::CalculateOverlayBounds() {
269   int x = is_visible_ ? kHorizontalOverlayOffset : -widget_size_.width();
270   return gfx::Rect(gfx::Point(x, kVerticalOverlayOffset), widget_size_);
271 }
272
273 void StickyKeysOverlay::OnLayerAnimationEnded(
274     ui::LayerAnimationSequence* sequence) {
275   ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
276   if (animator)
277     animator->RemoveObserver(this);
278   if (!is_visible_)
279     overlay_widget_->Hide();
280 }
281
282 void StickyKeysOverlay::OnLayerAnimationAborted(
283     ui::LayerAnimationSequence* sequence) {
284   ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
285   if (animator)
286     animator->RemoveObserver(this);
287 }
288
289 void StickyKeysOverlay::OnLayerAnimationScheduled(
290     ui::LayerAnimationSequence* sequence) {
291 }
292
293 }  // namespace ash