Skip to content
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

Replace Context::begin_frame/end_frame with fn run taking a closure #872

Merged
master from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add helper egui::__run_test_ctx for doctests
  • Loading branch information
emilk committed Nov 3, 2021
commit 65c5eb5083b036b18369f45e956d43db9065f2bf
5 changes: 2 additions & 3 deletions egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ impl State {
/// This forms the base of the [`Window`] container.
///
/// ```
/// # let mut ctx = egui::CtxRef::default();
/// # ctx.begin_frame(Default::default());
/// # let ctx = &ctx;
/// # egui::__run_test_ctx(|ctx| {
/// egui::Area::new("my_area")
/// .fixed_pos(egui::pos2(32.0, 32.0))
/// .show(ctx, |ui| {
/// ui.label("Floating text!");
/// });
/// # });
#[must_use = "You should call .show()"]
#[derive(Clone, Copy, Debug)]
pub struct Area {
Expand Down
15 changes: 6 additions & 9 deletions egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ impl Side {
/// See the [module level docs](crate::containers::panel) for more details.
///
/// ```
/// # let mut ctx = egui::CtxRef::default();
/// # ctx.begin_frame(Default::default());
/// # let ctx = &ctx;
/// # egui::__run_test_ctx(|ctx| {
/// egui::SidePanel::left("my_left_panel").show(ctx, |ui| {
/// ui.label("Hello World!");
/// });
/// # });
/// ```
///
/// See also [`TopBottomPanel`].
Expand Down Expand Up @@ -350,12 +349,11 @@ impl TopBottomSide {
/// See the [module level docs](crate::containers::panel) for more details.
///
/// ```
/// # let mut ctx = egui::CtxRef::default();
/// # ctx.begin_frame(Default::default());
/// # let ctx = &ctx;
/// # egui::__run_test_ctx(|ctx| {
/// egui::TopBottomPanel::top("my_panel").show(ctx, |ui| {
/// ui.label("Hello World!");
/// });
/// # });
/// ```
///
/// See also [`SidePanel`].
Expand Down Expand Up @@ -605,12 +603,11 @@ impl TopBottomPanel {
/// See the [module level docs](crate::containers::panel) for more details.
///
/// ```
/// # let mut ctx = egui::CtxRef::default();
/// # ctx.begin_frame(Default::default());
/// # let ctx = &ctx;
/// # egui::__run_test_ctx(|ctx| {
/// egui::CentralPanel::default().show(ctx, |ui| {
/// ui.label("Hello World!");
/// });
/// # });
/// ```
#[must_use = "You should call .show()"]
#[derive(Default)]
Expand Down
5 changes: 2 additions & 3 deletions egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use super::*;
/// * if there should be a close button (none by default)
///
/// ```
/// # let mut ctx = egui::CtxRef::default();
/// # ctx.begin_frame(Default::default());
/// # let ctx = &ctx;
/// # egui::__run_test_ctx(|ctx| {
/// egui::Window::new("My Window").show(ctx, |ui| {
/// ui.label("Hello World!");
/// });
/// # });
#[must_use = "You should call .show()"]
pub struct Window<'open> {
title: WidgetText,
Expand Down
12 changes: 10 additions & 2 deletions egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
//! get access to an [`Ui`] where you can put widgets. For example:
//!
//! ```
//! # let mut ctx = egui::CtxRef::default();
//! # ctx.begin_frame(Default::default());
//! # egui::__run_test_ctx(|ctx| {
//! egui::CentralPanel::default().show(&ctx, |ui| {
//! ui.add(egui::Label::new("Hello World!"));
//! ui.label("A shorter and more convenient way to add a label.");
//! if ui.button("Click me").clicked() {
//! // take some action here
//! }
//! });
//! # });
//! ```
//!
//! ### Quick start
Expand Down Expand Up @@ -576,6 +576,14 @@ pub enum WidgetType {

// ----------------------------------------------------------------------------

/// For use in tests; especially doctests.
pub fn __run_test_ctx(mut run_ui: impl FnMut(&CtxRef)) {
let mut ctx = CtxRef::default();
let _ = ctx.run(Default::default(), |ctx| {
run_ui(ctx);
});
}

/// For use in tests; especially doctests.
pub fn __run_test_ui(mut add_contents: impl FnMut(&mut Ui)) {
let mut ctx = CtxRef::default();
Expand Down