|
| 1 | +from travertino.size import at_least |
| 2 | + |
| 3 | +from ..libs import android_widgets, android |
| 4 | +from .base import Widget |
| 5 | +from toga_android.window import AndroidViewport |
| 6 | + |
| 7 | + |
| 8 | +class TogaOnTouchListener(android.View__OnTouchListener): |
| 9 | + is_scrolling_enabled = True |
| 10 | + |
| 11 | + def __init__(self): |
| 12 | + super().__init__() |
| 13 | + |
| 14 | + def onTouch(self, view, motion_event): |
| 15 | + if self.is_scrolling_enabled: |
| 16 | + return view.onTouchEvent(motion_event) |
| 17 | + else: |
| 18 | + return True |
| 19 | + |
| 20 | + |
| 21 | +class ScrollContainer(Widget): |
| 22 | + vScrollListener = None |
| 23 | + hScrollView = None |
| 24 | + hScrollListener = None |
| 25 | + |
| 26 | + def create(self): |
| 27 | + vScrollView = android_widgets.ScrollView(self._native_activity) |
| 28 | + vScrollView_layout_params = android_widgets.LinearLayout__LayoutParams( |
| 29 | + android_widgets.LinearLayout__LayoutParams.MATCH_PARENT, |
| 30 | + android_widgets.LinearLayout__LayoutParams.MATCH_PARENT |
| 31 | + ) |
| 32 | + vScrollView_layout_params.gravity = android_widgets.Gravity.TOP |
| 33 | + vScrollView.setLayoutParams(vScrollView_layout_params) |
| 34 | + self.vScrollListener = TogaOnTouchListener() |
| 35 | + self.vScrollListener.is_scrolling_enabled = self.interface.vertical |
| 36 | + vScrollView.setOnTouchListener(self.vScrollListener) |
| 37 | + self.native = vScrollView |
| 38 | + self.hScrollView = android_widgets.HorizontalScrollView(self._native_activity) |
| 39 | + hScrollView_layout_params = android_widgets.LinearLayout__LayoutParams( |
| 40 | + android_widgets.LinearLayout__LayoutParams.MATCH_PARENT, |
| 41 | + android_widgets.LinearLayout__LayoutParams.MATCH_PARENT |
| 42 | + ) |
| 43 | + hScrollView_layout_params.gravity = android_widgets.Gravity.LEFT |
| 44 | + self.hScrollListener = TogaOnTouchListener() |
| 45 | + self.hScrollListener.is_scrolling_enabled = self.interface.horizontal |
| 46 | + self.hScrollView.setOnTouchListener(self.hScrollListener) |
| 47 | + vScrollView.addView(self.hScrollView, hScrollView_layout_params) |
| 48 | + if self.interface.content is not None: |
| 49 | + self.set_content(self.interface.content) |
| 50 | + |
| 51 | + def set_content(self, widget): |
| 52 | + widget.viewport = AndroidViewport(widget.native) |
| 53 | + content_view_params = android_widgets.LinearLayout__LayoutParams( |
| 54 | + android_widgets.LinearLayout__LayoutParams.MATCH_PARENT, |
| 55 | + android_widgets.LinearLayout__LayoutParams.MATCH_PARENT |
| 56 | + ) |
| 57 | + widget_parent = widget.native.getParent() |
| 58 | + if widget_parent is not None: |
| 59 | + widget_parent.removeView(widget.native) |
| 60 | + self.hScrollView.addView(widget.native, content_view_params) |
| 61 | + for child in widget.interface.children: |
| 62 | + child._impl.container = widget |
| 63 | + |
| 64 | + def set_vertical(self, value): |
| 65 | + self.vScrollListener.is_scrolling_enabled = value |
| 66 | + |
| 67 | + def set_horizontal(self, value): |
| 68 | + self.hScrollListener.is_scrolling_enabled = value |
| 69 | + |
| 70 | + def rehint(self): |
| 71 | + # Android can crash when rendering some widgets until they have their layout params set. Guard for that case. |
| 72 | + if self.native.getLayoutParams() is None: |
| 73 | + return |
| 74 | + self.native.measure( |
| 75 | + android_widgets.View__MeasureSpec.UNSPECIFIED, |
| 76 | + android_widgets.View__MeasureSpec.UNSPECIFIED, |
| 77 | + ) |
| 78 | + self.interface.intrinsic.width = at_least(self.native.getMeasuredWidth()) |
| 79 | + self.interface.intrinsic.height = self.native.getMeasuredHeight() |
0 commit comments