hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en/latest/
MIT License
13.15k stars 685 forks source link

Callable when closing a tab #2140

Open wlwatkins opened 1 year ago

wlwatkins commented 1 year ago

Is your feature request related to a problem? Please describe. When closing a tab, a callback should be available in order to delete model data

Describe the solution you'd like Currently i have to close the tab through another button that has a callback however, this is not UX friendly

Describe alternatives you've considered There is a closable argument in tabs which should make a callable available

Additional context This issue has already been opened and flagged as pending tough the issue has been closed. https://github.com/hoffstadt/DearPyGui/issues/1758

KyleTheScientist commented 2 months ago

I've also been struggling to find a way to do this. The closest I've been able to get is to add a callback to the tab bar:

from dearpygui import dearpygui as dpg

def on_tab_changed():
    active_tab = dpg.get_value('tab_bar')
    print('Active tab:', active_tab)

    for tab in dpg.get_item_children('tab_bar')[1]:
        if not dpg.is_item_visible(tab):
            print('This tab was closed:', tab)
            dpg.delete_item(tab)

dpg.create_context()
dpg.create_viewport()
with dpg.window():
    with dpg.tab_bar(callback=on_tab_changed, tag='tab_bar'):
        dpg.add_tab(label='Tab 1', closable=True)
        dpg.add_tab(label='Tab 2', closable=True)
        dpg.add_tab(label='Tab 3', closable=True)

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

But there is no callback when the last tab is closed, and no callback if you close a tab that isn't focused. I'm unsure how to deal with this. Currently I just have one empty, uncloseable tab to make sure there is always at least one tab, which deals with the first scenario.

The order of the tabs is also not updated in the list provided by get_item_children(), which makes it difficult to implement something like a ctrl+tab shortcut to switch between the tabs in the order they are displayed.

I think the best solution to this would be if we could get a dpg.add_item_children_reordered_handler() for the tab bar and either a dpg.add_item_closed_handler() for the tabs themselves or a callback argument for the tabs that is triggered by the tabs entering focus and being closed.