-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8eb622
commit adae654
Showing
2 changed files
with
60 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,57 @@ | ||
use crate::{FlexContainerResponse, FlexContainerUi}; | ||
use egui::{Ui, Widget}; | ||
use egui::Ui; | ||
|
||
/// Implement this trait for a widget to make it usable in a flex container. | ||
/// | ||
/// The reason there is a separate trait is that we need to measure the content size independently | ||
/// of the frame size. (The content will stay at it's intrinsic size while the frame will be | ||
/// stretched according to the flex layout.) | ||
/// | ||
/// If your widget has no frmae you don't need to implement this trait and can use | ||
/// [`crate::FlexInstance::add_widget`] to add any [`egui::Widget`]. | ||
pub trait FlexWidget { | ||
type Response; | ||
fn ui(self, ui: &mut Ui, container: FlexContainerUi) -> FlexContainerResponse<Self::Response>; | ||
/// Show your widget here. Use the provided [`Ui`] to draw the container (e.g. using a [`egui::Frame`]) | ||
/// and in the frame ui use [`FlexContainerUi::content`] to draw your widget. | ||
/// The frame will grow according to the flex layout while the content will be centered / positioned | ||
/// based on [`crate::FlexItem::align_self_content`]. | ||
fn flex_ui( | ||
self, | ||
ui: &mut Ui, | ||
container: FlexContainerUi, | ||
) -> FlexContainerResponse<Self::Response>; | ||
} | ||
|
||
impl<T: Widget> FlexWidget for T { | ||
type Response = egui::Response; | ||
mod egui_widgets { | ||
use super::*; | ||
use egui::widgets::*; | ||
macro_rules! impl_widget { | ||
($($widget:ty),*) => { | ||
$( | ||
impl FlexWidget for $widget { | ||
type Response = egui::Response; | ||
|
||
fn ui(self, ui: &mut Ui, container: FlexContainerUi) -> FlexContainerResponse<Self::Response> { | ||
container.content_widget(ui, self) | ||
fn flex_ui(self, ui: &mut Ui, container: FlexContainerUi) -> FlexContainerResponse<Self::Response> { | ||
container.content_widget(ui, self) | ||
} | ||
} | ||
)* | ||
}; | ||
} | ||
impl_widget!( | ||
Button<'_>, | ||
Label, | ||
Checkbox<'_>, | ||
Image<'_>, | ||
DragValue<'_>, | ||
Hyperlink, | ||
ImageButton<'_>, | ||
ProgressBar, | ||
RadioButton, | ||
Link, | ||
SelectableLabel, | ||
Slider<'_>, | ||
TextEdit<'_>, | ||
Spinner | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters