1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Add RawInput::is_visible and InputState::is_visible helpers

Shorthand for `viewport().visible().unwrap_or(true)`, which was
repeated at every call site.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Emil Ernerfeldt
2026-08-04 17:08:50 +02:00
parent 51047205d8
commit 4d444aafb4
5 changed files with 28 additions and 6 deletions

View File

@@ -460,7 +460,7 @@ impl ContextImpl {
// If the window is minimized or occluded, the integration may still run passes // If the window is minimized or occluded, the integration may still run passes
// to keep app logic ticking, but without showing any ui. // to keep app logic ticking, but without showing any ui.
// We then skip all book-keeping that assumes ui was shown. // We then skip all book-keeping that assumes ui was shown.
let is_visible = new_raw_input.viewport().visible().unwrap_or(true); let is_visible = new_raw_input.is_visible();
self.memory.begin_pass(&new_raw_input, &all_viewport_ids); self.memory.begin_pass(&new_raw_input, &all_viewport_ids);
@@ -836,7 +836,7 @@ impl Context {
fn run_dyn(&self, mut new_input: RawInput, run_ui: &mut dyn FnMut(&Self)) -> FullOutput { fn run_dyn(&self, mut new_input: RawInput, run_ui: &mut dyn FnMut(&Self)) -> FullOutput {
profiling::function_scope!(); profiling::function_scope!();
let viewport_id = new_input.viewport_id; let viewport_id = new_input.viewport_id;
let is_visible = new_input.viewport().visible().unwrap_or(true); let is_visible = new_input.is_visible();
let max_passes = self.write(|ctx| ctx.memory.options.max_passes.get()); let max_passes = self.write(|ctx| ctx.memory.options.max_passes.get());
let mut output = FullOutput::default(); let mut output = FullOutput::default();
@@ -1751,7 +1751,7 @@ impl Context {
/// ///
/// See [`ViewportInfo::visible`]. /// See [`ViewportInfo::visible`].
pub fn is_visible(&self) -> bool { pub fn is_visible(&self) -> bool {
self.input(|i| i.viewport().visible().unwrap_or(true)) self.input(|i| i.is_visible())
} }
/// The total number of completed passes (usually there is one pass per rendered frame). /// The total number of completed passes (usually there is one pass per rendered frame).
@@ -2652,7 +2652,7 @@ impl ContextImpl {
// If nothing was shown this pass we skip all book-keeping that assumes ui was shown. // If nothing was shown this pass we skip all book-keeping that assumes ui was shown.
// See [`Context::is_visible`]. // See [`Context::is_visible`].
let is_visible = viewport.input.viewport().visible().unwrap_or(true); let is_visible = viewport.input.is_visible();
if is_visible { if is_visible {
// A pass without any ui uses no images and no widget ids, // A pass without any ui uses no images and no widget ids,

View File

@@ -117,6 +117,17 @@ impl RawInput {
self.viewports.get(&self.viewport_id).expect("Failed to find current viewport in egui RawInput. This is the fault of the egui backend") self.viewports.get(&self.viewport_id).expect("Failed to find current viewport in egui RawInput. This is the fault of the egui backend")
} }
/// Is the active viewport visible, i.e. neither minimized nor occluded?
///
/// Defaults to `true` if the integration doesn't report
/// [`ViewportInfo::minimized`] and [`ViewportInfo::occluded`].
///
/// See [`crate::Context::is_visible`].
#[inline]
pub fn is_visible(&self) -> bool {
self.viewport().visible().unwrap_or(true)
}
/// Helper: move volatile (deltas and events), clone the rest. /// Helper: move volatile (deltas and events), clone the rest.
/// ///
/// * [`Self::hovered_files`] is cloned. /// * [`Self::hovered_files`] is cloned.

View File

@@ -501,6 +501,17 @@ impl InputState {
self.raw.viewport() self.raw.viewport()
} }
/// Is the active viewport visible, i.e. neither minimized nor occluded?
///
/// Defaults to `true` if the integration doesn't report
/// [`ViewportInfo::minimized`] and [`ViewportInfo::occluded`].
///
/// See [`crate::Context::is_visible`].
#[inline]
pub fn is_visible(&self) -> bool {
self.raw.is_visible()
}
/// Returns the region of the screen that is safe for content rendering /// Returns the region of the screen that is safe for content rendering
/// ///
/// Returns the `viewport_rect` with the `safe_area_insets` removed. /// Returns the `viewport_rect` with the `safe_area_insets` removed.

View File

@@ -797,7 +797,7 @@ impl Memory {
self.options.begin_pass(new_raw_input); self.options.begin_pass(new_raw_input);
if new_raw_input.viewport().visible().unwrap_or(true) { if new_raw_input.is_visible() {
// When nothing is shown, no widget will ask for focus, // When nothing is shown, no widget will ask for focus,
// so leave the focus state alone. See [`crate::Context::is_visible`]. // so leave the focus state alone. See [`crate::Context::is_visible`].
self.focus self.focus

View File

@@ -259,7 +259,7 @@ impl<'a, State> Harness<'a, State> {
fn _step(&mut self, sizing_pass: bool) { fn _step(&mut self, sizing_pass: bool) {
self.input.predicted_dt = self.step_dt; self.input.predicted_dt = self.step_dt;
let is_visible = self.input.viewport().visible().unwrap_or(true); let is_visible = self.input.is_visible();
let mut output = self.ctx.run_ui(self.input.take(), |ui| { let mut output = self.ctx.run_ui(self.input.take(), |ui| {
self.response = self.app.run(ui, &mut self.state, sizing_pass); self.response = self.app.run(ui, &mut self.state, sizing_pass);