From da72455d358bffa481caf75ead21846506644128 Mon Sep 17 00:00:00 2001 From: jaguillo Date: Wed, 28 Oct 2015 20:56:28 +0100 Subject: [PATCH] Improve preview popup --- res/values/colors.xml | 4 +- res/values/dimens.xml | 4 +- srcs/juloo.keyboard2/KeyPreviewPopup.java | 83 ++++++++++++++++++----- srcs/juloo.keyboard2/Keyboard2View.java | 23 +++++-- 4 files changed, 88 insertions(+), 26 deletions(-) diff --git a/res/values/colors.xml b/res/values/colors.xml index 1d64e28..82f3e10 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -1,6 +1,6 @@ - #151515 + #1B1B1B #303030 #1B1B1B #FFFFFF @@ -8,7 +8,7 @@ #A0A0A0 #FFFFFF #202020 - #1B1B1B + #666666 #202020 #FFFFFF @color/emoji_button_bg diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 3f84659..7b19e38 100644 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -4,12 +4,12 @@ 2dp 5dp 2dp - 1.4dp + 1.3dp 50dp 4dp 16dp 10dp - 18dp + 16dp 6dp 1dp 4dp diff --git a/srcs/juloo.keyboard2/KeyPreviewPopup.java b/srcs/juloo.keyboard2/KeyPreviewPopup.java index 5cccefb..0aa5e03 100644 --- a/srcs/juloo.keyboard2/KeyPreviewPopup.java +++ b/srcs/juloo.keyboard2/KeyPreviewPopup.java @@ -1,5 +1,7 @@ package juloo.keyboard2; +import android.os.Handler; +import android.os.Message; import android.view.Gravity; import android.view.View; import android.view.View.MeasureSpec; @@ -8,12 +10,19 @@ import android.widget.PopupWindow; import android.widget.TextView; class KeyPreviewPopup extends PopupWindow + implements Handler.Callback { - private TextView _content; - private View _anchor; - private int _bottomMargin; + private final TextView _content; + private final View _anchor; - public KeyPreviewPopup(View anchor) + private final int _bottomMargin; + private final long _dismissTimeout; + + private final Handler _handler; + + private int _minWidth; + + public KeyPreviewPopup(View anchor, long dismissTimeout) { super(anchor.getContext()); _content = new TextView(anchor.getContext()); @@ -25,6 +34,9 @@ class KeyPreviewPopup extends PopupWindow _content.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); _anchor = anchor; _bottomMargin = (int)anchor.getResources().getDimension(R.dimen.preview_margin); + _dismissTimeout = dismissTimeout; + _handler = new Handler(this); + setMinWidth(0); setWidth(WindowManager.LayoutParams.WRAP_CONTENT); setHeight(WindowManager.LayoutParams.WRAP_CONTENT); setBackgroundDrawable(anchor.getResources().getDrawable(R.drawable.preview_popup)); @@ -33,26 +45,63 @@ class KeyPreviewPopup extends PopupWindow setTouchable(false); } - public void setPreview(String preview) + @Override + public boolean handleMessage(Message msg) { - if (preview == null) + forceDismiss(); + return (true); + } + + public void forceDismiss() + { + setMinWidth(0); + dismiss(); + } + + public void setPreview(KeyValue key, int flags) + { + StringBuilder preview; + + if (key == null) { - System.out.println("popup preview dismiss"); - dismiss(); - } - else - { - System.out.println("popup preview: " + preview); - _content.setText(preview); - if (!isShowing()) - show(); + _handler.sendEmptyMessageDelayed(0, _dismissTimeout); + return ; } + _handler.removeMessages(0); + preview = new StringBuilder(); + if ((flags & KeyValue.FLAG_CTRL) != 0) + preview.append("Ctrl-"); + if ((flags & KeyValue.FLAG_ALT) != 0) + preview.append("Alt-"); + if ((flags & KeyValue.FLAG_SHIFT) != 0 && !Character.isLetter(key.getChar(0))) + preview.append("Shift-"); + preview.append(key.getSymbol(flags)); + _content.setText(preview.toString()); + show(); + } + + private void setMinWidth(int minWidth) + { + _minWidth = minWidth; + _content.setMinWidth(minWidth); } private void show() { + int x; + int y; + int width; + int height; + _content.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); - showAtLocation(_anchor, Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, - -(_content.getMeasuredHeight() + _bottomMargin)); + width = _content.getMeasuredWidth(); + height = _content.getMeasuredHeight(); + if (width > _minWidth) + setMinWidth(width); + x = (_anchor.getMeasuredWidth() - width) / 2; + y = -(height + _bottomMargin); + if (!isShowing()) + showAtLocation(_anchor, Gravity.NO_GRAVITY, x, y); + update(x, y, width, height); } } diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 6cc8642..1158c80 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -43,7 +43,11 @@ public class Keyboard2View extends View ** TODO: move config values in a Config object ** TODO: settings: preview_enabled ** TODO: settings: preview_text_size + ** TODO: settings: preview_timeout + ** TODO: disable preview in password fields */ + private long _previewDismissTimeout = 150; // especialy this one + private float _marginTop; private float _keyWidth; private float _keyPadding; @@ -77,7 +81,7 @@ public class Keyboard2View extends View super(context, attrs); _vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); _handler = new Handler(this); - _previewPopup = new KeyPreviewPopup(this); + _previewPopup = new KeyPreviewPopup(this, _previewDismissTimeout); _horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin); _marginTop = getResources().getDimension(R.dimen.margin_top); _marginBottom = getResources().getDimension(R.dimen.margin_bottom); @@ -311,13 +315,22 @@ public class Keyboard2View extends View { if (key.value != null && (key.flags & (KeyValue.FLAG_LOCKED | KeyValue.FLAG_NOCHAR)) == 0) ((Keyboard2)getContext()).handleKeyUp(key.value, _flags); - _previewPopup.setPreview(null); // TODO: preview next down key + // previewNextKeyDown + for (KeyDown k : _downKeys) + if ((k.value.getFlags() & (KeyValue.FLAG_KEY_FONT | KeyValue.FLAG_NOREPEAT | KeyValue.FLAG_NOCHAR)) == 0) + { + _previewPopup.setPreview(k.value, _flags); + return ; + } + _previewPopup.setPreview(null, 0); } private void handleKeyDown(KeyValue key) { - if (key != null) - _previewPopup.setPreview(key.getSymbol(_flags)); + if (key == null) + return ; + if ((key.getFlags() & (KeyValue.FLAG_KEY_FONT | KeyValue.FLAG_NOREPEAT | KeyValue.FLAG_NOCHAR)) == 0) + _previewPopup.setPreview(key, _flags); vibrate(); } @@ -422,7 +435,7 @@ public class Keyboard2View extends View public void onDetachedFromWindow() { super.onDetachedFromWindow(); - _previewPopup.setPreview(null); + _previewPopup.forceDismiss(); } private void drawLabel(Canvas canvas, KeyValue k, float x, float y, boolean locked)