Can't make popups in a table to work #469
Unanswered
gantsevdenis
asked this question in
Q&A
Replies: 2 comments
|
Hi |
0 replies
Minimal Python repro showing both right-click and double-click on any cell of a table, with the popup displaying the (row, column, item_id) it was opened from: from imgui_bundle import imgui, immapp, ImVec2
ROWS = 30
COLS = 8
def make_rows():
rows = []
for r in range(ROWS):
item_id = f"ITEM-{r:03d}"
cells = [f"{(r + 1) * (c + 1) * 1.5:.2f}" for c in range(COLS - 1)]
prices = [[(r + 1) * (i + 1) * (j + 1) * 0.1 for j in range(4)] for i in range(4)]
rows.append((item_id, cells, prices))
return rows
ROW_DATA = make_rows()
def draw_prices_popup(item_id: str, row_idx: int, col_idx: int, prices: list[list[float]]) -> None:
imgui.text(f"Row {row_idx}, Column {col_idx} — {item_id}")
imgui.separator()
if imgui.begin_table("prices", 4, imgui.TableFlags_.borders | imgui.TableFlags_.row_bg):
for i in range(4):
imgui.table_next_row()
for j in range(4):
imgui.table_set_column_index(j)
imgui.text(f"{prices[i][j]:.2f}")
imgui.end_table()
def gui() -> None:
imgui.text("Right-click or double-click any cell to open a popup that knows its (row, column).")
imgui.spacing()
flags = (
imgui.TableFlags_.borders
| imgui.TableFlags_.row_bg
| imgui.TableFlags_.scroll_y
| imgui.TableFlags_.resizable
)
if imgui.begin_table("items", COLS, flags, ImVec2(0, 400)):
imgui.table_setup_column("ItemID")
for c in range(COLS - 1):
imgui.table_setup_column(f"col{c + 1}")
imgui.table_headers_row()
for row_idx, (item_id, cells, prices) in enumerate(ROW_DATA):
imgui.table_next_row()
row_cells = [item_id, *cells]
for col_idx, value in enumerate(row_cells):
imgui.table_set_column_index(col_idx)
# imgui.push_id: use unique ID per cell so popup IDs don't collide across rows/cols.
# (You may also do this only per row, if you don't need to distinguish between columns)
imgui.push_id(row_idx * COLS + col_idx)
# Selectable gives the cell a hit-testable item for both
# BeginPopupContextItem (right-click) and IsMouseDoubleClicked.
imgui.selectable(value, False)
# Right-click context menu, bound to the Selectable above.
if imgui.begin_popup_context_item("ctx"):
draw_prices_popup(item_id, row_idx, col_idx, prices)
imgui.end_popup()
# Double-click: OpenPopup + BeginPopup share the same ID scope
# (we're inside PushID), so the open request reaches the popup.
if imgui.is_item_hovered() and imgui.is_mouse_double_clicked(0):
imgui.open_popup("dbl")
if imgui.begin_popup("dbl"):
draw_prices_popup(item_id, row_idx, col_idx, prices)
imgui.end_popup()
imgui.pop_id()
imgui.end_table()
def main() -> None:
immapp.run(gui_function=gui, window_title="disc #469 — table popups", window_size=(900, 500))
if __name__ == "__main__":
main() |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
What i have:
What i want in the end:
Difficulties I have:
Sorry for no code, it is running on the corporate laptop :/
All reactions