Source code for enamlnative.android.android_drawer_layout
"""
Copyright (c) 2017-2022, CodeLV.
Distributed under the terms of the MIT License.
The full license is in the file LICENSE, distributed with this software.
Created on May 20, 2017
"""
from atom.api import List, Typed, set_default
from enamlnative.widgets.drawer_layout import ProxyDrawerLayout
from .android_view_group import AndroidViewGroup, MarginLayoutParams, ViewGroup
from .android_view import View
from .bridge import JavaCallback, JavaField, JavaMethod
package = "androidx.drawerlayout.widget"
class DrawerLayout(ViewGroup):
__nativeclass__ = f"{package}.DrawerLayout"
openDrawer = JavaMethod(View)
closeDrawer = JavaMethod(View)
addDrawerListener = JavaMethod(f"{package}.DrawerLayout$DrawerListener")
onDrawerClosed = JavaCallback(View)
onDrawerOpened = JavaCallback(View)
onDrawerSlide = JavaCallback(View, float)
onDrawerStateChanged = JavaCallback(int)
setDrawerElevation = JavaMethod(float)
setDrawerTitle = JavaMethod(int, "java.lang.CharSequence")
setDrawerLockMode = JavaMethod(int)
setScrimColor = JavaMethod("android.graphics.Color")
setStatusBarBackgroundColor = JavaMethod("android.graphics.Color")
LOCK_MODES = {
"unlocked": 0,
"locked_closed": 1,
"locked_open": 2,
"undefined": 3,
}
class DrawerLayoutParams(MarginLayoutParams):
"""Update the child widget with the given params"""
__nativeclass__ = f"{package}.DrawerLayout$LayoutParams"
gravity = JavaField(int)
[docs]class AndroidDrawerLayout(AndroidViewGroup, ProxyDrawerLayout):
"""An Android implementation of an Enaml ProxyDrawerLayout."""
#: A reference to the widget created by the proxy.
widget = Typed(DrawerLayout)
#: Drawer state
drawer_state = List()
#: Children use DrawerLayoutParams
layout_param_type = set_default(DrawerLayoutParams) # type: ignore
# -------------------------------------------------------------------------
# Initialization API
# -------------------------------------------------------------------------
[docs] def init_layout(self):
super().init_layout()
d = self.declaration
w = self.widget
if d.opened:
self.set_opened(d.opened)
#: Add drawer listener
w.addDrawerListener(w.getId())
w.onDrawerClosed.connect(self.on_drawer_closed)
w.onDrawerOpened.connect(self.on_drawer_opened)
w.onDrawerStateChanged.connect(self.on_drawer_state_changed)
def drawers(self):
for i, c in enumerate(self.children()):
if i != 0:
yield c
# -------------------------------------------------------------------------
# DrawerListener API
# -------------------------------------------------------------------------
def on_drawer_closed(self, view):
d = self.declaration
with self.widget.openDrawer.suppressed():
with self.widget.closeDrawer.suppressed():
#: Remove view from state
d.opened = [v for v in d.opened if v.proxy.widget.getId() != view]
def on_drawer_opened(self, view):
d = self.declaration
with self.widget.openDrawer.suppressed():
with self.widget.closeDrawer.suppressed():
#: Add view to state
for c in self.drawers():
if c.widget.getId() == view:
d.opened = list(set(d.opened + [c.declaration]))
break
def on_drawer_slide(self, view, offset):
pass
def on_drawer_state_changed(self, state):
pass
# -------------------------------------------------------------------------
# ProxyDrawerLayout API
# -------------------------------------------------------------------------
[docs] def set_opened(self, opened):
"""Opened is a tuple of the drawer sides that are open"""
self.drawer_state = opened
def _observe_drawer_state(self, change):
#: Diff old and new to find changes
old = set(change.get("oldvalue", []))
new = set(change.get("value", []))
#: Closed
for c in old.difference(new):
view = c.proxy.widget
self.widget.closeDrawer(view)
#: Opened
for c in new.difference(old):
view = c.proxy.widget
self.widget.openDrawer(view)
#: Rest unchanged
def set_drawer_width(self, width):
d = self.declaration
self.set_side(d.side)
def set_title(self, title):
d = self.declaration
self.widget.setDrawerTitle(d.title_gravity, title)
def set_title_gravity(self, gravity):
d = self.declaration
self.widget.setDrawerTitle(gravity, d.title)
def set_drawer_elevation(self, elevation):
self.widget.setDrawerElevation(elevation)
def set_lock_mode(self, lock_mode):
mode = DrawerLayout.LOCK_MODES[lock_mode]
self.widget.setDrawerLockMode(mode)
def set_scrim_color(self, color):
self.widget.setScrimColor(color)
def set_status_bar_background_color(self, color):
self.widget.setStatusBarBackgroundColor(color)
[docs] def create_layout_params(self, child, layout):
params = super().create_layout_params(child, layout)
if "gravity" in layout:
params.gravity = layout["gravity"]
return params