use egui::{vec2, NumExt, Vec2};
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct ViewportSize {
pub(crate) width: Option<f32>,
pub(crate) height: Option<f32>,
pub(crate) view_aspect: Option<f32>,
pub(crate) min_size: Vec2,
}
impl ViewportSize {
#[inline]
#[must_use]
pub(crate) fn view_aspect(mut self, view_aspect: f32) -> Self {
self.view_aspect = Some(view_aspect);
self
}
#[inline]
#[must_use]
pub(crate) fn width(mut self, width: f32) -> Self {
self.min_size.x = width;
self.width = Some(width);
self
}
#[inline]
#[must_use]
pub(crate) fn height(mut self, height: f32) -> Self {
self.min_size.y = height;
self.height = Some(height);
self
}
pub(crate) fn compute(self, ui: &egui::Ui) -> Vec2 {
let Self {
width,
height,
view_aspect,
min_size,
} = self;
let width = width
.unwrap_or_else(|| {
if let (Some(height), Some(aspect)) = (height, view_aspect) {
height * aspect
} else {
ui.available_size_before_wrap().x
}
})
.at_least(min_size.x);
let height = height
.unwrap_or_else(|| {
if let Some(aspect) = view_aspect {
width / aspect
} else {
ui.available_size_before_wrap().y
}
})
.at_least(min_size.y);
vec2(width, height)
}
}