abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/KeyPreviewPopup.java
blob: 0aa5e0393681e976eb104fc5f0f80e05d994c369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package juloo.keyboard2;

import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

class KeyPreviewPopup extends PopupWindow
	implements Handler.Callback
{
	private final TextView	_content;
	private final 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());
		_content.setTextColor(anchor.getResources().getColor(R.color.preview_text));
		_content.setTextSize(anchor.getResources().getDimension(R.dimen.preview_text));
		int padding = (int)anchor.getResources().getDimension(R.dimen.preview_padding);
		_content.setPaddingRelative(padding, padding, padding, padding);
		_content.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
		_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));
		setContentView(_content);
		setClippingEnabled(false);
		setTouchable(false);
	}

	@Override
	public boolean		handleMessage(Message msg)
	{
		forceDismiss();
		return (true);
	}

	public void			forceDismiss()
	{
		setMinWidth(0);
		dismiss();
	}

	public void			setPreview(KeyValue key, int flags)
	{
		StringBuilder		preview;

		if (key == null)
		{
			_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);
		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);
	}
}