Skip to content

Commit e8d30d5

Browse files
Fix scrollview
- Activity switch dont hide the window. - Correct scrollbar size. - Recycle based in main loop.
1 parent 669716c commit e8d30d5

7 files changed

Lines changed: 98 additions & 35 deletions

File tree

src/br/nullexcept/mux/app/Activity.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ public void finish() {
2424

2525
public void switchActivity(Valuable<Activity> provider) {
2626
Window window = mWindow;
27-
finish();
28-
Application.boot(window, provider.get());
27+
window.getWindowObserver().onDestroy();
28+
mWindow.setContentView(null);
29+
System.gc();
30+
Activity nw = provider.get();
31+
nw.mWindow = mWindow;
32+
mWindow = null;
33+
34+
window.setWindowObserver(Application.buildObserver(nw));
35+
window.getWindowObserver().onCreated();
2936
}
3037

3138
public <T extends View> T findViewById(String id) {

src/br/nullexcept/mux/app/Application.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ private static final void loop(){
5353
Looper.getMainLooper().post(Application::loop);
5454
}
5555

56-
static void boot(Window window, Activity activity) {
57-
window.destroy();
58-
window.setWindowObserver(new Window.WindowObserver() {
56+
static Window.WindowObserver buildObserver(Activity activity) {
57+
return new Window.WindowObserver() {
5958
@Override
6059
public void onCreated() {
6160
activity.onCreate();
@@ -80,7 +79,12 @@ public void onDestroy() {
8079
RUNNING_ACTIVITIES--;
8180
activity.onDestroy();
8281
}
83-
});
82+
};
83+
}
84+
85+
static void boot(Window window, Activity activity) {
86+
window.destroy();
87+
window.setWindowObserver(buildObserver(activity));
8488
activity.mWindow = window;
8589
window.create();
8690
window.setVisible(true);

src/br/nullexcept/mux/core/texel/GlfwEventManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ private void processMouseMove() {
113113
window.onMouseMoved(mouseMove);
114114
}
115115

116+
public void clear() {
117+
downKeyEvents.clear();
118+
downMouseEvents.clear();
119+
}
120+
116121
private class GlfwKeyEvent extends KeyEvent {
117122
private final int keyCode;
118123
public int modifiers = 0;

src/br/nullexcept/mux/core/texel/GlfwWindow.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class GlfwWindow extends Window {
2929
public GlfwWindow() {
3030
window = GLFW.glfwCreateWindow(512, 512, "[title]", 0, C.GLFW_CONTEXT);
3131
eventManager = new GlfwEventManager(this);
32+
GLFW.glfwSetWindowSizeLimits(window, 128,128, Integer.MAX_VALUE, Integer.MAX_VALUE);
3233
}
3334

3435
long getAddress(){
@@ -69,7 +70,7 @@ private void update() {
6970
eventManager.runFrame();
7071

7172
long time = System.currentTimeMillis();
72-
if (isVisible()) {
73+
if (isVisible() && container != null) {
7374
int ow = windowSize.x;
7475
int oh = windowSize.y;
7576
refresh();
@@ -100,8 +101,10 @@ private void update() {
100101
}
101102

102103
private void onResize(int width, int height) {
103-
container.resize(width, height);
104-
container.invalidateAll();
104+
if (container != null) {
105+
container.resize(width, height);
106+
container.invalidateAll();
107+
}
105108
}
106109

107110
@Override
@@ -130,16 +133,25 @@ public void setContentView(View view) {
130133
container.dispose();
131134
container= null;
132135
}
136+
eventManager.clear();
137+
if (view == null)
138+
return;
139+
140+
windowSize.set(0,0);
133141
container = new WindowContainer(view.getContext(), this);
134142
container.addChild(view);
135143
}
136144

137145
public void onMouseEvent(MouseEvent event) {
138-
container.performInputEvent(event);
146+
if (container != null) {
147+
container.performInputEvent(event);
148+
}
139149
}
140150

141151
public void onKeyEvent(KeyEvent event) {
142-
container.performInputEvent(event);
152+
if (container != null) {
153+
container.performInputEvent(event);
154+
}
143155
}
144156

145157
public void onCharEvent(CharEvent charEvent) {
@@ -170,6 +182,11 @@ public void setWindowObserver(WindowObserver observer) {
170182
this.observer = observer;
171183
}
172184

185+
@Override
186+
public WindowObserver getWindowObserver() {
187+
return observer;
188+
}
189+
173190
@Override
174191
public void create() {
175192
if(!destroyed){

src/br/nullexcept/mux/core/texel/TexelBitmap.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package br.nullexcept.mux.core.texel;
22

33
import br.nullexcept.mux.C;
4+
import br.nullexcept.mux.app.Looper;
45
import br.nullexcept.mux.graphics.Bitmap;
5-
import br.nullexcept.mux.hardware.GLES;
66
import br.nullexcept.mux.utils.Log;
77
import org.lwjgl.nanovg.NanoVG;
88
import org.lwjgl.nanovg.NanoVGGLES2;
99

1010
import java.nio.ByteBuffer;
1111

1212
class TexelBitmap implements Bitmap {
13-
private static final int TEXTURE_2D = GLES.GL_TEXTURE27;
1413
private final int width;
1514
private final int height;
1615
private final int id;
@@ -74,11 +73,11 @@ public void dispose() {
7473

7574
@Override
7675
protected void finalize() throws Throwable {
77-
try {
78-
if (!disposed){
79-
dispose();
80-
}
81-
} catch (Exception e){}
76+
Looper.getMainLooper().post(()->{
77+
try {
78+
NanoVG.nvgDeleteImage(C.VG_CONTEXT, id);
79+
} catch (Exception e){}
80+
});
8281
super.finalize();
8382
}
8483

src/br/nullexcept/mux/view/Window.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public abstract class Window {
1010
public abstract void setContentView(View view);
1111
public abstract void setVisible(boolean visible);
1212
public abstract void setWindowObserver(WindowObserver observer);
13+
public abstract WindowObserver getWindowObserver();
1314
public abstract void create();
1415
public abstract void destroy();
1516

src/br/nullexcept/mux/widget/ScrollView.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import br.nullexcept.mux.graphics.*;
55
import br.nullexcept.mux.graphics.drawable.ColorDrawable;
66
import br.nullexcept.mux.input.KeyEvent;
7+
import br.nullexcept.mux.input.MotionEvent;
78
import br.nullexcept.mux.input.MouseEvent;
89
import br.nullexcept.mux.res.AttributeList;
910
import br.nullexcept.mux.view.AttrList;
@@ -20,6 +21,7 @@ public class ScrollView extends ViewGroup {
2021
private int scrollbarWeight = 10;
2122
private final Point mouseScroll = new Point();
2223
private boolean capturedMouseScroll = true;
24+
private final Rect scrollbar = new Rect();
2325

2426
public ScrollView(Context context) {
2527
super(context);
@@ -33,6 +35,7 @@ public ScrollView(Context context, AttributeList attrs) {
3335

3436
{
3537
container = new ScrollContainer(getContext());
38+
container.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
3639
super.addChild(container);
3740
}
3841

@@ -45,18 +48,23 @@ public void setScrollbarDrawable(Drawable drawable) {
4548
@Override
4649
public void onDrawForeground(Canvas canvas) {
4750
super.onDrawForeground(canvas);
48-
if (space.y > 1){
49-
float vh = getHeight() - getPaddingTop() - getPaddingBottom();
51+
measureScroll();
5052

51-
float sh = vh / space.y;
52-
if (sh > 7.0)
53-
sh = 0.7f;
53+
int ch = container.getMeasuredHeight();
54+
int vh = getMeasuredHeight();
5455

55-
int h = Math.round(vh * sh);
56-
int y = (int) Math.round((vh - h) * scroll[1]);
57-
rect.set(getWidth()-scrollbarWeight, y+getPaddingTop(), getWidth(), getPaddingTop()+y+h);
58-
scrollDrawable.setBounds(rect);
56+
if (ch > vh){
57+
double rest = (double) vh / ch;
58+
float localHeight = getHeight() - getPaddingTop() - getPaddingBottom();
59+
60+
int h = (int) Math.round(localHeight * rest);
61+
h = Math.max(10, h);
62+
int y = (int) Math.round((localHeight - h) * scroll[1]);
63+
scrollbar.set(getWidth()-scrollbarWeight, y+getPaddingTop(), getWidth(), getPaddingTop()+y+h);
64+
scrollDrawable.setBounds(scrollbar);
5965
scrollDrawable.draw(canvas);
66+
} else {
67+
scrollbar.set(0,0,0,0);
6068
}
6169
}
6270

@@ -69,6 +77,20 @@ protected void onTreeChanged() {
6977

7078
@Override
7179
protected boolean dispatchMouseEvent(MouseEvent mouseEvent) {
80+
81+
if (mouseEvent.getAction() == MotionEvent.ACTION_DOWN && mouseEvent.getTarget() == -1) {
82+
if (scrollbar.inner(mouseEvent.getX(), mouseEvent.getY())) {
83+
mouseEvent.setTarget(hashCode());
84+
return true;
85+
}
86+
} else if (mouseEvent.getTarget() == hashCode()) {
87+
double percent = mouseEvent.getY() / getMeasuredHeight();
88+
percent = Math.max(0, Math.min(1.0, percent));
89+
scroll[1] = percent;
90+
measureContent();
91+
return true;
92+
}
93+
7294
if (capturedMouseScroll){
7395
capturedMouseScroll = false;
7496
mouseScroll.set(mouseEvent.getScroll().x, mouseEvent.getScroll().y);
@@ -106,13 +128,7 @@ protected void onChildAdded(View view) {
106128
measureContent();
107129
}
108130

109-
private void measureContent() {
110-
if (container.getChildrenCount() == 0) {
111-
return;
112-
}
113-
114-
measure();
115-
131+
private void measureScroll() {
116132
scroll[0] = Math.max(0.0, Math.min(1.0, scroll[0]));
117133
scroll[1] = Math.max(0.0, Math.min(1.0, scroll[1]));
118134

@@ -136,9 +152,17 @@ private void measureContent() {
136152
space.set(Math.abs(mw), Math.abs(mh));
137153

138154
pixelScroll.set(sx, sy);
155+
}
156+
157+
private void measureContent() {
158+
if (container.getChildrenCount() == 0) {
159+
return;
160+
}
161+
162+
measure();
163+
measureScroll();
139164
container.invalidate();
140165
invalidate();
141-
post(this::measureContent, 10);
142166
}
143167

144168
@Override
@@ -170,6 +194,12 @@ public void measure() {
170194
super.measure();
171195
}
172196

197+
@Override
198+
protected void onMeasure(int width, int height) {
199+
super.onMeasure(width, height);
200+
measureContent();
201+
}
202+
173203
@Override
174204
protected Point getChildLocation(View view) {
175205
return pixelScroll;

0 commit comments

Comments
 (0)