pthom / imgui_bundle

Your fast track to powerful GUIs: Dear ImGui Bundle, an extensive toolkit for Python and C++ with immediate mode efficiency.
https://pthom.github.io/imgui_bundle/
MIT License
703 stars 73 forks source link

Minimal docking example #268

Open neurosock opened 1 month ago

neurosock commented 1 month ago

For fun, and to have a template, I have been trying to write a minimal docking example using hello_imgui and immapp. Here is what I came up with. Can this get even more minimal/shorter (just to show two empty panels)?

minimal_dock

Code:

from imgui_bundle import imgui, immapp, hello_imgui

class AppState:
    var0: str = "Var0"
    var1: str = "Var1"

def gui_left(state: AppState):
    imgui.button('A')

def gui_main(state: AppState):
    imgui.button('B')

def main():

    # Default settings and callbacks, geometry, ini file name, 
    # and allow dragging panels outside main window 
    run_params = hello_imgui.RunnerParams()
    run_params.app_window_params.window_title = "minimal_docking"
    run_params.app_window_params.window_geometry.size = (800, 500)
    run_params.imgui_window_params.enable_viewports = True

    # Tell HelloImGui we want a dock space (strictly required)
    run_params.imgui_window_params.default_imgui_window_type = (
        hello_imgui.DefaultImGuiWindowType.provide_full_screen_dock_space
    )

    # Don't know what this does
    #run_params.docking_params.layout_condition = hello_imgui.DockingLayoutCondition.application_start    

    # Create vertical split (Note that "MainDockSpace" is provided by default)
    run_params.docking_params.docking_splits = [
        hello_imgui.DockingSplit(
            initial_dock_ = "MainDockSpace",
            new_dock_ = "LeftSpace",
            direction_ = imgui.Dir_.left,
            ratio_ = 0.25,
        )
    ]

    # Define the application state
    state = AppState()

    run_params.docking_params.dockable_windows = [
        hello_imgui.DockableWindow(
            label_ = "Main",
            dock_space_name_ = "MainDockSpace",
            gui_function_ = lambda: gui_main(state),
        ),
        hello_imgui.DockableWindow(
            label_ = "Left",
            dock_space_name_ = "LeftSpace",
            gui_function_ = lambda: gui_left(state),
        ),
    ]

    # Run the app
    addons_params = immapp.AddOnsParams()
    #addons_params.with_implot = True
    immapp.run(run_params, addons_params)

if __name__ == "__main__":
    main()
pthom commented 1 month ago

Hello,

Thank you for your post. Your code is quite good, and close to the minimal setup if we want it to be a good template starting point. Removing code would make it less extensible and/or less usable as a starting point.

Now, to address the question in the code: "what does run_params.docking_params.layout_condition stand for?": by default, if you do rearrange the docks when the app is running the modified layout is preserved when restarting the app.

If you want to always restore the default layout upon restarting, uncomment this line:

    run_params.docking_params.layout_condition = hello_imgui.DockingLayoutCondition.application_start  
neurosock commented 1 month ago

Awesome. Thanks!