Context

Struct Context 

Source
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

Source

pub fn new() -> CtxRef

👎Deprecated: Use CtxRef::default() instead
Source

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.

Source

pub fn memory(&self) -> AtomicRefMut<'_, Memory>

Stores all the egui state. If you want to store/restore egui, serialize this.

Source

pub fn output(&self) -> AtomicRefMut<'_, Output>

What egui outputs each frame.

Source

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.

Source

pub fn input(&self) -> &InputState

Source

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.

Source

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.

Source

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.

Source

pub fn style(&self) -> Arc<Style>

The Style used by all subsequent windows, panels etc.

Source

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);
Source

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 mode
Source

pub fn pixels_per_point(&self) -> f32

The number of physical pixels for each logical point.

Source

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.

Source

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.

Source

pub fn tessellate(&self, shapes: Vec<ClippedShape>) -> Vec<ClippedMesh>

Tessellate the given shapes into triangle meshes.

Source

pub fn used_rect(&self) -> Rect

How much space is used by panels and windows.

Source

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.

Source

pub fn is_pointer_over_area(&self) -> bool

Is the pointer (mouse/touch) over any egui area?

Source

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.

Source

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.

Source

pub fn wants_mouse_input(&self) -> bool

👎Deprecated: Renamed wants_pointer_input
Source

pub fn is_using_mouse(&self) -> bool

👎Deprecated: Renamed is_using_pointer
Source

pub fn wants_keyboard_input(&self) -> bool

If true, egui is currently listening on text input (e.g. typing text in a TextEdit).

Source

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).

Source

pub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId>

Top-most layer at the given position.

Source

pub fn debug_on_hover(&self) -> bool

Wether or not to debug widget layout on hover.

Source

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
Source

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.

Source

pub fn clear_animations(&self)

Clear memory of any animations.

Source§

impl Context

Source

pub fn settings_ui(&self, ui: &mut Ui)

Source

pub fn inspection_ui(&self, ui: &mut Ui)

Source

pub fn memory_ui(&self, ui: &mut Ui)

Source§

impl Context

Source

pub fn style_ui(&self, ui: &mut Ui)

Trait Implementations§

Source§

impl AsRef<Context> for CtxRef

Source§

fn as_ref(&self) -> &Context

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<Context> for CtxRef

Source§

fn borrow(&self) -> &Context

Immutably borrows from an owned value. Read more
Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Context

Source§

fn default() -> Context

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoService for T
where T: Send + Sync + 'static,

Source§

fn service(self) -> Service<Self>

Constructs wrapped service
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> AnyMapTrait for T
where T: 'static + Any + Clone + Send + Sync,

Source§

impl<T> Component for T
where T: Send + Sync + 'static,

Source§

impl<T> SystemContext for T
where T: Default + Send + Sync + 'static,