Skip to content

Commit

Permalink
[core] Refactory EditText
Browse files Browse the repository at this point in the history
-> EditText line breaks.
-> EditText hintColor
-> Obtain Looper from context
-> Delete unsed TransitionAttr file.
-> Allow multiples buffer size in text layout
  • Loading branch information
GabrielBRDeveloper committed Aug 1, 2024
1 parent 4927dff commit a7ce7ba
Show file tree
Hide file tree
Showing 17 changed files with 380 additions and 366 deletions.
10 changes: 9 additions & 1 deletion core/res/ex-layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@
width="match_parent"
height="wrap_content"
progress="50"
background="#F00"
max="100"
margin="5dp"/>

</LinearLayout>


<ScrollView
width="256dp"
height="match_parent">
<EditText
width="match_parent"
height="wrap_content"
margin="5dp"/>
</ScrollView>
</FrameLayout>
2 changes: 2 additions & 0 deletions core/res/style/defaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<item name="colorPrimary">#1F9EDE</item>
<item name="colorPrimaryVariant">#0B384F</item>
<item name="colorOnSurface">#FFF</item>
<item name="colorHint">#5FFF</item>
<item name="colorOnSurfaceVariant">#FFFFFF</item>
<item name="colorSurfaceVariant">#29282E</item>
<item name="colorSurface">#1C1B1F</item>
Expand Down Expand Up @@ -35,6 +36,7 @@

<style name="Widget.TextView">
<item name="textColor">?colorOnSurface</item>
<item name="hintColor">?colorHint</item>
</style>

<style name="Widget.EditText" parent="Widget.TextView">
Expand Down
1 change: 1 addition & 0 deletions core/src/br/nullexcept/mux/C.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static class Flags {
public static final boolean DEBUG_OVERLAY;
public static final boolean DISABLE_AUTO_REDRAW;
public static final boolean AUTO_GC = true;
public static boolean DUMB_VIEWS = false;
public static boolean RESOURCES_CACHE_XML = true;
public static boolean RESOURCES_CACHE_FONTS = true;

Expand Down
2 changes: 2 additions & 0 deletions core/src/br/nullexcept/mux/app/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ public <T extends Activity> void startActivity(Launch<T> launch) {
Activity nw = launch.make();
nw.stack = stack.newStack(nw);
nw._args = launch;
nw.setMainLooper(getMainLooper());
nw.mWindow = window;

window.setWindowObserver(getApplication().buildObserver(nw));
window.getWindowObserver().onCreated();
} else {
Activity nw = launch.make();
nw.setMainLooper(getMainLooper());
nw._args = launch;
nw.stack = new ActivityStack(nw);
getApplication().boot(getApplication().getBootstrap().makeWindow(), nw);
Expand Down
17 changes: 13 additions & 4 deletions core/src/br/nullexcept/mux/app/ApplicationRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ void start() {
this.resources = new Resources(this);

Activity nw = project.getLaunch().make();
nw.appRuntime = this;
nw.setMainLooper(getMainLooper());
nw.setAppRuntime(this);
nw.stack = new ActivityStack(nw);
looper.postDelayed(() -> boot(bootstrap.makeWindow(), nw), 0);
looper.post(this::loop);
Expand All @@ -83,9 +84,15 @@ void start() {
Looper.sleep(2000); // Wait for all services stop
}

@Override
public Looper getMainLooper() {
return looper;
}

void boot(Window window, Activity activity) {
window.reset();
activity.appRuntime = this;
activity.setAppRuntime(this);
activity.setMainLooper(getMainLooper());
activity.mWindow = window;
window.setWindowObserver(buildObserver(activity));
window.create();
Expand All @@ -106,8 +113,9 @@ synchronized <T extends Service> T beginService(Launch<T> launch) {

Looper serviceLooper = new Looper();
service.myLooper = serviceLooper;
service.setAppRuntime(this);
service.setMainLooper(getMainLooper());
service._args = launch;
service.appRuntime = this;

services.put(name, service);
new Thread(() -> {
Expand Down Expand Up @@ -191,7 +199,8 @@ Window.WindowObserver buildObserver(Activity activity) {
synchronized (activities) {
activities.add(activity.stack);
}
activity.appRuntime = this;
activity.setMainLooper(getMainLooper());
activity.setAppRuntime(this);
return new WindowObserver(activity);
}

Expand Down
19 changes: 18 additions & 1 deletion core/src/br/nullexcept/mux/app/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,29 @@ public class Context {
public static final String CLIPBOARD_APPLET = "applet.os.clipboard";
public static final String DISPLAY_APPLET = "applet.os.display";

ApplicationRuntime appRuntime;
private ApplicationRuntime appRuntime;
Launch _args;
private Looper mainLooper;

public Context(){
}

void setMainLooper(Looper mainLooper) {
if (mainLooper != null) {
this.mainLooper = mainLooper;
}
}

void setAppRuntime(ApplicationRuntime appRuntime) {
if (appRuntime != null) {
this.appRuntime = appRuntime;
}
}

public Looper getMainLooper() {
return mainLooper;
}

public File getFilesDir() {
return appRuntime.getFilesDir();
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/br/nullexcept/mux/graphics/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public class Color {
public static final int BLACK = Color.rgb(0,0,0);
public static final int WHITE = Color.rgb(255,255,255);
public static final int TRANSPARENT = Color.argb(0,0,0,0);
public static final int YELLOW = Color.rgb(255,255,0);
public static final int MAGENTA = Color.rgb(255,0,255);
public static final int ORANGE = Color.rgb(255,100,0);

public static int red(int color){
return (color >> 16) & 0xFF;
Expand Down
14 changes: 10 additions & 4 deletions core/src/br/nullexcept/mux/res/LayoutInflater.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.nullexcept.mux.res;

import br.nullexcept.mux.C;
import br.nullexcept.mux.app.Context;
import br.nullexcept.mux.lang.ValuedFunction;
import br.nullexcept.mux.lang.xml.XmlElement;
Expand Down Expand Up @@ -35,11 +36,16 @@ private <T extends View> T inflate(XmlElement xml){
return inflate(xml.attr("layout"));
}
Resources res = context.getResources();
FallbackAttributes agent = new FallbackAttributes(xml, (FallbackAttributes) res.obtainStyled("Widget."+xml.name()), res);
if (!registers.containsKey(xml.name())){
throw new RuntimeException("Invalid view class "+xml.name()+" you need register class before use.");
String viewName = xml.name();
if ((!registers.containsKey(viewName))){
if (C.Flags.DUMB_VIEWS) {
viewName = "View";
} else {
throw new RuntimeException("Invalid view class " + xml.name() + " you need register class before use.");
}
}
ViewRegister register = registers.get(xml.name());
FallbackAttributes agent = new FallbackAttributes(xml, (FallbackAttributes) res.obtainStyled("Widget."+viewName), res);
ViewRegister register = registers.get(viewName);
View view = register.create(context,agent);
view.setLayoutParams(parseLayoutParams(agent));
if (view instanceof ViewGroup){
Expand Down
43 changes: 30 additions & 13 deletions core/src/br/nullexcept/mux/text/TextLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@

public class TextLayout {
private final Editable text;
private final FontMetrics font;
private final Paint paint;
private FontMetrics font;
private final Size viewport = new Size();
private final Selection selection;
private TextRenderer drawer;

private int gravity;
// [INTERNAL LINE] => LINE | START | END | WIDTH
private int breakLines = 0;
private Size wrapSize = new Size();
private final int[][] lines = new int[10240][4]; //160KB of buffer allow 10240 lines
private final Size wrapSize = new Size();
private final int[][] lines;

public TextLayout(Editable text, Paint paint, TextRenderer drawer) {
this(text, paint, drawer, 10240); //160KB of buffer allow 10.240 lines
}

public TextLayout(Editable text, Paint paint, TextRenderer drawer, int lineBufferSize) {
this.lines = new int[lineBufferSize][4];
this.text = text;
this.selection = text.getSelection();
this.paint = paint;
this.font = paint.getFontMetrics();
this.drawer = drawer;
}
Expand All @@ -41,6 +48,7 @@ public int getHeight() {
/* NEED MAKE A WORD BREAKS */
public void measure(int width, int height, int gravity) {
this.gravity = gravity;
this.font = paint.getFontMetrics();
width = Math.max(1, width);
height = Math.max(1, height);

Expand Down Expand Up @@ -93,7 +101,7 @@ private int measureLine(int index, int start, int end, int line) {
int e = Math.min(start + x, end);
width = measureRange(start, e);
while (width > viewport.width && e > start) {
width -= font.measureChar(text.charAt(e & (text.length() - 1)));
width -= measureChar(text.charAt(e & (text.length() - 1)));
e--;
}

Expand All @@ -119,6 +127,10 @@ private int measureLine(int index, int start, int end, int line) {
return lineCount;
}

private float measureChar(char ch) {
return font.measureChar(ch);
}

private int findInternalLine(int index) {
int line = text.getLineIndex(index);
int c = Math.max(0, line-1);
Expand All @@ -132,7 +144,7 @@ private int findInternalLine(int index) {
private int measureRange(int start, int end) {
int w = 0;
for (int i = start; i < end; i++) {
w += font.measureChar(text.charAt(i));
w += measureChar(text.charAt(i));
}
return w;
}
Expand Down Expand Up @@ -176,30 +188,33 @@ private void drawCaret(Canvas canvas) {
}
int x = 0;
for (int i = lines[line][1]; i < lines[line][2] && i != l; i++) {
x += font.measureChar(text.charAt(i));
x += measureChar(text.charAt(i));
}

drawer.drawCaret(canvas,x, (int) (line*font.getLineHeight()));
int y = Gravity.apply(Gravity.vertical(gravity), viewport.height, Math.round(Math.max(1,breakLines) * font.getLineHeight()));
x += Gravity.apply(Gravity.horizontal(gravity),viewport.width, lines[line][3]);
drawer.drawCaret(canvas,x, (int) (line*font.getLineHeight())+y);
}

private void drawSelection(Canvas canvas) {
int startLine = findInternalLine(selection.low());
int endLine = findInternalLine(selection.high());

if (startLine == endLine) {
drawSelection(canvas, selection.low(), selection.end(), startLine);
drawSelection(canvas, selection.low(), selection.high(), startLine);
} else {
drawSelection(canvas, selection.low(), lines[startLine][2], startLine);
for (int i = startLine + 1; i < endLine; i++) {
drawSelection(canvas, lines[i][1], lines[i][2], i);
}
drawSelection(canvas, lines[endLine][1], selection.end(), endLine);
drawSelection(canvas, lines[endLine][1], selection.high(), endLine);
}
}

private void drawSelection(Canvas canvas, int start, int end, int line) {
int z = Gravity.apply(Gravity.vertical(gravity), viewport.height, Math.round(breakLines * font.getLineHeight()));
int w;
int xf = measureRange(lines[line][1], start);

if (start == lines[line][1] && end == lines[line][2]) {
w = lines[line][3];
Expand All @@ -208,19 +223,21 @@ private void drawSelection(Canvas canvas, int start, int end, int line) {
}

int y = (int) (line * font.getLineHeight()) + z;
int x = Gravity.apply(Gravity.horizontal(gravity), viewport.width, w);
drawer.drawSelection(canvas, x,y, w, Math.round(font.getLineHeight()));
int x = Gravity.apply(Gravity.horizontal(gravity), viewport.width, lines[line][3]);
drawer.drawSelection(canvas, x+xf,y, w, Math.round(font.getLineHeight()));
}

public void drawLine(Canvas canvas, int y, int index) {
y += font.getAscent();
int[] line = lines[index];
int x = Gravity.apply(Gravity.horizontal(gravity),viewport.width,line[3]);

int in = 0;
for (int i = line[1]; i < line[2]; i++) {
char ch = text.charAt(i);
drawer.drawCharacter(canvas, ch, x, y);
drawer.drawCharacter(canvas, ch, x, y,i, line[1], line[2]);
x += font.measureChar(ch);
in++;
}
}

Expand All @@ -234,7 +251,7 @@ public int getGravity() {

public interface TextRenderer {
void drawSelection(Canvas canvas, int x, int y, int width, int height);
void drawCharacter(Canvas canvas, char ch, int x, int y);
void drawCharacter(Canvas canvas, char ch, int x, int y, int charIndex, int lineStart, int lineEnd);
void drawCaret(Canvas canvas, int x, int y);
}
}
5 changes: 3 additions & 2 deletions core/src/br/nullexcept/mux/utils/LinearAnimation.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package br.nullexcept.mux.utils;

import br.nullexcept.mux.app.Looper;
import br.nullexcept.mux.view.View;

public abstract class LinearAnimation {
private int duration;
private boolean loop = false;
private final Looper looper;
public LinearAnimation(int duration) {
public LinearAnimation(View view, int duration) {
this.duration = duration;
this.looper = Looper.getCurrentLooper();
this.looper = view.getContext().getMainLooper();
}

public void play() {
Expand Down
2 changes: 2 additions & 0 deletions core/src/br/nullexcept/mux/view/AttrList.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ public class AttrList {
public static final String thumbDrawable = "thumbDrawable";
public static final String trackDrawable = "trackDrawable";
public static final String progressDrawable = "progressDrawable";
public static final String editable = "editable";
public static String hintColor = "hintColor";
}
5 changes: 2 additions & 3 deletions core/src/br/nullexcept/mux/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import br.nullexcept.mux.C;
import br.nullexcept.mux.app.Context;
import br.nullexcept.mux.app.Looper;
import br.nullexcept.mux.graphics.*;
import br.nullexcept.mux.input.*;
import br.nullexcept.mux.res.AttributeList;
Expand Down Expand Up @@ -94,7 +93,7 @@ public View(Context context, AttributeList attrs) {

state.set(StateList.CLICKABLE, false);

Looper.getCurrentLooper().postDelayed(()-> attributes = null,2);
context.getMainLooper().postDelayed(()-> attributes = null,2);
}

protected void showMenu(MenuItem menu, int x, int y) {
Expand Down Expand Up @@ -258,7 +257,7 @@ public void setOnClickListener(OnClickListener clickListener) {
}

public void post(Runnable runnable, long time) {
Looper.getCurrentLooper().postDelayed(() -> {
context.getMainLooper().postDelayed(() -> {
if (isVisible()) {
runnable.run();
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/br/nullexcept/mux/view/anim/AlphaAnimation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AlphaAnimation extends LinearAnimation {
private float src, from, to, diff;

public AlphaAnimation(View view, int duration) {
super(duration);
super(view, duration);
this.view = view;
setAlpha(0.0f, 1.0f);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RotationAnimation extends LinearAnimation {
private float old;

public RotationAnimation(View vw, int duration) {
super(duration);
super(vw, duration);
view = vw;
from = 0;
to = 360;
Expand Down
4 changes: 0 additions & 4 deletions core/src/br/nullexcept/mux/view/anim/TransitionAttr.java

This file was deleted.

Loading

0 comments on commit a7ce7ba

Please sign in to comment.