From 7f6c16832d3d8cfb98bb30d4b55231ed4800d91e Mon Sep 17 00:00:00 2001
From: Gabriel Machado <97042217+GabrielBRDeveloper@users.noreply.github.com>
Date: Wed, 24 Jul 2024 11:38:02 -0400
Subject: [PATCH] [core] Fix margins in LinearLayout and FrameLayout
---
core/res/ex-layout/main.xml | 12 +++++++++
.../br/nullexcept/mux/app/Application.java | 8 ++++--
.../mux/app/ApplicationRuntime.java | 8 ++++++
core/src/br/nullexcept/mux/app/Looper.java | 19 +++++++++++++-
.../br/nullexcept/mux/widget/FrameLayout.java | 25 ++++++++++++++++---
.../nullexcept/mux/widget/LinearLayout.java | 12 +++++----
6 files changed, 72 insertions(+), 12 deletions(-)
diff --git a/core/res/ex-layout/main.xml b/core/res/ex-layout/main.xml
index 2e38ea4..1569c57 100644
--- a/core/res/ex-layout/main.xml
+++ b/core/res/ex-layout/main.xml
@@ -44,9 +44,21 @@
src="@drawable/default_window_icon"
text="OK"/>
+
+
+
+
{
- new ApplicationRuntime(project).start();
+ app[0] = new ApplicationRuntime(project);
+ app[0].start();
}).start();
+ while (app[0] == null) {Looper.sleep(0,100);}
+ return app[0];
}
}
diff --git a/core/src/br/nullexcept/mux/app/ApplicationRuntime.java b/core/src/br/nullexcept/mux/app/ApplicationRuntime.java
index e8fc9da..fa5b7ad 100644
--- a/core/src/br/nullexcept/mux/app/ApplicationRuntime.java
+++ b/core/src/br/nullexcept/mux/app/ApplicationRuntime.java
@@ -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;
diff --git a/core/src/br/nullexcept/mux/app/Looper.java b/core/src/br/nullexcept/mux/app/Looper.java
index 601b735..e611c8b 100644
--- a/core/src/br/nullexcept/mux/app/Looper.java
+++ b/core/src/br/nullexcept/mux/app/Looper.java
@@ -8,7 +8,7 @@ public class Looper {
private final ArrayList executions = new ArrayList<>();
static Looper mainLooper;
private boolean stop = false;
-
+ private boolean running = false;
public static Looper getCurrentLooper() {
Thread current = Thread.currentThread();
@@ -36,6 +36,17 @@ public void postDelayed(Runnable runnable, long msTime){
}
}
+ public void cancel(Runnable run) {
+ synchronized (executions) {
+ ArrayList raw = new ArrayList<>(executions);
+ for (Callback callback: raw) {
+ if (callback.handle.equals(run)) {
+ executions.remove(callback);
+ }
+ }
+ }
+ }
+
public void post(Runnable runnable){
postDelayed(runnable, 1);
}
@@ -45,6 +56,7 @@ public void loop(){
synchronized (activeLoops) {
activeLoops.put(threadID, this);
}
+ running = true;
while (!stop) {
Callback[] list;
int i = 0;
@@ -65,6 +77,7 @@ public void loop(){
}
sleep(0, (int) (Math.random()*100));
}
+ running = false;
synchronized (activeLoops) {
activeLoops.remove(threadID);
}
@@ -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;
diff --git a/core/src/br/nullexcept/mux/widget/FrameLayout.java b/core/src/br/nullexcept/mux/widget/FrameLayout.java
index c4f2d93..457c772 100644
--- a/core/src/br/nullexcept/mux/widget/FrameLayout.java
+++ b/core/src/br/nullexcept/mux/widget/FrameLayout.java
@@ -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;
@@ -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();
@@ -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) {
diff --git a/core/src/br/nullexcept/mux/widget/LinearLayout.java b/core/src/br/nullexcept/mux/widget/LinearLayout.java
index fcd5a4d..8a7ad80 100644
--- a/core/src/br/nullexcept/mux/widget/LinearLayout.java
+++ b/core/src/br/nullexcept/mux/widget/LinearLayout.java
@@ -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) {
@@ -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;
}
@@ -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;
}