egui/
context.rs

1#![warn(missing_docs)] // Let's keep `Context` well-documented.
2
3use std::{borrow::Cow, cell::RefCell, panic::Location, sync::Arc, time::Duration};
4
5use emath::GuiRounding as _;
6use epaint::{
7    ClippedPrimitive, ClippedShape, Color32, ImageData, Pos2, Rect, StrokeKind,
8    TessellationOptions, TextureId, Vec2,
9    emath::{self, TSTransform},
10    mutex::RwLock,
11    stats::PaintStats,
12    tessellator,
13    text::{FontInsert, FontPriority, Fonts, FontsView},
14    vec2,
15};
16
17use crate::{
18    Align2, CursorIcon, DeferredViewportUiCallback, FontDefinitions, Grid, Id, ImmediateViewport,
19    ImmediateViewportRendererCallback, Key, KeyboardShortcut, Label, LayerId, Memory,
20    ModifierNames, Modifiers, NumExt as _, Order, Painter, RawInput, Response, RichText,
21    SafeAreaInsets, ScrollArea, Sense, Style, TextStyle, TextureHandle, TextureOptions, Ui,
22    ViewportBuilder, ViewportCommand, ViewportId, ViewportIdMap, ViewportIdPair, ViewportIdSet,
23    ViewportOutput, Widget as _, WidgetRect, WidgetText,
24    animation_manager::AnimationManager,
25    containers::{self, area::AreaState},
26    data::output::PlatformOutput,
27    epaint,
28    hit_test::WidgetHits,
29    input_state::{InputState, MultiTouchInfo, PointerEvent, SurrenderFocusOn},
30    interaction::InteractionSnapshot,
31    layers::GraphicLayers,
32    load::{self, Bytes, Loaders, SizedTexture},
33    memory::{Options, Theme},
34    os::OperatingSystem,
35    output::FullOutput,
36    pass_state::PassState,
37    plugin,
38    plugin::TypedPluginHandle,
39    resize, response, scroll_area,
40    util::IdTypeMap,
41    viewport::ViewportClass,
42};
43
44#[cfg(feature = "accesskit")]
45use crate::IdMap;
46
47/// Information given to the backend about when it is time to repaint the ui.
48///
49/// This is given in the callback set by [`Context::set_request_repaint_callback`].
50#[derive(Clone, Copy, Debug)]
51pub struct RequestRepaintInfo {
52    /// This is used to specify what viewport that should repaint.
53    pub viewport_id: ViewportId,
54
55    /// Repaint after this duration. If zero, repaint as soon as possible.
56    pub delay: Duration,
57
58    /// The number of fully completed passes, of the entire lifetime of the [`Context`].
59    ///
60    /// This can be compared to [`Context::cumulative_pass_nr`] to see if we we still
61    /// need another repaint (ui pass / frame), or if one has already happened.
62    pub current_cumulative_pass_nr: u64,
63}
64
65// ----------------------------------------------------------------------------
66
67thread_local! {
68    static IMMEDIATE_VIEWPORT_RENDERER: RefCell<Option<Box<ImmediateViewportRendererCallback>>> = Default::default();
69}
70
71// ----------------------------------------------------------------------------
72
73struct WrappedTextureManager(Arc<RwLock<epaint::TextureManager>>);
74
75impl Default for WrappedTextureManager {
76    fn default() -> Self {
77        let mut tex_mngr = epaint::textures::TextureManager::default();
78
79        // Will be filled in later
80        let font_id = tex_mngr.alloc(
81            "egui_font_texture".into(),
82            epaint::ColorImage::filled([0, 0], Color32::TRANSPARENT).into(),
83            Default::default(),
84        );
85        assert_eq!(
86            font_id,
87            TextureId::default(),
88            "font id should be equal to TextureId::default(), but was {font_id:?}",
89        );
90
91        Self(Arc::new(RwLock::new(tex_mngr)))
92    }
93}
94
95// ----------------------------------------------------------------------------
96
97/// Repaint-logic
98impl ContextImpl {
99    /// This is where we update the repaint logic.
100    fn begin_pass_repaint_logic(&mut self, viewport_id: ViewportId) {
101        let viewport = self.viewports.entry(viewport_id).or_default();
102
103        std::mem::swap(
104            &mut viewport.repaint.prev_causes,
105            &mut viewport.repaint.causes,
106        );
107        viewport.repaint.causes.clear();
108
109        viewport.repaint.prev_pass_paint_delay = viewport.repaint.repaint_delay;
110
111        if viewport.repaint.outstanding == 0 {
112            // We are repainting now, so we can wait a while for the next repaint.
113            viewport.repaint.repaint_delay = Duration::MAX;
114        } else {
115            viewport.repaint.repaint_delay = Duration::ZERO;
116            viewport.repaint.outstanding -= 1;
117            if let Some(callback) = &self.request_repaint_callback {
118                (callback)(RequestRepaintInfo {
119                    viewport_id,
120                    delay: Duration::ZERO,
121                    current_cumulative_pass_nr: viewport.repaint.cumulative_pass_nr,
122                });
123            }
124        }
125    }
126
127    fn request_repaint(&mut self, viewport_id: ViewportId, cause: RepaintCause) {
128        self.request_repaint_after(Duration::ZERO, viewport_id, cause);
129    }
130
131    fn request_repaint_after(
132        &mut self,
133        mut delay: Duration,
134        viewport_id: ViewportId,
135        cause: RepaintCause,
136    ) {
137        let viewport = self.viewports.entry(viewport_id).or_default();
138
139        if delay == Duration::ZERO {
140            // Each request results in two repaints, just to give some things time to settle.
141            // This solves some corner-cases of missing repaints on frame-delayed responses.
142            viewport.repaint.outstanding = 1;
143        } else {
144            // For non-zero delays, we only repaint once, because
145            // otherwise we would just schedule an immediate repaint _now_,
146            // which would then clear the delay and repaint again.
147            // Hovering a tooltip is a good example of a case where we want to repaint after a delay.
148        }
149
150        if let Ok(predicted_frame_time) = Duration::try_from_secs_f32(viewport.input.predicted_dt) {
151            // Make it less likely we over-shoot the target:
152            delay = delay.saturating_sub(predicted_frame_time);
153        }
154
155        viewport.repaint.causes.push(cause);
156
157        // We save some CPU time by only calling the callback if we need to.
158        // If the new delay is greater or equal to the previous lowest,
159        // it means we have already called the callback, and don't need to do it again.
160        if delay < viewport.repaint.repaint_delay {
161            viewport.repaint.repaint_delay = delay;
162
163            if let Some(callback) = &self.request_repaint_callback {
164                (callback)(RequestRepaintInfo {
165                    viewport_id,
166                    delay,
167                    current_cumulative_pass_nr: viewport.repaint.cumulative_pass_nr,
168                });
169            }
170        }
171    }
172
173    #[must_use]
174    fn requested_immediate_repaint_prev_pass(&self, viewport_id: &ViewportId) -> bool {
175        self.viewports
176            .get(viewport_id)
177            .is_some_and(|v| v.repaint.requested_immediate_repaint_prev_pass())
178    }
179
180    #[must_use]
181    fn has_requested_repaint(&self, viewport_id: &ViewportId) -> bool {
182        self.viewports
183            .get(viewport_id)
184            .is_some_and(|v| 0 < v.repaint.outstanding || v.repaint.repaint_delay < Duration::MAX)
185    }
186}
187
188// ----------------------------------------------------------------------------
189
190/// State stored per viewport.
191///
192/// Mostly for internal use.
193/// Things here may move and change without warning.
194#[derive(Default)]
195pub struct ViewportState {
196    /// The type of viewport.
197    ///
198    /// This will never be [`ViewportClass::Embedded`],
199    /// since those don't result in real viewports.
200    pub class: ViewportClass,
201
202    /// The latest delta
203    pub builder: ViewportBuilder,
204
205    /// The user-code that shows the GUI, used for deferred viewports.
206    ///
207    /// `None` for immediate viewports.
208    pub viewport_ui_cb: Option<Arc<DeferredViewportUiCallback>>,
209
210    pub input: InputState,
211
212    /// State that is collected during a pass and then cleared.
213    pub this_pass: PassState,
214
215    /// The final [`PassState`] from last pass.
216    ///
217    /// Only read from.
218    pub prev_pass: PassState,
219
220    /// Has this viewport been updated this pass?
221    pub used: bool,
222
223    /// State related to repaint scheduling.
224    repaint: ViewportRepaintInfo,
225
226    // ----------------------
227    // Updated at the start of the pass:
228    //
229    /// Which widgets are under the pointer?
230    pub hits: WidgetHits,
231
232    /// What widgets are being interacted with this pass?
233    ///
234    /// Based on the widgets from last pass, and input in this pass.
235    pub interact_widgets: InteractionSnapshot,
236
237    // ----------------------
238    // The output of a pass:
239    //
240    pub graphics: GraphicLayers,
241    // Most of the things in `PlatformOutput` are not actually viewport dependent.
242    pub output: PlatformOutput,
243    pub commands: Vec<ViewportCommand>,
244
245    // ----------------------
246    // Cross-frame statistics:
247    pub num_multipass_in_row: usize,
248}
249
250/// What called [`Context::request_repaint`] or [`Context::request_discard`]?
251#[derive(Clone, PartialEq, Eq, Hash)]
252pub struct RepaintCause {
253    /// What file had the call that requested the repaint?
254    pub file: &'static str,
255
256    /// What line number of the call that requested the repaint?
257    pub line: u32,
258
259    /// Explicit reason; human readable.
260    pub reason: Cow<'static, str>,
261}
262
263impl std::fmt::Debug for RepaintCause {
264    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
265        write!(f, "{}:{} {}", self.file, self.line, self.reason)
266    }
267}
268
269impl std::fmt::Display for RepaintCause {
270    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
271        write!(f, "{}:{} {}", self.file, self.line, self.reason)
272    }
273}
274
275impl RepaintCause {
276    /// Capture the file and line number of the call site.
277    #[expect(clippy::new_without_default)]
278    #[track_caller]
279    pub fn new() -> Self {
280        let caller = Location::caller();
281        Self {
282            file: caller.file(),
283            line: caller.line(),
284            reason: "".into(),
285        }
286    }
287
288    /// Capture the file and line number of the call site,
289    /// as well as add a reason.
290    #[track_caller]
291    pub fn new_reason(reason: impl Into<Cow<'static, str>>) -> Self {
292        let caller = Location::caller();
293        Self {
294            file: caller.file(),
295            line: caller.line(),
296            reason: reason.into(),
297        }
298    }
299}
300
301/// Per-viewport state related to repaint scheduling.
302struct ViewportRepaintInfo {
303    /// Monotonically increasing counter.
304    ///
305    /// Incremented at the end of [`Context::run`].
306    /// This can be smaller than [`Self::cumulative_pass_nr`],
307    /// but never larger.
308    cumulative_frame_nr: u64,
309
310    /// Monotonically increasing counter, counting the number of passes.
311    /// This can be larger than [`Self::cumulative_frame_nr`],
312    /// but never smaller.
313    cumulative_pass_nr: u64,
314
315    /// The duration which the backend will poll for new events
316    /// before forcing another egui update, even if there's no new events.
317    ///
318    /// Also used to suppress multiple calls to the repaint callback during the same pass.
319    ///
320    /// This is also returned in [`crate::ViewportOutput`].
321    repaint_delay: Duration,
322
323    /// While positive, keep requesting repaints. Decrement at the start of each pass.
324    outstanding: u8,
325
326    /// What caused repaints during this pass?
327    causes: Vec<RepaintCause>,
328
329    /// What triggered a repaint the previous pass?
330    /// (i.e: why are we updating now?)
331    prev_causes: Vec<RepaintCause>,
332
333    /// What was the output of `repaint_delay` on the previous pass?
334    ///
335    /// If this was zero, we are repainting as quickly as possible
336    /// (as far as we know).
337    prev_pass_paint_delay: Duration,
338}
339
340impl Default for ViewportRepaintInfo {
341    fn default() -> Self {
342        Self {
343            cumulative_frame_nr: 0,
344            cumulative_pass_nr: 0,
345
346            // We haven't scheduled a repaint yet.
347            repaint_delay: Duration::MAX,
348
349            // Let's run a couple of frames at the start, because why not.
350            outstanding: 1,
351
352            causes: Default::default(),
353            prev_causes: Default::default(),
354
355            prev_pass_paint_delay: Duration::MAX,
356        }
357    }
358}
359
360impl ViewportRepaintInfo {
361    pub fn requested_immediate_repaint_prev_pass(&self) -> bool {
362        self.prev_pass_paint_delay == Duration::ZERO
363    }
364}
365
366// ----------------------------------------------------------------------------
367
368#[derive(Default)]
369struct ContextImpl {
370    fonts: Option<Fonts>,
371    font_definitions: FontDefinitions,
372
373    memory: Memory,
374    animation_manager: AnimationManager,
375
376    plugins: plugin::Plugins,
377    safe_area: SafeAreaInsets,
378
379    /// All viewports share the same texture manager and texture namespace.
380    ///
381    /// In all viewports, [`TextureId::default`] is special, and points to the font atlas.
382    /// The font-atlas texture _may_ be different across viewports, as they may have different
383    /// `pixels_per_point`, so we do special book-keeping for that.
384    /// See <https://github.com/emilk/egui/issues/3664>.
385    tex_manager: WrappedTextureManager,
386
387    /// Set during the pass, becomes active at the start of the next pass.
388    new_zoom_factor: Option<f32>,
389
390    os: OperatingSystem,
391
392    /// How deeply nested are we?
393    viewport_stack: Vec<ViewportIdPair>,
394
395    /// What is the last viewport rendered?
396    last_viewport: ViewportId,
397
398    paint_stats: PaintStats,
399
400    request_repaint_callback: Option<Box<dyn Fn(RequestRepaintInfo) + Send + Sync>>,
401
402    viewport_parents: ViewportIdMap<ViewportId>,
403    viewports: ViewportIdMap<ViewportState>,
404
405    embed_viewports: bool,
406
407    #[cfg(feature = "accesskit")]
408    is_accesskit_enabled: bool,
409
410    loaders: Arc<Loaders>,
411}
412
413impl ContextImpl {
414    fn begin_pass(&mut self, mut new_raw_input: RawInput) {
415        let viewport_id = new_raw_input.viewport_id;
416        let parent_id = new_raw_input
417            .viewports
418            .get(&viewport_id)
419            .and_then(|v| v.parent)
420            .unwrap_or_default();
421        let ids = ViewportIdPair::from_self_and_parent(viewport_id, parent_id);
422
423        if let Some(safe_area) = new_raw_input.safe_area_insets {
424            self.safe_area = safe_area;
425        }
426
427        let is_outermost_viewport = self.viewport_stack.is_empty(); // not necessarily root, just outermost immediate viewport
428        self.viewport_stack.push(ids);
429
430        self.begin_pass_repaint_logic(viewport_id);
431
432        let viewport = self.viewports.entry(viewport_id).or_default();
433
434        if is_outermost_viewport && let Some(new_zoom_factor) = self.new_zoom_factor.take() {
435            let ratio = self.memory.options.zoom_factor / new_zoom_factor;
436            self.memory.options.zoom_factor = new_zoom_factor;
437
438            let input = &viewport.input;
439            // This is a bit hacky, but is required to avoid jitter:
440            let mut rect = input.content_rect();
441            rect.min = (ratio * rect.min.to_vec2()).to_pos2();
442            rect.max = (ratio * rect.max.to_vec2()).to_pos2();
443            new_raw_input.screen_rect = Some(rect);
444            // We should really scale everything else in the input too,
445            // but the `screen_rect` is the most important part.
446        }
447        let native_pixels_per_point = new_raw_input
448            .viewport()
449            .native_pixels_per_point
450            .unwrap_or(1.0);
451        let pixels_per_point = self.memory.options.zoom_factor * native_pixels_per_point;
452
453        let all_viewport_ids: ViewportIdSet = self.all_viewport_ids();
454
455        let viewport = self.viewports.entry(self.viewport_id()).or_default();
456
457        self.memory.begin_pass(&new_raw_input, &all_viewport_ids);
458
459        viewport.input = std::mem::take(&mut viewport.input).begin_pass(
460            new_raw_input,
461            viewport.repaint.requested_immediate_repaint_prev_pass(),
462            pixels_per_point,
463            self.memory.options.input_options,
464        );
465        let repaint_after = viewport.input.wants_repaint_after();
466
467        let content_rect = viewport.input.content_rect();
468
469        viewport.this_pass.begin_pass(content_rect);
470
471        {
472            let mut layers: Vec<LayerId> = viewport.prev_pass.widgets.layer_ids().collect();
473            layers.sort_by(|&a, &b| self.memory.areas().compare_order(a, b));
474
475            viewport.hits = if let Some(pos) = viewport.input.pointer.interact_pos() {
476                let interact_radius = self.memory.options.style().interaction.interact_radius;
477
478                crate::hit_test::hit_test(
479                    &viewport.prev_pass.widgets,
480                    &layers,
481                    &self.memory.to_global,
482                    pos,
483                    interact_radius,
484                )
485            } else {
486                WidgetHits::default()
487            };
488
489            viewport.interact_widgets = crate::interaction::interact(
490                &viewport.interact_widgets,
491                &viewport.prev_pass.widgets,
492                &viewport.hits,
493                &viewport.input,
494                self.memory.interaction_mut(),
495            );
496        }
497
498        // Ensure we register the background area so panels and background ui can catch clicks:
499        self.memory.areas_mut().set_state(
500            LayerId::background(),
501            AreaState {
502                pivot_pos: Some(content_rect.left_top()),
503                pivot: Align2::LEFT_TOP,
504                size: Some(content_rect.size()),
505                interactable: true,
506                last_became_visible_at: None,
507            },
508        );
509
510        #[cfg(feature = "accesskit")]
511        if self.is_accesskit_enabled {
512            profiling::scope!("accesskit");
513            use crate::pass_state::AccessKitPassState;
514            let id = crate::accesskit_root_id();
515            let mut root_node = accesskit::Node::new(accesskit::Role::Window);
516            let pixels_per_point = viewport.input.pixels_per_point();
517            root_node.set_transform(accesskit::Affine::scale(pixels_per_point.into()));
518            let mut nodes = IdMap::default();
519            nodes.insert(id, root_node);
520            viewport.this_pass.accesskit_state = Some(AccessKitPassState {
521                nodes,
522                parent_map: IdMap::default(),
523            });
524        }
525
526        self.update_fonts_mut();
527
528        if let Some(delay) = repaint_after {
529            self.request_repaint_after(delay, viewport_id, RepaintCause::new());
530        }
531    }
532
533    /// Load fonts unless already loaded.
534    fn update_fonts_mut(&mut self) {
535        profiling::function_scope!();
536        let input = &self.viewport().input;
537        let max_texture_side = input.max_texture_side;
538
539        if let Some(font_definitions) = self.memory.new_font_definitions.take() {
540            // New font definition loaded, so we need to reload all fonts.
541            self.fonts = None;
542            self.font_definitions = font_definitions;
543
544            log::trace!("Loading new font definitions");
545        }
546
547        if !self.memory.add_fonts.is_empty() {
548            let fonts = self.memory.add_fonts.drain(..);
549            for font in fonts {
550                self.fonts = None; // recreate all the fonts
551                for family in font.families {
552                    let fam = self
553                        .font_definitions
554                        .families
555                        .entry(family.family)
556                        .or_default();
557                    match family.priority {
558                        FontPriority::Highest => fam.insert(0, font.name.clone()),
559                        FontPriority::Lowest => fam.push(font.name.clone()),
560                    }
561                }
562                self.font_definitions
563                    .font_data
564                    .insert(font.name, Arc::new(font.data));
565            }
566
567            log::trace!("Adding new fonts");
568        }
569
570        let text_alpha_from_coverage = self.memory.options.style().visuals.text_alpha_from_coverage;
571
572        let mut is_new = false;
573
574        let fonts = self.fonts.get_or_insert_with(|| {
575            log::trace!("Creating new Fonts");
576
577            is_new = true;
578            profiling::scope!("Fonts::new");
579            Fonts::new(
580                max_texture_side,
581                text_alpha_from_coverage,
582                self.font_definitions.clone(),
583            )
584        });
585
586        {
587            profiling::scope!("Fonts::begin_pass");
588            fonts.begin_pass(max_texture_side, text_alpha_from_coverage);
589        }
590    }
591
592    #[cfg(feature = "accesskit")]
593    fn accesskit_node_builder(&mut self, id: Id) -> &mut accesskit::Node {
594        let state = self.viewport().this_pass.accesskit_state.as_mut().unwrap();
595        let builders = &mut state.nodes;
596        if let std::collections::hash_map::Entry::Vacant(entry) = builders.entry(id) {
597            entry.insert(Default::default());
598
599            /// Find the first ancestor that already has an accesskit node.
600            fn find_accesskit_parent(
601                parent_map: &IdMap<Id>,
602                node_map: &IdMap<accesskit::Node>,
603                id: Id,
604            ) -> Option<Id> {
605                if let Some(parent_id) = parent_map.get(&id) {
606                    if node_map.contains_key(parent_id) {
607                        Some(*parent_id)
608                    } else {
609                        find_accesskit_parent(parent_map, node_map, *parent_id)
610                    }
611                } else {
612                    None
613                }
614            }
615
616            let parent_id = find_accesskit_parent(&state.parent_map, builders, id)
617                .unwrap_or(crate::accesskit_root_id());
618
619            let parent_builder = builders.get_mut(&parent_id).unwrap();
620            parent_builder.push_child(id.accesskit_id());
621        }
622        builders.get_mut(&id).unwrap()
623    }
624
625    fn pixels_per_point(&mut self) -> f32 {
626        self.viewport().input.pixels_per_point
627    }
628
629    /// Return the `ViewportId` of the current viewport.
630    ///
631    /// For the root viewport this will return [`ViewportId::ROOT`].
632    pub(crate) fn viewport_id(&self) -> ViewportId {
633        self.viewport_stack.last().copied().unwrap_or_default().this
634    }
635
636    /// Return the `ViewportId` of his parent.
637    ///
638    /// For the root viewport this will return [`ViewportId::ROOT`].
639    pub(crate) fn parent_viewport_id(&self) -> ViewportId {
640        let viewport_id = self.viewport_id();
641        *self
642            .viewport_parents
643            .get(&viewport_id)
644            .unwrap_or(&ViewportId::ROOT)
645    }
646
647    fn all_viewport_ids(&self) -> ViewportIdSet {
648        self.viewports
649            .keys()
650            .copied()
651            .chain([ViewportId::ROOT])
652            .collect()
653    }
654
655    /// The current active viewport
656    pub(crate) fn viewport(&mut self) -> &mut ViewportState {
657        self.viewports.entry(self.viewport_id()).or_default()
658    }
659
660    fn viewport_for(&mut self, viewport_id: ViewportId) -> &mut ViewportState {
661        self.viewports.entry(viewport_id).or_default()
662    }
663}
664
665// ----------------------------------------------------------------------------
666
667/// Your handle to egui.
668///
669/// This is the first thing you need when working with egui.
670/// Contains the [`InputState`], [`Memory`], [`PlatformOutput`], and more.
671///
672/// [`Context`] is cheap to clone, and any clones refers to the same mutable data
673/// ([`Context`] uses refcounting internally).
674///
675/// ## Locking
676/// All methods are marked `&self`; [`Context`] has interior mutability protected by an [`RwLock`].
677///
678/// To access parts of a `Context` you need to use some of the helper functions that take closures:
679///
680/// ```
681/// # let ctx = egui::Context::default();
682/// if ctx.input(|i| i.key_pressed(egui::Key::A)) {
683///     ctx.copy_text("Hello!".to_owned());
684/// }
685/// ```
686///
687/// Within such a closure you may NOT recursively lock the same [`Context`], as that can lead to a deadlock.
688/// Therefore it is important that any lock of [`Context`] is short-lived.
689///
690/// These are effectively transactional accesses.
691///
692/// [`Ui`] has many of the same accessor functions, and the same applies there.
693///
694/// ## Example:
695///
696/// ``` no_run
697/// # fn handle_platform_output(_: egui::PlatformOutput) {}
698/// # fn paint(textures_delta: egui::TexturesDelta, _: Vec<egui::ClippedPrimitive>) {}
699/// let mut ctx = egui::Context::default();
700///
701/// // Game loop:
702/// loop {
703///     let raw_input = egui::RawInput::default();
704///     let full_output = ctx.run(raw_input, |ctx| {
705///         egui::CentralPanel::default().show(&ctx, |ui| {
706///             ui.label("Hello world!");
707///             if ui.button("Click me").clicked() {
708///                 // take some action here
709///             }
710///         });
711///     });
712///     handle_platform_output(full_output.platform_output);
713///     let clipped_primitives = ctx.tessellate(full_output.shapes, full_output.pixels_per_point);
714///     paint(full_output.textures_delta, clipped_primitives);
715/// }
716/// ```
717#[derive(Clone)]
718pub struct Context(Arc<RwLock<ContextImpl>>);
719
720impl std::fmt::Debug for Context {
721    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
722        f.debug_struct("Context").finish_non_exhaustive()
723    }
724}
725
726impl std::cmp::PartialEq for Context {
727    fn eq(&self, other: &Self) -> bool {
728        Arc::ptr_eq(&self.0, &other.0)
729    }
730}
731
732impl Default for Context {
733    fn default() -> Self {
734        let ctx_impl = ContextImpl {
735            embed_viewports: true,
736            viewports: std::iter::once((ViewportId::ROOT, ViewportState::default())).collect(),
737            ..Default::default()
738        };
739        let ctx = Self(Arc::new(RwLock::new(ctx_impl)));
740
741        ctx.add_plugin(plugin::CallbackPlugin::default());
742
743        // Register built-in plugins:
744        ctx.add_plugin(crate::debug_text::DebugTextPlugin::default());
745        ctx.add_plugin(crate::text_selection::LabelSelectionState::default());
746        ctx.add_plugin(crate::DragAndDrop::default());
747
748        ctx
749    }
750}
751
752impl Context {
753    /// Do read-only (shared access) transaction on Context
754    fn read<R>(&self, reader: impl FnOnce(&ContextImpl) -> R) -> R {
755        reader(&self.0.read())
756    }
757
758    /// Do read-write (exclusive access) transaction on Context
759    fn write<R>(&self, writer: impl FnOnce(&mut ContextImpl) -> R) -> R {
760        writer(&mut self.0.write())
761    }
762
763    /// Run the ui code for one frame.
764    ///
765    /// At most [`Options::max_passes`] calls will be issued to `run_ui`,
766    /// and only on the rare occasion that [`Context::request_discard`] is called.
767    /// Usually, it `run_ui` will only be called once.
768    ///
769    /// Put your widgets into a [`crate::SidePanel`], [`crate::TopBottomPanel`], [`crate::CentralPanel`], [`crate::Window`] or [`crate::Area`].
770    ///
771    /// Instead of calling `run`, you can alternatively use [`Self::begin_pass`] and [`Context::end_pass`].
772    ///
773    /// ```
774    /// // One egui context that you keep reusing:
775    /// let mut ctx = egui::Context::default();
776    ///
777    /// // Each frame:
778    /// let input = egui::RawInput::default();
779    /// let full_output = ctx.run(input, |ctx| {
780    ///     egui::CentralPanel::default().show(&ctx, |ui| {
781    ///         ui.label("Hello egui!");
782    ///     });
783    /// });
784    /// // handle full_output
785    /// ```
786    #[must_use]
787    pub fn run(&self, mut new_input: RawInput, mut run_ui: impl FnMut(&Self)) -> FullOutput {
788        profiling::function_scope!();
789        let viewport_id = new_input.viewport_id;
790        let max_passes = self.write(|ctx| ctx.memory.options.max_passes.get());
791
792        let mut output = FullOutput::default();
793        debug_assert_eq!(
794            output.platform_output.num_completed_passes, 0,
795            "output must be fresh, but had {} passes",
796            output.platform_output.num_completed_passes
797        );
798
799        loop {
800            profiling::scope!(
801                "pass",
802                output
803                    .platform_output
804                    .num_completed_passes
805                    .to_string()
806                    .as_str()
807            );
808
809            // We must move the `num_passes` (back) to the viewport output so that [`Self::will_discard`]
810            // has access to the latest pass count.
811            self.write(|ctx| {
812                let viewport = ctx.viewport_for(viewport_id);
813                viewport.output.num_completed_passes =
814                    std::mem::take(&mut output.platform_output.num_completed_passes);
815                output.platform_output.request_discard_reasons.clear();
816            });
817
818            self.begin_pass(new_input.take());
819            run_ui(self);
820            output.append(self.end_pass());
821            debug_assert!(
822                0 < output.platform_output.num_completed_passes,
823                "Completed passes was lower than 0, was {}",
824                output.platform_output.num_completed_passes
825            );
826
827            if !output.platform_output.requested_discard() {
828                break; // no need for another pass
829            }
830
831            if max_passes <= output.platform_output.num_completed_passes {
832                log::debug!(
833                    "Ignoring call request_discard, because max_passes={max_passes}. Requested from {:?}",
834                    output.platform_output.request_discard_reasons
835                );
836
837                break;
838            }
839        }
840
841        self.write(|ctx| {
842            let did_multipass = 1 < output.platform_output.num_completed_passes;
843            let viewport = ctx.viewport_for(viewport_id);
844            if did_multipass {
845                viewport.num_multipass_in_row += 1;
846            } else {
847                viewport.num_multipass_in_row = 0;
848            }
849            viewport.repaint.cumulative_frame_nr += 1;
850        });
851
852        output
853    }
854
855    /// An alternative to calling [`Self::run`].
856    ///
857    /// It is usually better to use [`Self::run`], because
858    /// `run` supports multi-pass layout using [`Self::request_discard`].
859    ///
860    /// ```
861    /// // One egui context that you keep reusing:
862    /// let mut ctx = egui::Context::default();
863    ///
864    /// // Each frame:
865    /// let input = egui::RawInput::default();
866    /// ctx.begin_pass(input);
867    ///
868    /// egui::CentralPanel::default().show(&ctx, |ui| {
869    ///     ui.label("Hello egui!");
870    /// });
871    ///
872    /// let full_output = ctx.end_pass();
873    /// // handle full_output
874    /// ```
875    pub fn begin_pass(&self, mut new_input: RawInput) {
876        profiling::function_scope!();
877
878        let plugins = self.read(|ctx| ctx.plugins.ordered_plugins());
879        plugins.on_input(&mut new_input);
880
881        self.write(|ctx| ctx.begin_pass(new_input));
882
883        // Plugins run just after the pass starts:
884        plugins.on_begin_pass(self);
885    }
886
887    /// See [`Self::begin_pass`].
888    #[deprecated = "Renamed begin_pass"]
889    pub fn begin_frame(&self, new_input: RawInput) {
890        self.begin_pass(new_input);
891    }
892}
893
894/// ## Borrows parts of [`Context`]
895/// These functions all lock the [`Context`].
896/// Please see the documentation of [`Context`] for how locking works!
897impl Context {
898    /// Read-only access to [`InputState`].
899    ///
900    /// Note that this locks the [`Context`].
901    ///
902    /// ```
903    /// # let mut ctx = egui::Context::default();
904    /// ctx.input(|i| {
905    ///     // ⚠️ Using `ctx` (even from other `Arc` reference) again here will lead to a deadlock!
906    /// });
907    ///
908    /// if let Some(pos) = ctx.input(|i| i.pointer.hover_pos()) {
909    ///     // This is fine!
910    /// }
911    /// ```
912    #[inline]
913    pub fn input<R>(&self, reader: impl FnOnce(&InputState) -> R) -> R {
914        self.write(move |ctx| reader(&ctx.viewport().input))
915    }
916
917    /// This will create a `InputState::default()` if there is no input state for that viewport
918    #[inline]
919    pub fn input_for<R>(&self, id: ViewportId, reader: impl FnOnce(&InputState) -> R) -> R {
920        self.write(move |ctx| reader(&ctx.viewport_for(id).input))
921    }
922
923    /// Read-write access to [`InputState`].
924    #[inline]
925    pub fn input_mut<R>(&self, writer: impl FnOnce(&mut InputState) -> R) -> R {
926        self.input_mut_for(self.viewport_id(), writer)
927    }
928
929    /// This will create a `InputState::default()` if there is no input state for that viewport
930    #[inline]
931    pub fn input_mut_for<R>(&self, id: ViewportId, writer: impl FnOnce(&mut InputState) -> R) -> R {
932        self.write(move |ctx| writer(&mut ctx.viewport_for(id).input))
933    }
934
935    /// Read-only access to [`Memory`].
936    #[inline]
937    pub fn memory<R>(&self, reader: impl FnOnce(&Memory) -> R) -> R {
938        self.read(move |ctx| reader(&ctx.memory))
939    }
940
941    /// Read-write access to [`Memory`].
942    #[inline]
943    pub fn memory_mut<R>(&self, writer: impl FnOnce(&mut Memory) -> R) -> R {
944        self.write(move |ctx| writer(&mut ctx.memory))
945    }
946
947    /// Read-only access to [`IdTypeMap`], which stores superficial widget state.
948    #[inline]
949    pub fn data<R>(&self, reader: impl FnOnce(&IdTypeMap) -> R) -> R {
950        self.read(move |ctx| reader(&ctx.memory.data))
951    }
952
953    /// Read-write access to [`IdTypeMap`], which stores superficial widget state.
954    #[inline]
955    pub fn data_mut<R>(&self, writer: impl FnOnce(&mut IdTypeMap) -> R) -> R {
956        self.write(move |ctx| writer(&mut ctx.memory.data))
957    }
958
959    /// Read-write access to [`GraphicLayers`], where painted [`crate::Shape`]s are written to.
960    #[inline]
961    pub fn graphics_mut<R>(&self, writer: impl FnOnce(&mut GraphicLayers) -> R) -> R {
962        self.write(move |ctx| writer(&mut ctx.viewport().graphics))
963    }
964
965    /// Read-only access to [`GraphicLayers`], where painted [`crate::Shape`]s are written to.
966    #[inline]
967    pub fn graphics<R>(&self, reader: impl FnOnce(&GraphicLayers) -> R) -> R {
968        self.write(move |ctx| reader(&ctx.viewport().graphics))
969    }
970
971    /// Read-only access to [`PlatformOutput`].
972    ///
973    /// This is what egui outputs each pass and frame.
974    ///
975    /// ```
976    /// # let mut ctx = egui::Context::default();
977    /// ctx.output_mut(|o| o.cursor_icon = egui::CursorIcon::Progress);
978    /// ```
979    #[inline]
980    pub fn output<R>(&self, reader: impl FnOnce(&PlatformOutput) -> R) -> R {
981        self.write(move |ctx| reader(&ctx.viewport().output))
982    }
983
984    /// Read-write access to [`PlatformOutput`].
985    #[inline]
986    pub fn output_mut<R>(&self, writer: impl FnOnce(&mut PlatformOutput) -> R) -> R {
987        self.write(move |ctx| writer(&mut ctx.viewport().output))
988    }
989
990    /// Read-only access to [`PassState`].
991    ///
992    /// This is only valid during the call to [`Self::run`] (between [`Self::begin_pass`] and [`Self::end_pass`]).
993    #[inline]
994    pub(crate) fn pass_state<R>(&self, reader: impl FnOnce(&PassState) -> R) -> R {
995        self.write(move |ctx| reader(&ctx.viewport().this_pass))
996    }
997
998    /// Read-write access to [`PassState`].
999    ///
1000    /// This is only valid during the call to [`Self::run`] (between [`Self::begin_pass`] and [`Self::end_pass`]).
1001    #[inline]
1002    pub(crate) fn pass_state_mut<R>(&self, writer: impl FnOnce(&mut PassState) -> R) -> R {
1003        self.write(move |ctx| writer(&mut ctx.viewport().this_pass))
1004    }
1005
1006    /// Read-only access to the [`PassState`] from the previous pass.
1007    ///
1008    /// This is swapped at the end of each pass.
1009    #[inline]
1010    pub(crate) fn prev_pass_state<R>(&self, reader: impl FnOnce(&PassState) -> R) -> R {
1011        self.write(move |ctx| reader(&ctx.viewport().prev_pass))
1012    }
1013
1014    /// Read-only access to [`Fonts`].
1015    ///
1016    /// Not valid until first call to [`Context::run()`].
1017    /// That's because since we don't know the proper `pixels_per_point` until then.
1018    #[inline]
1019    pub fn fonts<R>(&self, reader: impl FnOnce(&FontsView<'_>) -> R) -> R {
1020        self.write(move |ctx| {
1021            let pixels_per_point = ctx.pixels_per_point();
1022            reader(
1023                &ctx.fonts
1024                    .as_mut()
1025                    .expect("No fonts available until first call to Context::run()")
1026                    .with_pixels_per_point(pixels_per_point),
1027            )
1028        })
1029    }
1030
1031    /// Read-write access to [`Fonts`].
1032    ///
1033    /// Not valid until first call to [`Context::run()`].
1034    /// That's because since we don't know the proper `pixels_per_point` until then.
1035    #[inline]
1036    pub fn fonts_mut<R>(&self, reader: impl FnOnce(&mut FontsView<'_>) -> R) -> R {
1037        self.write(move |ctx| {
1038            let pixels_per_point = ctx.pixels_per_point();
1039            reader(
1040                &mut ctx
1041                    .fonts
1042                    .as_mut()
1043                    .expect("No fonts available until first call to Context::run()")
1044                    .with_pixels_per_point(pixels_per_point),
1045            )
1046        })
1047    }
1048
1049    /// Read-only access to [`Options`].
1050    #[inline]
1051    pub fn options<R>(&self, reader: impl FnOnce(&Options) -> R) -> R {
1052        self.read(move |ctx| reader(&ctx.memory.options))
1053    }
1054
1055    /// Read-write access to [`Options`].
1056    #[inline]
1057    pub fn options_mut<R>(&self, writer: impl FnOnce(&mut Options) -> R) -> R {
1058        self.write(move |ctx| writer(&mut ctx.memory.options))
1059    }
1060
1061    /// Read-only access to [`TessellationOptions`].
1062    #[inline]
1063    pub fn tessellation_options<R>(&self, reader: impl FnOnce(&TessellationOptions) -> R) -> R {
1064        self.read(move |ctx| reader(&ctx.memory.options.tessellation_options))
1065    }
1066
1067    /// Read-write access to [`TessellationOptions`].
1068    #[inline]
1069    pub fn tessellation_options_mut<R>(
1070        &self,
1071        writer: impl FnOnce(&mut TessellationOptions) -> R,
1072    ) -> R {
1073        self.write(move |ctx| writer(&mut ctx.memory.options.tessellation_options))
1074    }
1075
1076    /// If the given [`Id`] has been used previously the same pass at different position,
1077    /// then an error will be printed on screen.
1078    ///
1079    /// This function is already called for all widgets that do any interaction,
1080    /// but you can call this from widgets that store state but that does not interact.
1081    ///
1082    /// The given [`Rect`] should be approximately where the widget will be.
1083    /// The most important thing is that [`Rect::min`] is approximately correct,
1084    /// because that's where the warning will be painted. If you don't know what size to pick, just pick [`Vec2::ZERO`].
1085    pub fn check_for_id_clash(&self, id: Id, new_rect: Rect, what: &str) {
1086        let prev_rect = self.pass_state_mut(move |state| state.used_ids.insert(id, new_rect));
1087
1088        if !self.options(|opt| opt.warn_on_id_clash) {
1089            return;
1090        }
1091
1092        let Some(prev_rect) = prev_rect else { return };
1093
1094        // It is ok to reuse the same ID for e.g. a frame around a widget,
1095        // or to check for interaction with the same widget twice:
1096        let is_same_rect = prev_rect.expand(0.1).contains_rect(new_rect)
1097            || new_rect.expand(0.1).contains_rect(prev_rect);
1098        if is_same_rect {
1099            return;
1100        }
1101
1102        let show_error = |widget_rect: Rect, text: String| {
1103            let content_rect = self.content_rect();
1104
1105            let text = format!("🔥 {text}");
1106            let color = self.style().visuals.error_fg_color;
1107            let painter = self.debug_painter();
1108            painter.rect_stroke(widget_rect, 0.0, (1.0, color), StrokeKind::Outside);
1109
1110            let below = widget_rect.bottom() + 32.0 < content_rect.bottom();
1111
1112            let text_rect = if below {
1113                painter.debug_text(
1114                    widget_rect.left_bottom() + vec2(0.0, 2.0),
1115                    Align2::LEFT_TOP,
1116                    color,
1117                    text,
1118                )
1119            } else {
1120                painter.debug_text(
1121                    widget_rect.left_top() - vec2(0.0, 2.0),
1122                    Align2::LEFT_BOTTOM,
1123                    color,
1124                    text,
1125                )
1126            };
1127
1128            if let Some(pointer_pos) = self.pointer_hover_pos()
1129                && text_rect.contains(pointer_pos)
1130            {
1131                let tooltip_pos = if below {
1132                    text_rect.left_bottom() + vec2(2.0, 4.0)
1133                } else {
1134                    text_rect.left_top() + vec2(2.0, -4.0)
1135                };
1136
1137                painter.error(
1138                        tooltip_pos,
1139                        format!("Widget is {} this text.\n\n\
1140                             ID clashes happens when things like Windows or CollapsingHeaders share names,\n\
1141                             or when things like Plot and Grid:s aren't given unique id_salt:s.\n\n\
1142                             Sometimes the solution is to use ui.push_id.",
1143                                if below { "above" } else { "below" }),
1144                    );
1145            }
1146        };
1147
1148        let id_str = id.short_debug_format();
1149
1150        if prev_rect.min.distance(new_rect.min) < 4.0 {
1151            show_error(new_rect, format!("Double use of {what} ID {id_str}"));
1152        } else {
1153            show_error(prev_rect, format!("First use of {what} ID {id_str}"));
1154            show_error(new_rect, format!("Second use of {what} ID {id_str}"));
1155        }
1156    }
1157
1158    // ---------------------------------------------------------------------
1159
1160    /// Create a widget and check for interaction.
1161    ///
1162    /// If this is not called, the widget doesn't exist.
1163    ///
1164    /// You should use [`Ui::interact`] instead.
1165    ///
1166    /// If the widget already exists, its state (sense, Rect, etc) will be updated.
1167    ///
1168    /// `allow_focus` should usually be true, unless you call this function multiple times with the
1169    /// same widget, then `allow_focus` should only be true once (like in [`Ui::new`] (true) and [`Ui::remember_min_rect`] (false)).
1170    pub(crate) fn create_widget(&self, w: WidgetRect, allow_focus: bool) -> Response {
1171        let interested_in_focus = w.enabled
1172            && w.sense.is_focusable()
1173            && self.memory(|mem| mem.allows_interaction(w.layer_id));
1174
1175        // Remember this widget
1176        self.write(|ctx| {
1177            let viewport = ctx.viewport();
1178
1179            // We add all widgets here, even non-interactive ones,
1180            // because we need this list not only for checking for blocking widgets,
1181            // but also to know when we have reached the widget we are checking for cover.
1182            viewport.this_pass.widgets.insert(w.layer_id, w);
1183
1184            if allow_focus && interested_in_focus {
1185                ctx.memory.interested_in_focus(w.id, w.layer_id);
1186            }
1187        });
1188
1189        if allow_focus && !interested_in_focus {
1190            // Not interested or allowed input:
1191            self.memory_mut(|mem| mem.surrender_focus(w.id));
1192        }
1193
1194        if w.sense.interactive() || w.sense.is_focusable() {
1195            self.check_for_id_clash(w.id, w.rect, "widget");
1196        }
1197
1198        #[allow(clippy::let_and_return, clippy::allow_attributes)]
1199        let res = self.get_response(w);
1200
1201        #[cfg(feature = "accesskit")]
1202        if allow_focus && w.sense.is_focusable() {
1203            // Make sure anything that can receive focus has an AccessKit node.
1204            // TODO(mwcampbell): For nodes that are filled from widget info,
1205            // some information is written to the node twice.
1206            self.accesskit_node_builder(w.id, |builder| res.fill_accesskit_node_common(builder));
1207        }
1208
1209        #[cfg(feature = "accesskit")]
1210        self.write(|ctx| {
1211            use crate::{Align, pass_state::ScrollTarget, style::ScrollAnimation};
1212            let viewport = ctx.viewport_for(ctx.viewport_id());
1213
1214            viewport
1215                .input
1216                .consume_accesskit_action_requests(res.id, |request| {
1217                    // TODO(lucasmerlin): Correctly handle the scroll unit:
1218                    // https://github.com/AccessKit/accesskit/blob/e639c0e0d8ccbfd9dff302d972fa06f9766d608e/common/src/lib.rs#L2621
1219                    const DISTANCE: f32 = 100.0;
1220
1221                    match &request.action {
1222                        accesskit::Action::ScrollIntoView => {
1223                            viewport.this_pass.scroll_target = [
1224                                Some(ScrollTarget::new(
1225                                    res.rect.x_range(),
1226                                    Some(Align::Center),
1227                                    ScrollAnimation::none(),
1228                                )),
1229                                Some(ScrollTarget::new(
1230                                    res.rect.y_range(),
1231                                    Some(Align::Center),
1232                                    ScrollAnimation::none(),
1233                                )),
1234                            ];
1235                        }
1236                        accesskit::Action::ScrollDown => {
1237                            viewport.this_pass.scroll_delta.0 += DISTANCE * Vec2::UP;
1238                        }
1239                        accesskit::Action::ScrollUp => {
1240                            viewport.this_pass.scroll_delta.0 += DISTANCE * Vec2::DOWN;
1241                        }
1242                        accesskit::Action::ScrollLeft => {
1243                            viewport.this_pass.scroll_delta.0 += DISTANCE * Vec2::LEFT;
1244                        }
1245                        accesskit::Action::ScrollRight => {
1246                            viewport.this_pass.scroll_delta.0 += DISTANCE * Vec2::RIGHT;
1247                        }
1248                        _ => return false,
1249                    }
1250                    true
1251                });
1252        });
1253
1254        res
1255    }
1256
1257    /// Read the response of some widget, which may be called _before_ creating the widget (!).
1258    ///
1259    /// This is because widget interaction happens at the start of the pass, using the widget rects from the previous pass.
1260    ///
1261    /// If the widget was not visible the previous pass (or this pass), this will return `None`.
1262    ///
1263    /// If you try to read a [`Ui`]'s response, while still inside, this will return the [`Rect`] from the previous frame.
1264    pub fn read_response(&self, id: Id) -> Option<Response> {
1265        self.write(|ctx| {
1266            let viewport = ctx.viewport();
1267            let widget_rect = viewport
1268                .this_pass
1269                .widgets
1270                .get(id)
1271                .or_else(|| viewport.prev_pass.widgets.get(id))
1272                .copied();
1273            widget_rect.map(|mut rect| {
1274                // If the Rect is invalid the Ui hasn't registered its final Rect yet.
1275                // We return the Rect from last frame instead.
1276                if !(rect.rect.is_positive() && rect.rect.is_finite())
1277                    && let Some(prev_rect) = viewport.prev_pass.widgets.get(id)
1278                {
1279                    rect.rect = prev_rect.rect;
1280                }
1281                rect
1282            })
1283        })
1284        .map(|widget_rect| self.get_response(widget_rect))
1285    }
1286
1287    /// Do all interaction for an existing widget, without (re-)registering it.
1288    pub(crate) fn get_response(&self, widget_rect: WidgetRect) -> Response {
1289        use response::Flags;
1290
1291        let WidgetRect {
1292            id,
1293            layer_id,
1294            rect,
1295            interact_rect,
1296            sense,
1297            enabled,
1298        } = widget_rect;
1299
1300        // previous pass + "highlight next pass" == "highlight this pass"
1301        let highlighted = self.prev_pass_state(|fs| fs.highlight_next_pass.contains(&id));
1302
1303        let mut res = Response {
1304            ctx: self.clone(),
1305            layer_id,
1306            id,
1307            rect,
1308            interact_rect,
1309            sense,
1310            flags: Flags::empty(),
1311            interact_pointer_pos: None,
1312            intrinsic_size: None,
1313        };
1314
1315        res.flags.set(Flags::ENABLED, enabled);
1316        res.flags.set(Flags::HIGHLIGHTED, highlighted);
1317
1318        self.write(|ctx| {
1319            let viewport = ctx.viewports.entry(ctx.viewport_id()).or_default();
1320
1321            res.flags.set(
1322                Flags::CONTAINS_POINTER,
1323                viewport.interact_widgets.contains_pointer.contains(&id),
1324            );
1325
1326            let input = &viewport.input;
1327            let memory = &mut ctx.memory;
1328
1329            if enabled
1330                && sense.senses_click()
1331                && memory.has_focus(id)
1332                && (input.key_pressed(Key::Space) || input.key_pressed(Key::Enter))
1333            {
1334                // Space/enter works like a primary click for e.g. selected buttons
1335                res.flags.set(Flags::FAKE_PRIMARY_CLICKED, true);
1336            }
1337
1338            #[cfg(feature = "accesskit")]
1339            if enabled
1340                && sense.senses_click()
1341                && input.has_accesskit_action_request(id, accesskit::Action::Click)
1342            {
1343                res.flags.set(Flags::FAKE_PRIMARY_CLICKED, true);
1344            }
1345
1346            if enabled && sense.senses_click() && Some(id) == viewport.interact_widgets.long_touched
1347            {
1348                res.flags.set(Flags::LONG_TOUCHED, true);
1349            }
1350
1351            let interaction = memory.interaction();
1352
1353            res.flags.set(
1354                Flags::IS_POINTER_BUTTON_DOWN_ON,
1355                interaction.potential_click_id == Some(id)
1356                    || interaction.potential_drag_id == Some(id),
1357            );
1358
1359            if res.enabled() {
1360                res.flags.set(
1361                    Flags::HOVERED,
1362                    viewport.interact_widgets.hovered.contains(&id),
1363                );
1364                res.flags.set(
1365                    Flags::DRAGGED,
1366                    Some(id) == viewport.interact_widgets.dragged,
1367                );
1368                res.flags.set(
1369                    Flags::DRAG_STARTED,
1370                    Some(id) == viewport.interact_widgets.drag_started,
1371                );
1372                res.flags.set(
1373                    Flags::DRAG_STOPPED,
1374                    Some(id) == viewport.interact_widgets.drag_stopped,
1375                );
1376            }
1377
1378            let clicked = Some(id) == viewport.interact_widgets.clicked;
1379            let mut any_press = false;
1380
1381            for pointer_event in &input.pointer.pointer_events {
1382                match pointer_event {
1383                    PointerEvent::Moved(_) => {}
1384                    PointerEvent::Pressed { .. } => {
1385                        any_press = true;
1386                    }
1387                    PointerEvent::Released { click, .. } => {
1388                        if enabled && sense.senses_click() && clicked && click.is_some() {
1389                            res.flags.set(Flags::CLICKED, true);
1390                        }
1391
1392                        res.flags.set(Flags::IS_POINTER_BUTTON_DOWN_ON, false);
1393                        res.flags.set(Flags::DRAGGED, false);
1394                    }
1395                }
1396            }
1397
1398            // is_pointer_button_down_on is false when released, but we want interact_pointer_pos
1399            // to still work.
1400            let is_interacted_with = res.is_pointer_button_down_on()
1401                || res.long_touched()
1402                || clicked
1403                || res.drag_stopped();
1404            if is_interacted_with {
1405                res.interact_pointer_pos = input.pointer.interact_pos();
1406                if let (Some(to_global), Some(pos)) = (
1407                    memory.to_global.get(&res.layer_id),
1408                    &mut res.interact_pointer_pos,
1409                ) {
1410                    *pos = to_global.inverse() * *pos;
1411                }
1412            }
1413
1414            if input.pointer.any_down() && !is_interacted_with {
1415                // We don't hover widgets while interacting with *other* widgets:
1416                res.flags.set(Flags::HOVERED, false);
1417            }
1418
1419            let should_surrender_focus = match memory.options.input_options.surrender_focus_on {
1420                SurrenderFocusOn::Presses => any_press,
1421                SurrenderFocusOn::Clicks => input.pointer.any_click(),
1422                SurrenderFocusOn::Never => false,
1423            };
1424
1425            let pointer_clicked_elsewhere = should_surrender_focus && !res.hovered();
1426            if pointer_clicked_elsewhere && memory.has_focus(id) {
1427                memory.surrender_focus(id);
1428            }
1429        });
1430
1431        res
1432    }
1433
1434    /// This is called by [`Response::widget_info`], but can also be called directly.
1435    ///
1436    /// With some debug flags it will store the widget info in [`crate::WidgetRects`] for later display.
1437    #[inline]
1438    pub fn register_widget_info(&self, id: Id, make_info: impl Fn() -> crate::WidgetInfo) {
1439        #[cfg(debug_assertions)]
1440        self.write(|ctx| {
1441            if ctx.memory.options.style().debug.show_interactive_widgets {
1442                ctx.viewport().this_pass.widgets.set_info(id, make_info());
1443            }
1444        });
1445
1446        #[cfg(not(debug_assertions))]
1447        {
1448            _ = (self, id, make_info);
1449        }
1450    }
1451
1452    /// Get a full-screen painter for a new or existing layer
1453    pub fn layer_painter(&self, layer_id: LayerId) -> Painter {
1454        let content_rect = self.content_rect();
1455        Painter::new(self.clone(), layer_id, content_rect)
1456    }
1457
1458    /// Paint on top of everything else
1459    pub fn debug_painter(&self) -> Painter {
1460        Self::layer_painter(self, LayerId::debug())
1461    }
1462
1463    /// Print this text next to the cursor at the end of the pass.
1464    ///
1465    /// If you call this multiple times, the text will be appended.
1466    ///
1467    /// This only works if compiled with `debug_assertions`.
1468    ///
1469    /// ```
1470    /// # let ctx = egui::Context::default();
1471    /// # let state = true;
1472    /// ctx.debug_text(format!("State: {state:?}"));
1473    /// ```
1474    ///
1475    /// This is just a convenience for calling [`crate::debug_text::print`].
1476    #[track_caller]
1477    pub fn debug_text(&self, text: impl Into<WidgetText>) {
1478        crate::debug_text::print(self, text);
1479    }
1480
1481    /// What operating system are we running on?
1482    ///
1483    /// When compiling natively, this is
1484    /// figured out from the `target_os`.
1485    ///
1486    /// For web, this can be figured out from the user-agent,
1487    /// and is done so by [`eframe`](https://github.com/emilk/egui/tree/main/crates/eframe).
1488    pub fn os(&self) -> OperatingSystem {
1489        self.read(|ctx| ctx.os)
1490    }
1491
1492    /// Set the operating system we are running on.
1493    ///
1494    /// If you are writing wasm-based integration for egui you
1495    /// may want to set this based on e.g. the user-agent.
1496    pub fn set_os(&self, os: OperatingSystem) {
1497        self.write(|ctx| ctx.os = os);
1498    }
1499
1500    /// Set the cursor icon.
1501    ///
1502    /// Equivalent to:
1503    /// ```
1504    /// # let ctx = egui::Context::default();
1505    /// ctx.output_mut(|o| o.cursor_icon = egui::CursorIcon::PointingHand);
1506    /// ```
1507    pub fn set_cursor_icon(&self, cursor_icon: CursorIcon) {
1508        self.output_mut(|o| o.cursor_icon = cursor_icon);
1509    }
1510
1511    /// Add a command to [`PlatformOutput::commands`],
1512    /// for the integration to execute at the end of the frame.
1513    pub fn send_cmd(&self, cmd: crate::OutputCommand) {
1514        self.output_mut(|o| o.commands.push(cmd));
1515    }
1516
1517    /// Open an URL in a browser.
1518    ///
1519    /// Equivalent to:
1520    /// ```
1521    /// # let ctx = egui::Context::default();
1522    /// # let open_url = egui::OpenUrl::same_tab("http://www.example.com");
1523    /// ctx.send_cmd(egui::OutputCommand::OpenUrl(open_url));
1524    /// ```
1525    pub fn open_url(&self, open_url: crate::OpenUrl) {
1526        self.send_cmd(crate::OutputCommand::OpenUrl(open_url));
1527    }
1528
1529    /// Copy the given text to the system clipboard.
1530    ///
1531    /// Note that in web applications, the clipboard is only accessible in secure contexts (e.g.,
1532    /// HTTPS or localhost). If this method is used outside of a secure context, it will log an
1533    /// error and do nothing. See <https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts>.
1534    pub fn copy_text(&self, text: String) {
1535        self.send_cmd(crate::OutputCommand::CopyText(text));
1536    }
1537
1538    /// Copy the given image to the system clipboard.
1539    ///
1540    /// Note that in web applications, the clipboard is only accessible in secure contexts (e.g.,
1541    /// HTTPS or localhost). If this method is used outside of a secure context, it will log an
1542    /// error and do nothing. See <https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts>.
1543    pub fn copy_image(&self, image: crate::ColorImage) {
1544        self.send_cmd(crate::OutputCommand::CopyImage(image));
1545    }
1546
1547    fn can_show_modifier_symbols(&self) -> bool {
1548        let ModifierNames {
1549            alt,
1550            ctrl,
1551            shift,
1552            mac_cmd,
1553            ..
1554        } = ModifierNames::SYMBOLS;
1555
1556        let font_id = TextStyle::Body.resolve(&self.style());
1557        self.fonts_mut(|f| {
1558            let mut font = f.fonts.font(&font_id.family);
1559            font.has_glyphs(alt)
1560                && font.has_glyphs(ctrl)
1561                && font.has_glyphs(shift)
1562                && font.has_glyphs(mac_cmd)
1563        })
1564    }
1565
1566    /// Format the given modifiers in a human-readable way (e.g. `Ctrl+Shift+X`).
1567    pub fn format_modifiers(&self, modifiers: Modifiers) -> String {
1568        let os = self.os();
1569
1570        let is_mac = os.is_mac();
1571
1572        if is_mac && self.can_show_modifier_symbols() {
1573            ModifierNames::SYMBOLS.format(&modifiers, is_mac)
1574        } else {
1575            ModifierNames::NAMES.format(&modifiers, is_mac)
1576        }
1577    }
1578
1579    /// Format the given shortcut in a human-readable way (e.g. `Ctrl+Shift+X`).
1580    ///
1581    /// Can be used to get the text for [`crate::Button::shortcut_text`].
1582    pub fn format_shortcut(&self, shortcut: &KeyboardShortcut) -> String {
1583        let os = self.os();
1584
1585        let is_mac = os.is_mac();
1586
1587        if is_mac && self.can_show_modifier_symbols() {
1588            shortcut.format(&ModifierNames::SYMBOLS, is_mac)
1589        } else {
1590            shortcut.format(&ModifierNames::NAMES, is_mac)
1591        }
1592    }
1593
1594    /// The total number of completed frames.
1595    ///
1596    /// Starts at zero, and is incremented once at the end of each call to [`Self::run`].
1597    ///
1598    /// This is always smaller or equal to [`Self::cumulative_pass_nr`].
1599    pub fn cumulative_frame_nr(&self) -> u64 {
1600        self.cumulative_frame_nr_for(self.viewport_id())
1601    }
1602
1603    /// The total number of completed frames.
1604    ///
1605    /// Starts at zero, and is incremented once at the end of each call to [`Self::run`].
1606    ///
1607    /// This is always smaller or equal to [`Self::cumulative_pass_nr_for`].
1608    pub fn cumulative_frame_nr_for(&self, id: ViewportId) -> u64 {
1609        self.read(|ctx| {
1610            ctx.viewports
1611                .get(&id)
1612                .map(|v| v.repaint.cumulative_frame_nr)
1613                .unwrap_or_else(|| {
1614                    if cfg!(debug_assertions) {
1615                        panic!("cumulative_frame_nr_for failed to find the viewport {id:?}");
1616                    } else {
1617                        0
1618                    }
1619                })
1620        })
1621    }
1622
1623    /// The total number of completed passes (usually there is one pass per rendered frame).
1624    ///
1625    /// Starts at zero, and is incremented for each completed pass inside of [`Self::run`] (usually once).
1626    ///
1627    /// If you instead want to know which pass index this is within the current frame,
1628    /// use [`Self::current_pass_index`].
1629    pub fn cumulative_pass_nr(&self) -> u64 {
1630        self.cumulative_pass_nr_for(self.viewport_id())
1631    }
1632
1633    /// The total number of completed passes (usually there is one pass per rendered frame).
1634    ///
1635    /// Starts at zero, and is incremented for each completed pass inside of [`Self::run`] (usually once).
1636    pub fn cumulative_pass_nr_for(&self, id: ViewportId) -> u64 {
1637        self.read(|ctx| {
1638            ctx.viewports
1639                .get(&id)
1640                .map_or(0, |v| v.repaint.cumulative_pass_nr)
1641        })
1642    }
1643
1644    /// The index of the current pass in the current frame, starting at zero.
1645    ///
1646    /// Usually this is zero, but if something called [`Self::request_discard`] to do multi-pass layout,
1647    /// then this will be incremented for each pass.
1648    ///
1649    /// This just reads the value of [`PlatformOutput::num_completed_passes`].
1650    ///
1651    /// To know the total number of passes ever completed, use [`Self::cumulative_pass_nr`].
1652    pub fn current_pass_index(&self) -> usize {
1653        self.output(|o| o.num_completed_passes)
1654    }
1655
1656    /// Call this if there is need to repaint the UI, i.e. if you are showing an animation.
1657    ///
1658    /// If this is called at least once in a frame, then there will be another frame right after this.
1659    /// Call as many times as you wish, only one repaint will be issued.
1660    ///
1661    /// To request repaint with a delay, use [`Self::request_repaint_after`].
1662    ///
1663    /// If called from outside the UI thread, the UI thread will wake up and run,
1664    /// provided the egui integration has set that up via [`Self::set_request_repaint_callback`]
1665    /// (this will work on `eframe`).
1666    ///
1667    /// This will repaint the current viewport.
1668    #[track_caller]
1669    pub fn request_repaint(&self) {
1670        self.request_repaint_of(self.viewport_id());
1671    }
1672
1673    /// Call this if there is need to repaint the UI, i.e. if you are showing an animation.
1674    ///
1675    /// If this is called at least once in a frame, then there will be another frame right after this.
1676    /// Call as many times as you wish, only one repaint will be issued.
1677    ///
1678    /// To request repaint with a delay, use [`Self::request_repaint_after_for`].
1679    ///
1680    /// If called from outside the UI thread, the UI thread will wake up and run,
1681    /// provided the egui integration has set that up via [`Self::set_request_repaint_callback`]
1682    /// (this will work on `eframe`).
1683    ///
1684    /// This will repaint the specified viewport.
1685    #[track_caller]
1686    pub fn request_repaint_of(&self, id: ViewportId) {
1687        let cause = RepaintCause::new();
1688        self.write(|ctx| ctx.request_repaint(id, cause));
1689    }
1690
1691    /// Request repaint after at most the specified duration elapses.
1692    ///
1693    /// The backend can chose to repaint sooner, for instance if some other code called
1694    /// this method with a lower duration, or if new events arrived.
1695    ///
1696    /// The function can be multiple times, but only the *smallest* duration will be considered.
1697    /// So, if the function is called two times with `1 second` and `2 seconds`, egui will repaint
1698    /// after `1 second`
1699    ///
1700    /// This is primarily useful for applications who would like to save battery by avoiding wasted
1701    /// redraws when the app is not in focus. But sometimes the GUI of the app might become stale
1702    /// and outdated if it is not updated for too long.
1703    ///
1704    /// Let's say, something like a stopwatch widget that displays the time in seconds. You would waste
1705    /// resources repainting multiple times within the same second (when you have no input),
1706    /// just calculate the difference of duration between current time and next second change,
1707    /// and call this function, to make sure that you are displaying the latest updated time, but
1708    /// not wasting resources on needless repaints within the same second.
1709    ///
1710    /// ### Quirk:
1711    /// Duration begins at the next frame. Let's say for example that it's a very inefficient app
1712    /// and takes 500 milliseconds per frame at 2 fps. The widget / user might want a repaint in
1713    /// next 500 milliseconds. Now, app takes 1000 ms per frame (1 fps) because the backend event
1714    /// timeout takes 500 milliseconds AFTER the vsync swap buffer.
1715    /// So, it's not that we are requesting repaint within X duration. We are rather timing out
1716    /// during app idle time where we are not receiving any new input events.
1717    ///
1718    /// This repaints the current viewport.
1719    #[track_caller]
1720    pub fn request_repaint_after(&self, duration: Duration) {
1721        self.request_repaint_after_for(duration, self.viewport_id());
1722    }
1723
1724    /// Repaint after this many seconds.
1725    ///
1726    /// See [`Self::request_repaint_after`] for details.
1727    #[track_caller]
1728    pub fn request_repaint_after_secs(&self, seconds: f32) {
1729        if let Ok(duration) = std::time::Duration::try_from_secs_f32(seconds) {
1730            self.request_repaint_after(duration);
1731        }
1732    }
1733
1734    /// Request repaint after at most the specified duration elapses.
1735    ///
1736    /// The backend can chose to repaint sooner, for instance if some other code called
1737    /// this method with a lower duration, or if new events arrived.
1738    ///
1739    /// The function can be multiple times, but only the *smallest* duration will be considered.
1740    /// So, if the function is called two times with `1 second` and `2 seconds`, egui will repaint
1741    /// after `1 second`
1742    ///
1743    /// This is primarily useful for applications who would like to save battery by avoiding wasted
1744    /// redraws when the app is not in focus. But sometimes the GUI of the app might become stale
1745    /// and outdated if it is not updated for too long.
1746    ///
1747    /// Let's say, something like a stopwatch widget that displays the time in seconds. You would waste
1748    /// resources repainting multiple times within the same second (when you have no input),
1749    /// just calculate the difference of duration between current time and next second change,
1750    /// and call this function, to make sure that you are displaying the latest updated time, but
1751    /// not wasting resources on needless repaints within the same second.
1752    ///
1753    /// ### Quirk:
1754    /// Duration begins at the next frame. Let's say for example that it's a very inefficient app
1755    /// and takes 500 milliseconds per frame at 2 fps. The widget / user might want a repaint in
1756    /// next 500 milliseconds. Now, app takes 1000 ms per frame (1 fps) because the backend event
1757    /// timeout takes 500 milliseconds AFTER the vsync swap buffer.
1758    /// So, it's not that we are requesting repaint within X duration. We are rather timing out
1759    /// during app idle time where we are not receiving any new input events.
1760    ///
1761    /// This repaints the specified viewport.
1762    #[track_caller]
1763    pub fn request_repaint_after_for(&self, duration: Duration, id: ViewportId) {
1764        let cause = RepaintCause::new();
1765        self.write(|ctx| ctx.request_repaint_after(duration, id, cause));
1766    }
1767
1768    /// Was a repaint requested last pass for the current viewport?
1769    #[must_use]
1770    pub fn requested_repaint_last_pass(&self) -> bool {
1771        self.requested_repaint_last_pass_for(&self.viewport_id())
1772    }
1773
1774    /// Was a repaint requested last pass for the given viewport?
1775    #[must_use]
1776    pub fn requested_repaint_last_pass_for(&self, viewport_id: &ViewportId) -> bool {
1777        self.read(|ctx| ctx.requested_immediate_repaint_prev_pass(viewport_id))
1778    }
1779
1780    /// Has a repaint been requested for the current viewport?
1781    #[must_use]
1782    pub fn has_requested_repaint(&self) -> bool {
1783        self.has_requested_repaint_for(&self.viewport_id())
1784    }
1785
1786    /// Has a repaint been requested for the given viewport?
1787    #[must_use]
1788    pub fn has_requested_repaint_for(&self, viewport_id: &ViewportId) -> bool {
1789        self.read(|ctx| ctx.has_requested_repaint(viewport_id))
1790    }
1791
1792    /// Why are we repainting?
1793    ///
1794    /// This can be helpful in debugging why egui is constantly repainting.
1795    pub fn repaint_causes(&self) -> Vec<RepaintCause> {
1796        self.read(|ctx| {
1797            ctx.viewports
1798                .get(&ctx.viewport_id())
1799                .map(|v| v.repaint.prev_causes.clone())
1800        })
1801        .unwrap_or_default()
1802    }
1803
1804    /// For integrations: this callback will be called when an egui user calls [`Self::request_repaint`] or [`Self::request_repaint_after`].
1805    ///
1806    /// This lets you wake up a sleeping UI thread.
1807    ///
1808    /// Note that only one callback can be set. Any new call overrides the previous callback.
1809    pub fn set_request_repaint_callback(
1810        &self,
1811        callback: impl Fn(RequestRepaintInfo) + Send + Sync + 'static,
1812    ) {
1813        let callback = Box::new(callback);
1814        self.write(|ctx| ctx.request_repaint_callback = Some(callback));
1815    }
1816
1817    /// Request to discard the visual output of this pass,
1818    /// and to immediately do another one.
1819    ///
1820    /// This can be called to cover up visual glitches during a "sizing pass".
1821    /// For instance, when a [`crate::Grid`] is first shown we don't yet know the
1822    /// width and heights of its columns and rows. egui will do a best guess,
1823    /// but it will likely be wrong. Next pass it can read the sizes from the previous
1824    /// pass, and from there on the widths will be stable.
1825    /// This means the first pass will look glitchy, and ideally should not be shown to the user.
1826    /// So [`crate::Grid`] calls [`Self::request_discard`] to cover up this glitches.
1827    ///
1828    /// There is a limit to how many passes egui will perform, set by [`Options::max_passes`] (default=2).
1829    /// Therefore, the request might be declined.
1830    ///
1831    /// You can check if the current pass will be discarded with [`Self::will_discard`].
1832    ///
1833    /// You should be very conservative with when you call [`Self::request_discard`],
1834    /// as it will cause an extra ui pass, potentially leading to extra CPU use and frame judder.
1835    ///
1836    /// The given reason should be a human-readable string that explains why `request_discard`
1837    /// was called. This will be shown in certain debug situations, to help you figure out
1838    /// why a pass was discarded.
1839    #[track_caller]
1840    pub fn request_discard(&self, reason: impl Into<Cow<'static, str>>) {
1841        let cause = RepaintCause::new_reason(reason);
1842        self.output_mut(|o| o.request_discard_reasons.push(cause));
1843
1844        log::trace!(
1845            "request_discard: {}",
1846            if self.will_discard() {
1847                "allowed"
1848            } else {
1849                "denied"
1850            }
1851        );
1852    }
1853
1854    /// Will the visual output of this pass be discarded?
1855    ///
1856    /// If true, you can early-out from expensive graphics operations.
1857    ///
1858    /// See [`Self::request_discard`] for more.
1859    pub fn will_discard(&self) -> bool {
1860        self.write(|ctx| {
1861            let vp = ctx.viewport();
1862            // NOTE: `num_passes` is incremented
1863            vp.output.requested_discard()
1864                && vp.output.num_completed_passes + 1 < ctx.memory.options.max_passes.get()
1865        })
1866    }
1867}
1868
1869/// Callbacks
1870impl Context {
1871    /// Call the given callback at the start of each pass of each viewport.
1872    ///
1873    /// This is a convenience wrapper around [`Self::add_plugin`].
1874    pub fn on_begin_pass(&self, debug_name: &'static str, cb: plugin::ContextCallback) {
1875        self.with_plugin(|p: &mut crate::plugin::CallbackPlugin| {
1876            p.on_begin_plugins.push((debug_name, cb));
1877        });
1878    }
1879
1880    /// Call the given callback at the end of each pass of each viewport.
1881    ///
1882    /// This is a convenience wrapper around [`Self::add_plugin`].
1883    pub fn on_end_pass(&self, debug_name: &'static str, cb: plugin::ContextCallback) {
1884        self.with_plugin(|p: &mut crate::plugin::CallbackPlugin| {
1885            p.on_end_plugins.push((debug_name, cb));
1886        });
1887    }
1888
1889    /// Register a [`Plugin`](plugin::Plugin)
1890    ///
1891    /// Plugins are called in the order they are added.
1892    ///
1893    /// A plugin of the same type can only be added once (further calls with the same type will be ignored).
1894    /// This way it's convenient to add plugins in `eframe::run_simple_native`.
1895    pub fn add_plugin(&self, plugin: impl plugin::Plugin + 'static) {
1896        let handle = plugin::PluginHandle::new(plugin);
1897
1898        let added = self.write(|ctx| ctx.plugins.add(handle.clone()));
1899
1900        if added {
1901            handle.lock().dyn_plugin_mut().setup(self);
1902        }
1903    }
1904
1905    /// Call the provided closure with the plugin of type `T`, if it was registered.
1906    ///
1907    /// Returns `None` if the plugin was not registered.
1908    pub fn with_plugin<T: plugin::Plugin + 'static, R>(
1909        &self,
1910        f: impl FnOnce(&mut T) -> R,
1911    ) -> Option<R> {
1912        let plugin = self.read(|ctx| ctx.plugins.get(std::any::TypeId::of::<T>()));
1913        plugin.map(|plugin| f(plugin.lock().typed_plugin_mut()))
1914    }
1915
1916    /// Get a handle to the plugin of type `T`.
1917    ///
1918    /// ## Panics
1919    /// If the plugin of type `T` was not registered, this will panic.
1920    pub fn plugin<T: plugin::Plugin>(&self) -> TypedPluginHandle<T> {
1921        if let Some(plugin) = self.plugin_opt() {
1922            plugin
1923        } else {
1924            panic!("Plugin of type {:?} not found", std::any::type_name::<T>());
1925        }
1926    }
1927
1928    /// Get a handle to the plugin of type `T`, if it was registered.
1929    pub fn plugin_opt<T: plugin::Plugin>(&self) -> Option<TypedPluginHandle<T>> {
1930        let plugin = self.read(|ctx| ctx.plugins.get(std::any::TypeId::of::<T>()));
1931        plugin.map(TypedPluginHandle::new)
1932    }
1933
1934    /// Get a handle to the plugin of type `T`, or insert its default.
1935    pub fn plugin_or_default<T: plugin::Plugin + Default>(&self) -> TypedPluginHandle<T> {
1936        if let Some(plugin) = self.plugin_opt() {
1937            plugin
1938        } else {
1939            let default_plugin = T::default();
1940            self.add_plugin(default_plugin);
1941            self.plugin()
1942        }
1943    }
1944}
1945
1946impl Context {
1947    /// Tell `egui` which fonts to use.
1948    ///
1949    /// The default `egui` fonts only support latin and cyrillic alphabets,
1950    /// but you can call this to install additional fonts that support e.g. korean characters.
1951    ///
1952    /// The new fonts will become active at the start of the next pass.
1953    /// This will overwrite the existing fonts.
1954    pub fn set_fonts(&self, font_definitions: FontDefinitions) {
1955        profiling::function_scope!();
1956
1957        let mut update_fonts = true;
1958
1959        self.read(|ctx| {
1960            if let Some(current_fonts) = ctx.fonts.as_ref() {
1961                // NOTE: this comparison is expensive since it checks TTF data for equality
1962                if current_fonts.definitions() == &font_definitions {
1963                    update_fonts = false; // no need to update
1964                }
1965            }
1966        });
1967
1968        if update_fonts {
1969            self.memory_mut(|mem| mem.new_font_definitions = Some(font_definitions));
1970        }
1971    }
1972
1973    /// Tell `egui` which fonts to use.
1974    ///
1975    /// The default `egui` fonts only support latin and cyrillic alphabets,
1976    /// but you can call this to install additional fonts that support e.g. korean characters.
1977    ///
1978    /// The new font will become active at the start of the next pass.
1979    /// This will keep the existing fonts.
1980    pub fn add_font(&self, new_font: FontInsert) {
1981        profiling::function_scope!();
1982
1983        let mut update_fonts = true;
1984
1985        self.read(|ctx| {
1986            if let Some(current_fonts) = ctx.fonts.as_ref()
1987                && current_fonts
1988                    .definitions()
1989                    .font_data
1990                    .contains_key(&new_font.name)
1991            {
1992                update_fonts = false; // no need to update
1993            }
1994        });
1995
1996        if update_fonts {
1997            self.memory_mut(|mem| mem.add_fonts.push(new_font));
1998        }
1999    }
2000
2001    /// Does the OS use dark or light mode?
2002    /// This is used when the theme preference is set to [`crate::ThemePreference::System`].
2003    pub fn system_theme(&self) -> Option<Theme> {
2004        self.memory(|mem| mem.options.system_theme)
2005    }
2006
2007    /// The [`Theme`] used to select the appropriate [`Style`] (dark or light)
2008    /// used by all subsequent windows, panels etc.
2009    pub fn theme(&self) -> Theme {
2010        self.options(|opt| opt.theme())
2011    }
2012
2013    /// The [`Theme`] used to select between dark and light [`Self::style`]
2014    /// as the active style used by all subsequent windows, panels etc.
2015    ///
2016    /// Example:
2017    /// ```
2018    /// # let mut ctx = egui::Context::default();
2019    /// ctx.set_theme(egui::Theme::Light); // Switch to light mode
2020    /// ```
2021    pub fn set_theme(&self, theme_preference: impl Into<crate::ThemePreference>) {
2022        self.options_mut(|opt| opt.theme_preference = theme_preference.into());
2023    }
2024
2025    /// The currently active [`Style`] used by all subsequent windows, panels etc.
2026    pub fn style(&self) -> Arc<Style> {
2027        self.options(|opt| opt.style().clone())
2028    }
2029
2030    /// Mutate the currently active [`Style`] used by all subsequent windows, panels etc.
2031    /// Use [`Self::all_styles_mut`] to mutate both dark and light mode styles.
2032    ///
2033    /// Example:
2034    /// ```
2035    /// # let mut ctx = egui::Context::default();
2036    /// ctx.style_mut(|style| {
2037    ///     style.spacing.item_spacing = egui::vec2(10.0, 20.0);
2038    /// });
2039    /// ```
2040    pub fn style_mut(&self, mutate_style: impl FnOnce(&mut Style)) {
2041        self.options_mut(|opt| mutate_style(Arc::make_mut(opt.style_mut())));
2042    }
2043
2044    /// The currently active [`Style`] used by all new windows, panels etc.
2045    ///
2046    /// Use [`Self::all_styles_mut`] to mutate both dark and light mode styles.
2047    ///
2048    /// You can also change this using [`Self::style_mut`].
2049    ///
2050    /// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
2051    pub fn set_style(&self, style: impl Into<Arc<Style>>) {
2052        self.options_mut(|opt| *opt.style_mut() = style.into());
2053    }
2054
2055    /// Mutate the [`Style`]s used by all subsequent windows, panels etc. in both dark and light mode.
2056    ///
2057    /// Example:
2058    /// ```
2059    /// # let mut ctx = egui::Context::default();
2060    /// ctx.all_styles_mut(|style| {
2061    ///     style.spacing.item_spacing = egui::vec2(10.0, 20.0);
2062    /// });
2063    /// ```
2064    pub fn all_styles_mut(&self, mut mutate_style: impl FnMut(&mut Style)) {
2065        self.options_mut(|opt| {
2066            mutate_style(Arc::make_mut(&mut opt.dark_style));
2067            mutate_style(Arc::make_mut(&mut opt.light_style));
2068        });
2069    }
2070
2071    /// The [`Style`] used by all subsequent windows, panels etc.
2072    pub fn style_of(&self, theme: Theme) -> Arc<Style> {
2073        self.options(|opt| match theme {
2074            Theme::Dark => opt.dark_style.clone(),
2075            Theme::Light => opt.light_style.clone(),
2076        })
2077    }
2078
2079    /// Mutate the [`Style`] used by all subsequent windows, panels etc.
2080    ///
2081    /// Example:
2082    /// ```
2083    /// # let mut ctx = egui::Context::default();
2084    /// ctx.style_mut_of(egui::Theme::Dark, |style| {
2085    ///     style.spacing.item_spacing = egui::vec2(10.0, 20.0);
2086    /// });
2087    /// ```
2088    pub fn style_mut_of(&self, theme: Theme, mutate_style: impl FnOnce(&mut Style)) {
2089        self.options_mut(|opt| match theme {
2090            Theme::Dark => mutate_style(Arc::make_mut(&mut opt.dark_style)),
2091            Theme::Light => mutate_style(Arc::make_mut(&mut opt.light_style)),
2092        });
2093    }
2094
2095    /// The [`Style`] used by all new windows, panels etc.
2096    /// Use [`Self::set_theme`] to choose between dark and light mode.
2097    ///
2098    /// You can also change this using [`Self::style_mut_of`].
2099    ///
2100    /// You can use [`Ui::style_mut`] to change the style of a single [`Ui`].
2101    pub fn set_style_of(&self, theme: Theme, style: impl Into<Arc<Style>>) {
2102        let style = style.into();
2103        self.options_mut(|opt| match theme {
2104            Theme::Dark => opt.dark_style = style,
2105            Theme::Light => opt.light_style = style,
2106        });
2107    }
2108
2109    /// The [`crate::Visuals`] used by all subsequent windows, panels etc.
2110    ///
2111    /// You can also use [`Ui::visuals_mut`] to change the visuals of a single [`Ui`].
2112    ///
2113    /// Example:
2114    /// ```
2115    /// # let mut ctx = egui::Context::default();
2116    /// ctx.set_visuals_of(egui::Theme::Dark, egui::Visuals { panel_fill: egui::Color32::RED, ..Default::default() });
2117    /// ```
2118    pub fn set_visuals_of(&self, theme: Theme, visuals: crate::Visuals) {
2119        self.style_mut_of(theme, |style| style.visuals = visuals);
2120    }
2121
2122    /// The [`crate::Visuals`] used by all subsequent windows, panels etc.
2123    ///
2124    /// You can also use [`Ui::visuals_mut`] to change the visuals of a single [`Ui`].
2125    ///
2126    /// Example:
2127    /// ```
2128    /// # let mut ctx = egui::Context::default();
2129    /// ctx.set_visuals(egui::Visuals { panel_fill: egui::Color32::RED, ..Default::default() });
2130    /// ```
2131    pub fn set_visuals(&self, visuals: crate::Visuals) {
2132        self.style_mut_of(self.theme(), |style| style.visuals = visuals);
2133    }
2134
2135    /// The number of physical pixels for each logical point.
2136    ///
2137    /// This is calculated as [`Self::zoom_factor`] * [`Self::native_pixels_per_point`]
2138    #[inline(always)]
2139    pub fn pixels_per_point(&self) -> f32 {
2140        self.input(|i| i.pixels_per_point)
2141    }
2142
2143    /// Set the number of physical pixels for each logical point.
2144    /// Will become active at the start of the next pass.
2145    ///
2146    /// This will actually translate to a call to [`Self::set_zoom_factor`].
2147    pub fn set_pixels_per_point(&self, pixels_per_point: f32) {
2148        if pixels_per_point != self.pixels_per_point() {
2149            self.set_zoom_factor(pixels_per_point / self.native_pixels_per_point().unwrap_or(1.0));
2150        }
2151    }
2152
2153    /// The number of physical pixels for each logical point on this monitor.
2154    ///
2155    /// This is given as input to egui via [`crate::ViewportInfo::native_pixels_per_point`]
2156    /// and cannot be changed.
2157    #[inline(always)]
2158    pub fn native_pixels_per_point(&self) -> Option<f32> {
2159        self.input(|i| i.viewport().native_pixels_per_point)
2160    }
2161
2162    /// Global zoom factor of the UI.
2163    ///
2164    /// This is used to calculate the `pixels_per_point`
2165    /// for the UI as `pixels_per_point = zoom_factor * native_pixels_per_point`.
2166    ///
2167    /// The default is 1.0.
2168    /// Make larger to make everything larger.
2169    #[inline(always)]
2170    pub fn zoom_factor(&self) -> f32 {
2171        self.options(|o| o.zoom_factor)
2172    }
2173
2174    /// Sets zoom factor of the UI.
2175    /// Will become active at the start of the next pass.
2176    ///
2177    /// Note that calling this will not update [`Self::zoom_factor`] until the end of the pass.
2178    ///
2179    /// This is used to calculate the `pixels_per_point`
2180    /// for the UI as `pixels_per_point = zoom_fator * native_pixels_per_point`.
2181    ///
2182    /// The default is 1.0.
2183    /// Make larger to make everything larger.
2184    ///
2185    /// It is better to call this than modifying
2186    /// [`Options::zoom_factor`].
2187    #[inline(always)]
2188    pub fn set_zoom_factor(&self, zoom_factor: f32) {
2189        let cause = RepaintCause::new();
2190        self.write(|ctx| {
2191            if ctx.memory.options.zoom_factor != zoom_factor {
2192                ctx.new_zoom_factor = Some(zoom_factor);
2193                #[expect(clippy::iter_over_hash_type)]
2194                for viewport_id in ctx.all_viewport_ids() {
2195                    ctx.request_repaint(viewport_id, cause.clone());
2196                }
2197            }
2198        });
2199    }
2200
2201    /// Allocate a texture.
2202    ///
2203    /// This is for advanced users.
2204    /// Most users should use [`crate::Ui::image`] or [`Self::try_load_texture`]
2205    /// instead.
2206    ///
2207    /// In order to display an image you must convert it to a texture using this function.
2208    /// The function will hand over the image data to the egui backend, which will
2209    /// upload it to the GPU.
2210    ///
2211    /// ⚠️ Make sure to only call this ONCE for each image, i.e. NOT in your main GUI code.
2212    /// The call is NOT immediate safe.
2213    ///
2214    /// The given name can be useful for later debugging, and will be visible if you call [`Self::texture_ui`].
2215    ///
2216    /// For how to load an image, see [`crate::ImageData`] and [`crate::ColorImage::from_rgba_unmultiplied`].
2217    ///
2218    /// ```
2219    /// struct MyImage {
2220    ///     texture: Option<egui::TextureHandle>,
2221    /// }
2222    ///
2223    /// impl MyImage {
2224    ///     fn ui(&mut self, ui: &mut egui::Ui) {
2225    ///         let texture: &egui::TextureHandle = self.texture.get_or_insert_with(|| {
2226    ///             // Load the texture only once.
2227    ///             ui.ctx().load_texture(
2228    ///                 "my-image",
2229    ///                 egui::ColorImage::example(),
2230    ///                 Default::default()
2231    ///             )
2232    ///         });
2233    ///
2234    ///         // Show the image:
2235    ///         ui.image((texture.id(), texture.size_vec2()));
2236    ///     }
2237    /// }
2238    /// ```
2239    ///
2240    /// See also [`crate::ImageData`], [`crate::Ui::image`] and [`crate::Image`].
2241    pub fn load_texture(
2242        &self,
2243        name: impl Into<String>,
2244        image: impl Into<ImageData>,
2245        options: TextureOptions,
2246    ) -> TextureHandle {
2247        let name = name.into();
2248        let image = image.into();
2249        let max_texture_side = self.input(|i| i.max_texture_side);
2250        debug_assert!(
2251            image.width() <= max_texture_side && image.height() <= max_texture_side,
2252            "Texture {:?} has size {}x{}, but the maximum texture side is {}",
2253            name,
2254            image.width(),
2255            image.height(),
2256            max_texture_side
2257        );
2258        let tex_mngr = self.tex_manager();
2259        let tex_id = tex_mngr.write().alloc(name, image, options);
2260        TextureHandle::new(tex_mngr, tex_id)
2261    }
2262
2263    /// Low-level texture manager.
2264    ///
2265    /// In general it is easier to use [`Self::load_texture`] and [`TextureHandle`].
2266    ///
2267    /// You can show stats about the allocated textures using [`Self::texture_ui`].
2268    pub fn tex_manager(&self) -> Arc<RwLock<epaint::textures::TextureManager>> {
2269        self.read(|ctx| ctx.tex_manager.0.clone())
2270    }
2271
2272    // ---------------------------------------------------------------------
2273
2274    /// Constrain the position of a window/area so it fits within the provided boundary.
2275    pub(crate) fn constrain_window_rect_to_area(window: Rect, area: Rect) -> Rect {
2276        let mut pos = window.min;
2277
2278        // Constrain to screen, unless window is too large to fit:
2279        let margin_x = (window.width() - area.width()).at_least(0.0);
2280        let margin_y = (window.height() - area.height()).at_least(0.0);
2281
2282        pos.x = pos.x.at_most(area.right() + margin_x - window.width()); // move left if needed
2283        pos.x = pos.x.at_least(area.left() - margin_x); // move right if needed
2284        pos.y = pos.y.at_most(area.bottom() + margin_y - window.height()); // move right if needed
2285        pos.y = pos.y.at_least(area.top() - margin_y); // move down if needed
2286
2287        Rect::from_min_size(pos, window.size()).round_ui()
2288    }
2289}
2290
2291impl Context {
2292    /// Call at the end of each frame if you called [`Context::begin_pass`].
2293    #[must_use]
2294    pub fn end_pass(&self) -> FullOutput {
2295        profiling::function_scope!();
2296
2297        if self.options(|o| o.zoom_with_keyboard) {
2298            crate::gui_zoom::zoom_with_keyboard(self);
2299        }
2300
2301        // Plugins run just before the pass ends.
2302        let plugins = self.read(|ctx| ctx.plugins.ordered_plugins());
2303        plugins.on_end_pass(self);
2304
2305        #[cfg(debug_assertions)]
2306        self.debug_painting();
2307
2308        let mut output = self.write(|ctx| ctx.end_pass());
2309        plugins.on_output(&mut output);
2310        output
2311    }
2312
2313    /// Call at the end of each frame if you called [`Context::begin_pass`].
2314    #[must_use]
2315    #[deprecated = "Renamed end_pass"]
2316    pub fn end_frame(&self) -> FullOutput {
2317        self.end_pass()
2318    }
2319
2320    /// Called at the end of the pass.
2321    #[cfg(debug_assertions)]
2322    fn debug_painting(&self) {
2323        #![expect(clippy::iter_over_hash_type)] // ok to be sloppy in debug painting
2324
2325        let paint_widget = |widget: &WidgetRect, text: &str, color: Color32| {
2326            let rect = widget.interact_rect;
2327            if rect.is_positive() {
2328                let painter = Painter::new(self.clone(), widget.layer_id, Rect::EVERYTHING);
2329                painter.debug_rect(rect, color, text);
2330            }
2331        };
2332
2333        let paint_widget_id = |id: Id, text: &str, color: Color32| {
2334            if let Some(widget) =
2335                self.write(|ctx| ctx.viewport().this_pass.widgets.get(id).copied())
2336            {
2337                paint_widget(&widget, text, color);
2338            }
2339        };
2340
2341        if self.style().debug.show_interactive_widgets {
2342            // Show all interactive widgets:
2343            let rects = self.write(|ctx| ctx.viewport().this_pass.widgets.clone());
2344            for (layer_id, rects) in rects.layers() {
2345                let painter = Painter::new(self.clone(), *layer_id, Rect::EVERYTHING);
2346                for rect in rects {
2347                    if rect.sense.interactive() {
2348                        let (color, text) = if rect.sense.senses_click() && rect.sense.senses_drag()
2349                        {
2350                            (Color32::from_rgb(0x88, 0, 0x88), "click+drag")
2351                        } else if rect.sense.senses_click() {
2352                            (Color32::from_rgb(0x88, 0, 0), "click")
2353                        } else if rect.sense.senses_drag() {
2354                            (Color32::from_rgb(0, 0, 0x88), "drag")
2355                        } else {
2356                            // unreachable since we only show interactive
2357                            (Color32::from_rgb(0, 0, 0x88), "hover")
2358                        };
2359                        painter.debug_rect(rect.interact_rect, color, text);
2360                    }
2361                }
2362            }
2363
2364            // Show the ones actually interacted with:
2365            {
2366                let interact_widgets = self.write(|ctx| ctx.viewport().interact_widgets.clone());
2367                let InteractionSnapshot {
2368                    clicked,
2369                    long_touched: _,
2370                    drag_started: _,
2371                    dragged,
2372                    drag_stopped: _,
2373                    contains_pointer,
2374                    hovered,
2375                } = interact_widgets;
2376
2377                if true {
2378                    for &id in &contains_pointer {
2379                        paint_widget_id(id, "contains_pointer", Color32::BLUE);
2380                    }
2381
2382                    let widget_rects = self.write(|w| w.viewport().this_pass.widgets.clone());
2383
2384                    let mut contains_pointer: Vec<Id> = contains_pointer.iter().copied().collect();
2385                    contains_pointer.sort_by_key(|&id| {
2386                        widget_rects
2387                            .order(id)
2388                            .map(|(layer_id, order_in_layer)| (layer_id.order, order_in_layer))
2389                    });
2390
2391                    let mut debug_text = "Widgets in order:\n".to_owned();
2392                    for id in contains_pointer {
2393                        let mut widget_text = format!("{id:?}");
2394                        if let Some(rect) = widget_rects.get(id) {
2395                            widget_text +=
2396                                &format!(" {:?} {:?} {:?}", rect.layer_id, rect.rect, rect.sense);
2397                        }
2398                        if let Some(info) = widget_rects.info(id) {
2399                            widget_text += &format!(" {info:?}");
2400                        }
2401                        debug_text += &format!("{widget_text}\n");
2402                    }
2403                    self.debug_text(debug_text);
2404                }
2405                if true {
2406                    for widget in hovered {
2407                        paint_widget_id(widget, "hovered", Color32::WHITE);
2408                    }
2409                }
2410                if let Some(widget) = clicked {
2411                    paint_widget_id(widget, "clicked", Color32::RED);
2412                }
2413                if let Some(widget) = dragged {
2414                    paint_widget_id(widget, "dragged", Color32::GREEN);
2415                }
2416            }
2417        }
2418
2419        if self.style().debug.show_widget_hits {
2420            let hits = self.write(|ctx| ctx.viewport().hits.clone());
2421            let WidgetHits {
2422                close,
2423                contains_pointer,
2424                click,
2425                drag,
2426            } = hits;
2427
2428            if false {
2429                for widget in &close {
2430                    paint_widget(widget, "close", Color32::from_gray(70));
2431                }
2432            }
2433            if true {
2434                for widget in &contains_pointer {
2435                    paint_widget(widget, "contains_pointer", Color32::BLUE);
2436                }
2437            }
2438            if let Some(widget) = &click {
2439                paint_widget(widget, "click", Color32::RED);
2440            }
2441            if let Some(widget) = &drag {
2442                paint_widget(widget, "drag", Color32::GREEN);
2443            }
2444        }
2445
2446        if let Some(debug_rect) = self.pass_state_mut(|fs| fs.debug_rect.take()) {
2447            debug_rect.paint(&self.debug_painter());
2448        }
2449
2450        let num_multipass_in_row = self.viewport(|vp| vp.num_multipass_in_row);
2451        if 3 <= num_multipass_in_row {
2452            // If you see this message, it means we've been paying the cost of multi-pass for multiple frames in a row.
2453            // This is likely a bug. `request_discard` should only be called in rare situations, when some layout changes.
2454
2455            let mut warning = format!(
2456                "egui PERF WARNING: request_discard has been called {num_multipass_in_row} frames in a row"
2457            );
2458            self.viewport(|vp| {
2459                for reason in &vp.output.request_discard_reasons {
2460                    warning += &format!("\n  {reason}");
2461                }
2462            });
2463
2464            self.debug_painter()
2465                .debug_text(Pos2::ZERO, Align2::LEFT_TOP, Color32::RED, warning);
2466        }
2467    }
2468}
2469
2470impl ContextImpl {
2471    fn end_pass(&mut self) -> FullOutput {
2472        let ended_viewport_id = self.viewport_id();
2473        let viewport = self.viewports.entry(ended_viewport_id).or_default();
2474        let pixels_per_point = viewport.input.pixels_per_point;
2475
2476        self.loaders.end_pass(viewport.repaint.cumulative_pass_nr);
2477
2478        viewport.repaint.cumulative_pass_nr += 1;
2479
2480        self.memory.end_pass(&viewport.this_pass.used_ids);
2481
2482        if let Some(fonts) = self.fonts.as_mut() {
2483            let tex_mngr = &mut self.tex_manager.0.write();
2484            if let Some(font_image_delta) = fonts.font_image_delta() {
2485                // A partial font atlas update, e.g. a new glyph has been entered.
2486                tex_mngr.set(TextureId::default(), font_image_delta);
2487            }
2488        }
2489
2490        // Inform the backend of all textures that have been updated (including font atlas).
2491        let textures_delta = self.tex_manager.0.write().take_delta();
2492
2493        let mut platform_output: PlatformOutput = std::mem::take(&mut viewport.output);
2494
2495        #[cfg(feature = "accesskit")]
2496        {
2497            profiling::scope!("accesskit");
2498            let state = viewport.this_pass.accesskit_state.take();
2499            if let Some(state) = state {
2500                let root_id = crate::accesskit_root_id().accesskit_id();
2501                let nodes = {
2502                    state
2503                        .nodes
2504                        .into_iter()
2505                        .map(|(id, node)| (id.accesskit_id(), node))
2506                        .collect()
2507                };
2508                let focus_id = self
2509                    .memory
2510                    .focused()
2511                    .map_or(root_id, |id| id.accesskit_id());
2512                platform_output.accesskit_update = Some(accesskit::TreeUpdate {
2513                    nodes,
2514                    tree: Some(accesskit::Tree::new(root_id)),
2515                    focus: focus_id,
2516                });
2517            }
2518        }
2519
2520        let shapes = viewport
2521            .graphics
2522            .drain(self.memory.areas().order(), &self.memory.to_global);
2523
2524        let mut repaint_needed = false;
2525
2526        if self.memory.options.repaint_on_widget_change {
2527            profiling::scope!("compare-widget-rects");
2528            #[allow(clippy::allow_attributes, clippy::collapsible_if)] // false positive on wasm
2529            if viewport.prev_pass.widgets != viewport.this_pass.widgets {
2530                repaint_needed = true; // Some widget has moved
2531            }
2532        }
2533
2534        std::mem::swap(&mut viewport.prev_pass, &mut viewport.this_pass);
2535
2536        if repaint_needed {
2537            self.request_repaint(ended_viewport_id, RepaintCause::new());
2538        }
2539        //  -------------------
2540
2541        let all_viewport_ids = self.all_viewport_ids();
2542
2543        self.last_viewport = ended_viewport_id;
2544
2545        self.viewports.retain(|&id, viewport| {
2546            if id == ViewportId::ROOT {
2547                return true; // never remove the root
2548            }
2549
2550            let parent = *self.viewport_parents.entry(id).or_default();
2551
2552            if !all_viewport_ids.contains(&parent) {
2553                log::debug!(
2554                    "Removing viewport {:?} ({:?}): the parent is gone",
2555                    id,
2556                    viewport.builder.title
2557                );
2558
2559                return false;
2560            }
2561
2562            let is_our_child = parent == ended_viewport_id && id != ViewportId::ROOT;
2563            if is_our_child {
2564                if !viewport.used {
2565                    log::debug!(
2566                        "Removing viewport {:?} ({:?}): it was never used this pass",
2567                        id,
2568                        viewport.builder.title
2569                    );
2570
2571                    return false; // Only keep children that have been updated this pass
2572                }
2573
2574                viewport.used = false; // reset so we can check again next pass
2575            }
2576
2577            true
2578        });
2579
2580        // If we are an immediate viewport, this will resume the previous viewport.
2581        self.viewport_stack.pop();
2582
2583        // The last viewport is not necessarily the root viewport,
2584        // just the top _immediate_ viewport.
2585        let is_last = self.viewport_stack.is_empty();
2586
2587        let viewport_output = self
2588            .viewports
2589            .iter_mut()
2590            .map(|(&id, viewport)| {
2591                let parent = *self.viewport_parents.entry(id).or_default();
2592                let commands = if is_last {
2593                    // Let the primary immediate viewport handle the commands of its children too.
2594                    // This can make things easier for the backend, as otherwise we may get commands
2595                    // that affect a viewport while its egui logic is running.
2596                    std::mem::take(&mut viewport.commands)
2597                } else {
2598                    vec![]
2599                };
2600
2601                (
2602                    id,
2603                    ViewportOutput {
2604                        parent,
2605                        class: viewport.class,
2606                        builder: viewport.builder.clone(),
2607                        viewport_ui_cb: viewport.viewport_ui_cb.clone(),
2608                        commands,
2609                        repaint_delay: viewport.repaint.repaint_delay,
2610                    },
2611                )
2612            })
2613            .collect();
2614
2615        if is_last {
2616            // Remove dead viewports:
2617            self.viewports.retain(|id, _| all_viewport_ids.contains(id));
2618            debug_assert!(
2619                self.viewports.contains_key(&ViewportId::ROOT),
2620                "Bug in egui: we removed the root viewport"
2621            );
2622            self.viewport_parents
2623                .retain(|id, _| all_viewport_ids.contains(id));
2624        } else {
2625            let viewport_id = self.viewport_id();
2626            self.memory.set_viewport_id(viewport_id);
2627        }
2628
2629        platform_output.num_completed_passes += 1;
2630
2631        FullOutput {
2632            platform_output,
2633            textures_delta,
2634            shapes,
2635            pixels_per_point,
2636            viewport_output,
2637        }
2638    }
2639}
2640
2641impl Context {
2642    /// Tessellate the given shapes into triangle meshes.
2643    ///
2644    /// `pixels_per_point` is used for feathering (anti-aliasing).
2645    /// For this you can use [`FullOutput::pixels_per_point`], [`Self::pixels_per_point`],
2646    /// or whatever is appropriate for your viewport.
2647    pub fn tessellate(
2648        &self,
2649        shapes: Vec<ClippedShape>,
2650        pixels_per_point: f32,
2651    ) -> Vec<ClippedPrimitive> {
2652        profiling::function_scope!();
2653
2654        // A tempting optimization is to reuse the tessellation from last frame if the
2655        // shapes are the same, but just comparing the shapes takes about 50% of the time
2656        // it takes to tessellate them, so it is not a worth optimization.
2657
2658        self.write(|ctx| {
2659            let tessellation_options = ctx.memory.options.tessellation_options;
2660            let texture_atlas = if let Some(fonts) = ctx.fonts.as_ref() {
2661                fonts.texture_atlas()
2662            } else {
2663                log::warn!("No font size matching {pixels_per_point} pixels per point found.");
2664                ctx.fonts
2665                    .iter()
2666                    .next()
2667                    .expect("No fonts loaded")
2668                    .texture_atlas()
2669            };
2670
2671            let paint_stats = PaintStats::from_shapes(&shapes);
2672            let clipped_primitives = {
2673                profiling::scope!("tessellator::tessellate_shapes");
2674                tessellator::Tessellator::new(
2675                    pixels_per_point,
2676                    tessellation_options,
2677                    texture_atlas.size(),
2678                    texture_atlas.prepared_discs(),
2679                )
2680                .tessellate_shapes(shapes)
2681            };
2682            ctx.paint_stats = paint_stats.with_clipped_primitives(&clipped_primitives);
2683            clipped_primitives
2684        })
2685    }
2686
2687    // ---------------------------------------------------------------------
2688
2689    /// Returns the position and size of the egui area that is safe for content rendering.
2690    ///
2691    /// Returns [`Self::viewport_rect`] minus areas that might be partially covered by, for example,
2692    /// the OS status bar or display notches.
2693    ///
2694    /// If you want to render behind e.g. the dynamic island on iOS, use [`Self::viewport_rect`].
2695    pub fn content_rect(&self) -> Rect {
2696        self.input(|i| i.content_rect()).round_ui()
2697    }
2698
2699    /// Returns the position and size of the full area available to egui
2700    ///
2701    /// This includes reas that might be partially covered by, for example, the OS status bar or
2702    /// display notches. See [`Self::content_rect`] to get a rect that is safe for content.
2703    ///
2704    /// This rectangle includes e.g. the dynamic island on iOS.
2705    /// If you want to only render _below_ the that (not behind), then you should use
2706    /// [`Self::content_rect`] instead.
2707    ///
2708    /// See also [`RawInput::safe_area_insets`].
2709    pub fn viewport_rect(&self) -> Rect {
2710        self.input(|i| i.viewport_rect()).round_ui()
2711    }
2712
2713    /// Position and size of the egui area.
2714    #[deprecated(
2715        note = "screen_rect has been split into viewport_rect() and content_rect(). You likely should use content_rect()"
2716    )]
2717    pub fn screen_rect(&self) -> Rect {
2718        self.input(|i| i.content_rect()).round_ui()
2719    }
2720
2721    /// How much space is still available after panels have been added.
2722    pub fn available_rect(&self) -> Rect {
2723        self.pass_state(|s| s.available_rect()).round_ui()
2724    }
2725
2726    /// How much space is used by panels and windows.
2727    pub fn used_rect(&self) -> Rect {
2728        self.write(|ctx| {
2729            let mut used = ctx.viewport().this_pass.used_by_panels;
2730            for (_id, window) in ctx.memory.areas().visible_windows() {
2731                used |= window.rect();
2732            }
2733            used.round_ui()
2734        })
2735    }
2736
2737    /// How much space is used by panels and windows.
2738    ///
2739    /// You can shrink your egui area to this size and still fit all egui components.
2740    pub fn used_size(&self) -> Vec2 {
2741        (self.used_rect().max - Pos2::ZERO).round_ui()
2742    }
2743
2744    // ---------------------------------------------------------------------
2745
2746    /// Is the pointer (mouse/touch) over any egui area?
2747    pub fn is_pointer_over_area(&self) -> bool {
2748        let pointer_pos = self.input(|i| i.pointer.interact_pos());
2749        if let Some(pointer_pos) = pointer_pos {
2750            if let Some(layer) = self.layer_id_at(pointer_pos) {
2751                if layer.order == Order::Background {
2752                    !self.pass_state(|state| state.unused_rect.contains(pointer_pos))
2753                } else {
2754                    true
2755                }
2756            } else {
2757                false
2758            }
2759        } else {
2760            false
2761        }
2762    }
2763
2764    /// True if egui is currently interested in the pointer (mouse or touch).
2765    ///
2766    /// Could be the pointer is hovering over a [`crate::Window`] or the user is dragging a widget.
2767    /// If `false`, the pointer is outside of any egui area and so
2768    /// you may be interested in what it is doing (e.g. controlling your game).
2769    /// Returns `false` if a drag started outside of egui and then moved over an egui area.
2770    pub fn wants_pointer_input(&self) -> bool {
2771        self.is_using_pointer()
2772            || (self.is_pointer_over_area() && !self.input(|i| i.pointer.any_down()))
2773    }
2774
2775    /// Is egui currently using the pointer position (e.g. dragging a slider)?
2776    ///
2777    /// NOTE: this will return `false` if the pointer is just hovering over an egui area.
2778    pub fn is_using_pointer(&self) -> bool {
2779        self.memory(|m| m.interaction().is_using_pointer())
2780    }
2781
2782    /// If `true`, egui is currently listening on text input (e.g. typing text in a [`crate::TextEdit`]).
2783    pub fn wants_keyboard_input(&self) -> bool {
2784        self.memory(|m| m.focused().is_some())
2785    }
2786
2787    /// Highlight this widget, to make it look like it is hovered, even if it isn't.
2788    ///
2789    /// If you call this after the widget has been fully rendered,
2790    /// then it won't be highlighted until the next ui pass.
2791    ///
2792    /// See also [`Response::highlight`].
2793    pub fn highlight_widget(&self, id: Id) {
2794        self.pass_state_mut(|fs| fs.highlight_next_pass.insert(id));
2795    }
2796
2797    /// Is an egui context menu open?
2798    ///
2799    /// This only works with the old, deprecated [`crate::menu`] API.
2800    #[expect(deprecated)]
2801    #[deprecated = "Use `is_popup_open` instead"]
2802    pub fn is_context_menu_open(&self) -> bool {
2803        self.data(|d| {
2804            d.get_temp::<crate::menu::BarState>(crate::menu::CONTEXT_MENU_ID_STR.into())
2805                .is_some_and(|state| state.has_root())
2806        })
2807    }
2808
2809    /// Is a popup or (context) menu open?
2810    ///
2811    /// Will return false for [`crate::Tooltip`]s (which are technically popups as well).
2812    pub fn is_popup_open(&self) -> bool {
2813        self.pass_state_mut(|fs| {
2814            fs.layers
2815                .values()
2816                .any(|layer| !layer.open_popups.is_empty())
2817        })
2818    }
2819}
2820
2821// Ergonomic methods to forward some calls often used in 'if let' without holding the borrow
2822impl Context {
2823    /// Latest reported pointer position.
2824    ///
2825    /// When tapping a touch screen, this will be `None`.
2826    #[inline(always)]
2827    pub fn pointer_latest_pos(&self) -> Option<Pos2> {
2828        self.input(|i| i.pointer.latest_pos())
2829    }
2830
2831    /// If it is a good idea to show a tooltip, where is pointer?
2832    #[inline(always)]
2833    pub fn pointer_hover_pos(&self) -> Option<Pos2> {
2834        self.input(|i| i.pointer.hover_pos())
2835    }
2836
2837    /// If you detect a click or drag and wants to know where it happened, use this.
2838    ///
2839    /// Latest position of the mouse, but ignoring any [`crate::Event::PointerGone`]
2840    /// if there were interactions this pass.
2841    /// When tapping a touch screen, this will be the location of the touch.
2842    #[inline(always)]
2843    pub fn pointer_interact_pos(&self) -> Option<Pos2> {
2844        self.input(|i| i.pointer.interact_pos())
2845    }
2846
2847    /// Calls [`InputState::multi_touch`].
2848    pub fn multi_touch(&self) -> Option<MultiTouchInfo> {
2849        self.input(|i| i.multi_touch())
2850    }
2851}
2852
2853impl Context {
2854    /// Transform the graphics of the given layer.
2855    ///
2856    /// This will also affect input.
2857    /// The direction of the given transform is "into the global coordinate system".
2858    ///
2859    /// This is a sticky setting, remembered from one frame to the next.
2860    ///
2861    /// Can be used to implement pan and zoom (see relevant demo).
2862    ///
2863    /// For a temporary transform, use [`Self::transform_layer_shapes`] or
2864    /// [`Ui::with_visual_transform`].
2865    pub fn set_transform_layer(&self, layer_id: LayerId, transform: TSTransform) {
2866        self.memory_mut(|m| {
2867            if transform == TSTransform::IDENTITY {
2868                m.to_global.remove(&layer_id)
2869            } else {
2870                m.to_global.insert(layer_id, transform)
2871            }
2872        });
2873    }
2874
2875    /// Return how to transform the graphics of the given layer into the global coordinate system.
2876    ///
2877    /// Set this with [`Self::layer_transform_to_global`].
2878    pub fn layer_transform_to_global(&self, layer_id: LayerId) -> Option<TSTransform> {
2879        self.memory(|m| m.to_global.get(&layer_id).copied())
2880    }
2881
2882    /// Return how to transform the graphics of the global coordinate system into the local coordinate system of the given layer.
2883    ///
2884    /// This returns the inverse of [`Self::layer_transform_to_global`].
2885    pub fn layer_transform_from_global(&self, layer_id: LayerId) -> Option<TSTransform> {
2886        self.layer_transform_to_global(layer_id)
2887            .map(|t| t.inverse())
2888    }
2889
2890    /// Transform all the graphics at the given layer.
2891    ///
2892    /// Is used to implement drag-and-drop preview.
2893    ///
2894    /// This only applied to the existing graphics at the layer, not to new graphics added later.
2895    ///
2896    /// For a persistent transform, use [`Self::set_transform_layer`] instead.
2897    pub fn transform_layer_shapes(&self, layer_id: LayerId, transform: TSTransform) {
2898        if transform != TSTransform::IDENTITY {
2899            self.graphics_mut(|g| g.entry(layer_id).transform(transform));
2900        }
2901    }
2902
2903    /// Top-most layer at the given position.
2904    pub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId> {
2905        self.memory(|mem| mem.layer_id_at(pos))
2906    }
2907
2908    /// Moves the given area to the top in its [`Order`].
2909    ///
2910    /// [`crate::Area`]:s and [`crate::Window`]:s also do this automatically when being clicked on or interacted with.
2911    pub fn move_to_top(&self, layer_id: LayerId) {
2912        self.memory_mut(|mem| mem.areas_mut().move_to_top(layer_id));
2913    }
2914
2915    /// Mark the `child` layer as a sublayer of `parent`.
2916    ///
2917    /// Sublayers are moved directly above the parent layer at the end of the frame. This is mainly
2918    /// intended for adding a new [`crate::Area`] inside a [`crate::Window`].
2919    ///
2920    /// This currently only supports one level of nesting. If `parent` is a sublayer of another
2921    /// layer, the behavior is unspecified.
2922    pub fn set_sublayer(&self, parent: LayerId, child: LayerId) {
2923        self.memory_mut(|mem| mem.areas_mut().set_sublayer(parent, child));
2924    }
2925
2926    /// Retrieve the [`LayerId`] of the top level windows.
2927    pub fn top_layer_id(&self) -> Option<LayerId> {
2928        self.memory(|mem| mem.areas().top_layer_id(Order::Middle))
2929    }
2930
2931    /// Does the given rectangle contain the mouse pointer?
2932    ///
2933    /// Will return false if some other area is covering the given layer.
2934    ///
2935    /// The given rectangle is assumed to have been clipped by its parent clip rect.
2936    ///
2937    /// See also [`Response::contains_pointer`].
2938    pub fn rect_contains_pointer(&self, layer_id: LayerId, rect: Rect) -> bool {
2939        let rect = if let Some(to_global) = self.layer_transform_to_global(layer_id) {
2940            to_global * rect
2941        } else {
2942            rect
2943        };
2944        if !rect.is_positive() {
2945            return false;
2946        }
2947
2948        let pointer_pos = self.input(|i| i.pointer.interact_pos());
2949        let Some(pointer_pos) = pointer_pos else {
2950            return false;
2951        };
2952
2953        if !rect.contains(pointer_pos) {
2954            return false;
2955        }
2956
2957        if self.layer_id_at(pointer_pos) != Some(layer_id) {
2958            return false;
2959        }
2960
2961        true
2962    }
2963
2964    // ---------------------------------------------------------------------
2965
2966    /// Whether or not to debug widget layout on hover.
2967    #[cfg(debug_assertions)]
2968    pub fn debug_on_hover(&self) -> bool {
2969        self.options(|opt| opt.style().debug.debug_on_hover)
2970    }
2971
2972    /// Turn on/off whether or not to debug widget layout on hover.
2973    #[cfg(debug_assertions)]
2974    pub fn set_debug_on_hover(&self, debug_on_hover: bool) {
2975        self.all_styles_mut(|style| style.debug.debug_on_hover = debug_on_hover);
2976    }
2977}
2978
2979/// ## Animation
2980impl Context {
2981    /// Returns a value in the range [0, 1], to indicate "how on" this thing is.
2982    ///
2983    /// The first time called it will return `if value { 1.0 } else { 0.0 }`
2984    /// Calling this with `value = true` will always yield a number larger than zero, quickly going towards one.
2985    /// Calling this with `value = false` will always yield a number less than one, quickly going towards zero.
2986    ///
2987    /// The function will call [`Self::request_repaint()`] when appropriate.
2988    ///
2989    /// The animation time is taken from [`Style::animation_time`].
2990    #[track_caller] // To track repaint cause
2991    pub fn animate_bool(&self, id: Id, value: bool) -> f32 {
2992        let animation_time = self.style().animation_time;
2993        self.animate_bool_with_time_and_easing(id, value, animation_time, emath::easing::linear)
2994    }
2995
2996    /// Like [`Self::animate_bool`], but uses an easing function that makes the value move
2997    /// quickly in the beginning and slow down towards the end.
2998    ///
2999    /// The exact easing function may come to change in future versions of egui.
3000    #[track_caller] // To track repaint cause
3001    pub fn animate_bool_responsive(&self, id: Id, value: bool) -> f32 {
3002        self.animate_bool_with_easing(id, value, emath::easing::cubic_out)
3003    }
3004
3005    /// Like [`Self::animate_bool`] but allows you to control the easing function.
3006    #[track_caller] // To track repaint cause
3007    pub fn animate_bool_with_easing(&self, id: Id, value: bool, easing: fn(f32) -> f32) -> f32 {
3008        let animation_time = self.style().animation_time;
3009        self.animate_bool_with_time_and_easing(id, value, animation_time, easing)
3010    }
3011
3012    /// Like [`Self::animate_bool`] but allows you to control the animation time.
3013    #[track_caller] // To track repaint cause
3014    pub fn animate_bool_with_time(&self, id: Id, target_value: bool, animation_time: f32) -> f32 {
3015        self.animate_bool_with_time_and_easing(
3016            id,
3017            target_value,
3018            animation_time,
3019            emath::easing::linear,
3020        )
3021    }
3022
3023    /// Like [`Self::animate_bool`] but allows you to control the animation time and easing function.
3024    ///
3025    /// Use e.g. [`emath::easing::quadratic_out`]
3026    /// for a responsive start and a slow end.
3027    ///
3028    /// The easing function flips when `target_value` is `false`,
3029    /// so that when going back towards 0.0, we get
3030    #[track_caller] // To track repaint cause
3031    pub fn animate_bool_with_time_and_easing(
3032        &self,
3033        id: Id,
3034        target_value: bool,
3035        animation_time: f32,
3036        easing: fn(f32) -> f32,
3037    ) -> f32 {
3038        let animated_value = self.write(|ctx| {
3039            ctx.animation_manager.animate_bool(
3040                &ctx.viewports.entry(ctx.viewport_id()).or_default().input,
3041                animation_time,
3042                id,
3043                target_value,
3044            )
3045        });
3046
3047        let animation_in_progress = 0.0 < animated_value && animated_value < 1.0;
3048        if animation_in_progress {
3049            self.request_repaint();
3050        }
3051
3052        if target_value {
3053            easing(animated_value)
3054        } else {
3055            1.0 - easing(1.0 - animated_value)
3056        }
3057    }
3058
3059    /// Smoothly animate an `f32` value.
3060    ///
3061    /// At the first call the value is written to memory.
3062    /// When it is called with a new value, it linearly interpolates to it in the given time.
3063    #[track_caller] // To track repaint cause
3064    pub fn animate_value_with_time(&self, id: Id, target_value: f32, animation_time: f32) -> f32 {
3065        let animated_value = self.write(|ctx| {
3066            ctx.animation_manager.animate_value(
3067                &ctx.viewports.entry(ctx.viewport_id()).or_default().input,
3068                animation_time,
3069                id,
3070                target_value,
3071            )
3072        });
3073        let animation_in_progress = animated_value != target_value;
3074        if animation_in_progress {
3075            self.request_repaint();
3076        }
3077
3078        animated_value
3079    }
3080
3081    /// Clear memory of any animations.
3082    pub fn clear_animations(&self) {
3083        self.write(|ctx| ctx.animation_manager = Default::default());
3084    }
3085}
3086
3087impl Context {
3088    /// Show a ui for settings (style and tessellation options).
3089    pub fn settings_ui(&self, ui: &mut Ui) {
3090        let prev_options = self.options(|o| o.clone());
3091        let mut options = prev_options.clone();
3092
3093        ui.collapsing("🔠 Font tweak", |ui| {
3094            self.fonts_tweak_ui(ui);
3095        });
3096
3097        options.ui(ui);
3098
3099        if options != prev_options {
3100            self.options_mut(move |o| *o = options);
3101        }
3102    }
3103
3104    fn fonts_tweak_ui(&self, ui: &mut Ui) {
3105        let mut font_definitions = self.write(|ctx| ctx.font_definitions.clone());
3106        let mut changed = false;
3107
3108        for (name, data) in &mut font_definitions.font_data {
3109            ui.collapsing(name, |ui| {
3110                let mut tweak = data.tweak;
3111                if tweak.ui(ui).changed() {
3112                    Arc::make_mut(data).tweak = tweak;
3113                    changed = true;
3114                }
3115            });
3116        }
3117
3118        if changed {
3119            self.set_fonts(font_definitions);
3120        }
3121    }
3122
3123    /// Show the state of egui, including its input and output.
3124    pub fn inspection_ui(&self, ui: &mut Ui) {
3125        use crate::containers::CollapsingHeader;
3126
3127        crate::Grid::new("egui-inspection-grid")
3128            .num_columns(2)
3129            .striped(true)
3130            .show(ui, |ui| {
3131                ui.label("Total ui frames:");
3132                ui.monospace(ui.ctx().cumulative_frame_nr().to_string());
3133                ui.end_row();
3134
3135                ui.label("Total ui passes:");
3136                ui.monospace(ui.ctx().cumulative_pass_nr().to_string());
3137                ui.end_row();
3138
3139                ui.label("Is using pointer")
3140                    .on_hover_text("Is egui currently using the pointer actively (e.g. dragging a slider)?");
3141                ui.monospace(self.is_using_pointer().to_string());
3142                ui.end_row();
3143
3144                ui.label("Wants pointer input")
3145                    .on_hover_text("Is egui currently interested in the location of the pointer (either because it is in use, or because it is hovering over a window).");
3146                ui.monospace(self.wants_pointer_input().to_string());
3147                ui.end_row();
3148
3149                ui.label("Wants keyboard input").on_hover_text("Is egui currently listening for text input?");
3150                ui.monospace(self.wants_keyboard_input().to_string());
3151                ui.end_row();
3152
3153                ui.label("Keyboard focus widget").on_hover_text("Is egui currently listening for text input?");
3154                ui.monospace(self.memory(|m| m.focused())
3155                    .as_ref()
3156                    .map(Id::short_debug_format)
3157                    .unwrap_or_default());
3158                ui.end_row();
3159
3160                let pointer_pos = self
3161                    .pointer_hover_pos()
3162                    .map_or_else(String::new, |pos| format!("{pos:?}"));
3163                ui.label("Pointer pos");
3164                ui.monospace(pointer_pos);
3165                ui.end_row();
3166
3167                let top_layer = self
3168                    .pointer_hover_pos()
3169                    .and_then(|pos| self.layer_id_at(pos))
3170                    .map_or_else(String::new, |layer| layer.short_debug_format());
3171                ui.label("Top layer under mouse");
3172                ui.monospace(top_layer);
3173                ui.end_row();
3174            });
3175
3176        ui.add_space(16.0);
3177
3178        ui.label(format!(
3179            "There are {} text galleys in the layout cache",
3180            self.fonts(|f| f.num_galleys_in_cache())
3181        ))
3182        .on_hover_text("This is approximately the number of text strings on screen");
3183        ui.add_space(16.0);
3184
3185        CollapsingHeader::new("🔃 Repaint Causes")
3186            .default_open(false)
3187            .show(ui, |ui| {
3188                ui.set_min_height(120.0);
3189                ui.label("What caused egui to repaint:");
3190                ui.add_space(8.0);
3191                let causes = ui.ctx().repaint_causes();
3192                for cause in causes {
3193                    ui.label(cause.to_string());
3194                }
3195            });
3196
3197        CollapsingHeader::new("📥 Input")
3198            .default_open(false)
3199            .show(ui, |ui| {
3200                let input = ui.input(|i| i.clone());
3201                input.ui(ui);
3202            });
3203
3204        CollapsingHeader::new("📊 Paint stats")
3205            .default_open(false)
3206            .show(ui, |ui| {
3207                let paint_stats = self.read(|ctx| ctx.paint_stats);
3208                paint_stats.ui(ui);
3209            });
3210
3211        CollapsingHeader::new("🖼 Textures")
3212            .default_open(false)
3213            .show(ui, |ui| {
3214                self.texture_ui(ui);
3215            });
3216
3217        CollapsingHeader::new("🖼 Image loaders")
3218            .default_open(false)
3219            .show(ui, |ui| {
3220                self.loaders_ui(ui);
3221            });
3222
3223        CollapsingHeader::new("🔠 Font texture")
3224            .default_open(false)
3225            .show(ui, |ui| {
3226                let font_image_size = self.fonts(|f| f.font_image_size());
3227                crate::introspection::font_texture_ui(ui, font_image_size);
3228            });
3229
3230        CollapsingHeader::new("Label text selection state")
3231            .default_open(false)
3232            .show(ui, |ui| {
3233                ui.label(format!(
3234                    "{:#?}",
3235                    *ui.ctx()
3236                        .plugin::<crate::text_selection::LabelSelectionState>()
3237                        .lock()
3238                ));
3239            });
3240
3241        CollapsingHeader::new("Interaction")
3242            .default_open(false)
3243            .show(ui, |ui| {
3244                let interact_widgets = self.write(|ctx| ctx.viewport().interact_widgets.clone());
3245                interact_widgets.ui(ui);
3246            });
3247    }
3248
3249    /// Show stats about the allocated textures.
3250    pub fn texture_ui(&self, ui: &mut crate::Ui) {
3251        let tex_mngr = self.tex_manager();
3252        let tex_mngr = tex_mngr.read();
3253
3254        let mut textures: Vec<_> = tex_mngr.allocated().collect();
3255        textures.sort_by_key(|(id, _)| *id);
3256
3257        let mut bytes = 0;
3258        for (_, tex) in &textures {
3259            bytes += tex.bytes_used();
3260        }
3261
3262        ui.label(format!(
3263            "{} allocated texture(s), using {:.1} MB",
3264            textures.len(),
3265            bytes as f64 * 1e-6
3266        ));
3267        let max_preview_size = vec2(48.0, 32.0);
3268
3269        let pixels_per_point = self.pixels_per_point();
3270
3271        ui.group(|ui| {
3272            ScrollArea::vertical()
3273                .max_height(300.0)
3274                .auto_shrink([false, true])
3275                .show(ui, |ui| {
3276                    ui.style_mut().override_text_style = Some(TextStyle::Monospace);
3277                    Grid::new("textures")
3278                        .striped(true)
3279                        .num_columns(4)
3280                        .spacing(vec2(16.0, 2.0))
3281                        .min_row_height(max_preview_size.y)
3282                        .show(ui, |ui| {
3283                            for (&texture_id, meta) in textures {
3284                                let [w, h] = meta.size;
3285                                let point_size = vec2(w as f32, h as f32) / pixels_per_point;
3286
3287                                let mut size = point_size;
3288                                size *= (max_preview_size.x / size.x).min(1.0);
3289                                size *= (max_preview_size.y / size.y).min(1.0);
3290                                ui.image(SizedTexture::new(texture_id, size))
3291                                    .on_hover_ui(|ui| {
3292                                        // show larger on hover
3293                                        let max_size = 0.5 * ui.ctx().content_rect().size();
3294                                        let mut size = point_size;
3295                                        size *= max_size.x / size.x.max(max_size.x);
3296                                        size *= max_size.y / size.y.max(max_size.y);
3297                                        ui.image(SizedTexture::new(texture_id, size));
3298                                    });
3299
3300                                ui.label(format!("{w} x {h}"));
3301                                ui.label(format!("{:.3} MB", meta.bytes_used() as f64 * 1e-6));
3302                                ui.label(format!("{:?}", meta.name));
3303                                ui.end_row();
3304                            }
3305                        });
3306                });
3307        });
3308    }
3309
3310    /// Show stats about different image loaders.
3311    pub fn loaders_ui(&self, ui: &mut crate::Ui) {
3312        struct LoaderInfo {
3313            id: String,
3314            byte_size: usize,
3315        }
3316
3317        let mut byte_loaders = vec![];
3318        let mut image_loaders = vec![];
3319        let mut texture_loaders = vec![];
3320
3321        {
3322            let loaders = self.loaders();
3323            let Loaders {
3324                include: _,
3325                bytes,
3326                image,
3327                texture,
3328            } = loaders.as_ref();
3329
3330            for loader in bytes.lock().iter() {
3331                byte_loaders.push(LoaderInfo {
3332                    id: loader.id().to_owned(),
3333                    byte_size: loader.byte_size(),
3334                });
3335            }
3336            for loader in image.lock().iter() {
3337                image_loaders.push(LoaderInfo {
3338                    id: loader.id().to_owned(),
3339                    byte_size: loader.byte_size(),
3340                });
3341            }
3342            for loader in texture.lock().iter() {
3343                texture_loaders.push(LoaderInfo {
3344                    id: loader.id().to_owned(),
3345                    byte_size: loader.byte_size(),
3346                });
3347            }
3348        }
3349
3350        fn loaders_ui(ui: &mut crate::Ui, title: &str, loaders: &[LoaderInfo]) {
3351            let heading = format!("{} {title} loaders", loaders.len());
3352            crate::CollapsingHeader::new(heading)
3353                .default_open(true)
3354                .show(ui, |ui| {
3355                    Grid::new("loaders")
3356                        .striped(true)
3357                        .num_columns(2)
3358                        .show(ui, |ui| {
3359                            ui.label("ID");
3360                            ui.label("Size");
3361                            ui.end_row();
3362
3363                            for loader in loaders {
3364                                ui.label(&loader.id);
3365                                ui.label(format!("{:.3} MB", loader.byte_size as f64 * 1e-6));
3366                                ui.end_row();
3367                            }
3368                        });
3369                });
3370        }
3371
3372        loaders_ui(ui, "byte", &byte_loaders);
3373        loaders_ui(ui, "image", &image_loaders);
3374        loaders_ui(ui, "texture", &texture_loaders);
3375    }
3376
3377    /// Shows the contents of [`Self::memory`].
3378    pub fn memory_ui(&self, ui: &mut crate::Ui) {
3379        if ui
3380            .button("Reset all")
3381            .on_hover_text("Reset all egui state")
3382            .clicked()
3383        {
3384            self.memory_mut(|mem| *mem = Default::default());
3385        }
3386
3387        let (num_state, num_serialized) = self.data(|d| (d.len(), d.count_serialized()));
3388        ui.label(format!(
3389            "{num_state} widget states stored (of which {num_serialized} are serialized)."
3390        ));
3391
3392        ui.horizontal(|ui| {
3393            ui.label(format!(
3394                "{} areas (panels, windows, popups, …)",
3395                self.memory(|mem| mem.areas().count())
3396            ));
3397            if ui.button("Reset").clicked() {
3398                self.memory_mut(|mem| *mem.areas_mut() = Default::default());
3399            }
3400        });
3401        ui.indent("layers", |ui| {
3402            ui.label("Layers, ordered back to front.");
3403            let layers_ids: Vec<LayerId> = self.memory(|mem| mem.areas().order().to_vec());
3404            for layer_id in layers_ids {
3405                if let Some(area) = AreaState::load(self, layer_id.id) {
3406                    let is_visible = self.memory(|mem| mem.areas().is_visible(&layer_id));
3407                    if !is_visible {
3408                        continue;
3409                    }
3410                    let text = format!("{} - {:?}", layer_id.short_debug_format(), area.rect(),);
3411                    // TODO(emilk): `Sense::hover_highlight()`
3412                    let response =
3413                        ui.add(Label::new(RichText::new(text).monospace()).sense(Sense::click()));
3414                    if response.hovered() && is_visible {
3415                        ui.ctx()
3416                            .debug_painter()
3417                            .debug_rect(area.rect(), Color32::RED, "");
3418                    }
3419                } else {
3420                    ui.monospace(layer_id.short_debug_format());
3421                }
3422            }
3423        });
3424
3425        ui.horizontal(|ui| {
3426            ui.label(format!(
3427                "{} collapsing headers",
3428                self.data(|d| d.count::<containers::collapsing_header::InnerState>())
3429            ));
3430            if ui.button("Reset").clicked() {
3431                self.data_mut(|d| d.remove_by_type::<containers::collapsing_header::InnerState>());
3432            }
3433        });
3434
3435        #[expect(deprecated)]
3436        ui.horizontal(|ui| {
3437            ui.label(format!(
3438                "{} menu bars",
3439                self.data(|d| d.count::<crate::menu::BarState>())
3440            ));
3441            if ui.button("Reset").clicked() {
3442                self.data_mut(|d| d.remove_by_type::<crate::menu::BarState>());
3443            }
3444        });
3445
3446        ui.horizontal(|ui| {
3447            ui.label(format!(
3448                "{} scroll areas",
3449                self.data(|d| d.count::<scroll_area::State>())
3450            ));
3451            if ui.button("Reset").clicked() {
3452                self.data_mut(|d| d.remove_by_type::<scroll_area::State>());
3453            }
3454        });
3455
3456        ui.horizontal(|ui| {
3457            ui.label(format!(
3458                "{} resize areas",
3459                self.data(|d| d.count::<resize::State>())
3460            ));
3461            if ui.button("Reset").clicked() {
3462                self.data_mut(|d| d.remove_by_type::<resize::State>());
3463            }
3464        });
3465
3466        ui.shrink_width_to_current(); // don't let the text below grow this window wider
3467        ui.label("NOTE: the position of this window cannot be reset from within itself.");
3468
3469        ui.collapsing("Interaction", |ui| {
3470            let interaction = self.memory(|mem| mem.interaction().clone());
3471            interaction.ui(ui);
3472        });
3473    }
3474}
3475
3476impl Context {
3477    /// Edit the [`Style`].
3478    pub fn style_ui(&self, ui: &mut Ui, theme: Theme) {
3479        let mut style: Style = (*self.style_of(theme)).clone();
3480        style.ui(ui);
3481        self.set_style_of(theme, style);
3482    }
3483}
3484
3485/// ## Accessibility
3486impl Context {
3487    /// If AccessKit support is active for the current frame, get or create
3488    /// a node builder with the specified ID and return a mutable reference to it.
3489    /// For newly created nodes, the parent is the parent [`Ui`]s ID.
3490    /// And an [`Ui`]s parent can be set with [`crate::UiBuilder::accessibility_parent`].
3491    ///
3492    /// The `Context` lock is held while the given closure is called!
3493    ///
3494    /// Returns `None` if acesskit is off.
3495    // TODO(emilk): consider making both read-only and read-write versions
3496    #[cfg(feature = "accesskit")]
3497    pub fn accesskit_node_builder<R>(
3498        &self,
3499        id: Id,
3500        writer: impl FnOnce(&mut accesskit::Node) -> R,
3501    ) -> Option<R> {
3502        self.write(|ctx| {
3503            ctx.viewport()
3504                .this_pass
3505                .accesskit_state
3506                .is_some()
3507                .then(|| ctx.accesskit_node_builder(id))
3508                .map(writer)
3509        })
3510    }
3511
3512    #[cfg(feature = "accesskit")]
3513    pub(crate) fn register_accesskit_parent(&self, id: Id, parent_id: Id) {
3514        self.write(|ctx| {
3515            if let Some(state) = ctx.viewport().this_pass.accesskit_state.as_mut() {
3516                state.parent_map.insert(id, parent_id);
3517            }
3518        });
3519    }
3520
3521    /// Enable generation of AccessKit tree updates in all future frames.
3522    #[cfg(feature = "accesskit")]
3523    pub fn enable_accesskit(&self) {
3524        self.write(|ctx| ctx.is_accesskit_enabled = true);
3525    }
3526
3527    /// Disable generation of AccessKit tree updates in all future frames.
3528    #[cfg(feature = "accesskit")]
3529    pub fn disable_accesskit(&self) {
3530        self.write(|ctx| ctx.is_accesskit_enabled = false);
3531    }
3532}
3533
3534/// ## Image loading
3535impl Context {
3536    /// Associate some static bytes with a `uri`.
3537    ///
3538    /// The same `uri` may be passed to [`Ui::image`] later to load the bytes as an image.
3539    ///
3540    /// By convention, the `uri` should start with `bytes://`.
3541    /// Following that convention will lead to better error messages.
3542    pub fn include_bytes(&self, uri: impl Into<Cow<'static, str>>, bytes: impl Into<Bytes>) {
3543        self.loaders().include.insert(uri, bytes);
3544    }
3545
3546    /// Returns `true` if the chain of bytes, image, or texture loaders
3547    /// contains a loader with the given `id`.
3548    pub fn is_loader_installed(&self, id: &str) -> bool {
3549        let loaders = self.loaders();
3550
3551        loaders.bytes.lock().iter().any(|l| l.id() == id)
3552            || loaders.image.lock().iter().any(|l| l.id() == id)
3553            || loaders.texture.lock().iter().any(|l| l.id() == id)
3554    }
3555
3556    /// Add a new bytes loader.
3557    ///
3558    /// It will be tried first, before any already installed loaders.
3559    ///
3560    /// See [`load`] for more information.
3561    pub fn add_bytes_loader(&self, loader: Arc<dyn load::BytesLoader + Send + Sync + 'static>) {
3562        self.loaders().bytes.lock().push(loader);
3563    }
3564
3565    /// Add a new image loader.
3566    ///
3567    /// It will be tried first, before any already installed loaders.
3568    ///
3569    /// See [`load`] for more information.
3570    pub fn add_image_loader(&self, loader: Arc<dyn load::ImageLoader + Send + Sync + 'static>) {
3571        self.loaders().image.lock().push(loader);
3572    }
3573
3574    /// Add a new texture loader.
3575    ///
3576    /// It will be tried first, before any already installed loaders.
3577    ///
3578    /// See [`load`] for more information.
3579    pub fn add_texture_loader(&self, loader: Arc<dyn load::TextureLoader + Send + Sync + 'static>) {
3580        self.loaders().texture.lock().push(loader);
3581    }
3582
3583    /// Release all memory and textures related to the given image URI.
3584    ///
3585    /// If you attempt to load the image again, it will be reloaded from scratch.
3586    /// Also this cancels any ongoing loading of the image.
3587    pub fn forget_image(&self, uri: &str) {
3588        use load::BytesLoader as _;
3589
3590        profiling::function_scope!();
3591
3592        let loaders = self.loaders();
3593
3594        loaders.include.forget(uri);
3595        for loader in loaders.bytes.lock().iter() {
3596            loader.forget(uri);
3597        }
3598        for loader in loaders.image.lock().iter() {
3599            loader.forget(uri);
3600        }
3601        for loader in loaders.texture.lock().iter() {
3602            loader.forget(uri);
3603        }
3604    }
3605
3606    /// Release all memory and textures related to images used in [`Ui::image`] or [`crate::Image`].
3607    ///
3608    /// If you attempt to load any images again, they will be reloaded from scratch.
3609    pub fn forget_all_images(&self) {
3610        use load::BytesLoader as _;
3611
3612        profiling::function_scope!();
3613
3614        let loaders = self.loaders();
3615
3616        loaders.include.forget_all();
3617        for loader in loaders.bytes.lock().iter() {
3618            loader.forget_all();
3619        }
3620        for loader in loaders.image.lock().iter() {
3621            loader.forget_all();
3622        }
3623        for loader in loaders.texture.lock().iter() {
3624            loader.forget_all();
3625        }
3626    }
3627
3628    /// Try loading the bytes from the given uri using any available bytes loaders.
3629    ///
3630    /// Loaders are expected to cache results, so that this call is immediate-mode safe.
3631    ///
3632    /// This calls the loaders one by one in the order in which they were registered.
3633    /// If a loader returns [`LoadError::NotSupported`][not_supported],
3634    /// then the next loader is called. This process repeats until all loaders have
3635    /// been exhausted, at which point this returns [`LoadError::NotSupported`][not_supported].
3636    ///
3637    /// # Errors
3638    /// This may fail with:
3639    /// - [`LoadError::NotSupported`][not_supported] if none of the registered loaders support loading the given `uri`.
3640    /// - [`LoadError::Loading`][custom] if one of the loaders _does_ support loading the `uri`, but the loading process failed.
3641    ///
3642    /// ⚠ May deadlock if called from within a `BytesLoader`!
3643    ///
3644    /// [not_supported]: crate::load::LoadError::NotSupported
3645    /// [custom]: crate::load::LoadError::Loading
3646    pub fn try_load_bytes(&self, uri: &str) -> load::BytesLoadResult {
3647        profiling::function_scope!(uri);
3648
3649        let loaders = self.loaders();
3650        let bytes_loaders = loaders.bytes.lock();
3651
3652        // Try most recently added loaders first (hence `.rev()`)
3653        for loader in bytes_loaders.iter().rev() {
3654            let result = loader.load(self, uri);
3655            match result {
3656                Err(load::LoadError::NotSupported) => {}
3657                _ => return result,
3658            }
3659        }
3660
3661        Err(load::LoadError::NoMatchingBytesLoader)
3662    }
3663
3664    /// Try loading the image from the given uri using any available image loaders.
3665    ///
3666    /// Loaders are expected to cache results, so that this call is immediate-mode safe.
3667    ///
3668    /// This calls the loaders one by one in the order in which they were registered.
3669    /// If a loader returns [`LoadError::NotSupported`][not_supported],
3670    /// then the next loader is called. This process repeats until all loaders have
3671    /// been exhausted, at which point this returns [`LoadError::NotSupported`][not_supported].
3672    ///
3673    /// # Errors
3674    /// This may fail with:
3675    /// - [`LoadError::NoImageLoaders`][no_image_loaders] if tbere are no registered image loaders.
3676    /// - [`LoadError::NotSupported`][not_supported] if none of the registered loaders support loading the given `uri`.
3677    /// - [`LoadError::Loading`][custom] if one of the loaders _does_ support loading the `uri`, but the loading process failed.
3678    ///
3679    /// ⚠ May deadlock if called from within an `ImageLoader`!
3680    ///
3681    /// [no_image_loaders]: crate::load::LoadError::NoImageLoaders
3682    /// [not_supported]: crate::load::LoadError::NotSupported
3683    /// [custom]: crate::load::LoadError::Loading
3684    pub fn try_load_image(&self, uri: &str, size_hint: load::SizeHint) -> load::ImageLoadResult {
3685        profiling::function_scope!(uri);
3686
3687        let loaders = self.loaders();
3688        let image_loaders = loaders.image.lock();
3689        if image_loaders.is_empty() {
3690            return Err(load::LoadError::NoImageLoaders);
3691        }
3692
3693        let mut format = None;
3694
3695        // Try most recently added loaders first (hence `.rev()`)
3696        for loader in image_loaders.iter().rev() {
3697            match loader.load(self, uri, size_hint) {
3698                Err(load::LoadError::NotSupported) => {}
3699                Err(load::LoadError::FormatNotSupported { detected_format }) => {
3700                    format = format.or(detected_format);
3701                }
3702                result => return result,
3703            }
3704        }
3705
3706        Err(load::LoadError::NoMatchingImageLoader {
3707            detected_format: format,
3708        })
3709    }
3710
3711    /// Try loading the texture from the given uri using any available texture loaders.
3712    ///
3713    /// Loaders are expected to cache results, so that this call is immediate-mode safe.
3714    ///
3715    /// This calls the loaders one by one in the order in which they were registered.
3716    /// If a loader returns [`LoadError::NotSupported`][not_supported],
3717    /// then the next loader is called. This process repeats until all loaders have
3718    /// been exhausted, at which point this returns [`LoadError::NotSupported`][not_supported].
3719    ///
3720    /// # Errors
3721    /// This may fail with:
3722    /// - [`LoadError::NotSupported`][not_supported] if none of the registered loaders support loading the given `uri`.
3723    /// - [`LoadError::Loading`][custom] if one of the loaders _does_ support loading the `uri`, but the loading process failed.
3724    ///
3725    /// ⚠ May deadlock if called from within a `TextureLoader`!
3726    ///
3727    /// [not_supported]: crate::load::LoadError::NotSupported
3728    /// [custom]: crate::load::LoadError::Loading
3729    pub fn try_load_texture(
3730        &self,
3731        uri: &str,
3732        texture_options: TextureOptions,
3733        size_hint: load::SizeHint,
3734    ) -> load::TextureLoadResult {
3735        profiling::function_scope!(uri);
3736
3737        let loaders = self.loaders();
3738        let texture_loaders = loaders.texture.lock();
3739
3740        // Try most recently added loaders first (hence `.rev()`)
3741        for loader in texture_loaders.iter().rev() {
3742            match loader.load(self, uri, texture_options, size_hint) {
3743                Err(load::LoadError::NotSupported) => {}
3744                result => return result,
3745            }
3746        }
3747
3748        Err(load::LoadError::NoMatchingTextureLoader)
3749    }
3750
3751    /// The loaders of bytes, images, and textures.
3752    pub fn loaders(&self) -> Arc<Loaders> {
3753        self.read(|this| this.loaders.clone())
3754    }
3755
3756    /// Returns `true` if any image is currently being loaded.
3757    pub fn has_pending_images(&self) -> bool {
3758        self.read(|this| {
3759            this.loaders.image.lock().iter().any(|i| i.has_pending())
3760                || this.loaders.bytes.lock().iter().any(|i| i.has_pending())
3761        })
3762    }
3763}
3764
3765/// ## Viewports
3766impl Context {
3767    /// Return the `ViewportId` of the current viewport.
3768    ///
3769    /// If this is the root viewport, this will return [`ViewportId::ROOT`].
3770    ///
3771    /// Don't use this outside of `Self::run`, or after `Self::end_pass`.
3772    pub fn viewport_id(&self) -> ViewportId {
3773        self.read(|ctx| ctx.viewport_id())
3774    }
3775
3776    /// Return the `ViewportId` of his parent.
3777    ///
3778    /// If this is the root viewport, this will return [`ViewportId::ROOT`].
3779    ///
3780    /// Don't use this outside of `Self::run`, or after `Self::end_pass`.
3781    pub fn parent_viewport_id(&self) -> ViewportId {
3782        self.read(|ctx| ctx.parent_viewport_id())
3783    }
3784
3785    /// Read the state of the current viewport.
3786    pub fn viewport<R>(&self, reader: impl FnOnce(&ViewportState) -> R) -> R {
3787        self.write(|ctx| reader(ctx.viewport()))
3788    }
3789
3790    /// Read the state of a specific current viewport.
3791    pub fn viewport_for<R>(
3792        &self,
3793        viewport_id: ViewportId,
3794        reader: impl FnOnce(&ViewportState) -> R,
3795    ) -> R {
3796        self.write(|ctx| reader(ctx.viewport_for(viewport_id)))
3797    }
3798
3799    /// For integrations: Set this to render a sync viewport.
3800    ///
3801    /// This will only set the callback for the current thread,
3802    /// which most likely should be the main thread.
3803    ///
3804    /// When an immediate viewport is created with [`Self::show_viewport_immediate`] it will be rendered by this function.
3805    ///
3806    /// When called, the integration needs to:
3807    /// * Check if there already is a window for this viewport id, and if not open one
3808    /// * Set the window attributes (position, size, …) based on [`ImmediateViewport::builder`].
3809    /// * Call [`Context::run`] with [`ImmediateViewport::viewport_ui_cb`].
3810    /// * Handle the output from [`Context::run`], including rendering
3811    pub fn set_immediate_viewport_renderer(
3812        callback: impl for<'a> Fn(&Self, ImmediateViewport<'a>) + 'static,
3813    ) {
3814        let callback = Box::new(callback);
3815        IMMEDIATE_VIEWPORT_RENDERER.with(|render_sync| {
3816            render_sync.replace(Some(callback));
3817        });
3818    }
3819
3820    /// If `true`, [`Self::show_viewport_deferred`] and [`Self::show_viewport_immediate`] will
3821    /// embed the new viewports inside the existing one, instead of spawning a new native window.
3822    ///
3823    /// `eframe` sets this to `false` on supported platforms, but the default value is `true`.
3824    pub fn embed_viewports(&self) -> bool {
3825        self.read(|ctx| ctx.embed_viewports)
3826    }
3827
3828    /// If `true`, [`Self::show_viewport_deferred`] and [`Self::show_viewport_immediate`] will
3829    /// embed the new viewports inside the existing one, instead of spawning a new native window.
3830    ///
3831    /// `eframe` sets this to `false` on supported platforms, but the default value is `true`.
3832    pub fn set_embed_viewports(&self, value: bool) {
3833        self.write(|ctx| ctx.embed_viewports = value);
3834    }
3835
3836    /// Send a command to the current viewport.
3837    ///
3838    /// This lets you affect the current viewport, e.g. resizing the window.
3839    pub fn send_viewport_cmd(&self, command: ViewportCommand) {
3840        self.send_viewport_cmd_to(self.viewport_id(), command);
3841    }
3842
3843    /// Send a command to a specific viewport.
3844    ///
3845    /// This lets you affect another viewport, e.g. resizing its window.
3846    pub fn send_viewport_cmd_to(&self, id: ViewportId, command: ViewportCommand) {
3847        self.request_repaint_of(id);
3848
3849        if command.requires_parent_repaint() {
3850            self.request_repaint_of(self.parent_viewport_id());
3851        }
3852
3853        self.write(|ctx| ctx.viewport_for(id).commands.push(command));
3854    }
3855
3856    /// Show a deferred viewport, creating a new native window, if possible.
3857    ///
3858    /// The given id must be unique for each viewport.
3859    ///
3860    /// You need to call this each pass when the child viewport should exist.
3861    ///
3862    /// You can check if the user wants to close the viewport by checking the
3863    /// [`crate::ViewportInfo::close_requested`] flags found in [`crate::InputState::viewport`].
3864    ///
3865    /// The given callback will be called whenever the child viewport needs repainting,
3866    /// e.g. on an event or when [`Self::request_repaint`] is called.
3867    /// This means it may be called multiple times, for instance while the
3868    /// parent viewport (the caller) is sleeping but the child viewport is animating.
3869    ///
3870    /// You will need to wrap your viewport state in an `Arc<RwLock<T>>` or `Arc<Mutex<T>>`.
3871    /// When this is called again with the same id in `ViewportBuilder` the render function for that viewport will be updated.
3872    ///
3873    /// You can also use [`Self::show_viewport_immediate`], which uses a simpler `FnOnce`
3874    /// with no need for `Send` or `Sync`. The downside is that it will require
3875    /// the parent viewport (the caller) to repaint anytime the child is repainted,
3876    /// and vice versa.
3877    ///
3878    /// If [`Context::embed_viewports`] is `true` (e.g. if the current egui
3879    /// backend does not support multiple viewports), the given callback
3880    /// will be called immediately, embedding the new viewport in the current one.
3881    /// You can check this with the [`ViewportClass`] given in the callback.
3882    /// If you find [`ViewportClass::Embedded`], you need to create a new [`crate::Window`] for you content.
3883    ///
3884    /// See [`crate::viewport`] for more information about viewports.
3885    pub fn show_viewport_deferred(
3886        &self,
3887        new_viewport_id: ViewportId,
3888        viewport_builder: ViewportBuilder,
3889        viewport_ui_cb: impl Fn(&Self, ViewportClass) + Send + Sync + 'static,
3890    ) {
3891        profiling::function_scope!();
3892
3893        if self.embed_viewports() {
3894            viewport_ui_cb(self, ViewportClass::Embedded);
3895        } else {
3896            self.write(|ctx| {
3897                ctx.viewport_parents
3898                    .insert(new_viewport_id, ctx.viewport_id());
3899
3900                let viewport = ctx.viewports.entry(new_viewport_id).or_default();
3901                viewport.class = ViewportClass::Deferred;
3902                viewport.builder = viewport_builder;
3903                viewport.used = true;
3904                viewport.viewport_ui_cb = Some(Arc::new(move |ctx| {
3905                    (viewport_ui_cb)(ctx, ViewportClass::Deferred);
3906                }));
3907            });
3908        }
3909    }
3910
3911    /// Show an immediate viewport, creating a new native window, if possible.
3912    ///
3913    /// This is the easier type of viewport to use, but it is less performant
3914    /// at it requires both parent and child to repaint if any one of them needs repainting,
3915    /// which effectively produce double work for two viewports, and triple work for three viewports, etc.
3916    /// To avoid this, use [`Self::show_viewport_deferred`] instead.
3917    ///
3918    /// The given id must be unique for each viewport.
3919    ///
3920    /// You need to call this each pass when the child viewport should exist.
3921    ///
3922    /// You can check if the user wants to close the viewport by checking the
3923    /// [`crate::ViewportInfo::close_requested`] flags found in [`crate::InputState::viewport`].
3924    ///
3925    /// The given ui function will be called immediately.
3926    /// This may only be called on the main thread.
3927    /// This call will pause the current viewport and render the child viewport in its own window.
3928    /// This means that the child viewport will not be repainted when the parent viewport is repainted, and vice versa.
3929    ///
3930    /// If [`Context::embed_viewports`] is `true` (e.g. if the current egui
3931    /// backend does not support multiple viewports), the given callback
3932    /// will be called immediately, embedding the new viewport in the current one.
3933    /// You can check this with the [`ViewportClass`] given in the callback.
3934    /// If you find [`ViewportClass::Embedded`], you need to create a new [`crate::Window`] for you content.
3935    ///
3936    /// See [`crate::viewport`] for more information about viewports.
3937    pub fn show_viewport_immediate<T>(
3938        &self,
3939        new_viewport_id: ViewportId,
3940        builder: ViewportBuilder,
3941        mut viewport_ui_cb: impl FnMut(&Self, ViewportClass) -> T,
3942    ) -> T {
3943        profiling::function_scope!();
3944
3945        if self.embed_viewports() {
3946            return viewport_ui_cb(self, ViewportClass::Embedded);
3947        }
3948
3949        IMMEDIATE_VIEWPORT_RENDERER.with(|immediate_viewport_renderer| {
3950            let immediate_viewport_renderer = immediate_viewport_renderer.borrow();
3951            let Some(immediate_viewport_renderer) = immediate_viewport_renderer.as_ref() else {
3952                // This egui backend does not support multiple viewports.
3953                return viewport_ui_cb(self, ViewportClass::Embedded);
3954            };
3955
3956            let ids = self.write(|ctx| {
3957                let parent_viewport_id = ctx.viewport_id();
3958
3959                ctx.viewport_parents
3960                    .insert(new_viewport_id, parent_viewport_id);
3961
3962                let viewport = ctx.viewports.entry(new_viewport_id).or_default();
3963                viewport.builder = builder.clone();
3964                viewport.used = true;
3965                viewport.viewport_ui_cb = None; // it is immediate
3966
3967                ViewportIdPair::from_self_and_parent(new_viewport_id, parent_viewport_id)
3968            });
3969
3970            let mut out = None;
3971            {
3972                let out = &mut out;
3973
3974                let viewport = ImmediateViewport {
3975                    ids,
3976                    builder,
3977                    viewport_ui_cb: Box::new(move |context| {
3978                        *out = Some(viewport_ui_cb(context, ViewportClass::Immediate));
3979                    }),
3980                };
3981
3982                immediate_viewport_renderer(self, viewport);
3983            }
3984
3985            out.expect(
3986                "egui backend is implemented incorrectly - the user callback was never called",
3987            )
3988        })
3989    }
3990}
3991
3992/// ## Interaction
3993impl Context {
3994    /// Read you what widgets are currently being interacted with.
3995    pub fn interaction_snapshot<R>(&self, reader: impl FnOnce(&InteractionSnapshot) -> R) -> R {
3996        self.write(|w| reader(&w.viewport().interact_widgets))
3997    }
3998
3999    /// The widget currently being dragged, if any.
4000    ///
4001    /// For widgets that sense both clicks and drags, this will
4002    /// not be set until the mouse cursor has moved a certain distance.
4003    ///
4004    /// NOTE: if the widget was released this pass, this will be `None`.
4005    /// Use [`Self::drag_stopped_id`] instead.
4006    pub fn dragged_id(&self) -> Option<Id> {
4007        self.interaction_snapshot(|i| i.dragged)
4008    }
4009
4010    /// Is this specific widget being dragged?
4011    ///
4012    /// A widget that sense both clicks and drags is only marked as "dragged"
4013    /// when the mouse has moved a bit
4014    ///
4015    /// See also: [`crate::Response::dragged`].
4016    pub fn is_being_dragged(&self, id: Id) -> bool {
4017        self.dragged_id() == Some(id)
4018    }
4019
4020    /// This widget just started being dragged this pass.
4021    ///
4022    /// The same widget should also be found in [`Self::dragged_id`].
4023    pub fn drag_started_id(&self) -> Option<Id> {
4024        self.interaction_snapshot(|i| i.drag_started)
4025    }
4026
4027    /// This widget was being dragged, but was released this pass
4028    pub fn drag_stopped_id(&self) -> Option<Id> {
4029        self.interaction_snapshot(|i| i.drag_stopped)
4030    }
4031
4032    /// Set which widget is being dragged.
4033    pub fn set_dragged_id(&self, id: Id) {
4034        self.write(|ctx| {
4035            let vp = ctx.viewport();
4036            let i = &mut vp.interact_widgets;
4037            if i.dragged != Some(id) {
4038                i.drag_stopped = i.dragged.or(i.drag_stopped);
4039                i.dragged = Some(id);
4040                i.drag_started = Some(id);
4041            }
4042
4043            ctx.memory.interaction_mut().potential_drag_id = Some(id);
4044        });
4045    }
4046
4047    /// Stop dragging any widget.
4048    pub fn stop_dragging(&self) {
4049        self.write(|ctx| {
4050            let vp = ctx.viewport();
4051            let i = &mut vp.interact_widgets;
4052            if i.dragged.is_some() {
4053                i.drag_stopped = i.dragged;
4054                i.dragged = None;
4055            }
4056
4057            ctx.memory.interaction_mut().potential_drag_id = None;
4058        });
4059    }
4060
4061    /// Is something else being dragged?
4062    ///
4063    /// Returns true if we are dragging something, but not the given widget.
4064    #[inline(always)]
4065    pub fn dragging_something_else(&self, not_this: Id) -> bool {
4066        let dragged = self.dragged_id();
4067        dragged.is_some() && dragged != Some(not_this)
4068    }
4069}
4070
4071#[test]
4072fn context_impl_send_sync() {
4073    fn assert_send_sync<T: Send + Sync>() {}
4074    assert_send_sync::<Context>();
4075}
4076
4077#[cfg(test)]
4078mod test {
4079    use super::Context;
4080
4081    #[test]
4082    fn test_single_pass() {
4083        let ctx = Context::default();
4084        ctx.options_mut(|o| o.max_passes = 1.try_into().unwrap());
4085
4086        // A single call, no request to discard:
4087        {
4088            let mut num_calls = 0;
4089            let output = ctx.run(Default::default(), |ctx| {
4090                num_calls += 1;
4091                assert_eq!(ctx.output(|o| o.num_completed_passes), 0);
4092                assert!(!ctx.output(|o| o.requested_discard()));
4093                assert!(!ctx.will_discard());
4094            });
4095            assert_eq!(num_calls, 1);
4096            assert_eq!(output.platform_output.num_completed_passes, 1);
4097            assert!(!output.platform_output.requested_discard());
4098        }
4099
4100        // A single call, with a denied request to discard:
4101        {
4102            let mut num_calls = 0;
4103            let output = ctx.run(Default::default(), |ctx| {
4104                num_calls += 1;
4105                ctx.request_discard("test");
4106                assert!(!ctx.will_discard(), "The request should have been denied");
4107            });
4108            assert_eq!(num_calls, 1);
4109            assert_eq!(output.platform_output.num_completed_passes, 1);
4110            assert!(
4111                output.platform_output.requested_discard(),
4112                "The request should be reported"
4113            );
4114            assert_eq!(
4115                output
4116                    .platform_output
4117                    .request_discard_reasons
4118                    .first()
4119                    .unwrap()
4120                    .reason,
4121                "test"
4122            );
4123        }
4124    }
4125
4126    #[test]
4127    fn test_dual_pass() {
4128        let ctx = Context::default();
4129        ctx.options_mut(|o| o.max_passes = 2.try_into().unwrap());
4130
4131        // Normal single pass:
4132        {
4133            let mut num_calls = 0;
4134            let output = ctx.run(Default::default(), |ctx| {
4135                assert_eq!(ctx.output(|o| o.num_completed_passes), 0);
4136                assert!(!ctx.output(|o| o.requested_discard()));
4137                assert!(!ctx.will_discard());
4138                num_calls += 1;
4139            });
4140            assert_eq!(num_calls, 1);
4141            assert_eq!(output.platform_output.num_completed_passes, 1);
4142            assert!(!output.platform_output.requested_discard());
4143        }
4144
4145        // Request discard once:
4146        {
4147            let mut num_calls = 0;
4148            let output = ctx.run(Default::default(), |ctx| {
4149                assert_eq!(ctx.output(|o| o.num_completed_passes), num_calls);
4150
4151                assert!(!ctx.will_discard());
4152                if num_calls == 0 {
4153                    ctx.request_discard("test");
4154                    assert!(ctx.will_discard());
4155                }
4156
4157                num_calls += 1;
4158            });
4159            assert_eq!(num_calls, 2);
4160            assert_eq!(output.platform_output.num_completed_passes, 2);
4161            assert!(
4162                !output.platform_output.requested_discard(),
4163                "The request should have been cleared when fulfilled"
4164            );
4165        }
4166
4167        // Request discard twice:
4168        {
4169            let mut num_calls = 0;
4170            let output = ctx.run(Default::default(), |ctx| {
4171                assert_eq!(ctx.output(|o| o.num_completed_passes), num_calls);
4172
4173                assert!(!ctx.will_discard());
4174                ctx.request_discard("test");
4175                if num_calls == 0 {
4176                    assert!(ctx.will_discard(), "First request granted");
4177                } else {
4178                    assert!(!ctx.will_discard(), "Second request should be denied");
4179                }
4180
4181                num_calls += 1;
4182            });
4183            assert_eq!(num_calls, 2);
4184            assert_eq!(output.platform_output.num_completed_passes, 2);
4185            assert!(
4186                output.platform_output.requested_discard(),
4187                "The unfulfilled request should be reported"
4188            );
4189        }
4190    }
4191
4192    #[test]
4193    fn test_multi_pass() {
4194        let ctx = Context::default();
4195        ctx.options_mut(|o| o.max_passes = 10.try_into().unwrap());
4196
4197        // Request discard three times:
4198        {
4199            let mut num_calls = 0;
4200            let output = ctx.run(Default::default(), |ctx| {
4201                assert_eq!(ctx.output(|o| o.num_completed_passes), num_calls);
4202
4203                assert!(!ctx.will_discard());
4204                if num_calls <= 2 {
4205                    ctx.request_discard("test");
4206                    assert!(ctx.will_discard());
4207                }
4208
4209                num_calls += 1;
4210            });
4211            assert_eq!(num_calls, 4);
4212            assert_eq!(output.platform_output.num_completed_passes, 4);
4213            assert!(
4214                !output.platform_output.requested_discard(),
4215                "The request should have been cleared when fulfilled"
4216            );
4217        }
4218    }
4219}