Skip to content

Commit 686ad69

Browse files
add '_uix.modal_dialog.show_messagebox()'
1 parent a446057 commit 686ad69

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

examples/_uix/modal_dialog.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__all__ = ('ask_yes_no_question', )
1+
__all__ = ('show_messagebox', 'ask_yes_no_question', )
22

33
from typing import Unpack
44
from contextlib import asynccontextmanager
@@ -35,6 +35,45 @@ async def translate_rects_vertically(clock: Clock, rects, movement, duration):
3535
rect.y = org_y + v
3636

3737

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+
3877
async def ask_yes_no_question(
3978
question, *, dialog_size: Rect=None, font=None, text_yes='Yes', text_no='No',
4079
priority, **kwargs: Unpack[CommonParams]) -> bool:
@@ -44,21 +83,19 @@ async def ask_yes_no_question(
4483
result = await ask_yes_no_question("Do you like PyGame?", priority=0xFFFFFA00, **kwargs)
4584
'''
4685
bgcolor = "grey90"
47-
executor = kwargs["executor"]
48-
sdlevent = kwargs["sdlevent"]
4986
clock = kwargs["clock"]
5087
draw_target = kwargs["draw_target"]
5188
if font is None:
5289
font = SysFont(None, 40)
5390

54-
with block_input_events(sdlevent, priority):
91+
with block_input_events(kwargs["sdlevent"], priority):
5592
async with darken(priority=priority, **kwargs), asyncgui.open_nursery() as nursery:
5693
target_rect = draw_target.get_rect()
5794
if dialog_size is None:
5895
dialog_size = target_rect.inflate(-100, 0)
5996
dialog_size.height = dialog_size.width // 2
6097
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):
6299
label = AnchorLayout(
63100
nursery,
64101
font.render(question, True, "black", bgcolor).convert(draw_target),

examples/asking_question.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from _uix.touch_indicator import touch_indicator
1010
from _uix.ripple_button import RippleButton
11-
from _uix.modal_dialog import ask_yes_no_question
11+
from _uix.modal_dialog import ask_yes_no_question, show_messagebox
1212

1313

1414
async def main(**kwargs: Unpack[apg.CommonParams]):
@@ -33,7 +33,7 @@ async def main(**kwargs: Unpack[apg.CommonParams]):
3333
while True:
3434
await button.to_be_clicked()
3535
answer = await ask_yes_no_question("Do you like PyGame?", priority=0xFFFFFA00, **kwargs)
36-
print("YES" if answer else "NO")
36+
await show_messagebox(f"You answered '{'Yes' if answer else 'No'}'.", priority=0xFFFFFA00, **kwargs)
3737

3838

3939
if __name__ == "__main__":

0 commit comments

Comments
 (0)