diff --git a/src/imgui_scopes.py b/src/imgui_scopes.py new file mode 100644 index 0000000..cd6c626 --- /dev/null +++ b/src/imgui_scopes.py @@ -0,0 +1,78 @@ +from __future__ import annotations +import imgui_bundle + + +class ImGuiScope: + _entered: bool + _opened: bool | tuple + _begin_fn: object + _end_fn: object + _args: tuple[object, ...] + _kwargs: dict[str, object] + + def __init__(self, begin_fn: object, end_fn: object, *args: object, **kwargs: object) -> None: + self._begin_fn = begin_fn + self._end_fn = end_fn + self._args = args + self._kwargs = kwargs + self._opened = False + self._entered = False + + def __enter__(self) -> bool | tuple: + result = self._begin_fn(*self._args, **self._kwargs) + if isinstance(result, tuple): + self._opened = result[0] + else: + self._opened = result + self._entered = bool(self._opened) + return self._opened + + def __exit__(self, *args: object) -> bool: + if self._entered: + self._end_fn() + return False + + +def imgui_window(name: str, visible: bool = True, flags: int = 0) -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin, imgui_bundle.imgui.end, name, visible, flags) + + +def imgui_table(name: str, columns: int, flags: int = 0) -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin_table, imgui_bundle.imgui.end_table, name, columns, flags) + + +def imgui_menu_bar() -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin_menu_bar, imgui_bundle.imgui.end_menu_bar) + + +def imgui_menu(label: str) -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin_menu, imgui_bundle.imgui.end_menu, label) + + +def imgui_child(id_str: str, width: float = 0, height: float = 0, flags: int = 0) -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin_child, imgui_bundle.imgui.end_child, id_str, width, height, flags) + + +def imgui_group() -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin_group, imgui_bundle.imgui.end_group) + + +def imgui_popup(id_str: str) -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin_popup, imgui_bundle.imgui.end_popup, id_str) + + +def imgui_tooltip() -> ImGuiScope: + return ImGuiScope(imgui_bundle.imgui.begin_tooltip, imgui_bundle.imgui.end_tooltip) + + +def imgui_clipper(count: int) -> ImGuiScope: + return ImGuiScope( + lambda n: imgui_bundle.imgui.listing_builder.begin_clipper(n, -1), + imgui_bundle.imgui.listing_builder.end_clipper, + count + ) + + +def node_editor_scope(name: str) -> ImGuiScope: + ed = imgui_bundle.imgui_node_editor + return ImGuiScope(ed.begin, ed.end, name) \ No newline at end of file