use crate::internal::RawWrapper;
use crate::sys;
use std::cell::UnsafeCell;
#[repr(transparent)]
#[derive(Debug)]
pub struct Style(pub(crate) UnsafeCell<sys::ImGuiStyle>);
const _: [(); std::mem::size_of::<sys::ImGuiStyle>()] = [(); std::mem::size_of::<Style>()];
const _: [(); std::mem::align_of::<sys::ImGuiStyle>()] = [(); std::mem::align_of::<Style>()];
impl Style {
#[doc(alias = "ScaleAllSizes")]
pub fn scale_all_sizes(&mut self, scale_factor: f32) {
assert!(
scale_factor.is_finite() && scale_factor > 0.0,
"Style::scale_all_sizes() scale_factor must be finite and positive"
);
unsafe {
sys::ImGuiStyle_ScaleAllSizes(self.inner_mut(), scale_factor);
}
}
#[inline]
pub(super) fn inner(&self) -> &sys::ImGuiStyle {
unsafe { &*self.0.get() }
}
#[inline]
pub(super) fn inner_mut(&mut self) -> &mut sys::ImGuiStyle {
unsafe { &mut *self.0.get() }
}
}
impl Clone for Style {
fn clone(&self) -> Self {
Self(UnsafeCell::new(*self.inner()))
}
}
impl PartialEq for Style {
fn eq(&self, other: &Self) -> bool {
*self.inner() == *other.inner()
}
}
impl RawWrapper for Style {
type Raw = sys::ImGuiStyle;
unsafe fn raw(&self) -> &Self::Raw {
self.inner()
}
unsafe fn raw_mut(&mut self) -> &mut Self::Raw {
self.inner_mut()
}
}
#[cfg(test)]
mod tests {
#[test]
fn scale_all_sizes_uses_upstream_rounding_and_rejects_invalid_factors() {
let mut ctx = crate::Context::create();
let style = ctx.style_mut();
style.set_window_padding([3.0, 5.0]);
style.scale_all_sizes(2.0);
assert_eq!(style.window_padding(), [6.0, 10.0]);
for scale_factor in [0.0, -1.0, f32::NAN, f32::INFINITY] {
assert!(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
style.scale_all_sizes(scale_factor);
}))
.is_err()
);
}
}
}