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.
5 #include "ash/sticky_keys/sticky_keys_overlay.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"
29 // Horizontal offset of the overlay from the top left of the screen.
30 const int kHorizontalOverlayOffset = 18;
32 // Vertical offset of the overlay from the top left of the screen.
33 const int kVerticalOverlayOffset = 18;
35 // Font style used for modifier key labels.
36 const ui::ResourceBundle::FontStyle kKeyLabelFontStyle =
37 ui::ResourceBundle::LargeFont;
39 // Duration of slide animation when overlay is shown or hidden.
40 const int kSlideAnimationDurationMs = 100;
44 ///////////////////////////////////////////////////////////////////////////////
45 // StickyKeyOverlayLabel
46 class StickyKeyOverlayLabel : public views::Label {
48 explicit StickyKeyOverlayLabel(const std::string& key_name);
50 virtual ~StickyKeyOverlayLabel();
52 StickyKeyState state() const { return state_; }
54 void SetKeyState(StickyKeyState state);
57 // views::Label overrides:
58 virtual void PaintText(gfx::Canvas* canvas,
59 const base::string16& text,
60 const gfx::Rect& text_bounds,
63 StickyKeyState state_;
65 DISALLOW_COPY_AND_ASSIGN(StickyKeyOverlayLabel);
68 StickyKeyOverlayLabel::StickyKeyOverlayLabel(const std::string& key_name)
69 : state_(STICKY_KEY_STATE_DISABLED) {
70 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
72 SetText(base::UTF8ToUTF16(key_name));
73 SetHorizontalAlignment(gfx::ALIGN_LEFT);
74 SetFontList(rb->GetFontList(kKeyLabelFontStyle));
75 SetAutoColorReadabilityEnabled(false);
77 SetEnabledColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
78 SetDisabledColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
81 StickyKeyOverlayLabel::~StickyKeyOverlayLabel() {
84 void StickyKeyOverlayLabel::SetKeyState(StickyKeyState state) {
89 case STICKY_KEY_STATE_ENABLED:
90 style = gfx::Font::NORMAL;
91 label_color = SkColorSetA(enabled_color(), 0xFF);
93 case STICKY_KEY_STATE_LOCKED:
94 style = gfx::Font::UNDERLINE;
95 label_color = SkColorSetA(enabled_color(), 0xFF);
98 style = gfx::Font::NORMAL;
99 label_color = SkColorSetA(enabled_color(), 0x80);
102 SetEnabledColor(label_color);
103 SetDisabledColor(label_color);
104 SetFontList(font_list().DeriveWithStyle(style));
107 void StickyKeyOverlayLabel::PaintText(gfx::Canvas* canvas,
108 const base::string16& text,
109 const gfx::Rect& text_bounds,
111 views::Label::PaintText(canvas,
114 flags | gfx::Canvas::NO_SUBPIXEL_RENDERING);
118 ///////////////////////////////////////////////////////////////////////////////
119 // StickyKeyOverlayLabel
120 class StickyKeysOverlayView : public views::WidgetDelegateView {
122 StickyKeysOverlayView();
124 virtual ~StickyKeysOverlayView();
126 // views::WidgetDelegateView overrides:
127 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
129 void SetKeyState(ui::EventFlags modifier, StickyKeyState state);
131 StickyKeyState GetKeyState(ui::EventFlags modifier);
134 void AddKeyLabel(ui::EventFlags modifier, const std::string& key_label);
136 typedef std::map<ui::EventFlags, StickyKeyOverlayLabel*> ModifierLabelMap;
137 ModifierLabelMap modifier_label_map_;
139 DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView);
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();
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;
154 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
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));
166 StickyKeysOverlayView::~StickyKeysOverlayView() {}
168 void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) {
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);
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);
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();
192 void StickyKeysOverlayView::AddKeyLabel(ui::EventFlags modifier,
193 const std::string& key_label) {
194 StickyKeyOverlayLabel* label = new StickyKeyOverlayLabel(key_label);
196 modifier_label_map_[modifier] = label;
199 ///////////////////////////////////////////////////////////////////////////////
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");
225 StickyKeysOverlay::~StickyKeysOverlay() {}
227 void StickyKeysOverlay::Show(bool visible) {
228 if (is_visible_ == visible)
231 is_visible_ = visible;
233 overlay_widget_->Show();
234 overlay_widget_->SetBounds(CalculateOverlayBounds());
236 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
237 animator->AddObserver(this);
239 // Ensure transform is correct before beginning animation.
240 if (!animator->is_animating()) {
241 int sign = is_visible_ ? -1 : 1;
242 gfx::Transform transform;
244 sign * (widget_size_.width() + kHorizontalOverlayOffset), 0);
245 overlay_widget_->GetLayer()->SetTransform(transform);
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));
255 overlay_widget_->GetLayer()->SetTransform(gfx::Transform());
258 void StickyKeysOverlay::SetModifierKeyState(ui::EventFlags modifier,
259 StickyKeyState state) {
260 overlay_view_->SetKeyState(modifier, state);
263 StickyKeyState StickyKeysOverlay::GetModifierKeyState(
264 ui::EventFlags modifier) {
265 return overlay_view_->GetKeyState(modifier);
268 gfx::Rect StickyKeysOverlay::CalculateOverlayBounds() {
269 int x = is_visible_ ? kHorizontalOverlayOffset : -widget_size_.width();
270 return gfx::Rect(gfx::Point(x, kVerticalOverlayOffset), widget_size_);
273 void StickyKeysOverlay::OnLayerAnimationEnded(
274 ui::LayerAnimationSequence* sequence) {
275 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
277 animator->RemoveObserver(this);
279 overlay_widget_->Hide();
282 void StickyKeysOverlay::OnLayerAnimationAborted(
283 ui::LayerAnimationSequence* sequence) {
284 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
286 animator->RemoveObserver(this);
289 void StickyKeysOverlay::OnLayerAnimationScheduled(
290 ui::LayerAnimationSequence* sequence) {