Repository navigation
|
Hi, Firstly, thanks for the awesome python-binding. I'm just struggling heavily getting things to work, but mostly because of the immediate model paradigm. This is my current code: from imgui_bundle import imgui, immapp, hello_imgui
from imgui_bundle import ImVec2
class Menu:
def __init__(self) -> None:
self.themes = [
"imgui_colors_classic",
"imgui_colors_dark",
"imgui_colors_light",
]
self.selected_theme = 0
def draw_menu(self):
self.style = imgui.get_style()
if imgui.begin_menu("Launch"):
imgui.separator_text("Tools")
if imgui.menu_item("App A", "CTRL+A", False)[0]:
pass
if imgui.menu_item("App B", "", False)[0]:
pass
if imgui.menu_item("App C", "", False)[0]:
pass
imgui.end_menu()
if imgui.begin_menu("Settings"):
if imgui.begin_menu("Scaling"):
changed, new_value = imgui.slider_float("FontScaleDpi", self.style.font_scale_dpi, 0.5, 3.0, "%.1f")
if changed:
self.style.font_scale_dpi = new_value
imgui.end_menu()
if imgui.begin_menu("Theme"):
changed, self.selected_theme = imgui.combo("Select Theme", self.selected_theme, self.themes)
if changed:
hello_imgui.apply_theme(hello_imgui.ImGuiTheme_(self.selected_theme))
imgui.end_menu()
imgui.end_menu()
# if imgui.begin_menu("Help"):
# # Doesn't work
# if imgui.menu_item("Test", "", False)[0]:
# imgui.open_popup("testing")
# imgui.end_menu()
def main() -> None:
class App:
def __init__(self) -> None:
self.menu = Menu()
def mainloop(self):
if imgui.begin_main_menu_bar():
self.menu.draw_menu()
if imgui.begin_menu("Help"):
# Doesn't work
if imgui.menu_item("Test", "", False)[0]:
imgui.open_popup("testing")
imgui.end_menu()
imgui.end_main_menu_bar()
# Works
if imgui.button("Make Popup"):
imgui.open_popup("testing")
center = imgui.get_main_viewport().get_center()
imgui.set_next_window_pos(center, imgui.Cond_.appearing, ImVec2(0.5, 0.5))
if imgui.begin_popup_modal("testing", None, imgui.WindowFlags_.always_auto_resize)[0]:
imgui.text("All those beautiful files will be deleted.\nThis operation cannot be undone!")
imgui.separator()
if imgui.button("OK", ImVec2(120, 0)):
imgui.close_current_popup()
imgui.set_item_default_focus()
imgui.same_line()
if imgui.button("Cancel", ImVec2(120, 0)):
imgui.close_current_popup()
imgui.end_popup()
app = App()
immapp.run(gui_function=app.mainloop)
if __name__ == "__main__":
main()I have kind of two issues:
There are also many different way of handling the menu_bar.
I am uncertain as to which approach is the most suitable or which serves a specific purpose. Edit: Also the changing the |
Replies: 3 comments 1 reply
|
Hi, By default, the background window does not provide a menu bar. app = App()
immapp.run(gui_function=app.mainloop)By the longer version where you specify full runner params: app = App()
#immapp.run(gui_function=app.mainloop)
runner_params = hello_imgui.RunnerParams()
runner_params.callbacks.show_gui = app.mainloop
runner_params.imgui_window_params.show_menu_bar = True # Show a menu bar
runner_params.imgui_window_params.show_menu_app = False # But remove the default menu provided by hello_imgui
runner_params.imgui_window_params.show_menu_view = False
immapp.run(runner_params) |
Yes, with your slider the user is interacting with a slider whose size and position varies depending on the scale it sets! Solution: place it at a know location + know size. For example this would work (but is not very user friendly): if imgui.begin_menu("Launch"):
# place the slider in the left menu (known position), not in a submenu (position would vary), with a fixed size
imgui.set_next_item_width(100)
changed, new_value = imgui.slider_float("FontScaleDpi", self.style.font_scale_dpi, 0.5, 3.0, "%.1f")
if changed:
self.style.font_scale_dpi = new_value |
You are hitting a small gotcha with popups in ImGui: "Popup identifiers are relative to the current ID stack, so OpenPopup and BeginPopup generally needs to be at the same level of the stack.". It will get easier after these initial bumps, I'm certain of it :-)
Then With the button, both open_popup and begin_popup_modal run in the same scope, so the IDs match and it works. Two standard fixes:
open_test_popup = False
if imgui.begin_main_menu_bar():
...
if imgui.begin_menu("Help"):
if imgui.menu_item("Test", "", False)[0]:
open_test_popup = True
imgui.end_menu()
imgui.end_main_menu_bar()
if open_test_popup:
imgui.open_popup("testing")
|
You are hitting a small gotcha with popups in ImGui: "Popup identifiers are relative to the current ID stack, so OpenPopup and BeginPopup generally needs to be at the same level of the stack.".
It will get easier after these initial bumps, I'm certain of it :-)
open_popup("testing")keys the popup byGetID("testing")computed against the current ID stack.begin_menu("Help")pushes its own ID onto that stack, so the popup opened is registered underID = hash("Help" + "testing").Then
begin_popup_modal("testing", ...)runs at the top‑level scope (the menu bar/menu has already ended), so it looks for ID = hash("tes…