Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1698)

Unified Diff: ash/sticky_keys/sticky_keys_overlay.cc

Issue 137373003: Show overlay displaying the state of all sticky keys when it is enabled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ash/sticky_keys/sticky_keys_overlay.cc
diff --git a/ash/sticky_keys/sticky_keys_overlay.cc b/ash/sticky_keys/sticky_keys_overlay.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b818c16fffac1e3106da2569ba0caecac57b6f6d
--- /dev/null
+++ b/ash/sticky_keys/sticky_keys_overlay.cc
@@ -0,0 +1,255 @@
+// Copyright 2014 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 "ash/sticky_keys/sticky_keys_overlay.h"
+
+#include "ash/shell.h"
+#include "ash/shell_window_ids.h"
+#include "ash/sticky_keys/sticky_keys_controller.h"
+#include "base/strings/utf_string_conversions.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/gfx/animation/slide_animation.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/font_list.h"
+#include "ui/gfx/text_utils.h"
+#include "ui/views/border.h"
+#include "ui/views/controls/label.h"
+#include "ui/views/layout/box_layout.h"
+#include "ui/views/view.h"
+#include "ui/views/widget/widget.h"
+#include "ui/views/widget/widget_delegate.h"
+
+namespace ash {
+
+namespace {
+
+// Horizontal and vertical offset of the overlay on the screen.
James Cook 2014/01/14 00:59:03 Offset from... top-left corner? And maybe this sh
Tim Song 2014/01/14 03:01:10 Done.
+const int kOverlayOffset = 18;
+
+// Spacing between overlay contents and border.
+const int kHorizontalBorderSpacing = 9;
+const int kVerticalBorderSpacing = 4;
+
+// Spacing between modifier key labels.
+const int kKeyLabelSpacing = 7;
+
+// Label names for each modifier key.
+const char kControlKeyLabel[] = "ctrl";
+const char kAltKeyLabel[] = "alt";
+const char kShiftKeyLabel[] = "shift";
+const char kSearchKeyLabel[] = "search";
+
+// Duration of slide animation when overlay is shown or hidden.
+const int kSlideAnimationDurationMs = 100;
+
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// StickyKeyOverlayLabel
+class StickyKeyOverlayLabel : public views::Label {
+ public:
+ StickyKeyOverlayLabel(const std::string& key_name);
James Cook 2014/01/14 00:59:03 explicit
Tim Song 2014/01/14 03:01:10 Done.
+
+ virtual ~StickyKeyOverlayLabel();
+
+ void SetKeyState(StickyKeyState state);
+
+ private:
+ virtual void PaintText(gfx::Canvas* canvas,
+ const base::string16& text,
+ const gfx::Rect& text_bounds,
+ int flags) OVERRIDE;
+
+ DISALLOW_COPY_AND_ASSIGN(StickyKeyOverlayLabel);
+};
+
+StickyKeyOverlayLabel::StickyKeyOverlayLabel(const std::string& key_name) {
+ SetText(base::UTF8ToUTF16(key_name));
James Cook 2014/01/14 00:59:03 Should you be using localized strings here? I don'
Tim Song 2014/01/14 03:01:10 Done. Good point, I'm not really sure, but if I ha
+ SetHorizontalAlignment(gfx::ALIGN_LEFT);
+ SetFontList(
+ font_list().DeriveFontListWithSize(18));
+ SetMultiLine(true);
James Cook 2014/01/14 00:59:03 Does it have to be multiline? The text is single l
Tim Song 2014/01/14 03:01:10 Done.
+ SetAutoColorReadabilityEnabled(false);
+ SetFocusable(false);
+ SetEnabledColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
+ SetDisabledColor(SkColorSetARGB(0x80, 0xFF, 0xFF, 0xFF));
+ set_border(views::Border::CreateEmptyBorder(0, 0, 0, 0));
+}
+
+StickyKeyOverlayLabel::~StickyKeyOverlayLabel() {
+}
+
+void StickyKeyOverlayLabel::SetKeyState(StickyKeyState state) {
+ SkColor label_color = enabled_color();
+ int style;
James Cook 2014/01/14 00:59:03 either init style or don't init label_color, it's
Tim Song 2014/01/14 03:01:10 Done.
+ switch (state) {
+ case STICKY_KEY_STATE_ENABLED:
+ style = gfx::Font::NORMAL;
+ label_color = SkColorSetA(label_color, 0xFF);
+ break;
+ case STICKY_KEY_STATE_LOCKED:
+ style = gfx::Font::UNDERLINE;
+ label_color = SkColorSetA(label_color, 0xFF);
+ break;
+ default:
+ style = gfx::Font::NORMAL;
+ label_color = SkColorSetA(label_color, 0x80);
+ }
+
+ SetEnabledColor(label_color);
+ SetDisabledColor(label_color);
+ SetFontList(font_list().DeriveFontListWithSizeDeltaAndStyle(0, style));
+}
+
+void StickyKeyOverlayLabel::PaintText(gfx::Canvas* canvas,
+ const base::string16& text,
+ const gfx::Rect& text_bounds,
+ int flags) {
+ views::Label::PaintText(canvas,
+ text,
+ text_bounds,
+ flags | gfx::Canvas::NO_SUBPIXEL_RENDERING);
James Cook 2014/01/14 00:59:03 Why are you turning off subpixel rendering? Becaus
Tim Song 2014/01/14 03:01:10 Yeah, I was seeing some weird artifacts with the t
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+// StickyKeyOverlayLabel
+class StickyKeysOverlayView : public views::WidgetDelegateView {
+ public:
+ StickyKeysOverlayView();
+
+ virtual ~StickyKeysOverlayView();
+
+ virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
+
+ void SetKeyState(StickyKeyModifier modifier, StickyKeyState state);
+
+ private:
+ void AddKeyLabel(StickyKeyModifier modifier, const std::string& key_label);
+
+ typedef std::map<StickyKeyModifier, StickyKeyOverlayLabel*> ModifierLabelMap;
+ ModifierLabelMap modifier_label_map_;
+
+ DISALLOW_COPY_AND_ASSIGN(StickyKeysOverlayView);
+};
+
+StickyKeysOverlayView::StickyKeysOverlayView() {
+ SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
+ kHorizontalBorderSpacing,
+ kVerticalBorderSpacing,
+ kKeyLabelSpacing));
+ AddKeyLabel(STICKY_KEY_CONTROL, kControlKeyLabel);
+ AddKeyLabel(STICKY_KEY_ALT, kAltKeyLabel);
+ AddKeyLabel(STICKY_KEY_SHIFT, kShiftKeyLabel);
+ AddKeyLabel(STICKY_KEY_SEARCH, kSearchKeyLabel);
+}
+
+StickyKeysOverlayView::~StickyKeysOverlayView() {}
+
+void StickyKeysOverlayView::OnPaint(gfx::Canvas* canvas) {
+ SkPaint paint;
+ paint.setStyle(SkPaint::kFill_Style);
+ paint.setColor(SkColorSetARGB(0xB3, 0x55, 0x55, 0x55));
+ canvas->DrawRoundRect(GetLocalBounds(), 2, paint);
+ views::WidgetDelegateView::OnPaint(canvas);
+}
+
+void StickyKeysOverlayView::SetKeyState(StickyKeyModifier modifier,
+ StickyKeyState state) {
+ ModifierLabelMap::iterator it = modifier_label_map_.find(modifier);
+ if (it != modifier_label_map_.end()) {
James Cook 2014/01/14 00:59:03 optional: You could also DCHECK(it != map_.end()),
Tim Song 2014/01/14 03:01:10 Done.
+ StickyKeyOverlayLabel* label = it->second;
+ label->SetKeyState(state);
+ } else {
+ NOTREACHED();
+ }
+}
+
+void StickyKeysOverlayView::AddKeyLabel(StickyKeyModifier modifier,
+ const std::string& key_label) {
+ StickyKeyOverlayLabel* label = new StickyKeyOverlayLabel(key_label);
+ AddChildView(label);
+ modifier_label_map_[modifier] = label;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// StickyKeysOverlay
+StickyKeysOverlay::StickyKeysOverlay()
+ : is_showing_(false),
+ overlay_view_(NULL) {
+ overlay_view_ = new StickyKeysOverlayView();
James Cook 2014/01/14 00:59:03 do this in overlay_view_() above
Tim Song 2014/01/14 03:01:10 Done.
+
+ views::Widget::InitParams params;
+ params.type = views::Widget::InitParams::TYPE_POPUP;
+ params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
+ params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ params.accept_events = false;
+ params.can_activate = false;
+ params.keep_on_top = true;
+ params.remove_standard_frame = true;
+ params.delegate = overlay_view_;
+ params.bounds = gfx::Rect(gfx::Point(kOverlayOffset, kOverlayOffset),
+ GetWidgetSize());
+ params.parent = Shell::GetContainer(
+ Shell::GetTargetRootWindow(),
+ internal::kShellWindowId_OverlayContainer);
+ overlay_widget_.reset(new views::Widget);
+ overlay_widget_->Init(params);
+ overlay_widget_->SetVisibilityChangedAnimationsEnabled(false);
+ overlay_widget_->SetContentsView(overlay_view_);
+ overlay_widget_->GetNativeView()->SetName("StickyKeysOverlay");
+
+ animation_.reset(new gfx::SlideAnimation(this));
James Cook 2014/01/14 00:59:03 I think you should use layer animations for this.
+ animation_->SetSlideDuration(kSlideAnimationDurationMs);
+}
+
+StickyKeysOverlay::~StickyKeysOverlay() {}
+
+void StickyKeysOverlay::Show(bool visible) {
+ if (visible)
+ animation_->Show();
+ else
+ animation_->Hide();
+
+ is_showing_ = visible;
+}
+
+void StickyKeysOverlay::SetModifierKeyState(StickyKeyModifier modifier,
+ StickyKeyState state) {
+ overlay_view_->SetKeyState(modifier, state);
+}
+
+gfx::Size StickyKeysOverlay::GetWidgetSize() {
+ // Cache widget size so we don't recalculate the size multiple times during
+ // transition animations.
+ if (!widget_size_.get())
+ widget_size_.reset(new gfx::Size(overlay_view_->GetPreferredSize()));
+ return *widget_size_.get();
+}
+
+void StickyKeysOverlay::AnimationEnded(const gfx::Animation* animation) {
+ if (animation == animation_.get() && !animation_->IsShowing())
+ overlay_widget_->Hide();
+}
+
+void StickyKeysOverlay::AnimationProgressed(const gfx::Animation* animation) {
+ if (animation == animation_.get()) {
+ gfx::Size widget_size = GetWidgetSize();
+ int max_translation = widget_size.width() + kOverlayOffset;
+ int x = -widget_size.width() +
+ (animation_->GetCurrentValue() * max_translation);
+ overlay_widget_->SetBounds(gfx::Rect(gfx::Point(x, kOverlayOffset),
+ widget_size));
+ overlay_widget_->Show();
+ }
+}
+
+void StickyKeysOverlay::AnimationCanceled(const gfx::Animation* animation) {
+ gfx::Size widget_size = GetWidgetSize();
+ int x = is_showing_ ? kOverlayOffset : -widget_size.width();
+ overlay_widget_->SetBounds(gfx::Rect(gfx::Point(x, kOverlayOffset),
+ widget_size));
+}
+
+} // ash

Powered by Google App Engine
This is Rietveld 408576698