1
- __all__ = ('ask_yes_no_question' , )
1
+ __all__ = ('show_messagebox' , ' ask_yes_no_question' , )
2
2
3
3
from typing import Unpack
4
4
from contextlib import asynccontextmanager
@@ -35,6 +35,45 @@ async def translate_rects_vertically(clock: Clock, rects, movement, duration):
35
35
rect .y = org_y + v
36
36
37
37
38
+ async def show_messagebox (
39
+ message , * , dialog_size : Rect = None , font = None , text_ok = 'OK' ,
40
+ priority , ** kwargs : Unpack [CommonParams ]) -> bool :
41
+ '''
42
+ .. code-block::
43
+
44
+ await show_messagebox("Hello World", priority=0xFFFFFA00, **kwargs)
45
+ '''
46
+ bgcolor = "grey90"
47
+ clock = kwargs ["clock" ]
48
+ draw_target = kwargs ["draw_target" ]
49
+ if font is None :
50
+ font = SysFont (None , 40 )
51
+
52
+ with block_input_events (kwargs ["sdlevent" ], priority ):
53
+ async with darken (priority = priority , ** kwargs ), asyncgui .open_nursery () as nursery :
54
+ target_rect = draw_target .get_rect ()
55
+ if dialog_size is None :
56
+ dialog_size = target_rect .inflate (- 100 , 0 )
57
+ dialog_size .height = dialog_size .width // 2
58
+ dialog_dest = dialog_size .move_to (bottom = target_rect .top )
59
+ with kwargs ["executor" ].register (partial (draw_target .fill , bgcolor , dialog_dest ), priority = priority + 1 ):
60
+ label = AnchorLayout (
61
+ nursery ,
62
+ font .render (message , True , "black" , bgcolor ).convert (draw_target ),
63
+ dialog_dest .scale_by (1.0 , 0.7 ).move_to (top = dialog_dest .top ).inflate (- 10 , - 10 ),
64
+ priority = priority + 2 , ** kwargs )
65
+ ok_button = RippleButton (
66
+ nursery ,
67
+ font .render (text_ok , True , "white" ),
68
+ dialog_dest .scale_by (0.5 , 0.3 ).move_to (midbottom = dialog_dest .midbottom ).inflate (- 20 , - 20 ),
69
+ priority = priority + 2 , ** kwargs )
70
+ rects = (dialog_dest , label .dest , ok_button .dest , )
71
+ y_movement = target_rect .centery - dialog_dest .centery
72
+ await translate_rects_vertically (clock , rects , y_movement , duration = 200 )
73
+ await ok_button .to_be_clicked ()
74
+ await translate_rects_vertically (clock , rects , - y_movement , duration = 200 )
75
+
76
+
38
77
async def ask_yes_no_question (
39
78
question , * , dialog_size : Rect = None , font = None , text_yes = 'Yes' , text_no = 'No' ,
40
79
priority , ** kwargs : Unpack [CommonParams ]) -> bool :
@@ -44,21 +83,19 @@ async def ask_yes_no_question(
44
83
result = await ask_yes_no_question("Do you like PyGame?", priority=0xFFFFFA00, **kwargs)
45
84
'''
46
85
bgcolor = "grey90"
47
- executor = kwargs ["executor" ]
48
- sdlevent = kwargs ["sdlevent" ]
49
86
clock = kwargs ["clock" ]
50
87
draw_target = kwargs ["draw_target" ]
51
88
if font is None :
52
89
font = SysFont (None , 40 )
53
90
54
- with block_input_events (sdlevent , priority ):
91
+ with block_input_events (kwargs [ " sdlevent" ] , priority ):
55
92
async with darken (priority = priority , ** kwargs ), asyncgui .open_nursery () as nursery :
56
93
target_rect = draw_target .get_rect ()
57
94
if dialog_size is None :
58
95
dialog_size = target_rect .inflate (- 100 , 0 )
59
96
dialog_size .height = dialog_size .width // 2
60
97
dialog_dest = dialog_size .move_to (bottom = target_rect .top )
61
- with executor .register (partial (draw_target .fill , bgcolor , dialog_dest ), priority = priority + 1 ):
98
+ with kwargs [ " executor" ] .register (partial (draw_target .fill , bgcolor , dialog_dest ), priority = priority + 1 ):
62
99
label = AnchorLayout (
63
100
nursery ,
64
101
font .render (question , True , "black" , bgcolor ).convert (draw_target ),
0 commit comments