Skip to content

Commit

Permalink
[core] EditText - fix measure crash
Browse files Browse the repository at this point in the history
> EditText customize fonts
> EditText fix measure crash
> Allow load direct font from resources
  • Loading branch information
GabrielBRDeveloper committed Aug 1, 2024
1 parent ba4ff63 commit 43e56f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
4 changes: 3 additions & 1 deletion core/res/ex-layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
<Button
width="match_parent"
height="wrap_content"
text="Hello"
margin="5dp"/>
<Button
width="match_parent"
height="wrap_content"
text="World"
margin="5dp"/>
<Button
width="match_parent"
Expand All @@ -37,7 +39,7 @@


<ScrollView
width="256dp"
width="400dp"
height="match_parent">
<EditText
width="match_parent"
Expand Down
6 changes: 5 additions & 1 deletion core/src/br/nullexcept/mux/res/Resources.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ public Typeface getFont(String family, int style) {
if (family.toLowerCase().equals("default")) {
family = "roboto";
}
XmlElement fonts = requestXml(fixPath(family, "font"));
String path = fixPath(family, "font");
if (Manager.exists(path+".ttf")) {
return requestFont(path);
}
XmlElement fonts = requestXml(path);
Typeface font = Typeface.DEFAULT;

boolean bold = (style & Typeface.STYLE_BOLD) != 0;
Expand Down
55 changes: 42 additions & 13 deletions core/src/br/nullexcept/mux/widget/EditText.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import br.nullexcept.mux.view.View;
import br.nullexcept.mux.view.ViewGroup;

import java.util.Arrays;
import java.util.List;

public class EditText extends View {
private final Paint paint = new Paint();
private final Paint paintSelection = new Paint();
Expand Down Expand Up @@ -47,16 +50,36 @@ public EditText(Context context, AttributeList init) {
super(context, init);
setFocusable(true);
setOnClickListener(view -> requestFocus());
AttributeList list = initialAttributes();
list.searchColorList(AttrList.textColor, this::setTextColor);
list.searchDimension(AttrList.textSize, this::setTextSize);
list.searchBoolean(AttrList.singleLine, this::setSingleLine);
list.searchBoolean(AttrList.editable, this::setEditable);
list.searchColorList(AttrList.selectionColor, this::setSelectionColor);
list.searchText(AttrList.text, this::setText);
list.searchText(AttrList.hint, this::setHint);
AttributeList attrs = initialAttributes();
attrs.searchColorList(AttrList.textColor, this::setTextColor);
attrs.searchDimension(AttrList.textSize, this::setTextSize);
attrs.searchBoolean(AttrList.singleLine, this::setSingleLine);
attrs.searchBoolean(AttrList.editable, this::setEditable);
attrs.searchColorList(AttrList.selectionColor, this::setSelectionColor);
attrs.searchText(AttrList.text, this::setText);
attrs.searchText(AttrList.hint, this::setHint);

attrs.searchColorList(AttrList.hintColor, this::setHintColor);

{ // Customize typeface
String[] fontFamily = new String[]{"default"};
int[] fontStyle = new int[1];
attrs.searchRaw(AttrList.fontFamily, value -> fontFamily[0] = value);
attrs.searchRaw(AttrList.textStyle, value -> {
List<String> values = Arrays.asList(value.toLowerCase().split("\\|"));
if (values.contains("italic")) {
fontStyle[0] |= Typeface.STYLE_ITALIC;
}
if (values.contains("bold")) {
fontStyle[0] |= Typeface.STYLE_BOLD;
}
});

list.searchColorList(AttrList.hintColor, this::setHintColor);
Typeface font = context.getResources().getFont(fontFamily[0], fontStyle[0]);
if (font != null) {
setTypeface(font);
}
}
}

public void setOnTextChangedListener(OnTextChangedListener textChangedListener) {
Expand Down Expand Up @@ -239,10 +262,12 @@ protected void onKeyEvent(KeyEvent keyEvent) {
}break;
case KeyEvent.KEY_V: {
if (keyEvent.hasCtrl()) {
String content = ((ClipboardApplet)getContext().getApplet(Context.CLIPBOARD_APPLET)).getContent();
if (content == null) break;
if (text.getSelection().length() > 0) {
text.delete();
}
text.insert(((ClipboardApplet)getContext().getApplet(Context.CLIPBOARD_APPLET)).getContent());
text.insert(preFormatText(content));
changeText();
}
} break;
Expand Down Expand Up @@ -295,8 +320,12 @@ private void checkMeasure() {
}

private void measureText(boolean force) {
int mw = (int) (getMeasuredWidth() - getPaddingLeft() - getPaddingRight() - paint.getFontMetrics().measureText(" "));
int mh = (int) (getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - paint.getFontMetrics().measureText(" "));
measureText(force, getMeasuredWidth(), getMeasuredHeight());
}

private void measureText(boolean force, int width, int height) {
int mw = (int) (width - getPaddingLeft() - getPaddingRight() - paint.getFontMetrics().measureText(" "));
int mh = (int) (width - getPaddingTop() - getPaddingBottom() - paint.getFontMetrics().measureText(" "));
mw = Math.max(1, mw);
mh = Math.max(1, mh);
if (force || (textViewport.width != mw || textViewport.height != mw)) {
Expand Down Expand Up @@ -330,7 +359,7 @@ protected Size onMeasureContent(int parentWidth, int parentHeight) {
h = parentHeight;
}

measureText(true);
measureText(true, w,h);

return new Size(currentLayout().getWrapSize());
}
Expand Down

0 comments on commit 43e56f4

Please sign in to comment.