-
Notifications
You must be signed in to change notification settings - Fork 186
Support scope id stack handling for widgets with the same name #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I've removed the behavior of the context manager where it would create an arbitrary object to be the scope. After running my project against the patch it seems like that dosen't work for all cases. It appears to be important that the scope object is the same between frames for things that have state managed by imgui (e.g. collapsed state of a treenode.) That is, if you have multiple To go with this, I've fixed it so that when you call It's up for debate as far as what we want to offer in the context manager. Since creating an new object for the scope each frame only does what you want some of the time, it seems dangerous to have (at least as a default.) Therefore I removed that and it's almost exactly what was suggested in the issue. Maybe we want some utility class that is a context manager that pushes itself as the scope, thereby allowing you to keep a reference to it across frames? Here is a quick recipe for that: class Scope:
def __enter__(self):
imgui.push_id(self)
def __exit__(self, *args):
imgui.pop_id()
#used like
s=Scope()
#and then each frame in your ui code
with s:
imgui.button('abc') I'm not sure if that's useful enough to merit inclusion in extra (at least, I don't think I'd find myself using that over using the piece of data I am constructing the UI for as my scope.) |
Your observations about scopes are very good so it nice you have captured them in the docs. I will just have to take closer look at this case with int but generally the PR looks very nice. Also I have few comments related to docs and style so if you have spare time you can fix them in the meantime. About the mentioned Thanks for contribution! |
""" | ||
core.push_id(arg) | ||
yield | ||
core.pop_id() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing newline at the end of file
the same object (is-equality in python) unless it is an integer or string. | ||
Consider using the :func:`scope` context manager instead to manage the | ||
push_id and pop_id pair. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add Args:
and .. wraps::
sections as in other functions? These sections are later properly processed with Sphinx for documentation purposes.
The tree_node
function can be a good example for .. wraps::
because it similarly wraps multiple oveloaded calls.
"""Pop an id scope. | ||
Undoes the work of :func:`push_id`. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, .. wraps::
Undoes the work of :func:`push_id`. | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary newline
cimgui.PushID(_bytes(scope_id)) | ||
else: | ||
cimgui.PushID(<void*>scope_id) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have always two newlines between every top-level function/class definition
Consider using the :func:`scope` context manager instead to manage the | ||
push_id and pop_id pair. | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary newline
|
||
@contextmanager | ||
def scope(arg): | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could describe it without mentioning pushes and pops? E.g. "Wrap code block with isolated scope for widget identifiers."
And maybe later just quicly describe that this may be useful if multliple widgets within the same window need to have same ID with some usage example.
Any progress on this? I'd fine it useful. |
I will take another look at this MR next week |
Will this be added soon to the latest release? |
I'm closing this because alternative implementation has been already merged. |
Feature released in 1.2.0 |
This adds functions to expose the PushID and PopID capability in imgui for manipulating the widget id stack. This allows you to elegantly have multiple widgets with the same implicit ids (usually their name.)
It also adds a simple context manager to the
extra
module that automates the pushing and popping of scope ids, and allows use without manually specifying an object to be the scope id (by creating an anonymous object for this purpose.)Not sure what the conclusion was on #33 , but I placed the context manager in the extra module per discussion.
This closes #56 , assuming it is to @swistakm 's liking.