#![allow(clippy::float_cmp)]
use std::{hash::Hash, sync::Arc};
use crate::{color::*, containers::*, layout::*, mutex::MutexGuard, paint::*, widgets::*, *};
pub struct Ui {
id: Id,
painter: Painter,
min_rect: Rect,
max_rect: Rect,
style: Arc<Style>,
layout: Layout,
cursor: Pos2,
child_count: usize,
}
impl Ui {
pub fn new(
ctx: Arc<Context>,
layer_id: LayerId,
id: Id,
max_rect: Rect,
clip_rect: Rect,
) -> Self {
let style = ctx.style();
let layout = Layout::default();
let cursor = layout.initial_cursor(max_rect);
let min_size = Vec2::zero();
let min_rect = layout.rect_from_cursor_size(cursor, min_size);
Ui {
id,
painter: Painter::new(ctx, layer_id, clip_rect),
min_rect,
max_rect,
style,
layout,
cursor,
child_count: 0,
}
}
pub fn child_ui(&mut self, max_rect: Rect, layout: Layout) -> Self {
self.child_count += 1;
let id = self.make_position_id();
let cursor = layout.initial_cursor(max_rect);
let min_size = Vec2::zero();
let min_rect = layout.rect_from_cursor_size(cursor, min_size);
Ui {
id,
painter: self.painter.clone(),
min_rect,
max_rect,
style: self.style.clone(),
layout,
cursor,
child_count: 0,
}
}
pub fn __test() -> Self {
let mut ctx = Context::new();
ctx.begin_frame(Default::default());
let id = Id::new("__test");
let layer_id = LayerId::new(Order::Middle, id);
let rect = Rect::from_min_size(Pos2::new(0.0, 0.0), vec2(1000.0, 1000.0));
Self::new(ctx, layer_id, id, rect, rect)
}
pub fn id(&self) -> Id {
self.id
}
pub fn style(&self) -> &Style {
&self.style
}
pub fn style_mut(&mut self) -> &mut Style {
Arc::make_mut(&mut self.style)
}
pub fn set_style(&mut self, style: impl Into<Arc<Style>>) {
self.style = style.into();
}
pub fn ctx(&self) -> &Arc<Context> {
self.painter.ctx()
}
pub fn painter(&self) -> &Painter {
&self.painter
}
pub fn layout(&self) -> &Layout {
&self.layout
}
pub fn painter_at(&self, rect: Rect) -> Painter {
self.painter().sub_region(rect)
}
pub fn layer_id(&self) -> LayerId {
self.painter().layer_id()
}
pub fn input(&self) -> &InputState {
self.ctx().input()
}
pub fn memory(&self) -> MutexGuard<'_, Memory> {
self.ctx().memory()
}
pub fn output(&self) -> MutexGuard<'_, Output> {
self.ctx().output()
}
pub fn fonts(&self) -> &Fonts {
self.ctx().fonts()
}
pub fn clip_rect(&self) -> Rect {
self.painter.clip_rect()
}
pub fn set_clip_rect(&mut self, clip_rect: Rect) {
self.painter.set_clip_rect(clip_rect);
}
}
impl Ui {
pub fn min_rect(&self) -> Rect {
self.min_rect
}
pub fn min_size(&self) -> Vec2 {
self.min_rect.size()
}
pub fn max_rect(&self) -> Rect {
self.max_rect
}
pub(crate) fn force_set_min_rect(&mut self, min_rect: Rect) {
self.min_rect = min_rect;
}
pub fn max_rect_finite(&self) -> Rect {
let mut result = self.max_rect;
if !result.min.x.is_finite() {
result.min.x = self.min_rect.min.x;
}
if !result.min.y.is_finite() {
result.min.y = self.min_rect.min.y;
}
if !result.max.x.is_finite() {
result.max.x = self.min_rect.max.x;
}
if !result.max.y.is_finite() {
result.max.y = self.min_rect.max.y;
}
result
}
pub fn set_max_size(&mut self, size: Vec2) {
self.set_max_width(size.x);
self.set_max_height(size.y);
}
pub fn set_max_width(&mut self, width: f32) {
if self.layout.dir() == Direction::Horizontal && self.layout.is_reversed() {
debug_assert_eq!(self.min_rect.max.x, self.max_rect.max.x);
self.max_rect.min.x = self.max_rect.max.x - width.at_least(self.min_rect.width());
} else {
debug_assert_eq!(self.min_rect.min.x, self.max_rect.min.x);
self.max_rect.max.x = self.max_rect.min.x + width.at_least(self.min_rect.width());
}
}
pub fn set_max_height(&mut self, height: f32) {
if self.layout.dir() == Direction::Vertical && self.layout.is_reversed() {
debug_assert_eq!(self.min_rect.max.y, self.max_rect.max.y);
self.max_rect.min.y = self.max_rect.max.y - height.at_least(self.min_rect.height());
} else {
debug_assert_eq!(self.min_rect.min.y, self.max_rect.min.y);
self.max_rect.max.y = self.max_rect.min.y + height.at_least(self.min_rect.height());
}
}
pub fn set_min_size(&mut self, size: Vec2) {
self.set_min_width(size.x);
self.set_min_height(size.y);
}
pub fn set_min_width(&mut self, width: f32) {
if self.layout.dir() == Direction::Horizontal && self.layout.is_reversed() {
debug_assert_eq!(self.min_rect.max.x, self.max_rect.max.x);
self.min_rect.min.x = self.min_rect.min.x.min(self.min_rect.max.x - width);
} else {
debug_assert_eq!(self.min_rect.min.x, self.max_rect.min.x);
self.min_rect.max.x = self.min_rect.max.x.max(self.min_rect.min.x + width);
}
self.max_rect = self.max_rect.union(self.min_rect);
}
pub fn set_min_height(&mut self, height: f32) {
if self.layout.dir() == Direction::Vertical && self.layout.is_reversed() {
debug_assert_eq!(self.min_rect.max.y, self.max_rect.max.y);
self.min_rect.min.y = self.min_rect.min.y.min(self.min_rect.max.y - height);
} else {
debug_assert_eq!(self.min_rect.min.y, self.max_rect.min.y);
self.min_rect.max.y = self.min_rect.max.y.max(self.min_rect.min.y + height);
}
self.max_rect = self.max_rect.union(self.min_rect);
}
pub fn shrink_width_to_current(&mut self) {
self.set_max_width(self.min_rect().width())
}
pub fn shrink_height_to_current(&mut self) {
self.set_max_height(self.min_rect().height())
}
pub fn expand_to_include_rect(&mut self, rect: Rect) {
self.min_rect = self.min_rect.union(rect);
self.max_rect = self.max_rect.union(rect);
}
pub fn available(&self) -> Rect {
self.layout.available(self.cursor, self.max_rect())
}
pub fn available_finite(&self) -> Rect {
self.layout.available(self.cursor, self.max_rect_finite())
}
}
impl Ui {
pub fn make_persistent_id<IdSource>(&self, id_source: IdSource) -> Id
where
IdSource: Hash + std::fmt::Debug,
{
self.id.with(&id_source)
}
pub fn make_position_id(&self) -> Id {
self.id.with(self.child_count)
}
}
impl Ui {
pub fn interact(&self, rect: Rect, id: Id, sense: Sense) -> Response {
self.ctx()
.interact(self.layer_id(), self.clip_rect(), rect, Some(id), sense)
}
pub fn interact_hover(&self, rect: Rect) -> Response {
self.ctx().interact(
self.layer_id(),
self.clip_rect(),
rect,
None,
Sense::nothing(),
)
}
pub fn hovered(&self, rect: Rect) -> bool {
self.interact_hover(rect).hovered
}
pub fn contains_mouse(&self, rect: Rect) -> bool {
self.ctx()
.contains_mouse(self.layer_id(), self.clip_rect(), rect)
}
pub fn advance_cursor(&mut self, amount: f32) {
self.layout.advance_cursor(&mut self.cursor, amount);
}
pub fn allocate_space(&mut self, desired_size: Vec2) -> Rect {
let too_wide = desired_size.x > self.available().width();
let too_high = desired_size.x > self.available().height();
let rect = self.reserve_space_impl(desired_size);
if self.style().visuals.debug_widget_rects {
self.painter.rect_stroke(rect, 0.0, (1.0, LIGHT_BLUE));
let color = color::srgba(200, 0, 0, 255);
let width = 2.5;
let paint_line_seg = |a, b| self.painter().line_segment([a, b], (width, color));
if too_wide {
paint_line_seg(rect.left_top(), rect.left_bottom());
paint_line_seg(rect.left_center(), rect.right_center());
paint_line_seg(rect.right_top(), rect.right_bottom());
}
if too_high {
paint_line_seg(rect.left_top(), rect.right_top());
paint_line_seg(rect.center_top(), rect.center_bottom());
paint_line_seg(rect.left_bottom(), rect.right_bottom());
}
}
rect
}
fn reserve_space_impl(&mut self, child_size: Vec2) -> Rect {
let available_size = self.available_finite().size();
let child_rect = self
.layout
.allocate_space(&mut self.cursor, available_size, child_size);
let item_spacing = self.style().spacing.item_spacing;
self.layout.advance_cursor2(&mut self.cursor, item_spacing);
self.expand_to_include_rect(child_rect);
self.child_count += 1;
child_rect
}
}
impl Ui {
pub fn add(&mut self, widget: impl Widget) -> Response {
widget.ui(self)
}
pub fn label(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into())
}
pub fn heading(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().heading())
}
pub fn monospace(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().monospace())
}
pub fn small(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().small())
}
pub fn hyperlink(&mut self, url: impl Into<String>) -> Response {
self.add(Hyperlink::new(url))
}
pub fn text_edit(&mut self, text: &mut String) -> Response {
self.add(TextEdit::new(text))
}
#[must_use = "You should check if the user clicked this with `if ui.button(...).clicked { ... } "]
pub fn button(&mut self, text: impl Into<String>) -> Response {
self.add(Button::new(text))
}
pub fn checkbox(&mut self, checked: &mut bool, text: impl Into<String>) -> Response {
self.add(Checkbox::new(checked, text))
}
pub fn radio(&mut self, checked: bool, text: impl Into<String>) -> Response {
self.add(RadioButton::new(checked, text))
}
pub fn radio_value<Value: PartialEq>(
&mut self,
current_value: &mut Value,
radio_value: Value,
text: impl Into<String>,
) -> Response {
let response = self.radio(*current_value == radio_value, text);
if response.clicked {
*current_value = radio_value;
}
response
}
pub fn separator(&mut self) -> Response {
self.add(Separator::new())
}
pub fn drag_angle(&mut self, radians: &mut f32) -> Response {
#![allow(clippy::float_cmp)]
let mut degrees = radians.to_degrees();
let response = self.add(DragValue::f32(&mut degrees).speed(1.0).suffix("°"));
if degrees != radians.to_degrees() {
*radians = degrees.to_radians();
}
response
}
pub fn image(&mut self, texture_id: TextureId, desired_size: Vec2) -> Response {
self.add(Image::new(texture_id, desired_size))
}
}
impl Ui {
pub fn color_edit_button_srgba(&mut self, srgba: &mut Srgba) -> Response {
widgets::color_picker::color_edit_button_srgba(self, srgba)
}
pub fn color_edit_button_hsva(&mut self, hsva: &mut Hsva) -> Response {
widgets::color_picker::color_edit_button_hsva(self, hsva)
}
pub fn color_edit_button_srgba_premultiplied(&mut self, srgba: &mut [u8; 4]) -> Response {
let mut color = Srgba(*srgba);
let response = self.color_edit_button_srgba(&mut color);
*srgba = color.0;
response
}
pub fn color_edit_button_srgba_unmultiplied(&mut self, srgba: &mut [u8; 4]) -> Response {
let mut hsva = Hsva::from_srgba_unmultiplied(*srgba);
let response = self.color_edit_button_hsva(&mut hsva);
*srgba = hsva.to_srgba_unmultiplied();
response
}
pub fn color_edit_button_rgba_premultiplied(&mut self, rgba: &mut [f32; 4]) -> Response {
let mut hsva = Hsva::from_rgba_premultiplied(*rgba);
let response = self.color_edit_button_hsva(&mut hsva);
*rgba = hsva.to_rgba_premultiplied();
response
}
pub fn color_edit_button_rgba_unmultiplied(&mut self, rgba: &mut [f32; 4]) -> Response {
let mut hsva = Hsva::from_rgba_unmultiplied(*rgba);
let response = self.color_edit_button_hsva(&mut hsva);
*rgba = hsva.to_rgba_unmultiplied();
response
}
}
impl Ui {
pub fn wrap<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
let child_rect = self.available();
let mut child_ui = self.child_ui(child_rect, self.layout);
let ret = add_contents(&mut child_ui);
let size = child_ui.min_size();
let rect = self.allocate_space(size);
(ret, self.interact_hover(rect))
}
pub fn with_layer_id<R>(
&mut self,
layer_id: LayerId,
add_contents: impl FnOnce(&mut Self) -> R,
) -> (R, Response) {
self.wrap(|ui| {
ui.painter.set_layer_id(layer_id);
add_contents(ui)
})
}
pub fn add_custom_contents(&mut self, size: Vec2, add_contents: impl FnOnce(&mut Ui)) -> Rect {
let size = size.at_most(self.available().size());
let child_rect = self.layout.rect_from_cursor_size(self.cursor, size);
let mut child_ui = self.child_ui(child_rect, self.layout);
add_contents(&mut child_ui);
self.allocate_space(child_ui.min_size())
}
pub fn collapsing<R>(
&mut self,
heading: impl Into<String>,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> CollapsingResponse<R> {
CollapsingHeader::new(heading).show(self, add_contents)
}
pub fn indent<R>(
&mut self,
id_source: impl Hash,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
assert!(
self.layout().dir() == Direction::Vertical,
"You can only indent vertical layouts"
);
let indent = vec2(self.style().spacing.indent, 0.0);
let child_rect = Rect::from_min_max(self.cursor + indent, self.max_rect.right_bottom());
let mut child_ui = Self {
id: self.id.with(id_source),
..self.child_ui(child_rect, self.layout)
};
let ret = add_contents(&mut child_ui);
let size = child_ui.min_size();
let line_start = child_rect.min - indent * 0.5;
let line_start = self.painter().round_pos_to_pixels(line_start);
let line_end = pos2(line_start.x, line_start.y + size.y - 2.0);
self.painter.line_segment(
[line_start, line_end],
self.style().visuals.widgets.noninteractive.bg_stroke,
);
let rect = self.allocate_space(indent + size);
(ret, self.interact_hover(rect))
}
pub fn left_column(&mut self, width: f32) -> Self {
self.column(Align::Min, width)
}
pub fn centered_column(&mut self, width: f32) -> Self {
self.column(Align::Center, width)
}
pub fn right_column(&mut self, width: f32) -> Self {
self.column(Align::Max, width)
}
pub fn column(&mut self, column_position: Align, width: f32) -> Self {
let x = match column_position {
Align::Min => 0.0,
Align::Center => self.available().width() / 2.0 - width / 2.0,
Align::Max => self.available().width() - width,
};
self.child_ui(
Rect::from_min_size(
self.cursor + vec2(x, 0.0),
vec2(width, self.available().height()),
),
self.layout,
)
}
pub fn horizontal<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
let initial_size = vec2(
self.available().width(),
self.style().spacing.interact_size.y,
);
let right_to_left =
(self.layout.dir(), self.layout.align()) == (Direction::Vertical, Some(Align::Max));
self.inner_layout(
Layout::horizontal(Align::Center).with_reversed(right_to_left),
initial_size,
add_contents,
)
}
pub fn vertical<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Response) {
self.with_layout(Layout::vertical(Align::Min), add_contents)
}
fn inner_layout<R>(
&mut self,
layout: Layout,
initial_size: Vec2,
add_contents: impl FnOnce(&mut Self) -> R,
) -> (R, Response) {
let child_rect = self.layout.rect_from_cursor_size(self.cursor, initial_size);
let mut child_ui = self.child_ui(child_rect, layout);
let ret = add_contents(&mut child_ui);
let size = child_ui.min_size();
let rect = self.allocate_space(size);
(ret, self.interact_hover(rect))
}
pub fn with_layout<R>(
&mut self,
layout: Layout,
add_contents: impl FnOnce(&mut Self) -> R,
) -> (R, Response) {
let mut child_ui = self.child_ui(self.available(), layout);
let ret = add_contents(&mut child_ui);
let size = child_ui.min_size();
let rect = self.allocate_space(size);
(ret, self.interact_hover(rect))
}
pub fn columns<F, R>(&mut self, num_columns: usize, add_contents: F) -> R
where
F: FnOnce(&mut [Self]) -> R,
{
let spacing = self.style().spacing.item_spacing.x;
let total_spacing = spacing * (num_columns as f32 - 1.0);
let column_width = (self.available().width() - total_spacing) / (num_columns as f32);
let mut columns: Vec<Self> = (0..num_columns)
.map(|col_idx| {
let pos = self.cursor + vec2((col_idx as f32) * (column_width + spacing), 0.0);
let child_rect = Rect::from_min_max(
pos,
pos2(pos.x + column_width, self.max_rect.right_bottom().y),
);
self.child_ui(child_rect, self.layout)
})
.collect();
let result = add_contents(&mut columns[..]);
let mut sum_width = total_spacing;
for column in &columns {
sum_width += column.min_rect.width();
}
let mut max_height = 0.0;
for ui in columns {
let size = ui.min_size();
max_height = size.y.max(max_height);
}
let size = vec2(self.available().width().max(sum_width), max_height);
self.allocate_space(size);
result
}
}
impl Ui {
pub fn debug_paint_cursor(&self) {
self.layout.debug_paint_cursor(self.cursor, &self.painter);
}
}