44import br .nullexcept .mux .graphics .*;
55import br .nullexcept .mux .graphics .drawable .ColorDrawable ;
66import br .nullexcept .mux .input .KeyEvent ;
7+ import br .nullexcept .mux .input .MotionEvent ;
78import br .nullexcept .mux .input .MouseEvent ;
89import br .nullexcept .mux .res .AttributeList ;
910import 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