pub struct Context { /* private fields */ }Expand description
This is the first thing you need when working with egui. Create using CtxRef.
Contains the InputState, Memory, Output, and more.
Your handle to Egui.
Almost all methods are marked &self, Context has interior mutability (protected by mutexes).
Multi-threaded access to a Context is behind the feature flag multi_threaded.
Normally you’d always do all ui work on one thread, or perhaps use multiple contexts,
but if you really want to access the same Context from multiple threads, it SHOULD be fine,
but you are likely the first person to try it.
Implementations§
Source§impl Context
impl Context
pub fn new() -> CtxRef
Sourcepub fn available_rect(&self) -> Rect
pub fn available_rect(&self) -> Rect
How much space is still available after panels has been added. This is the “background” area, what egui doesn’t cover with panels (but may cover with windows). This is also the area to which windows are constrained.
Sourcepub fn memory(&self) -> AtomicRefMut<'_, Memory>
pub fn memory(&self) -> AtomicRefMut<'_, Memory>
Stores all the egui state. If you want to store/restore egui, serialize this.
Sourcepub fn output(&self) -> AtomicRefMut<'_, Output>
pub fn output(&self) -> AtomicRefMut<'_, Output>
What egui outputs each frame.
Sourcepub fn request_repaint(&self)
pub fn request_repaint(&self)
Call this if there is need to repaint the UI, i.e. if you are showing an animation. If this is called at least once in a frame, then there will be another frame right after this. Call as many times as you wish, only one repaint will be issued.
pub fn input(&self) -> &InputState
Sourcepub fn fonts(&self) -> &Fonts
pub fn fonts(&self) -> &Fonts
Not valid until first call to CtxRef::begin_frame().
That’s because since we don’t know the proper pixels_per_point until then.
Sourcepub fn texture(&self) -> Arc<Texture>
pub fn texture(&self) -> Arc<Texture>
The egui texture, containing font characters etc.
Not valid until first call to CtxRef::begin_frame().
That’s because since we don’t know the proper pixels_per_point until then.
Sourcepub fn set_fonts(&self, font_definitions: FontDefinitions)
pub fn set_fonts(&self, font_definitions: FontDefinitions)
Tell egui which fonts to use.
The default egui fonts only support latin and cyrillic alphabets,
but you can call this to install additional fonts that support e.g. korean characters.
The new fonts will become active at the start of the next frame.
Sourcepub fn set_style(&self, style: impl Into<Arc<Style>>)
pub fn set_style(&self, style: impl Into<Arc<Style>>)
The Style used by all new windows, panels etc.
You can also use Ui::style_mut to change the style of a single Ui.
Example:
let mut style: egui::Style = (*ctx.style()).clone();
style.spacing.item_spacing = egui::vec2(10.0, 20.0);
ctx.set_style(style);Sourcepub fn set_visuals(&self, visuals: Visuals)
pub fn set_visuals(&self, visuals: Visuals)
The Visuals used by all subsequent windows, panels etc.
You can also use Ui::visuals_mut to change the visuals of a single Ui.
Example:
ctx.set_visuals(egui::Visuals::light()); // Switch to light modeSourcepub fn pixels_per_point(&self) -> f32
pub fn pixels_per_point(&self) -> f32
The number of physical pixels for each logical point.
Sourcepub fn set_pixels_per_point(&self, pixels_per_point: f32)
pub fn set_pixels_per_point(&self, pixels_per_point: f32)
Set the number of physical pixels for each logical point. Will become active at the start of the next frame.
Note that this may be overwritten by input from the integration via RawInput::pixels_per_point.
For instance, when using egui_web the browsers native zoom level will always be used.
Sourcepub fn end_frame(&self) -> (Output, Vec<ClippedShape>)
pub fn end_frame(&self) -> (Output, Vec<ClippedShape>)
Call at the end of each frame.
Returns what has happened this frame crate::Output as well as what you need to paint.
You can transform the returned shapes into triangles with a call to Context::tessellate.
Sourcepub fn tessellate(&self, shapes: Vec<ClippedShape>) -> Vec<ClippedMesh>
pub fn tessellate(&self, shapes: Vec<ClippedShape>) -> Vec<ClippedMesh>
Tessellate the given shapes into triangle meshes.
Sourcepub fn used_size(&self) -> Vec2
pub fn used_size(&self) -> Vec2
How much space is used by panels and windows. You can shrink your egui area to this size and still fit all egui components.
Sourcepub fn is_pointer_over_area(&self) -> bool
pub fn is_pointer_over_area(&self) -> bool
Is the pointer (mouse/touch) over any egui area?
Sourcepub fn wants_pointer_input(&self) -> bool
pub fn wants_pointer_input(&self) -> bool
True if egui is currently interested in the pointer (mouse or touch).
Could be the pointer is hovering over a Window or the user is dragging a widget.
If false, the pointer is outside of any egui area and so
you may be interested in what it is doing (e.g. controlling your game).
Returns false if a drag started outside of egui and then moved over an egui area.
Sourcepub fn is_using_pointer(&self) -> bool
pub fn is_using_pointer(&self) -> bool
Is egui currently using the pointer position (e.g. dragging a slider).
NOTE: this will return false if the pointer is just hovering over an egui area.
pub fn wants_mouse_input(&self) -> bool
pub fn is_using_mouse(&self) -> bool
Sourcepub fn wants_keyboard_input(&self) -> bool
pub fn wants_keyboard_input(&self) -> bool
If true, egui is currently listening on text input (e.g. typing text in a TextEdit).
Sourcepub fn translate_layer(&self, layer_id: LayerId, delta: Vec2)
pub fn translate_layer(&self, layer_id: LayerId, delta: Vec2)
Move all the graphics at the given layer. Can be used to implement drag-and-drop (see relevant demo).
Sourcepub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId>
pub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId>
Top-most layer at the given position.
Sourcepub fn debug_on_hover(&self) -> bool
pub fn debug_on_hover(&self) -> bool
Wether or not to debug widget layout on hover.
Sourcepub fn set_debug_on_hover(&self, debug_on_hover: bool)
pub fn set_debug_on_hover(&self, debug_on_hover: bool)
Turn on/off wether or not to debug widget layout on hover.
Source§impl Context
§Animation
impl Context
§Animation
Sourcepub fn animate_bool(&self, id: Id, value: bool) -> f32
pub fn animate_bool(&self, id: Id, value: bool) -> f32
Returns a value in the range [0, 1], to indicate “how on” this thing is.
The first time called it will return if value { 1.0 } else { 0.0 }
Calling this with value = true will always yield a number larger than zero, quickly going towards one.
Calling this with value = false will always yield a number less than one, quickly going towards zero.
The function will call Self::request_repaint() when appropriate.
Sourcepub fn clear_animations(&self)
pub fn clear_animations(&self)
Clear memory of any animations.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Context
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more