Skip to content

Commit

Permalink
[core] Fix margins in LinearLayout and FrameLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper committed Jul 24, 2024
1 parent 0b55ddd commit 7f6c168
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
12 changes: 12 additions & 0 deletions core/res/ex-layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,21 @@
src="@drawable/default_window_icon"
text="OK"/>

<FrameLayout
width="256dp"
height="256dp"
background="#A00">
<Button
width="128dp"
height="128dp"
layoutGravity="center|bottom"
margin="10dp"/>
</FrameLayout>

<CardLayout
width="256dp"
height="256dp"
marginBottom="90dp"
radius="16dp">

<View
Expand Down
8 changes: 6 additions & 2 deletions core/src/br/nullexcept/mux/app/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import br.nullexcept.mux.app.base.Project;

public class Application {
public static void initialize(Project project){
public static ApplicationRuntime initialize(Project project){
ApplicationRuntime[] app = new ApplicationRuntime[1];
new Thread(()->{
new ApplicationRuntime(project).start();
app[0] = new ApplicationRuntime(project);
app[0].start();
}).start();
while (app[0] == null) {Looper.sleep(0,100);}
return app[0];
}
}
8 changes: 8 additions & 0 deletions core/src/br/nullexcept/mux/app/ApplicationRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ private void loop() {
looper.postDelayed(this::loop, 0);
}

public void destroy() {
looper.post(this::stop);
}

public boolean isRunning() {
return looper.isRunning();
}

@Override
public Resources getResources() {
return resources;
Expand Down
19 changes: 18 additions & 1 deletion core/src/br/nullexcept/mux/app/Looper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Looper {
private final ArrayList<Callback> executions = new ArrayList<>();
static Looper mainLooper;
private boolean stop = false;

private boolean running = false;

public static Looper getCurrentLooper() {
Thread current = Thread.currentThread();
Expand Down Expand Up @@ -36,6 +36,17 @@ public void postDelayed(Runnable runnable, long msTime){
}
}

public void cancel(Runnable run) {
synchronized (executions) {
ArrayList<Callback> raw = new ArrayList<>(executions);
for (Callback callback: raw) {
if (callback.handle.equals(run)) {
executions.remove(callback);
}
}
}
}

public void post(Runnable runnable){
postDelayed(runnable, 1);
}
Expand All @@ -45,6 +56,7 @@ public void loop(){
synchronized (activeLoops) {
activeLoops.put(threadID, this);
}
running = true;
while (!stop) {
Callback[] list;
int i = 0;
Expand All @@ -65,6 +77,7 @@ public void loop(){
}
sleep(0, (int) (Math.random()*100));
}
running = false;
synchronized (activeLoops) {
activeLoops.remove(threadID);
}
Expand All @@ -84,6 +97,10 @@ public void stop(){
this.stop = true;
}

public boolean isRunning() {
return running;
}

private static class Callback {
private static long current;
private final Runnable handle;
Expand Down
25 changes: 21 additions & 4 deletions core/src/br/nullexcept/mux/widget/FrameLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import br.nullexcept.mux.app.Context;
import br.nullexcept.mux.graphics.Point;
import br.nullexcept.mux.graphics.Size;
import br.nullexcept.mux.res.AttributeList;
import br.nullexcept.mux.view.Gravity;
import br.nullexcept.mux.view.MarginLayoutParams;
Expand All @@ -24,11 +25,14 @@ protected Point getChildLocation(View view) {
int w = getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
int h = getMeasuredHeight()-getPaddingTop()-getPaddingBottom();

int x = Gravity.apply(Gravity.horizontal(params.gravity), w, view.getMeasuredWidth());
int y = Gravity.apply(Gravity.vertical(params.gravity), h, view.getMeasuredHeight());
int cw = view.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight();
int ch = view.getMeasuredHeight() + params.getMarginTop() + params.getMarginTop();

x += params.getMarginLeft() - params.getMarginRight();
y += params.getMarginTop() - params.getMarginBottom();
int x = Gravity.apply(Gravity.horizontal(params.gravity), w, cw);
int y = Gravity.apply(Gravity.vertical(params.gravity), h, ch);

y += params.getMarginTop();
x += params.getMarginLeft();

x += getPaddingLeft();
y += getPaddingTop();
Expand All @@ -38,6 +42,19 @@ protected Point getChildLocation(View view) {
return point;
}

@Override
protected Size onMeasureContent(int parentWidth, int parentHeight) {
Size size = new Size();
for (View child: getChildren()){
LayoutParams params = (LayoutParams) child.getLayoutParams();
int x = Math.max(0, getChildLocation(child).x - getPaddingLeft());
size.width = Math.max(x + child.getMeasuredWidth() + params.getMarginRight(), size.width);
int y = Math.max(0, getChildLocation(child).y - getPaddingTop());
size.height = Math.max(y+child.getMeasuredHeight() + params.getMarginBottom(), size.height);
}
return size;
}

@Override
public void addChild(View view, ViewGroup.LayoutParams params) {
if (params == null) {
Expand Down
12 changes: 7 additions & 5 deletions core/src/br/nullexcept/mux/widget/LinearLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ private Size measureVertical() {
LayoutParams params = (LayoutParams) child.getLayoutParams();
size.width = Math.max(size.width, child.getMeasuredWidth() + params.getMarginLeft() + params.getMarginRight());
size.height += child.getMeasuredHeight() + params.getMarginTop() + params.getMarginBottom();
if (i != getChildrenCount()) size.height += dividerSize;
if (i < getChildrenCount()-1) {
size.height += dividerSize;
}
}
return size;
}

@Override
protected Size onMeasureContent(int parentWidth, int parentHeight) {
return super.onMeasureContent(parentWidth, parentHeight);
return orientation == ORIENTATION_VERTICAL ? measureVertical() : measureHorizontal();
}

private Point getChildLocationVertical(View view) {
Expand Down Expand Up @@ -111,8 +113,8 @@ private Point getChildLocationVertical(View view) {
pos.x = posX + Gravity.apply(Gravity.horizontal(getGravity()),wrapSize.width,view.getMeasuredWidth());
pos.x += params.getMarginLeft();

pos.y += getPaddingTop();
pos.x += getPaddingLeft();
pos.y += getPaddingTop() + params.getMarginTop();
pos.x += getPaddingLeft() + params.getMarginLeft();

return pos;
}
Expand Down Expand Up @@ -145,7 +147,7 @@ private Point getChildLocationHorizontal(View view) {

pos.y = posY + Gravity.apply(Gravity.vertical(getGravity()),wrapSize.height,view.getMeasuredHeight());
pos.y += params.getMarginTop() + getPaddingTop();
pos.x += getPaddingLeft();
pos.x += getPaddingLeft() + params.getMarginLeft();

return pos;
}
Expand Down

0 comments on commit 7f6c168

Please sign in to comment.