Improve and expand documentation tests

This commit is contained in:
Emil Ernerfeldt 2020-10-26 08:34:49 +01:00
parent 8787eb77cf
commit cbdfc03378
6 changed files with 46 additions and 11 deletions

View file

@ -7,10 +7,12 @@
//! With it you can then get access to an `Ui` where you can put widgets.
//! Use one of `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`. For instace:
//!
//! ``` ignore
//! egui::CentralPanel::default().show(ctx, |ui| {
//! ```
//! # let mut ctx = egui::Context::new();
//! # ctx.begin_frame(Default::default());
//! egui::CentralPanel::default().show(&ctx, |ui| {
//! ui.label("Hello");
//! })
//! });
//! ```
//!
//!

View file

@ -144,6 +144,11 @@ impl Memory {
}
}
/// Stop editing of active `TextEdit` (if any).
pub fn stop_text_input(&mut self) {
self.interaction.kb_focus_id = None;
}
/// Forget window positions, sizes etc.
/// Can be used to auto-layout windows.
pub fn reset_areas(&mut self) {

View file

@ -1,7 +1,7 @@
//! Menu bar functionality (very basic so far).
//!
//! Usage:
//! ``` rust
//! ```
//! fn show_menu(ui: &mut egui::Ui) {
//! use egui::{menu, Button};
//!

View file

@ -153,10 +153,12 @@ impl std::ops::BitOr for Response {
/// To summarize the response from many widgets you can use this pattern:
///
/// ``` ignore
/// let mut response = ui.add(some_widget);
/// response |= ui.add(some_other_widget);
/// response |= ui.add(some_widget);
/// ```
/// # let mut ui = egui::Ui::__test();
/// # let (widget_a, widget_b, widget_c) = (egui::Label::new("a"), egui::Label::new("b"), egui::Label::new("c"));
/// let mut response = ui.add(widget_a);
/// response |= ui.add(widget_b);
/// response |= ui.add(widget_c);
/// if response.active { ui.label("You are interacting with one of the widgets"); }
/// ```
impl std::ops::BitOrAssign for Response {

View file

@ -104,6 +104,19 @@ impl Ui {
}
}
/// Empty `Ui` for use in tests.
pub fn __test() -> Self {
let mut ctx = Context::new();
ctx.begin_frame(Default::default());
let id = Id::new("__test");
let layer_id = LayerId {
order: 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 {
@ -799,10 +812,11 @@ impl Ui {
/// Temporarily split split an Ui into several columns.
///
/// ``` ignore
/// ```
/// # let mut ui = egui::Ui::__test();
/// ui.columns(2, |columns| {
/// columns[0].add(egui::widgets::label!("First column"));
/// columns[1].add(egui::widgets::label!("Second column"));
/// columns[0].label("First column");
/// columns[1].label("Second column");
/// });
/// ```
pub fn columns<F, R>(&mut self, num_columns: usize, add_contents: F) -> R

View file

@ -9,6 +9,18 @@ pub(crate) struct State {
}
/// A text region that the user can edit the contents of.
///
/// Example:
///
/// ```
/// # let mut ui = egui::Ui::__test();
/// # let mut my_string = String::new();
/// let response = ui.add(egui::TextEdit::new(&mut my_string).multiline(false));
/// if response.has_kb_focus && ui.input().key_pressed(egui::Key::Enter) {
/// ui.memory().stop_text_input();
/// // use my_string
/// }
/// ```
#[derive(Debug)]
pub struct TextEdit<'t> {
text: &'t mut String,