modalview: allow to close the modalview by hitting escape, if the auto_dismiss is allowed. closes #776

This commit is contained in:
Mathieu Virbel 2012-11-19 16:46:23 +01:00
parent 3d45421b2d
commit 7180c1ceb2
2 changed files with 18 additions and 4 deletions

View File

@ -255,8 +255,7 @@ class ShowcaseApp(App):
content.add_widget(Label(text='Hello world'))
content.add_widget(btnclose)
popup = Popup(content=content, title='Modal popup example',
size_hint=(None, None), size=('300dp', '300dp'),
auto_dismiss=False)
size_hint=(None, None), size=('300dp', '300dp'))
btnclose.bind(on_release=popup.dismiss)
button = Button(text='Open popup', size_hint=(None, None),
size=('150sp', '70dp'),

View File

@ -61,6 +61,12 @@ view from closing by explictly returning True from your callback ::
view.bind(on_dismiss=my_callback)
view.open()
.. versionchanged:: 1.5.0
The ModalView can be closed by hitting escape key on the keyboard, if the
:data:`ModalView.auto_dismiss` is allowed.
'''
__all__ = ('ModalView', )
@ -165,7 +171,9 @@ class ModalView(AnchorLayout):
Logger.warning('ModalView: cannot open view, no window found.')
return self
self._window.add_widget(self)
self._window.bind(on_resize=self._align_center)
self._window.bind(
on_resize=self._align_center,
on_keyboard=self._handle_keyboard)
self.center = self._window.center
a = Animation(_anim_alpha=1., d=self._anim_duration)
a.bind(on_complete=lambda *x: self.dispatch('on_open'))
@ -227,7 +235,9 @@ class ModalView(AnchorLayout):
def on__anim_alpha(self, instance, value):
if value == 0 and self._window is not None:
self._window.remove_widget(self)
self._window.unbind(on_resize=self._align_center)
self._window.unbind(
on_resize=self._align_center,
on_keyboard=self._handle_keyboard)
self._window = None
def on_open(self):
@ -236,6 +246,11 @@ class ModalView(AnchorLayout):
def on_dismiss(self):
pass
def _handle_keyboard(self, window, key, *largs):
if key == 27 and self.auto_dismiss:
self.dismiss()
return True
if __name__ == '__main__':
from kivy.base import runTouchApp