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

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: Fix .gitignore and other issues 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
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..546edc2fa843b4b0222837ca7107d686cf349cfb 100644
--- a/services/keyboard_native/key_layout.cc
+++ b/services/keyboard_native/key_layout.cc
@@ -5,9 +5,14 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.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/SkTypeface.h"
+#include "ui/gfx/codec/png_codec.h"
namespace keyboard {
@@ -20,6 +25,7 @@ KeyLayout::TextKey::TextKey(
KeyLayout::TextKey::~TextKey() {
}
+// Key implementation.
viettrungluu 2015/06/02 22:29:35 You can probably skip this comment in the .cc file
APW 2015/06/02 23:16:55 Done.
void KeyLayout::TextKey::Draw(SkCanvas* canvas,
SkPaint paint,
const gfx::RectF& rect) {
@@ -36,6 +42,56 @@ void KeyLayout::TextKey::OnTouchUp() {
touch_up_callback_.Run(*this);
}
+KeyLayout::ImageKey::ImageKey(
+ const char* text,
+ base::Callback<void(const 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_);
+ DCHECK(result);
+ if (result) {
+ DCHECK(bitmap_.width() > 0);
viettrungluu 2015/06/02 22:29:35 nit: DCHECK_GT (though I suppose if it fails, you
APW 2015/06/02 23:16:54 Done.
+ DCHECK(bitmap_.height() > 0);
+ bitmap_valid_ = bitmap_.width() > 0 && bitmap_.height() > 0;
viettrungluu 2015/06/02 22:29:35 ... though personally I might just do: bitmap_val
APW 2015/06/02 23:16:54 Done.
+ }
+}
+
+KeyLayout::ImageKey::~ImageKey() {
+}
+
+// Key implementation.
viettrungluu 2015/06/02 22:29:35 ...
APW 2015/06/02 23:16:55 Done.
+void KeyLayout::ImageKey::Draw(SkCanvas* canvas,
+ SkPaint paint,
+ const gfx::RectF& rect) {
+ // 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* KeyLayout::ImageKey::ToText() const {
+ return text_key_.ToText();
+}
+
+void KeyLayout::ImageKey::OnTouchUp() {
+ text_key_.OnTouchUp();
+}
+
KeyLayout::KeyLayout()
: on_text_callback_(),
layout_(&letters_layout_),
@@ -199,6 +255,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 +288,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 +296,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 +334,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 +342,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 +388,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,7 +397,7 @@ 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,

Powered by Google App Engine
This is Rietveld 408576698