Skip to content
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

Add methods for custom number formatting in DragValue and Slider #1851

Merged
merged 7 commits into from Jul 25, 2022

Conversation

Adanos020
Copy link
Contributor

@Adanos020 Adanos020 commented Jul 24, 2022

Closes #1221.

As the title says, this PR adds a custom_formatter method for both DragValue and Slider. A custom formatter takes a f64 for the numeric value and a RangeInclusive<usize> representing the decimal range.

The following code:

ui.add(egui::Slider::new(&mut self.age, 0..=120)
    .prefix("0x")
    .custom_formatter(|n, _| format!("{:X}", n as i64)));
ui.add(egui::DragValue::new(&mut self.age)
    .custom_formatter(|n, _| format!("{:03}", n as i64)));

produces the following result:

image

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition!

egui/src/widgets/drag_value.rs Show resolved Hide resolved
egui/src/widgets/slider.rs Show resolved Hide resolved
@Adanos020
Copy link
Contributor Author

Applied suggested amendments and took care of the Github Actions' errors.

@Adanos020 Adanos020 requested a review from emilk July 25, 2022 14:52
@emilk
Copy link
Owner

emilk commented Jul 25, 2022

Thanks!

@emilk emilk merged commit 36cdae9 into emilk:master Jul 25, 2022
@Adanos020
Copy link
Contributor Author

No worries, looking forward to the next release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom formatters for Slider/DragValue etc
2 participants