55
66def dialog_selection_callback (sender , unused , user_data ) -> bool :
77 if user_data [1 ]:
8- logger .info ( " User chose OK" )
8+ logger .debug ( "[magenta] User chose OK" )
99 dpg .delete_item (user_data [0 ])
1010 return True
1111 else :
12- logger .info ( " User chose Cancel" )
12+ logger .debug ( "[magenta] User chose Cancel" )
1313 dpg .delete_item (user_data [0 ])
1414 return False
1515
16- def prompt (message :str , title :str = "Dialog" , show_ok :bool = True , show_cancel :bool = False , wrap :int = 400 , ** kwargs ) -> bool | None :
16+ def prompt (message :str , title :str = "Dialog" , show_ok :bool = True , show_cancel :bool = False , width : int | None = None , height : int | None = None , wrap :int = 400 , ** kwargs ) -> bool | None :
1717 if dpg .does_item_exist ("dialog" ):
1818 dpg .delete_item ("dialog" )
19+
20+ viewport_width = dpg .get_viewport_client_width ()
21+ viewport_height = dpg .get_viewport_client_height ()
1922
20- with dpg .mutex ():
2123
22- viewport_width = dpg .get_viewport_client_width ()
23- viewport_height = dpg .get_viewport_client_height ()
24+ # Calculate height
25+ if not height :
26+ height = 0
27+ char_height_factor = 15
28+ extra_for_buttons = 100
2429
25- with dpg .window (label = title , modal = True , no_title_bar = True , tag = "dialog" , ** kwargs ):
26- dpg .add_text (message , wrap = wrap )
27- dpg .add_separator ()
28- with dpg .group (horizontal = True , tag = "dialog_buttons" ):
29-
30- if show_ok :
31- dpg .add_button (label = "Ok" , user_data = ("dialog" , True ), callback = dialog_selection_callback )
32- if show_cancel :
33- dpg .add_button (label = "Cancel" , user_data = ("dialog" , False ), callback = dialog_selection_callback )
34-
35- dpg .split_frame ()
30+ explicit_lines = message .split ("\n " )
31+ height += (len (explicit_lines ) * char_height_factor )
32+ for line in explicit_lines :
33+ if len (line ) > wrap :
34+ height += char_height_factor
35+
36+ height += extra_for_buttons
37+
38+ if not width :
39+ width = wrap
3640
37- # CENTER DIALOG
38- width = dpg .get_item_width ("dialog" )
39- height = dpg .get_item_height ("dialog" )
40- if width and height :
41- dpg .set_item_pos ("dialog" , [viewport_width // 2 - width // 2 , viewport_height // 2 - height // 2 ])
42- dpg .configure_item ("dialog" , show = True )
41+ with dpg .window (label = title , modal = True , no_title_bar = True , tag = "dialog" , width = width , height = height , pos = [viewport_width // 2 - wrap // 2 , viewport_height // 2 - height // 2 ], ** kwargs ):
42+ dpg .add_text (message , wrap = wrap )
43+ dpg .add_separator ()
44+ with dpg .group (horizontal = True , tag = "dialog_buttons" ):
45+
46+ if show_ok :
47+ dpg .add_button (label = "Ok" , user_data = ("dialog" , True ), callback = dialog_selection_callback )
48+ if show_cancel :
49+ dpg .add_button (label = "Cancel" , user_data = ("dialog" , False ), callback = dialog_selection_callback )
50+
4351
0 commit comments