| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
'''NavigationDrawer |
| 22 |
================ |
| 23 |
|
| 24 |
The NavigationDrawer widget provides a hidden panel view designed to |
| 25 |
duplicate the popular Android layout. The user views one main widget |
| 26 |
but can slide from the left of the screen to view a second, previously |
| 27 |
hidden widget. The transition between open/closed is smoothly |
| 28 |
animated, with the parameters (anim time, panel width, touch |
| 29 |
detection) all user configurable. If the panel is released without |
| 30 |
being fully open or closed, it animates to an appropriate |
| 31 |
configuration. |
| 32 |
|
| 33 |
NavigationDrawer supports many different animation properties, |
| 34 |
including moving one or both of the side/main panels, darkening |
| 35 |
either/both widgets, changing side panel opacity, and changing which |
| 36 |
widget is on top. The user can edit these individually to taste (this |
| 37 |
is enough rope to hang oneself, it's easy to make a useless or silly |
| 38 |
configuration!), or use one of a few preset animations. |
| 39 |
|
| 40 |
The hidden panel might normally a set of navigation buttons, but the |
| 41 |
implementation lets the user use any widget(s). |
| 42 |
|
| 43 |
The first widget added to the NavigationDrawer is automatically used |
| 44 |
as the side panel, and the second widget as the main panel. No further |
| 45 |
widgets can be added, further changes are left to the user via editing |
| 46 |
the panel widgets. |
| 47 |
|
| 48 |
Usage summary |
| 49 |
------------- |
| 50 |
|
| 51 |
- The first widget added to a NavigationDrawer is used as the hidden |
| 52 |
side panel. |
| 53 |
- The second widget added is used as the main panel. |
| 54 |
- Both widgets can be removed with remove_widget, or alternatively |
| 55 |
set/removed with set_main_panel and set_side_panel. |
| 56 |
- The hidden side panel can be revealed by dragging from the left of |
| 57 |
the NavigationDrawer. The touch detection width is the |
| 58 |
touch_accept_width property. |
| 59 |
- Every animation property is user-editable, or default animations |
| 60 |
can be chosen by setting anim_type. |
| 61 |
|
| 62 |
See the example and docstrings for information on individual properties. |
| 63 |
|
| 64 |
|
| 65 |
Example:: |
| 66 |
|
| 67 |
class ExampleApp(App): |
| 68 |
|
| 69 |
def build(self): |
| 70 |
navigationdrawer = NavigationDrawer() |
| 71 |
|
| 72 |
side_panel = BoxLayout(orientation='vertical') |
| 73 |
side_panel.add_widget(Label(text='Panel label')) |
| 74 |
side_panel.add_widget(Button(text='A button')) |
| 75 |
side_panel.add_widget(Button(text='Another button')) |
| 76 |
navigationdrawer.add_widget(side_panel) |
| 77 |
|
| 78 |
label_head = ( |
| 79 |
'[b]Example label filling main panel[/b]\n\n[color=ff0000](p' |
| 80 |
'ull from left to right!)[/color]\n\nIn this example, the le' |
| 81 |
'ft panel is a simple boxlayout menu, and this main panel is' |
| 82 |
' a BoxLayout with a label and example image.\n\nSeveral pre' |
| 83 |
'set layouts are available (see buttons below), but users ma' |
| 84 |
'y edit every parameter for much more customisation.') |
| 85 |
main_panel = BoxLayout(orientation='vertical') |
| 86 |
label_bl = BoxLayout(orientation='horizontal') |
| 87 |
label = Label(text=label_head, font_size='15sp', |
| 88 |
markup=True, valign='top') |
| 89 |
label_bl.add_widget(Widget(size_hint_x=None, width=dp(10))) |
| 90 |
label_bl.add_widget(label) |
| 91 |
label_bl.add_widget(Widget(size_hint_x=None, width=dp(10))) |
| 92 |
main_panel.add_widget(Widget(size_hint_y=None, height=dp(10))) |
| 93 |
main_panel.add_widget(label_bl) |
| 94 |
main_panel.add_widget(Widget(size_hint_y=None, height=dp(10))) |
| 95 |
navigationdrawer.add_widget(main_panel) |
| 96 |
label.bind(size=label.setter('text_size')) |
| 97 |
|
| 98 |
def set_anim_type(name): |
| 99 |
navigationdrawer.anim_type = name |
| 100 |
modes_layout = BoxLayout(orientation='horizontal') |
| 101 |
modes_layout.add_widget(Label(text='preset\nanims:')) |
| 102 |
slide_an = Button(text='slide_\nabove_\nanim') |
| 103 |
slide_an.bind(on_press=lambda j: set_anim_type('slide_above_anim')) |
| 104 |
slide_sim = Button(text='slide_\nabove_\nsimple') |
| 105 |
slide_sim.bind(on_press=lambda j: set_anim_type('slide_above_simple')) |
| 106 |
fade_in_button = Button(text='fade_in') |
| 107 |
fade_in_button.bind(on_press=lambda j: set_anim_type('fade_in')) |
| 108 |
reveal_button = Button(text='reveal_\nbelow_\nanim') |
| 109 |
reveal_button.bind(on_press= |
| 110 |
lambda j: set_anim_type('reveal_below_anim')) |
| 111 |
slide_button = Button(text='reveal_\nbelow_\nsimple') |
| 112 |
slide_button.bind(on_press= |
| 113 |
lambda j: set_anim_type('reveal_below_simple')) |
| 114 |
modes_layout.add_widget(slide_an) |
| 115 |
modes_layout.add_widget(slide_sim) |
| 116 |
modes_layout.add_widget(fade_in_button) |
| 117 |
modes_layout.add_widget(reveal_button) |
| 118 |
modes_layout.add_widget(slide_button) |
| 119 |
main_panel.add_widget(modes_layout) |
| 120 |
|
| 121 |
button = Button(text='toggle NavigationDrawer state (animate)', |
| 122 |
size_hint_y=0.2) |
| 123 |
button.bind(on_press=lambda j: navigationdrawer.toggle_state()) |
| 124 |
button2 = Button(text='toggle NavigationDrawer state (jump)', |
| 125 |
size_hint_y=0.2) |
| 126 |
button2.bind(on_press=lambda j: navigationdrawer.toggle_state(False)) |
| 127 |
button3 = Button(text='toggle _main_above', size_hint_y=0.2) |
| 128 |
button3.bind(on_press=navigationdrawer.toggle_main_above) |
| 129 |
main_panel.add_widget(button) |
| 130 |
main_panel.add_widget(button2) |
| 131 |
main_panel.add_widget(button3) |
| 132 |
|
| 133 |
return navigationdrawer |
| 134 |
|
| 135 |
ExampleApp().run() |
| 136 |
|
| 137 |
''' |
| 138 |
|
| 139 |
__all__ = ('NavigationDrawer', ) |
| 140 |
|
| 141 |
from kivy.animation import Animation |
| 142 |
from kivy.uix.widget import Widget |
| 143 |
from kivy.uix.stencilview import StencilView |
| 144 |
from kivy.metrics import dp |
| 145 |
from kivy.clock import Clock |
| 146 |
from kivy.properties import (ObjectProperty, NumericProperty, OptionProperty, |
| 147 |
BooleanProperty, StringProperty) |
| 148 |
from kivy.resources import resource_add_path |
| 149 |
from kivy.lang import Builder |
| 150 |
import os.path |
| 151 |
|
| 152 |
resource_add_path(os.path.dirname(__file__)) |
| 153 |
|
| 154 |
Builder.load_string(''' |
| 155 |
<NavigationDrawer>: |
| 156 |
size_hint: (1,1) |
| 157 |
_side_panel: sidepanel |
| 158 |
_main_panel: mainpanel |
| 159 |
_join_image: joinimage |
| 160 |
side_panel_width: min(dp(250), 0.5*self.width) |
| 161 |
BoxLayout: |
| 162 |
id: sidepanel |
| 163 |
y: root.y |
| 164 |
x: root.x - \ |
| 165 |
(1-root._anim_progress)* \ |
| 166 |
root.side_panel_init_offset*root.side_panel_width |
| 167 |
height: root.height |
| 168 |
width: root.side_panel_width |
| 169 |
opacity: root.side_panel_opacity + \ |
| 170 |
(1-root.side_panel_opacity)*root._anim_progress |
| 171 |
canvas: |
| 172 |
Color: |
| 173 |
#rgba: (0,0,0,1) |
| 174 |
# https://github.com/kivy-garden/garden.navigationdrawer/issues/12 |
| 175 |
rgba: (0.259,0.259,0.259,1) |
| 176 |
Rectangle: |
| 177 |
pos: self.pos |
| 178 |
size: self.size |
| 179 |
canvas.after: |
| 180 |
Color: |
| 181 |
rgba: (1,0,0,(1-root._anim_progress)*root.side_panel_darkness) |
| 182 |
Rectangle: |
| 183 |
size: self.size |
| 184 |
pos: self.pos |
| 185 |
BoxLayout: |
| 186 |
id: mainpanel |
| 187 |
x: root.x + \ |
| 188 |
root._anim_progress * \ |
| 189 |
root.side_panel_width * \ |
| 190 |
root.main_panel_final_offset |
| 191 |
y: root.y |
| 192 |
size: root.size |
| 193 |
canvas: |
| 194 |
Color: |
| 195 |
#rgba: (0,0,0,1) |
| 196 |
# https://github.com/kivy-garden/garden.navigationdrawer/issues/12 |
| 197 |
rgba: (0.188,0.188,0.188,1) |
| 198 |
Rectangle: |
| 199 |
pos: self.pos |
| 200 |
size: self.size |
| 201 |
canvas.after: |
| 202 |
Color: |
| 203 |
rgba: (0,0,0,root._anim_progress*root.main_panel_darkness) |
| 204 |
Rectangle: |
| 205 |
size: self.size |
| 206 |
pos: self.pos |
| 207 |
Image: |
| 208 |
id: joinimage |
| 209 |
opacity: min(sidepanel.opacity, 0 if root._anim_progress < 0.00001 \ |
| 210 |
else min(root._anim_progress*40,1)) |
| 211 |
source: root._choose_image(root._main_above, root.separator_image) |
| 212 |
mipmap: False |
| 213 |
width: root.separator_image_width |
| 214 |
height: root._side_panel.height |
| 215 |
x: (mainpanel.x - self.width + 1) if root._main_above \ |
| 216 |
else (sidepanel.x + sidepanel.width - 1) |
| 217 |
y: root.y |
| 218 |
allow_stretch: True |
| 219 |
keep_ratio: False |
| 220 |
''') |
| 221 |
|
| 222 |
|
| 223 |
class NavigationDrawerException(Exception): |
| 224 |
'''Raised when add_widget or remove_widget called incorrectly on a |
| 225 |
NavigationDrawer. |
| 226 |
|
| 227 |
''' |
| 228 |
|
| 229 |
|
| 230 |
class NavigationDrawer(StencilView): |
| 231 |
'''Widget taking two children, a side panel and a main panel, |
| 232 |
displaying them in a way that replicates the popular Android |
| 233 |
functionality. See module documentation for more info. |
| 234 |
|
| 235 |
''' |
| 236 |
|
| 237 |
|
| 238 |
_side_panel = ObjectProperty() |
| 239 |
_main_panel = ObjectProperty() |
| 240 |
_join_image = ObjectProperty() |
| 241 |
|
| 242 |
side_panel = ObjectProperty(None, allownone=True) |
| 243 |
'''Automatically bound to whatever widget is added as the hidden panel.''' |
| 244 |
main_panel = ObjectProperty(None, allownone=True) |
| 245 |
'''Automatically bound to whatever widget is added as the main panel.''' |
| 246 |
|
| 247 |
|
| 248 |
side_panel_width = NumericProperty() |
| 249 |
'''The width of the hidden side panel. Defaults to the minimum of |
| 250 |
250dp or half the NavigationDrawer width.''' |
| 251 |
separator_image = StringProperty('') |
| 252 |
'''The path to an image that will be placed between the side and main |
| 253 |
panels. If set to `''`, defaults to a gradient from black to |
| 254 |
transparent in an appropriate direction (left->right if side panel |
| 255 |
above main, right->left if main panel on top).''' |
| 256 |
separator_image_width = NumericProperty(dp(10)) |
| 257 |
'''The width of the separator image. Defaults to 10dp''' |
| 258 |
|
| 259 |
|
| 260 |
touch_accept_width = NumericProperty('14dp') |
| 261 |
'''Distance from the left of the NavigationDrawer in which to grab the |
| 262 |
touch and allow revealing of the hidden panel.''' |
| 263 |
_touch = ObjectProperty(None, allownone=True) |
| 264 |
|
| 265 |
|
| 266 |
state = OptionProperty('closed', options=('open', 'closed')) |
| 267 |
'''Specifies the state of the widget. Must be one of 'open' or |
| 268 |
'closed'. Setting its value automatically jumps to the relevant state, |
| 269 |
or users may use the anim_to_state() method to animate the |
| 270 |
transition.''' |
| 271 |
anim_time = NumericProperty(0.3) |
| 272 |
'''The time taken for the panel to slide to the open/closed state when |
| 273 |
released or manually animated with anim_to_state.''' |
| 274 |
min_dist_to_open = NumericProperty(0.7) |
| 275 |
'''Must be between 0 and 1. Specifies the fraction of the hidden panel |
| 276 |
width beyond which the NavigationDrawer will relax to open state when |
| 277 |
released. Defaults to 0.7.''' |
| 278 |
_anim_progress = NumericProperty(0) |
| 279 |
|
| 280 |
_anim_init_progress = NumericProperty(0) |
| 281 |
|
| 282 |
|
| 283 |
top_panel = OptionProperty('main', options=['main', 'side']) |
| 284 |
'''Denotes which panel should be drawn on top of the other. Must be |
| 285 |
one of 'main' or 'side'. Defaults to 'main'.''' |
| 286 |
_main_above = BooleanProperty(True) |
| 287 |
|
| 288 |
side_panel_init_offset = NumericProperty(0.5) |
| 289 |
'''Intial offset (to the left of the widget) of the side panel, in |
| 290 |
units of its total width. Opening the panel moves it smoothly to its |
| 291 |
final position at the left of the screen.''' |
| 292 |
|
| 293 |
side_panel_darkness = NumericProperty(0.8) |
| 294 |
'''Controls the fade-to-black of the side panel in its hidden |
| 295 |
state. Must be between 0 (no fading) and 1 (fades to totally |
| 296 |
black).''' |
| 297 |
|
| 298 |
side_panel_opacity = NumericProperty(1) |
| 299 |
'''Controls the opacity of the side panel in its hidden state. Must be |
| 300 |
between 0 (fade to transparent) and 1 (no transparency)''' |
| 301 |
|
| 302 |
main_panel_final_offset = NumericProperty(1) |
| 303 |
'''Final offset (to the right of the normal position) of the main |
| 304 |
panel, in units of the side panel width.''' |
| 305 |
|
| 306 |
main_panel_darkness = NumericProperty(0) |
| 307 |
'''Controls the fade-to-black of the main panel when the side panel is |
| 308 |
in its hidden state. Must be between 0 (no fading) and 1 (fades to |
| 309 |
totally black). |
| 310 |
''' |
| 311 |
|
| 312 |
opening_transition = StringProperty('out_cubic') |
| 313 |
'''The name of the animation transition type to use when animating to |
| 314 |
an open state. Defaults to 'out_cubic'.''' |
| 315 |
|
| 316 |
closing_transition = StringProperty('in_cubic') |
| 317 |
'''The name of the animation transition type to use when animating to |
| 318 |
a closed state. Defaults to 'out_cubic'.''' |
| 319 |
|
| 320 |
anim_type = OptionProperty('reveal_from_below', |
| 321 |
options=['slide_above_anim', |
| 322 |
'slide_above_simple', |
| 323 |
'fade_in', |
| 324 |
'reveal_below_anim', |
| 325 |
'reveal_below_simple', |
| 326 |
]) |
| 327 |
'''The default animation type to use. Several options are available, |
| 328 |
modifying all possibly animation properties including darkness, |
| 329 |
opacity, movement and draw height. Users may also (and are |
| 330 |
encouaged to) edit these properties individually, for a vastly |
| 331 |
larger range of possible animations. Defaults to reveal_below_anim. |
| 332 |
''' |
| 333 |
|
| 334 |
def __init__(self, **kwargs): |
| 335 |
super(NavigationDrawer, self).__init__(**kwargs) |
| 336 |
Clock.schedule_once(self.on__main_above, 0) |
| 337 |
|
| 338 |
def on_anim_type(self, *args): |
| 339 |
anim_type = self.anim_type |
| 340 |
if anim_type == 'slide_above_anim': |
| 341 |
self.top_panel = 'side' |
| 342 |
self.side_panel_darkness = 0 |
| 343 |
self.side_panel_opacity = 1 |
| 344 |
self.main_panel_final_offset = 0.5 |
| 345 |
self.main_panel_darkness = 0.5 |
| 346 |
self.side_panel_init_offset = 1 |
| 347 |
if anim_type == 'slide_above_simple': |
| 348 |
self.top_panel = 'side' |
| 349 |
self.side_panel_darkness = 0 |
| 350 |
self.side_panel_opacity = 1 |
| 351 |
self.main_panel_final_offset = 0 |
| 352 |
self.main_panel_darkness = 0 |
| 353 |
self.side_panel_init_offset = 1 |
| 354 |
elif anim_type == 'fade_in': |
| 355 |
self.top_panel = 'side' |
| 356 |
self.side_panel_darkness = 0 |
| 357 |
self.side_panel_opacity = 0 |
| 358 |
self.main_panel_final_offset = 0 |
| 359 |
self.main_panel_darkness = 0 |
| 360 |
self.side_panel_init_offset = 0.5 |
| 361 |
elif anim_type == 'reveal_below_anim': |
| 362 |
self.top_panel = 'main' |
| 363 |
self.side_panel_darkness = 0.8 |
| 364 |
self.side_panel_opacity = 1 |
| 365 |
self.main_panel_final_offset = 1 |
| 366 |
self.main_panel_darkness = 0 |
| 367 |
self.side_panel_init_offset = 0.5 |
| 368 |
elif anim_type == 'reveal_below_simple': |
| 369 |
self.top_panel = 'main' |
| 370 |
self.side_panel_darkness = 0 |
| 371 |
self.side_panel_opacity = 1 |
| 372 |
self.main_panel_final_offset = 1 |
| 373 |
self.main_panel_darkness = 0 |
| 374 |
self.side_panel_init_offset = 0 |
| 375 |
|
| 376 |
def on_top_panel(self, *args): |
| 377 |
if self.top_panel == 'main': |
| 378 |
self._main_above = True |
| 379 |
else: |
| 380 |
self._main_above = False |
| 381 |
|
| 382 |
def on__main_above(self, *args): |
| 383 |
newval = self._main_above |
| 384 |
main_panel = self._main_panel |
| 385 |
side_panel = self._side_panel |
| 386 |
self.canvas.remove(main_panel.canvas) |
| 387 |
self.canvas.remove(side_panel.canvas) |
| 388 |
if newval: |
| 389 |
self.canvas.insert(0, main_panel.canvas) |
| 390 |
self.canvas.insert(0, side_panel.canvas) |
| 391 |
else: |
| 392 |
self.canvas.insert(0, side_panel.canvas) |
| 393 |
self.canvas.insert(0, main_panel.canvas) |
| 394 |
|
| 395 |
def toggle_main_above(self, *args): |
| 396 |
if self._main_above: |
| 397 |
self._main_above = False |
| 398 |
else: |
| 399 |
self._main_above = True |
| 400 |
|
| 401 |
def add_widget(self, widget): |
| 402 |
if len(self.children) == 0: |
| 403 |
super(NavigationDrawer, self).add_widget(widget) |
| 404 |
self._side_panel = widget |
| 405 |
elif len(self.children) == 1: |
| 406 |
super(NavigationDrawer, self).add_widget(widget) |
| 407 |
self._main_panel = widget |
| 408 |
elif len(self.children) == 2: |
| 409 |
super(NavigationDrawer, self).add_widget(widget) |
| 410 |
self._join_image = widget |
| 411 |
elif self.side_panel is None: |
| 412 |
self._side_panel.add_widget(widget) |
| 413 |
self.side_panel = widget |
| 414 |
elif self.main_panel is None: |
| 415 |
self._main_panel.add_widget(widget) |
| 416 |
self.main_panel = widget |
| 417 |
else: |
| 418 |
raise NavigationDrawerException( |
| 419 |
'Can\'t add more than two widgets' |
| 420 |
'directly to NavigationDrawer') |
| 421 |
|
| 422 |
def remove_widget(self, widget): |
| 423 |
if widget is self.side_panel: |
| 424 |
self._side_panel.remove_widget(widget) |
| 425 |
self.side_panel = None |
| 426 |
elif widget is self.main_panel: |
| 427 |
self._main_panel.remove_widget(widget) |
| 428 |
self.main_panel = None |
| 429 |
else: |
| 430 |
raise NavigationDrawerException( |
| 431 |
'Widget is neither the side or main panel, can\'t remove it.') |
| 432 |
|
| 433 |
def set_side_panel(self, widget): |
| 434 |
'''Removes any existing side panel widgets, and replaces them with the |
| 435 |
argument `widget`. |
| 436 |
''' |
| 437 |
|
| 438 |
if len(self._side_panel.children) > 0: |
| 439 |
for child in self._side_panel.children: |
| 440 |
self._side_panel.remove(child) |
| 441 |
|
| 442 |
self._side_panel.add_widget(widget) |
| 443 |
self.side_panel = widget |
| 444 |
|
| 445 |
def set_main_panel(self, widget): |
| 446 |
'''Removes any existing main panel widgets, and replaces them with the |
| 447 |
argument `widget`. |
| 448 |
''' |
| 449 |
|
| 450 |
if len(self._main_panel.children) > 0: |
| 451 |
for child in self._main_panel.children: |
| 452 |
self._main_panel.remove_widget(child) |
| 453 |
|
| 454 |
self._main_panel.add_widget(widget) |
| 455 |
self.main_panel = widget |
| 456 |
|
| 457 |
def on__anim_progress(self, *args): |
| 458 |
if self._anim_progress > 1: |
| 459 |
self._anim_progress = 1 |
| 460 |
elif self._anim_progress < 0: |
| 461 |
self._anim_progress = 0 |
| 462 |
if self._anim_progress >= 1: |
| 463 |
self.state = 'open' |
| 464 |
elif self._anim_progress <= 0: |
| 465 |
self.state = 'closed' |
| 466 |
|
| 467 |
def on_state(self, *args): |
| 468 |
Animation.cancel_all(self) |
| 469 |
if self.state == 'open': |
| 470 |
self._anim_progress = 1 |
| 471 |
else: |
| 472 |
self._anim_progress = 0 |
| 473 |
|
| 474 |
def anim_to_state(self, state): |
| 475 |
'''If not already in state `state`, animates smoothly to it, taking |
| 476 |
the time given by self.anim_time. State may be either 'open' |
| 477 |
or 'closed'. |
| 478 |
|
| 479 |
''' |
| 480 |
if state == 'open': |
| 481 |
anim = Animation(_anim_progress=1, |
| 482 |
duration=self.anim_time, |
| 483 |
t=self.closing_transition) |
| 484 |
anim.start(self) |
| 485 |
elif state == 'closed': |
| 486 |
anim = Animation(_anim_progress=0, |
| 487 |
duration=self.anim_time, |
| 488 |
t=self.opening_transition) |
| 489 |
anim.start(self) |
| 490 |
else: |
| 491 |
raise NavigationDrawerException( |
| 492 |
'Invalid state received, should be one of `open` or `closed`') |
| 493 |
|
| 494 |
def toggle_state(self, animate=True): |
| 495 |
'''Toggles from open to closed or vice versa, optionally animating or |
| 496 |
simply jumping.''' |
| 497 |
if self.state == 'open': |
| 498 |
if animate: |
| 499 |
self.anim_to_state('closed') |
| 500 |
else: |
| 501 |
self.state = 'closed' |
| 502 |
elif self.state == 'closed': |
| 503 |
if animate: |
| 504 |
self.anim_to_state('open') |
| 505 |
else: |
| 506 |
self.state = 'open' |
| 507 |
|
| 508 |
def on_touch_down(self, touch): |
| 509 |
col_self = self.collide_point(*touch.pos) |
| 510 |
col_side = self._side_panel.collide_point(*touch.pos) |
| 511 |
col_main = self._main_panel.collide_point(*touch.pos) |
| 512 |
|
| 513 |
if self._anim_progress < 0.001: |
| 514 |
valid_region = (self.x <= |
| 515 |
touch.x <= |
| 516 |
(self.x + self.touch_accept_width)) |
| 517 |
if not valid_region: |
| 518 |
self._main_panel.on_touch_down(touch) |
| 519 |
return False |
| 520 |
else: |
| 521 |
if col_side and not self._main_above: |
| 522 |
self._side_panel.on_touch_down(touch) |
| 523 |
return False |
| 524 |
valid_region = (self._main_panel.x <= |
| 525 |
touch.x <= |
| 526 |
(self._main_panel.x + self._main_panel.width)) |
| 527 |
if not valid_region: |
| 528 |
if self._main_above: |
| 529 |
if col_main: |
| 530 |
self._main_panel.on_touch_down(touch) |
| 531 |
elif col_side: |
| 532 |
self._side_panel.on_touch_down(touch) |
| 533 |
else: |
| 534 |
if col_side: |
| 535 |
self._side_panel.on_touch_down(touch) |
| 536 |
elif col_main: |
| 537 |
self._main_panel.on_touch_down(touch) |
| 538 |
return False |
| 539 |
Animation.cancel_all(self) |
| 540 |
self._anim_init_progress = self._anim_progress |
| 541 |
self._touch = touch |
| 542 |
touch.ud['type'] = self.state |
| 543 |
touch.ud['panels_jiggled'] = False |
| 544 |
|
| 545 |
|
| 546 |
touch.grab(self) |
| 547 |
return True |
| 548 |
|
| 549 |
def on_touch_move(self, touch): |
| 550 |
if touch is self._touch: |
| 551 |
dx = touch.x - touch.ox |
| 552 |
self._anim_progress = max(0, min(self._anim_init_progress + |
| 553 |
(dx / self.side_panel_width), 1)) |
| 554 |
if self._anim_progress < 0.975: |
| 555 |
touch.ud['panels_jiggled'] = True |
| 556 |
else: |
| 557 |
super(NavigationDrawer, self).on_touch_move(touch) |
| 558 |
return |
| 559 |
|
| 560 |
def on_touch_up(self, touch): |
| 561 |
if touch is self._touch: |
| 562 |
self._touch = None |
| 563 |
init_state = touch.ud['type'] |
| 564 |
touch.ungrab(self) |
| 565 |
jiggled = touch.ud['panels_jiggled'] |
| 566 |
if init_state == 'open' and not jiggled: |
| 567 |
if self._anim_progress >= 0.975: |
| 568 |
self.anim_to_state('closed') |
| 569 |
else: |
| 570 |
self._anim_relax() |
| 571 |
else: |
| 572 |
self._anim_relax() |
| 573 |
else: |
| 574 |
super(NavigationDrawer, self).on_touch_up(touch) |
| 575 |
return |
| 576 |
|
| 577 |
def _anim_relax(self): |
| 578 |
'''Animates to the open or closed position, depending on whether the |
| 579 |
current position is past self.min_dist_to_open. |
| 580 |
|
| 581 |
''' |
| 582 |
if self._anim_progress > self.min_dist_to_open: |
| 583 |
self.anim_to_state('open') |
| 584 |
else: |
| 585 |
self.anim_to_state('closed') |
| 586 |
|
| 587 |
def _choose_image(self, *args): |
| 588 |
'''Chooses which image to display as the main/side separator, based on |
| 589 |
_main_above.''' |
| 590 |
if self.separator_image: |
| 591 |
return self.separator_image |
| 592 |
if self._main_above: |
| 593 |
return 'navigationdrawer_gradient_rtol.png' |
| 594 |
else: |
| 595 |
return 'navigationdrawer_gradient_ltor.png' |
| 596 |
|
| 597 |
if __name__ == '__main__': |
| 598 |
from kivy.base import runTouchApp |
| 599 |
from kivy.uix.boxlayout import BoxLayout |
| 600 |
from kivy.uix.label import Label |
| 601 |
from kivy.uix.button import Button |
| 602 |
from kivy.uix.popup import Popup |
| 603 |
from kivy.uix.image import Image |
| 604 |
from kivy.core.window import Window |
| 605 |
|
| 606 |
navigationdrawer = NavigationDrawer() |
| 607 |
|
| 608 |
side_panel = BoxLayout(orientation='vertical') |
| 609 |
side_panel.add_widget(Label(text='Panel label')) |
| 610 |
popup = Popup(title='Sidebar popup', |
| 611 |
content=Label( |
| 612 |
text='You clicked the sidebar\npopup button'), |
| 613 |
size_hint=(0.7, 0.7)) |
| 614 |
first_button = Button(text='Popup\nbutton') |
| 615 |
first_button.bind(on_release=popup.open) |
| 616 |
side_panel.add_widget(first_button) |
| 617 |
side_panel.add_widget(Button(text='Another\nbutton')) |
| 618 |
navigationdrawer.add_widget(side_panel) |
| 619 |
|
| 620 |
label_head = ( |
| 621 |
'[b]Example label filling main panel[/b]\n\n[color=ff0000](p' |
| 622 |
'ull from left to right!)[/color]\n\nIn this example, the le' |
| 623 |
'ft panel is a simple boxlayout menu, and this main panel is' |
| 624 |
' a BoxLayout with a label and example image.\n\nSeveral pre' |
| 625 |
'set layouts are available (see buttons below), but users ma' |
| 626 |
'y edit every parameter for much more customisation.') |
| 627 |
main_panel = BoxLayout(orientation='vertical') |
| 628 |
label_bl = BoxLayout(orientation='horizontal') |
| 629 |
label = Label(text=label_head, font_size='15sp', |
| 630 |
markup=True, valign='top') |
| 631 |
label_bl.add_widget(Widget(size_hint_x=None, width=dp(10))) |
| 632 |
label_bl.add_widget(label) |
| 633 |
label_bl.add_widget(Widget(size_hint_x=None, width=dp(10))) |
| 634 |
main_panel.add_widget(Widget(size_hint_y=None, height=dp(10))) |
| 635 |
main_panel.add_widget(label_bl) |
| 636 |
main_panel.add_widget(Widget(size_hint_y=None, height=dp(10))) |
| 637 |
navigationdrawer.add_widget(main_panel) |
| 638 |
label.bind(size=label.setter('text_size')) |
| 639 |
|
| 640 |
def set_anim_type(name): |
| 641 |
navigationdrawer.anim_type = name |
| 642 |
|
| 643 |
def set_transition(name): |
| 644 |
navigationdrawer.opening_transition = name |
| 645 |
navigationdrawer.closing_transition = name |
| 646 |
|
| 647 |
modes_layout = BoxLayout(orientation='horizontal') |
| 648 |
modes_layout.add_widget(Label(text='preset\nanims:')) |
| 649 |
slide_an = Button(text='slide_\nabove_\nanim') |
| 650 |
slide_an.bind(on_press=lambda j: set_anim_type('slide_above_anim')) |
| 651 |
slide_sim = Button(text='slide_\nabove_\nsimple') |
| 652 |
slide_sim.bind(on_press=lambda j: set_anim_type('slide_above_simple')) |
| 653 |
fade_in_button = Button(text='fade_in') |
| 654 |
fade_in_button.bind(on_press=lambda j: set_anim_type('fade_in')) |
| 655 |
reveal_button = Button(text='reveal_\nbelow_\nanim') |
| 656 |
reveal_button.bind(on_press= |
| 657 |
lambda j: set_anim_type('reveal_below_anim')) |
| 658 |
slide_button = Button(text='reveal_\nbelow_\nsimple') |
| 659 |
slide_button.bind(on_press= |
| 660 |
lambda j: set_anim_type('reveal_below_simple')) |
| 661 |
modes_layout.add_widget(slide_an) |
| 662 |
modes_layout.add_widget(slide_sim) |
| 663 |
modes_layout.add_widget(fade_in_button) |
| 664 |
modes_layout.add_widget(reveal_button) |
| 665 |
modes_layout.add_widget(slide_button) |
| 666 |
main_panel.add_widget(modes_layout) |
| 667 |
|
| 668 |
transitions_layout = BoxLayout(orientation='horizontal') |
| 669 |
transitions_layout.add_widget(Label(text='anim\ntransitions')) |
| 670 |
out_cubic = Button(text='out_cubic') |
| 671 |
out_cubic.bind(on_press= |
| 672 |
lambda j: set_transition('out_cubic')) |
| 673 |
in_quint = Button(text='in_quint') |
| 674 |
in_quint.bind(on_press= |
| 675 |
lambda j: set_transition('in_quint')) |
| 676 |
linear = Button(text='linear') |
| 677 |
linear.bind(on_press= |
| 678 |
lambda j: set_transition('linear')) |
| 679 |
out_sine = Button(text='out_sine') |
| 680 |
out_sine.bind(on_press= |
| 681 |
lambda j: set_transition('out_sine')) |
| 682 |
transitions_layout.add_widget(out_cubic) |
| 683 |
transitions_layout.add_widget(in_quint) |
| 684 |
transitions_layout.add_widget(linear) |
| 685 |
transitions_layout.add_widget(out_sine) |
| 686 |
main_panel.add_widget(transitions_layout) |
| 687 |
|
| 688 |
button = Button(text='toggle NavigationDrawer state (animate)', |
| 689 |
size_hint_y=0.2) |
| 690 |
button.bind(on_press=lambda j: navigationdrawer.toggle_state()) |
| 691 |
button2 = Button(text='toggle NavigationDrawer state (jump)', |
| 692 |
size_hint_y=0.2) |
| 693 |
button2.bind(on_press=lambda j: navigationdrawer.toggle_state(False)) |
| 694 |
button3 = Button(text='toggle _main_above', size_hint_y=0.2) |
| 695 |
button3.bind(on_press=navigationdrawer.toggle_main_above) |
| 696 |
main_panel.add_widget(button) |
| 697 |
main_panel.add_widget(button2) |
| 698 |
main_panel.add_widget(button3) |
| 699 |
|
| 700 |
Window.add_widget(navigationdrawer) |
| 701 |
|
| 702 |
runTouchApp() |