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

Unified Diff: services/keyboard_native/key_layout.cc

Issue 1149293002: Add the ability for keys to be drawn as an image. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Addressed nits Created 5 years, 7 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
« no previous file with comments | « services/keyboard_native/key_layout.h ('k') | services/keyboard_native/res/ArrowDown.png.sha1 » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: services/keyboard_native/key_layout.cc
diff --git a/services/keyboard_native/key_layout.cc b/services/keyboard_native/key_layout.cc
index 13957cb4b7ba347c43712b0a58fc89489b868c6b..53b849d616dbc2a8f01f604ff6435500604f2b79 100644
--- a/services/keyboard_native/key_layout.cc
+++ b/services/keyboard_native/key_layout.cc
@@ -5,36 +5,100 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
+#include "mojo/tools/embed/data.h"
+#include "services/keyboard_native/kActionIcon.h"
+#include "services/keyboard_native/kDeleteIcon.h"
+#include "services/keyboard_native/kLowerCaseIcon.h"
+#include "services/keyboard_native/kUpperCaseIcon.h"
#include "services/keyboard_native/key_layout.h"
#include "skia/ext/refptr.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkImageDecoder.h"
+#include "third_party/skia/include/core/SkStream.h"
#include "third_party/skia/include/core/SkTypeface.h"
+#include "ui/gfx/codec/png_codec.h"
+#include "ui/gfx/geometry/point.h"
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/geometry/rect_f.h"
namespace keyboard {
-KeyLayout::TextKey::TextKey(
- const char* text,
- base::Callback<void(const TextKey&)> touch_up_callback)
- : text_(text), touch_up_callback_(touch_up_callback) {
-}
+// An implementation of Key that draws itself as ASCII text.
+class KeyLayout::TextKey : public Key {
+ public:
+ TextKey(const char* text,
+ base::Callback<void(const TextKey&)> touch_up_callback)
+ : text_(text), touch_up_callback_(touch_up_callback) {}
+
+ ~TextKey() override {}
+
+ void Draw(SkCanvas* canvas,
+ const SkPaint& paint,
+ const gfx::RectF& rect) override {
+ float text_baseline_offset = rect.height() / 5.0f;
+ canvas->drawText(text_, strlen(text_), rect.x() + (rect.width() / 2.0f),
+ rect.y() + rect.height() - text_baseline_offset, paint);
+ }
-KeyLayout::TextKey::~TextKey() {
-}
+ const char* ToText() const override { return text_; }
-void KeyLayout::TextKey::Draw(SkCanvas* canvas,
- SkPaint paint,
- const gfx::RectF& rect) {
- float text_baseline_offset = rect.height() / 5.0f;
- canvas->drawText(text_, strlen(text_), rect.x() + (rect.width() / 2.0f),
- rect.y() + rect.height() - text_baseline_offset, paint);
-}
+ void OnTouchUp() override { touch_up_callback_.Run(*this); }
-const char* KeyLayout::TextKey::ToText() const {
- return text_;
-}
+ private:
+ const char* text_;
+ base::Callback<void(const TextKey&)> touch_up_callback_;
-void KeyLayout::TextKey::OnTouchUp() {
- touch_up_callback_.Run(*this);
-}
+ DISALLOW_COPY_AND_ASSIGN(TextKey);
+};
+
+// An implementation of Key that draws itself as an image.
+class ImageKey : public KeyLayout::Key {
+ public:
+ ImageKey(const char* text,
+ base::Callback<void(const KeyLayout::TextKey&)> touch_up_callback,
+ const mojo::embed::Data& data)
+ : text_key_(text, touch_up_callback), bitmap_valid_(false), bitmap_() {
+ bool result = gfx::PNGCodec::Decode(
+ reinterpret_cast<const unsigned char*>(data.data), data.size, &bitmap_);
+ bitmap_valid_ = result && bitmap_.width() > 0 && bitmap_.height() > 0;
+ DCHECK(bitmap_valid_);
+ }
+ ~ImageKey() override {}
+
+ // Key implementation.
+ void Draw(SkCanvas* canvas,
+ const SkPaint& paint,
+ const gfx::RectF& rect) override {
+ // If our bitmap is somehow invalid, default to drawing the text of the key.
+ if (!bitmap_valid_) {
+ text_key_.Draw(canvas, paint, rect);
+ return;
+ }
+
+ float width_scale = rect.width() / bitmap_.width();
+ float height_scale = rect.height() / bitmap_.height();
+ float scale = width_scale > height_scale ? height_scale : width_scale;
+ float target_width = bitmap_.width() * scale;
+ float target_height = bitmap_.height() * scale;
+ float delta_width = rect.width() - target_width;
+ float target_x = rect.x() + (delta_width / 2.0f);
+ float delta_height = rect.height() - target_height;
+ float target_y = rect.y() + (delta_height / 2.0f);
+ canvas->drawBitmapRect(
+ bitmap_,
+ SkRect::MakeXYWH(target_x, target_y, target_width, target_height),
+ &paint);
+ }
+ const char* ToText() const override { return text_key_.ToText(); }
+ void OnTouchUp() override { text_key_.OnTouchUp(); }
+
+ private:
+ KeyLayout::TextKey text_key_;
+ bool bitmap_valid_;
+ SkBitmap bitmap_;
+
+ DISALLOW_COPY_AND_ASSIGN(ImageKey);
+};
KeyLayout::KeyLayout()
: on_text_callback_(),
@@ -199,6 +263,15 @@ void KeyLayout::InitKeyMaps() {
base::Callback<void(const TextKey&)> switch_to_symbols_callback =
base::Bind(&KeyLayout::OnKeySwitchToSymbols, weak_factory_.GetWeakPtr());
+ ImageKey* switch_to_upper_case_image_key = new ImageKey(
+ "/\\", switch_to_upper_case_callback, keyboard_native::kUpperCaseIcon);
+ ImageKey* switch_to_lower_case_image_key = new ImageKey(
+ "\\/", switch_to_lower_case_callback, keyboard_native::kLowerCaseIcon);
+ ImageKey* delete_image_key =
+ new ImageKey("<-", delete_callback, keyboard_native::kDeleteIcon);
+ ImageKey* action_image_key =
+ new ImageKey(":)", do_nothing_callback, keyboard_native::kActionIcon);
+
std::vector<Key*> lower_case_key_map_row_one = {
new TextKey("q", emit_text_callback),
new TextKey("w", emit_text_callback),
@@ -223,7 +296,7 @@ void KeyLayout::InitKeyMaps() {
new TextKey("l", emit_text_callback)};
std::vector<Key*> lower_case_key_map_row_three = {
- new TextKey("/\\", switch_to_upper_case_callback),
+ switch_to_upper_case_image_key,
new TextKey("z", emit_text_callback),
new TextKey("x", emit_text_callback),
new TextKey("c", emit_text_callback),
@@ -231,14 +304,14 @@ void KeyLayout::InitKeyMaps() {
new TextKey("b", emit_text_callback),
new TextKey("n", emit_text_callback),
new TextKey("m", emit_text_callback),
- new TextKey("del", delete_callback)};
+ delete_image_key};
std::vector<Key*> lower_case_key_map_row_four = {
new TextKey("sym", switch_to_symbols_callback),
new TextKey(",", emit_text_callback),
new TextKey(" ", emit_text_callback),
new TextKey(".", emit_text_callback),
- new TextKey("act", do_nothing_callback)};
+ action_image_key};
lower_case_key_map_ = {lower_case_key_map_row_one,
lower_case_key_map_row_two,
@@ -269,7 +342,7 @@ void KeyLayout::InitKeyMaps() {
new TextKey("L", emit_text_callback)};
std::vector<Key*> upper_case_key_map_row_three = {
- new TextKey("\\/", switch_to_lower_case_callback),
+ switch_to_lower_case_image_key,
new TextKey("Z", emit_text_callback),
new TextKey("X", emit_text_callback),
new TextKey("C", emit_text_callback),
@@ -277,14 +350,14 @@ void KeyLayout::InitKeyMaps() {
new TextKey("B", emit_text_callback),
new TextKey("N", emit_text_callback),
new TextKey("M", emit_text_callback),
- new TextKey("DEL", delete_callback)};
+ delete_image_key};
std::vector<Key*> upper_case_key_map_row_four = {
new TextKey("SYM", switch_to_symbols_callback),
new TextKey(",", emit_text_callback),
new TextKey(" ", emit_text_callback),
new TextKey(".", emit_text_callback),
- new TextKey("ACT", do_nothing_callback)};
+ action_image_key};
upper_case_key_map_ = {upper_case_key_map_row_one,
upper_case_key_map_row_two,
@@ -323,7 +396,7 @@ void KeyLayout::InitKeyMaps() {
new TextKey(";", emit_text_callback),
new TextKey("!", emit_text_callback),
new TextKey("?", emit_text_callback),
- new TextKey("del", delete_callback)};
+ delete_image_key};
std::vector<Key*> symbols_key_map_row_four = {
new TextKey("ABC", switch_to_lower_case_callback),
@@ -332,12 +405,12 @@ void KeyLayout::InitKeyMaps() {
new TextKey(" ", emit_text_callback),
new TextKey("/", emit_text_callback),
new TextKey(".", emit_text_callback),
- new TextKey("act", do_nothing_callback)};
+ action_image_key};
symbols_key_map_ = {symbols_key_map_row_one,
symbols_key_map_row_two,
symbols_key_map_row_three,
symbols_key_map_row_four};
}
-
-} // namespace keyboard
+}
+// namespace keyboard
« no previous file with comments | « services/keyboard_native/key_layout.h ('k') | services/keyboard_native/res/ArrowDown.png.sha1 » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698