Skip to content

Instantly share code, notes, and snippets.

@lubieowoce
Created December 14, 2018 04:47
Show Gist options
  • Save lubieowoce/3e215bac79d6b8dfcda8903633d9e7dd to your computer and use it in GitHub Desktop.
Save lubieowoce/3e215bac79d6b8dfcda8903633d9e7dd to your computer and use it in GitHub Desktop.
pyimgui window constraints -- problems with python callbacks in C code
# pyimgui version:
# https://github.com/lubieowoce/pyimgui/commit/62bd989bf3621df125f68d0e07423668a68bf9c9
def limit_aspect_ratio_2(position, current_size, desired_size) -> 'Tuple[float, float]':
print('limit_aspect_ratio_2(_, current_size={}, desired_size={})'.format(current_size, desired_size))
w, h = desired_size
return (h*2, h)
def draw():
# Different ways of passing a callback to `imgui.unsafe_set_next_window_size_constraints(...)`
# 1. ok
imgui.unsafe_set_next_window_size_constraints(
min_width=10, min_height=10,
max_width=500, max_height=500,
callback = limit_aspect_ratio_2,
)
# # 2. CRASH - should be equivalent to 1, but crashes -
# # the lambda is garbage collected before `imgui.begin(...)`
# imgui.unsafe_set_next_window_size_constraints(
# min_width=10, min_height=10,
# max_width=500, max_height=500,
# callback = lambda pos, cs, ds: limit_aspect_ratio_2(pos, cs, ds),
# )
# # 3a. ok - we keep a reference to the lambda, so it doesn't get collected
# cb = lambda pos, cs, ds: limit_aspect_ratio_2(pos, cs, ds)
# imgui.unsafe_set_next_window_size_constraints(
# min_width=10, min_height=10,
# max_width=500, max_height=500,
# callback = cb,
# )
# # 3b. ok - we keep a reference to the lambda, so it doesn't get collected
# # (`unsafe_set_next_window_size_constraints` returns its callback for testing purposes)
# callback_keepalive = imgui.unsafe_set_next_window_size_constraints(
# min_width=10, min_height=10,
# max_width=500, max_height=500,
# callback = lambda pos, cs, ds: limit_aspect_ratio_2(pos, cs, ds),
# )
print('begin drawing `constrained`')
imgui.begin("constrained")
imgui.text("window size: {}".format(imgui.get_window_size()))
imgui.end()
print('end drawing `constrained`\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment