Skip to main content

egui_charts/ui_kit/ext/
widget_ext.rs

1//! WidgetExt - Extension traits for egui widgets
2//!
3//! Provides convenience methods for common widget variations following rerun patterns.
4//!
5//! # Usage
6//!
7//! ```
8//! use egui_charts::ui_kit::ext::ButtonWidgetExt;
9//!
10//! let button = egui::Button::new("Save")
11//!     .fill_width()
12//!     .min_height(36.0);
13//! ```
14
15use egui::{Button, TextEdit, Vec2};
16
17/// Extension trait for `egui::Button`
18pub trait ButtonWidgetExt<'a>: Sized {
19    /// Make the button fill the available width
20    fn fill_width(self) -> Self;
21
22    /// Set a minimum width for the button
23    fn min_width(self, width: f32) -> Self;
24
25    /// Set a minimum height for the button
26    fn min_height(self, height: f32) -> Self;
27
28    /// Set exact dimensions
29    fn exact_size(self, size: Vec2) -> Self;
30}
31
32impl<'a> ButtonWidgetExt<'a> for Button<'a> {
33    fn fill_width(self) -> Self {
34        self.min_size(Vec2::new(f32::INFINITY, 0.0))
35    }
36
37    fn min_width(self, width: f32) -> Self {
38        self.min_size(Vec2::new(width, 0.0))
39    }
40
41    fn min_height(self, height: f32) -> Self {
42        self.min_size(Vec2::new(0.0, height))
43    }
44
45    fn exact_size(self, size: Vec2) -> Self {
46        self.min_size(size)
47    }
48}
49
50/// Extension trait for `egui::TextEdit`
51pub trait TextEditWidgetExt<'a>: Sized {
52    /// Make the text edit fill available width
53    fn fill_width(self) -> Self;
54
55    /// Set a fixed width
56    fn fixed_width(self, width: f32) -> Self;
57
58    /// Add a character limit
59    fn char_limit(self, limit: usize) -> Self;
60}
61
62impl<'a> TextEditWidgetExt<'a> for TextEdit<'a> {
63    fn fill_width(self) -> Self {
64        self.desired_width(f32::INFINITY)
65    }
66
67    fn fixed_width(self, width: f32) -> Self {
68        self.desired_width(width)
69    }
70
71    fn char_limit(self, limit: usize) -> Self {
72        self.char_limit(limit)
73    }
74}
75
76/// Generic extension trait for widgets that support rounding
77pub trait RoundingExt: Sized {
78    /// Apply standard button rounding
79    fn button_rounding(self) -> Self;
80
81    /// Apply panel rounding
82    fn panel_rounding(self) -> Self;
83
84    /// Apply no rounding
85    fn no_rounding(self) -> Self;
86
87    /// Apply pill rounding (fully rounded)
88    fn pill_rounding(self) -> Self;
89}
90
91// Note: Implementation for specific widget types that support corner_radius
92// would be added here, but egui's Button::corner_radius returns Self,
93// so we can chain these if needed.