Improve preview popup

This commit is contained in:
jaguillo 2015-10-28 20:56:28 +01:00
parent 8fbe456074
commit da72455d35
4 changed files with 88 additions and 26 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="bg" type="color">#151515</item>
<item name="bg" type="color">#1B1B1B</item>
<item name="key_bg" type="color">#303030</item>
<item name="key_down_bg" type="color">#1B1B1B</item>
<item name="key_label" type="color">#FFFFFF</item>
@ -8,7 +8,7 @@
<item name="key_sub_label" type="color">#A0A0A0</item>
<item name="preview_text" type="color">#FFFFFF</item>
<item name="preview_bg" type="color">#202020</item>
<item name="preview_stroke" type="color">#1B1B1B</item>
<item name="preview_stroke" type="color">#666666</item>
<item name="emoji_button_bg" type="color">#202020</item>
<item name="emoji_color" type="color">#FFFFFF</item>
<item name="emoji_key_bg" type="color">@color/emoji_button_bg</item>

View File

@ -4,12 +4,12 @@
<dimen name="margin_top">2dp</dimen>
<dimen name="margin_bottom">5dp</dimen>
<dimen name="key_padding">2dp</dimen>
<dimen name="key_bg_padding">1.4dp</dimen>
<dimen name="key_bg_padding">1.3dp</dimen>
<dimen name="key_height">50dp</dimen>
<dimen name="key_round">4dp</dimen>
<dimen name="label_text_size">16dp</dimen>
<dimen name="sublabel_text_size">10dp</dimen>
<dimen name="preview_text">18dp</dimen>
<dimen name="preview_text">16dp</dimen>
<dimen name="preview_corners">6dp</dimen>
<dimen name="preview_stroke">1dp</dimen>
<dimen name="preview_margin">4dp</dimen>

View File

@ -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);
}
}

View File

@ -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)