forked from kivy/kivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
2111 lines (1670 loc) · 71.6 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint: disable=W0611
# coding: utf-8
'''
Window
======
Core class for creating the default Kivy window. Kivy supports only one window
per application: please don't try to create more than one.
'''
__all__ = ('Keyboard', 'WindowBase', 'Window')
from os.path import join, exists
from os import getcwd
from kivy.core import core_select_lib
from kivy.clock import Clock
from kivy.config import Config
from kivy.logger import Logger
from kivy.base import EventLoop, stopTouchApp
from kivy.modules import Modules
from kivy.event import EventDispatcher
from kivy.properties import ListProperty, ObjectProperty, AliasProperty, \
NumericProperty, OptionProperty, StringProperty, BooleanProperty, \
ColorProperty
from kivy.utils import platform, reify, deprecated, pi_version
from kivy.context import get_current_context
from kivy.uix.behaviors import FocusBehavior
from kivy.setupconfig import USE_SDL2
from kivy.graphics.transformation import Matrix
from kivy.graphics.cgl import cgl_get_backend_name
# late import
VKeyboard = None
android = None
Animation = None
class Keyboard(EventDispatcher):
'''Keyboard interface that is returned by
:meth:`WindowBase.request_keyboard`. When you request a keyboard,
you'll get an instance of this class. Whatever the keyboard input is
(system or virtual keyboard), you'll receive events through this
instance.
:Events:
`on_key_down`: keycode, text, modifiers
Fired when a new key is pressed down
`on_key_up`: keycode
Fired when a key is released (up)
Here is an example of how to request a Keyboard in accordance with the
current configuration:
.. include:: ../../examples/widgets/keyboardlistener.py
:literal:
'''
# Keycodes mapping, between str <-> int. These keycodes are
# currently taken from pygame.key. But when a new provider will be
# used, it must do the translation to these keycodes too.
keycodes = {
# specials keys
'backspace': 8, 'tab': 9, 'enter': 13, 'rshift': 303, 'shift': 304,
'alt': 308, 'rctrl': 306, 'lctrl': 305,
'super': 309, 'alt-gr': 307, 'compose': 311, 'pipe': 310,
'capslock': 301, 'escape': 27, 'spacebar': 32, 'pageup': 280,
'pagedown': 281, 'end': 279, 'home': 278, 'left': 276, 'up':
273, 'right': 275, 'down': 274, 'insert': 277, 'delete': 127,
'numlock': 300, 'print': 144, 'screenlock': 145, 'pause': 19,
# a-z keys
'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101, 'f': 102, 'g': 103,
'h': 104, 'i': 105, 'j': 106, 'k': 107, 'l': 108, 'm': 109, 'n': 110,
'o': 111, 'p': 112, 'q': 113, 'r': 114, 's': 115, 't': 116, 'u': 117,
'v': 118, 'w': 119, 'x': 120, 'y': 121, 'z': 122,
# 0-9 keys
'0': 48, '1': 49, '2': 50, '3': 51, '4': 52,
'5': 53, '6': 54, '7': 55, '8': 56, '9': 57,
# numpad
'numpad0': 256, 'numpad1': 257, 'numpad2': 258, 'numpad3': 259,
'numpad4': 260, 'numpad5': 261, 'numpad6': 262, 'numpad7': 263,
'numpad8': 264, 'numpad9': 265, 'numpaddecimal': 266,
'numpaddivide': 267, 'numpadmul': 268, 'numpadsubstract': 269,
'numpadadd': 270, 'numpadenter': 271,
# F1-15
'f1': 282, 'f2': 283, 'f3': 284, 'f4': 285, 'f5': 286, 'f6': 287,
'f7': 288, 'f8': 289, 'f9': 290, 'f10': 291, 'f11': 292, 'f12': 293,
'f13': 294, 'f14': 295, 'f15': 296,
# other keys
'(': 40, ')': 41,
'[': 91, ']': 93,
'{': 123, '}': 125,
':': 58, ';': 59,
'=': 61, '+': 43,
'-': 45, '_': 95,
'/': 47, '*': 42,
'?': 47,
'`': 96, '~': 126,
'´': 180, '¦': 166,
'\\': 92, '|': 124,
'"': 34, "'": 39,
',': 44, '.': 46,
'<': 60, '>': 62,
'@': 64, '!': 33,
'#': 35, '$': 36,
'%': 37, '^': 94,
'&': 38, '¬': 172,
'¨': 168, '…': 8230,
'ù': 249, 'à': 224,
'é': 233, 'è': 232,
}
__events__ = ('on_key_down', 'on_key_up', 'on_textinput')
def __init__(self, **kwargs):
super(Keyboard, self).__init__()
#: Window which the keyboard is attached too
self.window = kwargs.get('window', None)
#: Callback that will be called when the keyboard is released
self.callback = kwargs.get('callback', None)
#: Target that have requested the keyboard
self.target = kwargs.get('target', None)
#: VKeyboard widget, if allowed by the configuration
self.widget = kwargs.get('widget', None)
def on_key_down(self, keycode, text, modifiers):
pass
def on_key_up(self, keycode):
pass
def on_textinput(self, text):
pass
def release(self):
'''Call this method to release the current keyboard.
This will ensure that the keyboard is no longer attached to your
callback.'''
if self.window:
self.window.release_keyboard(self.target)
self.target = None
def _on_window_textinput(self, instance, text):
return self.dispatch('on_textinput', text)
def _on_window_key_down(self, instance, keycode, scancode, text,
modifiers):
keycode = (keycode, self.keycode_to_string(keycode))
if text == '\x04':
Window.trigger_keyboard_height()
return
return self.dispatch('on_key_down', keycode, text, modifiers)
def _on_window_key_up(self, instance, keycode, *largs):
keycode = (keycode, self.keycode_to_string(keycode))
return self.dispatch('on_key_up', keycode)
def _on_vkeyboard_key_down(self, instance, keycode, text, modifiers):
if keycode is None:
keycode = text.lower()
keycode = (self.string_to_keycode(keycode), keycode)
return self.dispatch('on_key_down', keycode, text, modifiers)
def _on_vkeyboard_key_up(self, instance, keycode, text, modifiers):
if keycode is None:
keycode = text
keycode = (self.string_to_keycode(keycode), keycode)
return self.dispatch('on_key_up', keycode)
def _on_vkeyboard_textinput(self, instance, text):
return self.dispatch('on_textinput', text)
def string_to_keycode(self, value):
'''Convert a string to a keycode number according to the
:attr:`Keyboard.keycodes`. If the value is not found in the
keycodes, it will return -1.
'''
return Keyboard.keycodes.get(value, -1)
def keycode_to_string(self, value):
'''Convert a keycode number to a string according to the
:attr:`Keyboard.keycodes`. If the value is not found in the
keycodes, it will return ''.
'''
keycodes = list(Keyboard.keycodes.values())
if value in keycodes:
return list(Keyboard.keycodes.keys())[keycodes.index(value)]
return ''
class WindowBase(EventDispatcher):
'''WindowBase is an abstract window widget for any window implementation.
:Parameters:
`borderless`: str, one of ('0', '1')
Set the window border state. Check the
:mod:`~kivy.config` documentation for a
more detailed explanation on the values.
`fullscreen`: str, one of ('0', '1', 'auto', 'fake')
Make the window fullscreen. Check the
:mod:`~kivy.config` documentation for a
more detailed explanation on the values.
`width`: int
Width of the window.
`height`: int
Height of the window.
`minimum_width`: int
Minimum width of the window (only works for sdl2 window provider).
`minimum_height`: int
Minimum height of the window (only works for sdl2 window provider).
`allow_screensaver`: bool
Allow the device to show a screen saver, or to go to sleep
on mobile devices. Defaults to True. Only works for sdl2 window
provider.
:Events:
`on_motion`: etype, motionevent
Fired when a new :class:`~kivy.input.motionevent.MotionEvent` is
dispatched
`on_touch_down`:
Fired when a new touch event is initiated.
`on_touch_move`:
Fired when an existing touch event changes location.
`on_touch_up`:
Fired when an existing touch event is terminated.
`on_draw`:
Fired when the :class:`Window` is being drawn.
`on_flip`:
Fired when the :class:`Window` GL surface is being flipped.
`on_rotate`: rotation
Fired when the :class:`Window` is being rotated.
`on_close`:
Fired when the :class:`Window` is closed.
`on_request_close`:
Fired when the event loop wants to close the window, or if the
escape key is pressed and `exit_on_escape` is `True`. If a function
bound to this event returns `True`, the window will not be closed.
If the the event is triggered because of the keyboard escape key,
the keyword argument `source` is dispatched along with a value of
`keyboard` to the bound functions.
.. versionadded:: 1.9.0
`on_cursor_enter`:
Fired when the cursor enters the window.
.. versionadded:: 1.9.1
`on_cursor_leave`:
Fired when the cursor leaves the window.
.. versionadded:: 1.9.1
`on_minimize`:
Fired when the window is minimized.
.. versionadded:: 1.10.0
`on_maximize`:
Fired when the window is maximized.
.. versionadded:: 1.10.0
`on_restore`:
Fired when the window is restored.
.. versionadded:: 1.10.0
`on_hide`:
Fired when the window is hidden.
.. versionadded:: 1.10.0
`on_show`:
Fired when when the window is shown.
.. versionadded:: 1.10.0
`on_keyboard`: key, scancode, codepoint, modifier
Fired when the keyboard is used for input.
.. versionchanged:: 1.3.0
The *unicode* parameter has been deprecated in favor of
codepoint, and will be removed completely in future versions.
`on_key_down`: key, scancode, codepoint, modifier
Fired when a key pressed.
.. versionchanged:: 1.3.0
The *unicode* parameter has been deprecated in favor of
codepoint, and will be removed completely in future versions.
`on_key_up`: key, scancode, codepoint
Fired when a key is released.
.. versionchanged:: 1.3.0
The *unicode* parameter has be deprecated in favor of
codepoint, and will be removed completely in future versions.
`on_dropfile`: filename (bytes or string):
Fired when a file is dropped on the application.
.. note::
This event doesn't work for apps with elevated permissions,
because the OS API calls are filtered. Check issue
`#4999 <https://github.com/kivy/kivy/issues/4999>`_ for
pointers to workarounds.
`on_memorywarning`:
Fired when the platform have memory issue (iOS / Android mostly)
You can listen to this one, and clean whatever you can.
.. versionadded:: 1.9.0
`on_textedit(self, text)`:
Fired when inputting with IME.
The string inputting with IME is set as the parameter of
this event.
.. versionadded:: 1.10.1
'''
__instance = None
__initialized = False
_fake_fullscreen = False
# private properties
_density = NumericProperty(1)
_size = ListProperty([0, 0])
_modifiers = ListProperty([])
_rotation = NumericProperty(0)
_clearcolor = ObjectProperty([0, 0, 0, 1])
_focus = BooleanProperty(True)
gl_backends_allowed = []
"""
A list of Kivy gl backend names, which if not empty, will be the
exclusive list of gl backends that can be used with this window.
"""
gl_backends_ignored = []
"""
A list of Kivy gl backend names that may not be used with this window.
"""
managed_textinput = False
"""
True if this Window class uses `on_textinput` to insert text, internal.
"""
children = ListProperty([])
'''List of the children of this window.
:attr:`children` is a :class:`~kivy.properties.ListProperty` instance and
defaults to an empty list.
Use :meth:`add_widget` and :meth:`remove_widget` to manipulate the list of
children. Don't manipulate the list directly unless you know what you are
doing.
'''
parent = ObjectProperty(None, allownone=True)
'''Parent of this window.
:attr:`parent` is a :class:`~kivy.properties.ObjectProperty` instance and
defaults to None. When created, the parent is set to the window itself.
You must take care of it if you are doing a recursive check.
'''
icon = StringProperty()
'''A path to the window icon.
.. versionadded:: 1.1.2
:attr:`icon` is a :class:`~kivy.properties.StringProperty`.
'''
def _get_modifiers(self):
return self._modifiers
modifiers = AliasProperty(_get_modifiers, None, bind=('_modifiers',))
'''List of keyboard modifiers currently active.
.. versionadded:: 1.0.9
:attr:`modifiers` is an :class:`~kivy.properties.AliasProperty`.
'''
def _get_size(self):
r = self._rotation
w, h = self._size
if self._density != 1:
w, h = self._win._get_gl_size()
if self.softinput_mode == 'resize':
h -= self.keyboard_height
if r in (0, 180):
return w, h
return h, w
def _set_size(self, size):
if self._size != size:
r = self._rotation
if r in (0, 180):
self._size = size
else:
self._size = size[1], size[0]
self.dispatch('on_pre_resize', *size)
minimum_width = NumericProperty(0)
'''The minimum width to restrict the window to.
.. versionadded:: 1.9.1
:attr:`minimum_width` is a :class:`~kivy.properties.NumericProperty` and
defaults to 0.
'''
minimum_height = NumericProperty(0)
'''The minimum height to restrict the window to.
.. versionadded:: 1.9.1
:attr:`minimum_height` is a :class:`~kivy.properties.NumericProperty` and
defaults to 0.
'''
allow_screensaver = BooleanProperty(True)
'''Whether the screen saver is enabled, or on mobile devices whether the
device is allowed to go to sleep while the app is open.
.. versionadded:: 1.10.0
:attr:`allow_screensaver` is a :class:`~kivy.properties.BooleanProperty`
and defaults to True.
'''
size = AliasProperty(_get_size, _set_size,
bind=('_size', '_rotation', 'softinput_mode',
'keyboard_height'))
'''Get the rotated size of the window. If :attr:`rotation` is set, then the
size will change to reflect the rotation.
.. versionadded:: 1.0.9
:attr:`size` is an :class:`~kivy.properties.AliasProperty`.
'''
def _get_clearcolor(self):
return self._clearcolor
def _set_clearcolor(self, value):
if value is not None:
if type(value) not in (list, tuple):
raise Exception('Clearcolor must be a list or tuple')
if len(value) != 4:
raise Exception('Clearcolor must contain 4 values')
self._clearcolor = value
clearcolor = AliasProperty(_get_clearcolor, _set_clearcolor,
bind=('_clearcolor', ))
'''Color used to clear the window.
::
from kivy.core.window import Window
# red background color
Window.clearcolor = (1, 0, 0, 1)
# don't clear background at all
Window.clearcolor = None
.. versionchanged:: 1.7.2
The clearcolor default value is now: (0, 0, 0, 1).
.. versionadded:: 1.0.9
:attr:`clearcolor` is an :class:`~kivy.properties.AliasProperty` and
defaults to (0, 0, 0, 1).
'''
# make some property read-only
def _get_width(self):
_size = self._size
if self._density != 1:
_size = self._win._get_gl_size()
r = self._rotation
if r == 0 or r == 180:
return _size[0]
return _size[1]
width = AliasProperty(_get_width, None, bind=('_rotation', '_size'))
'''Rotated window width.
:attr:`width` is a read-only :class:`~kivy.properties.AliasProperty`.
'''
def _get_height(self):
'''Rotated window height'''
r = self._rotation
_size = self._size
if self._density != 1:
_size = self._win._get_gl_size()
kb = self.keyboard_height if self.softinput_mode == 'resize' else 0
if r == 0 or r == 180:
return _size[1] - kb
return _size[0] - kb
height = AliasProperty(_get_height, None,
bind=('_rotation', '_size', 'softinput_mode',
'keyboard_height'))
'''Rotated window height.
:attr:`height` is a read-only :class:`~kivy.properties.AliasProperty`.
'''
def _get_center(self):
return self.width / 2., self.height / 2.
center = AliasProperty(_get_center,
bind=('width', 'height', '_density'),
cache=True)
'''Center of the rotated window.
.. versionadded:: 1.0.9
:attr:`center` is an :class:`~kivy.properties.AliasProperty`.
'''
def _get_rotation(self):
return self._rotation
def _set_rotation(self, x):
x = int(x % 360)
if x == self._rotation:
return
if x not in (0, 90, 180, 270):
raise ValueError('can rotate only 0, 90, 180, 270 degrees')
self._rotation = x
if not self.initialized:
return
self.dispatch('on_pre_resize', *self.size)
self.dispatch('on_rotate', x)
rotation = AliasProperty(_get_rotation, _set_rotation,
bind=('_rotation', ))
'''Get/set the window content rotation. Can be one of 0, 90, 180, 270
degrees.
.. versionadded:: 1.0.9
:attr:`rotation` is an :class:`~kivy.properties.AliasProperty`.
'''
softinput_mode = OptionProperty(
'', options=('', 'below_target', 'pan', 'scale', 'resize')
)
'''This specifies the behavior of window contents on display of the soft
keyboard on mobile platforms. It can be one of '', 'pan', 'scale',
'resize' or 'below_target'. Their effects are listed below.
+----------------+-------------------------------------------------------+
| Value | Effect |
+================+=======================================================+
| '' | The main window is left as is, allowing you to use |
| | the :attr:`keyboard_height` to manage the window |
| | contents manually. |
+----------------+-------------------------------------------------------+
| 'pan' | The main window pans, moving the bottom part of the |
| | window to be always on top of the keyboard. |
+----------------+-------------------------------------------------------+
| 'resize' | The window is resized and the contents scaled to fit |
| | the remaining space. |
+----------------+-------------------------------------------------------+
| 'below_target' | The window pans so that the current target TextInput |
| | widget requesting the keyboard is presented just above|
| | the soft keyboard. |
+----------------+-------------------------------------------------------+
:attr:`softinput_mode` is an :class:`~kivy.properties.OptionProperty` and
defaults to ''.
.. note:: The `resize` option does not currently work with SDL2 on Android.
.. versionadded:: 1.9.0
.. versionchanged:: 1.9.1
The 'below_target' option was added.
'''
_keyboard_changed = BooleanProperty(False)
_kheight = NumericProperty(0)
_kanimation = None
def _free_kanimation(self, *largs):
WindowBase._kanimation = None
def _animate_content(self):
'''Animate content to IME height.
'''
kargs = self.keyboard_anim_args
global Animation
if not Animation:
from kivy.animation import Animation
if WindowBase._kanimation:
WindowBase._kanimation.cancel(self)
WindowBase._kanimation = kanim = Animation(
_kheight=self.keyboard_height + self.keyboard_padding,
d=kargs['d'], t=kargs['t'])
kanim.bind(on_complete=self._free_kanimation)
kanim.start(self)
def _upd_kbd_height(self, *kargs):
self._keyboard_changed = not self._keyboard_changed
self._animate_content()
def _get_ios_kheight(self):
import ios
return ios.get_kheight()
def _get_android_kheight(self):
if USE_SDL2: # Placeholder until the SDL2 bootstrap supports this
return 0
global android
if not android:
import android
return android.get_keyboard_height()
def _get_kheight(self):
if platform == 'android':
return self._get_android_kheight()
if platform == 'ios':
return self._get_ios_kheight()
return 0
keyboard_height = AliasProperty(_get_kheight, bind=('_keyboard_changed',))
'''Returns the height of the softkeyboard/IME on mobile platforms.
Will return 0 if not on mobile platform or if IME is not active.
.. note:: This property returns 0 with SDL2 on Android, but setting
Window.softinput_mode does work.
.. versionadded:: 1.9.0
:attr:`keyboard_height` is a read-only
:class:`~kivy.properties.AliasProperty` and defaults to 0.
'''
keyboard_anim_args = {'t': 'in_out_quart', 'd': .5}
'''The attributes for animating softkeyboard/IME.
`t` = `transition`, `d` = `duration`. This value will have no effect on
desktops.
.. versionadded:: 1.10.0
:attr:`keyboard_anim_args` is a dict and defaults to
{'t': 'in_out_quart', 'd': `.5`}.
'''
keyboard_padding = NumericProperty(0)
'''The padding to have between the softkeyboard/IME & target
or bottom of window. Will have no effect on desktops.
.. versionadded:: 1.10.0
:attr:`keyboard_padding` is a
:class:`~kivy.properties.NumericProperty` and defaults to 0.
'''
def _set_system_size(self, size):
self._size = size
def _get_system_size(self):
if self.softinput_mode == 'resize':
return self._size[0], self._size[1] - self.keyboard_height
return self._size
system_size = AliasProperty(_get_system_size, _set_system_size,
bind=('_size', 'softinput_mode',
'keyboard_height'),
cache=True)
'''Real size of the window ignoring rotation. If the density is
not 1, the :attr:`system_size` is the :attr:`size` divided by
density.
.. versionadded:: 1.0.9
:attr:`system_size` is an :class:`~kivy.properties.AliasProperty`.
'''
def _get_effective_size(self):
'''On density=1 and non-ios displays, return :attr:`system_size`,
else return scaled / rotated :attr:`size`.
Used by MouseMotionEvent.update_graphics() and WindowBase.on_motion().
'''
w, h = self.system_size
if platform == 'ios' or self._density != 1:
w, h = self.size
return w, h
borderless = BooleanProperty(False)
'''When set to True, this property removes the window border/decoration.
Check the :mod:`~kivy.config` documentation for a more detailed
explanation on the values.
.. versionadded:: 1.9.0
:attr:`borderless` is a :class:`~kivy.properties.BooleanProperty` and
defaults to False.
'''
fullscreen = OptionProperty(False, options=(True, False, 'auto', 'fake'))
'''This property sets the fullscreen mode of the window. Available options
are: True, False, 'auto' and 'fake'. Check the :mod:`~kivy.config`
documentation for more detailed explanations on these values.
fullscreen is an :class:`~kivy.properties.OptionProperty` and defaults to
`False`.
.. versionadded:: 1.2.0
.. note::
The 'fake' option has been deprecated, use the :attr:`borderless`
property instead.
'''
mouse_pos = ObjectProperty([0, 0])
'''2d position of the mouse within the window.
.. versionadded:: 1.2.0
:attr:`mouse_pos` is an :class:`~kivy.properties.ObjectProperty` and
defaults to [0, 0].
'''
show_cursor = BooleanProperty(True)
'''Set whether or not the cursor is shown on the window.
.. versionadded:: 1.9.1
:attr:`show_cursor` is a :class:`~kivy.properties.BooleanProperty` and
defaults to True.
'''
def _get_focus(self):
return self._focus
focus = AliasProperty(_get_focus, None, bind=('_focus',))
'''Check whether or not the window currently has focus.
.. versionadded:: 1.9.1
:attr:`focus` is a read-only :class:`~kivy.properties.AliasProperty` and
defaults to True.
'''
def _set_cursor_state(self, value):
pass
def set_system_cursor(self, cursor_name):
'''Set type of a mouse cursor in the Window.
It can be one of 'arrow', 'ibeam', 'wait', 'crosshair', 'wait_arrow',
'size_nwse', 'size_nesw', 'size_we', 'size_ns', 'size_all', 'no', or
'hand'.
On some platforms there might not be a specific cursor supported and
such an option falls back to one of the substitutable alternatives:
+------------+-----------+------------+-----------+---------------+
| | Windows | MacOS | Linux X11 | Linux Wayland |
+============+===========+============+===========+===============+
| arrow | arrow | arrow | arrow | arrow |
+------------+-----------+------------+-----------+---------------+
| ibeam | ibeam | ibeam | ibeam | ibeam |
+------------+-----------+------------+-----------+---------------+
| wait | wait | arrow | wait | wait |
+------------+-----------+------------+-----------+---------------+
| crosshair | crosshair | crosshair | crosshair | hand |
+------------+-----------+------------+-----------+---------------+
| wait_arrow | arrow | arrow | wait | wait |
+------------+-----------+------------+-----------+---------------+
| size_nwse | size_nwse | size_all | size_all | hand |
+------------+-----------+------------+-----------+---------------+
| size_nesw | size_nesw | size_all | size_all | hand |
+------------+-----------+------------+-----------+---------------+
| size_we | size_we | size_we | size_we | hand |
+------------+-----------+------------+-----------+---------------+
| size_ns | size_ns | size_ns | size_ns | hand |
+------------+-----------+------------+-----------+---------------+
| size_all | size_all | size_all | size_all | hand |
+------------+-----------+------------+-----------+---------------+
| no | no | no | no | ibeam |
+------------+-----------+------------+-----------+---------------+
| hand | hand | hand | hand | hand |
+------------+-----------+------------+-----------+---------------+
.. versionadded:: 1.10.1
.. note::
This feature requires the SDL2 window provider and is currently
only supported on desktop platforms.
'''
pass
def _get_window_pos(self):
pass
def _set_window_pos(self, x, y):
pass
def _get_left(self):
if not self.initialized:
return self._left
return self._get_window_pos()[0]
def _set_left(self, value):
pos = self._get_window_pos()
self._set_window_pos(value, pos[1])
def _get_top(self):
if not self.initialized:
return self._top
return self._get_window_pos()[1]
def _set_top(self, value):
pos = self._get_window_pos()
self._set_window_pos(pos[0], value)
top = AliasProperty(_get_top, _set_top)
'''Top position of the window.
.. note:: It's an SDL2 property with `[0, 0]` in the top-left corner.
.. versionchanged:: 1.10.0
:attr:`top` is now an :class:`~kivy.properties.AliasProperty`
.. versionadded:: 1.9.1
:attr:`top` is an :class:`~kivy.properties.AliasProperty` and defaults to
the position set in :class:`~kivy.config.Config`.
'''
left = AliasProperty(_get_left, _set_left)
'''Left position of the window.
.. note:: It's an SDL2 property with `[0, 0]` in the top-left corner.
.. versionchanged:: 1.10.0
:attr:`left` is now an :class:`~kivy.properties.AliasProperty`
.. versionadded:: 1.9.1
:attr:`left` is an :class:`~kivy.properties.AliasProperty` and defaults to
the position set in :class:`~kivy.config.Config`.
'''
@property
def __self__(self):
return self
position = OptionProperty('auto', options=['auto', 'custom'])
render_context = ObjectProperty(None)
canvas = ObjectProperty(None)
title = StringProperty('Kivy')
trigger_create_window = None
__events__ = (
'on_draw', 'on_flip', 'on_rotate', 'on_resize', 'on_move',
'on_close', 'on_minimize', 'on_maximize', 'on_restore',
'on_hide', 'on_show', 'on_motion', 'on_touch_down',
'on_touch_move', 'on_touch_up', 'on_mouse_down',
'on_mouse_move', 'on_mouse_up', 'on_keyboard', 'on_key_down',
'on_key_up', 'on_textinput', 'on_dropfile', 'on_request_close',
'on_cursor_enter', 'on_cursor_leave', 'on_joy_axis',
'on_joy_hat', 'on_joy_ball', 'on_joy_button_down',
'on_joy_button_up', 'on_memorywarning', 'on_textedit',
# internal
'on_pre_resize')
def __new__(cls, **kwargs):
if cls.__instance is None:
cls.__instance = EventDispatcher.__new__(cls)
return cls.__instance
def __init__(self, **kwargs):
force = kwargs.pop('force', False)
# don't init window 2 times,
# except if force is specified
if WindowBase.__instance is not None and not force:
return
self.initialized = False
self._is_desktop = Config.getboolean('kivy', 'desktop')
# create a trigger for update/create the window when one of window
# property changes
self.trigger_create_window = Clock.create_trigger(
self.create_window, -1)
# Create a trigger for updating the keyboard height
self.trigger_keyboard_height = Clock.create_trigger(
self._upd_kbd_height, .5)
self.bind(_kheight=lambda *args: self.update_viewport())
# set the default window parameter according to the configuration
if 'borderless' not in kwargs:
kwargs['borderless'] = Config.getboolean('graphics', 'borderless')
if 'fullscreen' not in kwargs:
fullscreen = Config.get('graphics', 'fullscreen')
if fullscreen not in ('auto', 'fake'):
fullscreen = fullscreen.lower() in ('true', '1', 'yes')
kwargs['fullscreen'] = fullscreen
if 'width' not in kwargs:
kwargs['width'] = Config.getint('graphics', 'width')
if 'height' not in kwargs:
kwargs['height'] = Config.getint('graphics', 'height')
if 'minimum_width' not in kwargs:
kwargs['minimum_width'] = Config.getint('graphics',
'minimum_width')
if 'minimum_height' not in kwargs:
kwargs['minimum_height'] = Config.getint('graphics',
'minimum_height')
if 'allow_screensaver' not in kwargs:
kwargs['allow_screensaver'] = Config.getboolean(
'graphics', 'allow_screensaver')
if 'rotation' not in kwargs:
kwargs['rotation'] = Config.getint('graphics', 'rotation')
if 'position' not in kwargs:
kwargs['position'] = Config.getdefault('graphics', 'position',
'auto')
if 'top' in kwargs:
kwargs['position'] = 'custom'
self._top = kwargs['top']
else:
self._top = Config.getint('graphics', 'top')
if 'left' in kwargs:
kwargs['position'] = 'custom'
self._left = kwargs['left']
else:
self._left = Config.getint('graphics', 'left')
kwargs['_size'] = (kwargs.pop('width'), kwargs.pop('height'))
if 'show_cursor' not in kwargs:
kwargs['show_cursor'] = Config.getboolean(
'graphics', 'show_cursor'
)
if 'shape_image' not in kwargs:
kwargs['shape_image'] = Config.get('kivy', 'window_shape')
super(WindowBase, self).__init__(**kwargs)
# bind all the properties that need to recreate the window
self._bind_create_window()
self.bind(size=self.trigger_keyboard_height,
rotation=self.trigger_keyboard_height)
self.bind(softinput_mode=lambda *dt: self.update_viewport(),
keyboard_height=lambda *dt: self.update_viewport())
self.bind(show_cursor=lambda *dt: self._set_cursor_state(dt[1]))
# init privates
self._system_keyboard = Keyboard(window=self)
self._keyboards = {'system': self._system_keyboard}
self._vkeyboard_cls = None
self.children = []
self.parent = self
# before creating the window
import kivy.core.gl # NOQA
# configure the window
self.create_window()
self.register()
# manage keyboard(s)
self.configure_keyboards()
# assign the default context of the widget creation
if not hasattr(self, '_context'):
self._context = get_current_context()
# because Window is created as soon as imported, if we bound earlier,
# metrics would be imported when dp is set during window creation.
# Instead, don't process dpi changes until everything is set