from typing import Callable import dearpygui.dearpygui as dpg class Dialogs: """Show some dialogs""" def __init__(self): self._loading_window = None self._loading_title = None self._loading_message = None self._alert_window = None self._alert_title = None self._alert_message = None self._alert_dismiss = None self._alert_confirm = None def create_dialogs(self): with dpg.window( modal=True, show=False, autosize=True, no_title_bar=True, no_resize=True, no_move=True, ) as loading_window: self._loading_window = loading_window with dpg.group(horizontal=True): dpg.add_loading_indicator() with dpg.group(): self._loading_title = dpg.add_text('title') self._loading_message = dpg.add_text('message') with dpg.window( modal=True, show=False, autosize=True, no_title_bar=True, no_resize=True, no_move=True, ) as alert_window: self._alert_window = alert_window with dpg.group(horizontal=True): dpg.add_loading_indicator( circle_count=3, color=(240, 140, 0, 255), secondary_color=(180, 100, 0, 255) ) with dpg.group(): self._alert_title = dpg.add_text('title') self._alert_message = dpg.add_text('message') dpg.add_spacer(height=10) with dpg.group(horizontal=True): self._alert_dismiss = dpg.add_button(label='Cancel') self._alert_confirm = dpg.add_button(label='OK') def show_loading(self, title='', message=''): dpg.set_value(self._loading_title, title) dpg.set_value(self._loading_message, message) dpg.configure_item(self._loading_window, show=True) dpg.split_frame() self._center_dialog_window(self._loading_window) def hide_loading(self): dpg.configure_item(self._loading_window, show=False) def show_alert( self, title='', message='', confirm='OK', dismiss='Cancel', on_confirm: Callable = None, on_dismiss: Callable = None ): dpg.set_value(self._alert_title, title) dpg.set_value(self._alert_message, message) dpg.configure_item(self._alert_confirm, label=confirm, callback=on_confirm, show=on_confirm is not None) dpg.configure_item(self._alert_dismiss, label=dismiss, callback=on_dismiss, show=on_dismiss is not None) dpg.configure_item(self._alert_window, show=True) dpg.split_frame() self._center_dialog_window(self._alert_window) def hide_alert(self): dpg.configure_item(self._alert_window, show=False) @staticmethod def _center_dialog_window(dialog): if dpg.does_item_exist(dialog): main_width = dpg.get_viewport_client_width() main_height = dpg.get_viewport_client_height() dialog_width = dpg.get_item_width(dialog) dialog_height = dpg.get_item_height(dialog) dpg.set_item_pos(dialog, [(main_width - dialog_width) / 2, (main_height - dialog_height) / 3])