-
Notifications
You must be signed in to change notification settings - Fork 380
Add Style accessor functions to Ui #526
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
Don't mind this @lwiklendt, but is there a reason we shouldn't just expose an |
I don't understand myself why originally |
Okay, I have some answers for you on why it's set up the way it is -- effectively, we violate memory safety if we give an I created this simple program: let style = ui.style();
let _t = ui.push_style_var(imgui::StyleVar::Alpha(0.2));
let new_style = ui.style();
println!(
"equals, {}\n{:?}\n{:?}",
style == new_style,
style,
new_style
);
// in ui...
pub fn style(&self) -> &crate::Style {
self.ctx.style()
} My expectation was that this would yield I think we have three options:
What do you think? |
I like 2, but I suspect it could require massive changes to peoples codebase to use. 3 and 4 might be the best option. Unfortunately, IIRC the style can be a number of different types, which... complicates things. |
In the spirit of 3, there could be a Although not all |
I think that pretty much kills option 3.
I suspect this is all correct -- I mostly pass around I think a larger conversation about mutability in the codebase will have to wait, but for now, I'm in favor of accepting the PR as is with the addition of 4 (we will likely forget to update these functions as the style struct changes at some point, which we should endeavor to not do, but having the emergency escape of 4 will be useful). @lwiklendt would you mind adding that option 4? should return a |
A better option might be an |
imgui/src/utils.rs
Outdated
@@ -173,4 +173,15 @@ impl<'ui> Ui<'ui> { | |||
pub fn style_color(&self, style_color: StyleColor) -> [f32; 4] { | |||
self.ctx.style()[style_color] | |||
} | |||
/// Returns a shared reference to the current [`Style`]. This function is tagged as `unsafe` |
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 should note that users who would like to avoid unsafe can use clone_style()
.
…ssors # Conflicts: # imgui/src/utils.rs
imgui/src/utils.rs
Outdated
/// [`ColorStackToken::pop`](crate::ColorStackToken::pop) or | ||
/// [`StyleStackToken::pop`](crate::StyleStackToken::pop) will modify the values in the returned | ||
/// shared reference. Therefore, you should not retain this reference across calls to push and | ||
/// pop. The [`clone_style`](Ui::clone_style) version may instead be used to avoid `unsafe`. |
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.
apologies for the specifics, but can you format this as:
Returns a shared reference to the current [`Style`].
## Safety
This function is tagged as `unsafe`...etc
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.
No worries, it's good to keep a clean uniform style.
left one comment, once that's resolved, good to merge. Thank you and apologies that you ran into hotel order issue of mutability of the |
Thank you! |
Proposed solution to issue #525