Skip to content

Commit c68dc01

Browse files
committed
allow floating windows, add basic debug window
1 parent 29b098d commit c68dc01

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

fastplotlib/layouts/_imgui_figure.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import pygfx
1313

1414
from ._figure import Figure
15-
from ..ui import EdgeWindow, SubplotToolbar, StandardRightClickMenu, Popup, GUI_EDGES
15+
from ..ui import Window, EdgeWindow, SubplotToolbar, StandardRightClickMenu, Popup, GUI_EDGES
1616
from ..ui import ColormapPicker
1717

1818

@@ -45,7 +45,7 @@ def __init__(
4545
size: tuple[int, int] = (500, 300),
4646
names: list | np.ndarray = None,
4747
):
48-
self._guis: dict[str, EdgeWindow] = {k: None for k in GUI_EDGES}
48+
self._guis: dict[str, Window] = {k: None for k in GUI_EDGES}
4949

5050
super().__init__(
5151
shape=shape,
@@ -101,7 +101,7 @@ def __init__(
101101
self.register_popup(ColormapPicker)
102102

103103
@property
104-
def guis(self) -> dict[str, EdgeWindow]:
104+
def guis(self) -> dict[str, Window]:
105105
"""GUI windows added to the Figure"""
106106
return self._guis
107107

@@ -148,30 +148,34 @@ def _draw_imgui(self) -> imgui.ImDrawData:
148148

149149
return imgui.get_draw_data()
150150

151-
def add_gui(self, gui: EdgeWindow):
151+
def add_gui(self, gui: Window):
152152
"""
153153
Add a GUI to the Figure. GUIs can be added to the top, bottom, left or right edge.
154154
155155
Parameters
156156
----------
157-
gui: EdgeWindow
158-
A GUI EdgeWindow instance
157+
gui: Window
158+
A GUI Window instance
159159
160160
"""
161-
if not isinstance(gui, EdgeWindow):
161+
if not isinstance(gui, Window):
162162
raise TypeError(
163-
f"GUI must be of type: {EdgeWindow} you have passed a {type(gui)}"
163+
f"GUI must be of type: {Window} you have passed a {type(gui)}"
164164
)
165165

166-
location = gui.location
166+
if isinstance(gui, EdgeWindow):
167+
location = gui.location
167168

168-
if location not in GUI_EDGES:
169-
raise ValueError(
170-
f"GUI does not have a valid location, valid locations are: {GUI_EDGES}, you have passed: {location}"
171-
)
169+
if location not in GUI_EDGES:
170+
raise ValueError(
171+
f"GUI does not have a valid location, valid locations are: {GUI_EDGES}, you have passed: {location}"
172+
)
172173

173-
if self.guis[location] is not None:
174-
raise ValueError(f"GUI already exists in the desired location: {location}")
174+
if self.guis[location] is not None:
175+
raise ValueError(f"GUI already exists in the desired location: {location}")
176+
else:
177+
# just use mem address for a location key
178+
location = hex(id(gui))
175179

176180
self.guis[location] = gui
177181

fastplotlib/ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from ._base import BaseGUI, Window, EdgeWindow, Popup, GUI_EDGES
22
from ._subplot_toolbar import SubplotToolbar
33
from .right_click_menus import StandardRightClickMenu, ColormapPicker
4+
from ._debug import DebugWindow

fastplotlib/ui/_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ def update(self):
3131

3232
class Window(BaseGUI):
3333
"""Base class for imgui windows drawn within Figures"""
34+
def draw_window(self):
35+
"""
36+
Must be implemented in subclass
3437
35-
pass
38+
This must include the imgui.begin() and imgui.end() calls that define an imgui window.
39+
"""
40+
raise NotImplementedError
3641

3742

3843
class EdgeWindow(Window):

fastplotlib/ui/_debug.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from imgui_bundle import imgui
2+
from icecream import ic
3+
4+
from ._base import Window
5+
6+
7+
class DebugWindow(Window):
8+
def __init__(self, objs: list):
9+
self._objs = objs
10+
super().__init__()
11+
12+
@property
13+
def objs(self) -> tuple:
14+
return tuple(self._objs)
15+
16+
def add(self, obj):
17+
self._objs.append(obj)
18+
19+
def draw_window(self):
20+
imgui.set_next_window_pos((300, 0), imgui.Cond_.appearing)
21+
imgui.set_next_window_pos((0, 0), imgui.Cond_.appearing)
22+
23+
imgui.begin("Debug", None)
24+
25+
info = list()
26+
27+
for obj in self.objs:
28+
if callable(obj):
29+
info.append(ic.format(obj()))
30+
else:
31+
info.append(ic.format(obj))
32+
33+
imgui.text_wrapped("\n\n".join(info))
34+
35+
imgui.end()

0 commit comments

Comments
 (0)