Skip to content

Commit

Permalink
[core] Implment maxWidth and maxHeight in FrameLayout
Browse files Browse the repository at this point in the history
-> Implement maxWidth and maxHeight
-> Implement FlowLayout
  • Loading branch information
GabrielBRDeveloper committed Jul 24, 2024
1 parent 7f6c168 commit ac8d6be
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 62 deletions.
81 changes: 22 additions & 59 deletions core/res/ex-layout/main.xml
Original file line number Diff line number Diff line change
@@ -1,73 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout
<FrameLayout
width="match_parent"
height="wrap_content"
paddingBottom="8dp"
orientation="horizontal"
padding="10dp"
background="#AAA">
background="#AAA"
gravity="center">

<LinearLayout
width="match_parent"
height="wrap_content"
layoutGravity="center|top"
maxWidth="1024dp">

<Button
width="match_parent"
height="wrap_content"
orientation="vertical"
padding="10dp"
background="#555"
gravity="center">

margin="5dp"/>
<Button
width="256dp"
height="32dp"
margin="5dp"
text="OK"/>

<TextView
width="256dp"
height="32dp"
margin="5dp"
text="OK"/>

<EditText
width="256dp"
height="32dp"
margin="5dp"
text="OK"/>

<Switch
margin="5dp"
text="OK"/>

<ImageView
width="128dp"
height="128dp"
margin="5dp"
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
width="256dp"
height="256dp"
background="#000"/>

</CardLayout>
width="match_parent"
height="wrap_content"
margin="5dp"/>
<Button
width="match_parent"
height="wrap_content"
margin="5dp"/>
<Button
width="match_parent"
height="wrap_content"
margin="5dp"/>

</LinearLayout>

</LinearLayout>
</FrameLayout>
6 changes: 5 additions & 1 deletion core/src/br/nullexcept/mux/res/LayoutInflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import br.nullexcept.mux.app.Context;
import br.nullexcept.mux.lang.ValuedFunction;
import br.nullexcept.mux.lang.xml.XmlElement;
import br.nullexcept.mux.view.AttrList;
import br.nullexcept.mux.view.Gravity;
import br.nullexcept.mux.view.View;
import br.nullexcept.mux.view.ViewGroup;
Expand Down Expand Up @@ -85,9 +84,13 @@ private ViewGroup.LayoutParams parseLayoutParams(AttributeList attr){
return result[0];
};


params.width = parseParams.run(width);
params.height = parseParams.run(height);

attr.searchDimension("maxWidth", x -> params.maxWidth = x.intValue());
attr.searchDimension("maxHeight", x -> params.maxHeight = x.intValue());

return params;
}

Expand All @@ -96,6 +99,7 @@ private ViewGroup.LayoutParams parseLayoutParams(AttributeList attr){
registerView("CardLayout", CardLayout::new);
registerView("LinearLayout", LinearLayout::new);
registerView("FrameLayout", FrameLayout::new);
registerView("FlowLayout", FlowLayout::new);
registerView("ScrollView", ScrollView::new);

registerView("ImageView", ImageView::new);
Expand Down
10 changes: 8 additions & 2 deletions core/src/br/nullexcept/mux/view/ViewGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ protected int measureSpec(int parent, int spec, int wrap, int padding) {
}
}

protected Size onMeasureChild(View child, int width, int height) {
return new Size(width,height);
}

protected boolean measureChild(View child) {
LayoutParams params = child.getLayoutParams();
if (child.getVisibility() == VISIBILITY_GONE) {
Expand All @@ -61,8 +65,10 @@ protected boolean measureChild(View child) {

int w = measureSpec(getMeasuredWidth()-getPaddingLeft()-getPaddingRight()-left, params.width, size.width, child.getPaddingLeft() + child.getPaddingRight());
int h = measureSpec(getMeasuredHeight()-getPaddingTop()-getPaddingBottom()-top, params.height, size.height, child.getPaddingTop() + child.getPaddingBottom());
if (w != child.getMeasuredWidth() || h != child.getMeasuredHeight()) {
child.onMeasure(w,h);

Size measured = onMeasureChild(child, w,h);
if (measured.width != child.getMeasuredWidth() || measured.height != child.getMeasuredHeight()) {
child.onMeasure(measured.width, measured.height);
return true;
}
return false;
Expand Down
84 changes: 84 additions & 0 deletions core/src/br/nullexcept/mux/widget/FlowLayout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package br.nullexcept.mux.widget;

import br.nullexcept.mux.app.Context;
import br.nullexcept.mux.graphics.Point;
import br.nullexcept.mux.res.AttributeList;
import br.nullexcept.mux.view.Gravity;
import br.nullexcept.mux.view.View;
import br.nullexcept.mux.view.ViewGroup;

public class FlowLayout extends ViewGroup {
private int dividerSize = 16;
public FlowLayout(Context context) {
super(context);
}

public FlowLayout(Context context, AttributeList attrs) {
super(context, attrs);
}

@Override
protected Point getChildLocation(View view) {
int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
int height = getMeasuredHeight();
int y = 0;
int x = 0;
boolean find = false;
int lineCount = 0;
int w = 0;
int mh = 0;
for (int i = 0; i < getChildrenCount(); i++) {
View child = getChildAt(i);
if (child.getMeasuredWidth() + w > width) {
if (find) break;
if (lineCount == 0) {
mh = Math.max(mh, child.getMeasuredHeight());
if (child.equals(view)) {
w = child.getMeasuredWidth();
break;
}
y += mh + dividerSize;
} else {
y += mh + dividerSize;
i--;
}

x = 0;
w = 0;
mh = 0;
lineCount = 0;
} else {
if (child.equals(view)) {
find = true;
}
w += child.getMeasuredWidth() + dividerSize;
if (!find){
x += child.getMeasuredWidth() + dividerSize;
}
mh = Math.max(mh, child.getMeasuredHeight());
lineCount++;
}
}

int offsetX = Gravity.apply(Gravity.horizontal(getGravity()),width,lineCount == 0 ? w : w - dividerSize);
int offsetY = Gravity.apply(Gravity.vertical(getGravity()),mh, view.getMeasuredHeight());

x += getPaddingLeft() + offsetX;
y += getPaddingTop() + offsetY;
return new Point(x,y);
}

@Override
public void addChild(View view, ViewGroup.LayoutParams params) {
LayoutParams newParams = new LayoutParams(params.width,params.height);
newParams.from(params);
super.addChild(view, newParams);
}

public static class LayoutParams extends ViewGroup.LayoutParams {

public LayoutParams(int width, int height) {
super(width, height);
}
}
}
16 changes: 16 additions & 0 deletions core/src/br/nullexcept/mux/widget/FrameLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,22 @@ public void addChild(View view, ViewGroup.LayoutParams params) {
super.addChild(view, newParams);
}

@Override
protected Size onMeasureChild(View child, int width, int height) {
LayoutParams params = (LayoutParams) child.getLayoutParams();
if (width > params.maxWidth) {
width = params.maxWidth;
}
if (height > params.maxHeight) {
height = params.maxHeight;
}
return super.onMeasureChild(child, width, height);
}

public static class LayoutParams extends MarginLayoutParams {
private int gravity = Gravity.LEFT;
public int maxWidth = Integer.MAX_VALUE;
public int maxHeight = Integer.MAX_VALUE;

public LayoutParams(int width, int height) {
super(width, height);
Expand All @@ -81,6 +95,8 @@ public void setGravity(int gravity) {
public void from(ViewGroup.LayoutParams params) {
if (params instanceof LayoutParams) {
this.gravity = ((LayoutParams) params).gravity;
this.maxWidth = ((LayoutParams) params).maxWidth;
this.maxHeight = ((LayoutParams) params).maxHeight;
}
super.from(params);
}
Expand Down

0 comments on commit ac8d6be

Please sign in to comment.