1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00
Files
egui/crates/egui_inspection/src/lib.rs
Lucas Meurer 86fcffb229 Add egui_inspection protocol and plugin (#8234)
Introduces live inspection for running egui apps over a small TCP
request/response protocol, plus the `egui::Plugin` that serves it.

This is the minimal surface to get the egui mcp in, we may want to
extend this in the future to add support for the inspection gui.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 09:26:18 +00:00

51 lines
1.8 KiB
Rust

#![cfg_attr(doc, doc = include_str!("../README.md"))]
//!
//! ## Feature flags
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
pub mod protocol;
pub use protocol::{
EncodedPng, MAX_MESSAGE_BYTES, PROTOCOL_MAGIC, PROTOCOL_VERSION, Request, Response,
read_message, write_message,
};
/// The single environment variable that controls inspection.
///
/// Interpreted by [`bind_addr_from_env`]: falsy (unset / empty / `0` / `false`) disables it;
/// truthy (`1` / `true`) enables it on [`DEFAULT_INSPECTION_ADDR`]; anything else is taken as
/// a `host:port` bind address (e.g. `0.0.0.0:5719` to expose it across the network).
pub const INSPECTION_ENV_VAR: &str = "EGUI_INSPECTION";
/// Default bind address used when [`INSPECTION_ENV_VAR`] is just truthy.
///
/// Loopback only, on a fixed well-known port. The `egui_mcp` server defaults its `attach` to
/// this same port.
pub const DEFAULT_INSPECTION_ADDR: &str = "127.0.0.1:5719";
#[cfg(feature = "png")]
mod png;
#[cfg(feature = "plugin")]
mod plugin;
#[cfg(feature = "plugin")]
pub use plugin::InspectionPlugin;
#[cfg(all(feature = "plugin", not(target_arch = "wasm32")))]
pub use plugin::{attach_from_env, serve};
/// Resolve the bind address from [`INSPECTION_ENV_VAR`], returning `None` when inspection is
/// disabled. Used by [`attach_from_env`] and eframe's auto-attach.
#[cfg(feature = "plugin")]
pub fn bind_addr_from_env() -> Option<String> {
let value = std::env::var(INSPECTION_ENV_VAR).ok()?;
match value.trim() {
"" | "0" => None,
v if v.eq_ignore_ascii_case("false") => None,
"1" => Some(DEFAULT_INSPECTION_ADDR.to_owned()),
v if v.eq_ignore_ascii_case("true") => Some(DEFAULT_INSPECTION_ADDR.to_owned()),
addr => Some(addr.to_owned()),
}
}