|
16 | 16 | import br.nullexcept.mux.view.View; |
17 | 17 | import br.nullexcept.mux.view.ViewGroup; |
18 | 18 |
|
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | + |
19 | 22 | public class EditText extends View { |
20 | 23 | private final Paint paint = new Paint(); |
21 | 24 | private final Paint paintSelection = new Paint(); |
@@ -47,16 +50,36 @@ public EditText(Context context, AttributeList init) { |
47 | 50 | super(context, init); |
48 | 51 | setFocusable(true); |
49 | 52 | setOnClickListener(view -> requestFocus()); |
50 | | - AttributeList list = initialAttributes(); |
51 | | - list.searchColorList(AttrList.textColor, this::setTextColor); |
52 | | - list.searchDimension(AttrList.textSize, this::setTextSize); |
53 | | - list.searchBoolean(AttrList.singleLine, this::setSingleLine); |
54 | | - list.searchBoolean(AttrList.editable, this::setEditable); |
55 | | - list.searchColorList(AttrList.selectionColor, this::setSelectionColor); |
56 | | - list.searchText(AttrList.text, this::setText); |
57 | | - list.searchText(AttrList.hint, this::setHint); |
| 53 | + AttributeList attrs = initialAttributes(); |
| 54 | + attrs.searchColorList(AttrList.textColor, this::setTextColor); |
| 55 | + attrs.searchDimension(AttrList.textSize, this::setTextSize); |
| 56 | + attrs.searchBoolean(AttrList.singleLine, this::setSingleLine); |
| 57 | + attrs.searchBoolean(AttrList.editable, this::setEditable); |
| 58 | + attrs.searchColorList(AttrList.selectionColor, this::setSelectionColor); |
| 59 | + attrs.searchText(AttrList.text, this::setText); |
| 60 | + attrs.searchText(AttrList.hint, this::setHint); |
| 61 | + |
| 62 | + attrs.searchColorList(AttrList.hintColor, this::setHintColor); |
| 63 | + |
| 64 | + { // Customize typeface |
| 65 | + String[] fontFamily = new String[]{"default"}; |
| 66 | + int[] fontStyle = new int[1]; |
| 67 | + attrs.searchRaw(AttrList.fontFamily, value -> fontFamily[0] = value); |
| 68 | + attrs.searchRaw(AttrList.textStyle, value -> { |
| 69 | + List<String> values = Arrays.asList(value.toLowerCase().split("\\|")); |
| 70 | + if (values.contains("italic")) { |
| 71 | + fontStyle[0] |= Typeface.STYLE_ITALIC; |
| 72 | + } |
| 73 | + if (values.contains("bold")) { |
| 74 | + fontStyle[0] |= Typeface.STYLE_BOLD; |
| 75 | + } |
| 76 | + }); |
58 | 77 |
|
59 | | - list.searchColorList(AttrList.hintColor, this::setHintColor); |
| 78 | + Typeface font = context.getResources().getFont(fontFamily[0], fontStyle[0]); |
| 79 | + if (font != null) { |
| 80 | + setTypeface(font); |
| 81 | + } |
| 82 | + } |
60 | 83 | } |
61 | 84 |
|
62 | 85 | public void setOnTextChangedListener(OnTextChangedListener textChangedListener) { |
@@ -239,10 +262,12 @@ protected void onKeyEvent(KeyEvent keyEvent) { |
239 | 262 | }break; |
240 | 263 | case KeyEvent.KEY_V: { |
241 | 264 | if (keyEvent.hasCtrl()) { |
| 265 | + String content = ((ClipboardApplet)getContext().getApplet(Context.CLIPBOARD_APPLET)).getContent(); |
| 266 | + if (content == null) break; |
242 | 267 | if (text.getSelection().length() > 0) { |
243 | 268 | text.delete(); |
244 | 269 | } |
245 | | - text.insert(((ClipboardApplet)getContext().getApplet(Context.CLIPBOARD_APPLET)).getContent()); |
| 270 | + text.insert(preFormatText(content)); |
246 | 271 | changeText(); |
247 | 272 | } |
248 | 273 | } break; |
@@ -295,8 +320,12 @@ private void checkMeasure() { |
295 | 320 | } |
296 | 321 |
|
297 | 322 | private void measureText(boolean force) { |
298 | | - int mw = (int) (getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - paint.getFontMetrics().measureText(" ")); |
299 | | - int mh = (int) (getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - paint.getFontMetrics().measureText(" ")); |
| 323 | + measureText(force, getMeasuredWidth(), getMeasuredHeight()); |
| 324 | + } |
| 325 | + |
| 326 | + private void measureText(boolean force, int width, int height) { |
| 327 | + int mw = (int) (width - getPaddingLeft() - getPaddingRight() - paint.getFontMetrics().measureText(" ")); |
| 328 | + int mh = (int) (width - getPaddingTop() - getPaddingBottom() - paint.getFontMetrics().measureText(" ")); |
300 | 329 | mw = Math.max(1, mw); |
301 | 330 | mh = Math.max(1, mh); |
302 | 331 | if (force || (textViewport.width != mw || textViewport.height != mw)) { |
@@ -330,7 +359,7 @@ protected Size onMeasureContent(int parentWidth, int parentHeight) { |
330 | 359 | h = parentHeight; |
331 | 360 | } |
332 | 361 |
|
333 | | - measureText(true); |
| 362 | + measureText(true, w,h); |
334 | 363 |
|
335 | 364 | return new Size(currentLayout().getWrapSize()); |
336 | 365 | } |
|
0 commit comments