Skip to content

Commit

Permalink
Merge branch 'main' into workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Oct 19, 2022
2 parents e9f5d00 + 138b673 commit 1cbe045
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
15 changes: 5 additions & 10 deletions src/android/src/toga_android/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ class Paths:
def __context(self):
return App.app._impl.native.getApplicationContext()

def __init__(self):
# On Android, __main__ only exists during app startup, so cache its location now.
self._app = Path(sys.modules["__main__"].__file__).parent

@property
def app(self):
try:
return Path(sys.modules["__main__"].__file__).parent
except KeyError:
# If we're running in test conditions,
# there is no __main__ module.
return Path.cwd()
except AttributeError:
# If we're running at an interactive prompt,
# the __main__ module isn't file-based.
return Path.cwd()
return self._app

@property
def data(self):
Expand Down
2 changes: 1 addition & 1 deletion src/android/src/toga_android/widgets/scrollcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create(self):
self.set_content(self.interface.content)

def set_content(self, widget):
widget.viewport = AndroidViewport(self.native)
widget.viewport = AndroidViewport(self.native, self.interface)
content_view_params = LinearLayout__LayoutParams(
LinearLayout__LayoutParams.MATCH_PARENT,
LinearLayout__LayoutParams.MATCH_PARENT
Expand Down
58 changes: 32 additions & 26 deletions src/android/src/toga_android/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,57 @@
from .libs.android.view import ViewTreeObserver__OnGlobalLayoutListener


class AndroidViewport:
# `content_parent` should be the view that will become the parent of the widget passed to
# `Window.set_content`. This ensures that the viewport `width` and `height` attributes
# return the usable area of the app, not including the action bar or status bar.
def __init__(self, content_parent):
self.content_parent = content_parent
self.dpi = content_parent.getContext().getResources().getDisplayMetrics().densityDpi
class AndroidViewport(ViewTreeObserver__OnGlobalLayoutListener):
def __init__(self, native, container):
"""
:param native: A native widget whose size will be tracked.
:param container: An object with a ``content`` attribute, which will have
``refresh()`` called on it whenever the native widget's size changes.
"""
super().__init__()
self.native = native
self.container = container
self.last_size = (None, None)
native.getViewTreeObserver().addOnGlobalLayoutListener(self)

self.dpi = native.getContext().getResources().getDisplayMetrics().densityDpi
# Toga needs to know how the current DPI compares to the platform default,
# which is 160: https://developer.android.com/training/multiscreen/screendensities
self.baseline_dpi = 160
self.scale = float(self.dpi) / self.baseline_dpi

def onGlobalLayout(self):
"""This listener is run after each native layout pass. If any view's size or
position has changed, the new values will be visible here.
"""
new_size = (self.width, self.height)
if self.last_size != new_size:
self.last_size = new_size
if self.container.content:
self.container.content.refresh()

@property
def width(self):
return self.content_parent.getWidth()
return self.native.getWidth()

@property
def height(self):
return self.content_parent.getHeight()
return self.native.getHeight()


class Window(ViewTreeObserver__OnGlobalLayoutListener):
class Window:
def __init__(self, interface, title, position, size):
super().__init__()
self.interface = interface
self.interface._impl = self
self.last_size = (None, None)

# self.set_title(title)

def set_app(self, app):
self.app = app
content_parent = self.app.native.findViewById(R__id.content).__global__()
self.viewport = AndroidViewport(content_parent)
content_parent.getViewTreeObserver().addOnGlobalLayoutListener(self)

def onGlobalLayout(self):
"""This listener is run after each native layout pass. If any view's size or position has
changed, the new values will be visible here.
"""
new_size = (self.viewport.width, self.viewport.height)
if self.last_size != new_size:
self.last_size = new_size
if self.interface.content:
self.interface.content.refresh()
self.viewport = AndroidViewport(
self.app.native.findViewById(R__id.content).__global__(),
self.interface
)

def clear_content(self):
if self.interface.content:
Expand Down Expand Up @@ -79,7 +85,7 @@ def set_position(self, position):
pass

def get_size(self):
return self.last_size
return self.viewport.last_size

def set_size(self, size):
# Does nothing on mobile
Expand Down

0 comments on commit 1cbe045

Please sign in to comment.