-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experiment with a new Group
container
#4631
Draft
emilk
wants to merge
1
commit into
master
Choose a base branch
from
emilk/group
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,159 @@ | ||
//! Frame container | ||
|
||
use crate::{layers::ShapeIdx, *}; | ||
use epaint::*; | ||
|
||
/// A group of widgets with a unique id | ||
/// that can be used in centered layouts. | ||
#[derive(Clone, Copy, Debug, PartialEq)] | ||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] | ||
#[must_use = "You should call .show()"] | ||
pub struct Group { | ||
id_source: Id, | ||
frame: Frame, | ||
} | ||
|
||
impl Group { | ||
pub fn new(id_source: impl Into<Id>) -> Self { | ||
Self { | ||
id_source: id_source.into(), | ||
frame: Frame::default(), | ||
} | ||
} | ||
|
||
pub fn frame(mut self, frame: Frame) -> Self { | ||
self.frame = frame; | ||
self | ||
} | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
pub struct Prepared { | ||
id: Id, | ||
|
||
/// The frame that was prepared. | ||
/// | ||
/// The margin has already been read and used, | ||
/// but the rest of the fields may be modified. | ||
pub frame: Frame, | ||
|
||
/// This is where we will insert the frame shape so it ends up behind the content. | ||
where_to_put_background: ShapeIdx, | ||
|
||
/// Add your widgets to this UI so it ends up within the frame. | ||
pub content_ui: Ui, | ||
} | ||
|
||
impl Group { | ||
/// Begin a dynamically colored frame. | ||
/// | ||
/// This is a more advanced API. | ||
/// Usually you want to use [`Self::show`] instead. | ||
/// | ||
/// See docs for [`Group`] for an example. | ||
pub fn begin(self, ui: &mut Ui) -> Prepared { | ||
let Self { id_source, frame } = self; | ||
let id = ui.make_persistent_id(id_source); | ||
|
||
let where_to_put_background = ui.painter().add(Shape::Noop); | ||
|
||
let prev_inner_size: Option<Vec2> = ui.data(|data| data.get_temp(id)); | ||
|
||
let mut inner_rect = if let Some(prev_inner_size) = prev_inner_size { | ||
let (_, outer_rect) = ui.allocate_space(prev_inner_size + frame.total_margin().sum()); | ||
outer_rect - frame.total_margin() | ||
} else { | ||
// Invisible sizing pass | ||
let outer_rect_bounds = ui.available_rect_before_wrap(); | ||
outer_rect_bounds - frame.total_margin() | ||
}; | ||
|
||
// Make sure we don't shrink to the negative: | ||
inner_rect.max.x = inner_rect.max.x.max(inner_rect.min.x); | ||
inner_rect.max.y = inner_rect.max.y.max(inner_rect.min.y); | ||
|
||
let sizing_pass = prev_inner_size.is_none(); | ||
|
||
let mut layout = *ui.layout(); | ||
|
||
if sizing_pass { | ||
// TODO(emilk): this code is duplicated from `ui.rs` | ||
// During the sizing pass we want widgets to use up as little space as possible, | ||
// so that we measure the only the space we _need_. | ||
layout.cross_justify = false; | ||
if layout.cross_align == Align::Center { | ||
layout.cross_align = Align::Min; | ||
} | ||
} | ||
|
||
let mut content_ui = ui.child_ui( | ||
inner_rect, | ||
layout, | ||
Some(UiStackInfo::new(UiKind::Frame).with_frame(frame)), | ||
); | ||
|
||
if sizing_pass { | ||
content_ui.set_sizing_pass(); | ||
} | ||
|
||
Prepared { | ||
id, | ||
frame, | ||
where_to_put_background, | ||
content_ui, | ||
} | ||
} | ||
|
||
/// Show the given ui surrounded by this frame. | ||
pub fn show<R>(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> { | ||
self.show_dyn(ui, Box::new(add_contents)) | ||
} | ||
|
||
/// Show using dynamic dispatch. | ||
pub fn show_dyn<'c, R>( | ||
self, | ||
ui: &mut Ui, | ||
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>, | ||
) -> InnerResponse<R> { | ||
let mut prepared = self.begin(ui); | ||
let ret = add_contents(&mut prepared.content_ui); | ||
let response = prepared.end(ui); | ||
InnerResponse::new(ret, response) | ||
} | ||
} | ||
|
||
impl Prepared { | ||
/// Allocate the space that was used by [`Self::content_ui`]. | ||
/// | ||
/// This MUST be called, or the parent ui will not know how much space this widget used. | ||
/// | ||
/// This can be called before or after [`Self::paint`]. | ||
pub fn allocate_space(&self, ui: &mut Ui) -> Response { | ||
let inner_rect = self.content_ui.min_rect(); | ||
let outer_rect = inner_rect + self.frame.total_margin(); | ||
|
||
// Remember size to next frame | ||
ui.data_mut(|data| data.insert_temp(self.id, inner_rect.size())); | ||
|
||
ui.allocate_rect(outer_rect, Sense::hover()) | ||
} | ||
|
||
/// Paint the frame. | ||
/// | ||
/// This can be called before or after [`Self::allocate_space`]. | ||
pub fn paint(&self, ui: &Ui) { | ||
let paint_rect = self.content_ui.min_rect() + self.frame.inner_margin; | ||
|
||
if ui.is_rect_visible(paint_rect) { | ||
let shape = self.frame.paint(paint_rect); | ||
ui.painter().set(self.where_to_put_background, shape); | ||
} | ||
} | ||
|
||
/// Convenience for calling [`Self::allocate_space`] and [`Self::paint`]. | ||
pub fn end(self, ui: &mut Ui) -> Response { | ||
self.paint(ui); | ||
self.allocate_space(ui) | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "group_layout" | ||
version = "0.1.0" | ||
authors = ["Emil Ernerfeldt <emil.ernerfeldt@gmail.com>"] | ||
license = "MIT OR Apache-2.0" | ||
edition = "2021" | ||
rust-version = "1.76" | ||
publish = false | ||
|
||
[lints] | ||
workspace = true | ||
|
||
|
||
[dependencies] | ||
eframe = { workspace = true, features = [ | ||
"default", | ||
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO | ||
] } | ||
env_logger = { version = "0.10", default-features = false, features = [ | ||
"auto-color", | ||
"humantime", | ||
] } |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release | ||
#![allow(rustdoc::missing_crate_level_docs)] // it's an example | ||
|
||
use eframe::egui; | ||
|
||
fn main() -> Result<(), eframe::Error> { | ||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). | ||
|
||
let options = eframe::NativeOptions { | ||
viewport: egui::ViewportBuilder::default().with_inner_size([640.0, 480.0]), | ||
..Default::default() | ||
}; | ||
|
||
eframe::run_simple_native("My egui App", options, move |ctx, _frame| { | ||
egui::CentralPanel::default().show(ctx, |ui| { | ||
ui.vertical_centered(|ui| { | ||
egui::Group::new("my_group") | ||
.frame(egui::Frame::group(ui.style())) | ||
.show(ui, |ui| { | ||
ui.horizontal(|ui| { | ||
ui.label("Hello"); | ||
ui.code("world!"); | ||
}); | ||
|
||
if ui.button("Reset egui").clicked() { | ||
ui.memory_mut(|mem| *mem = Default::default()); | ||
} | ||
}); | ||
}); | ||
}); | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
Ui::group
going to be changed to use this? Or maybe renamed to avoid confusion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know yet. This is all very much related to #4634 in that I would like a simple way to create a new
Ui
which has opt-in support for the new "grouping" behavior of this PR, as well as a frame (likeui.group
does).