pub struct ViewCtx<'a, Msg> {
pub ui: &'a mut Ui,
/* private fields */
}Expand description
Fields§
§ui: &'a mut UiThe egui UI handle
Implementations§
Source§impl<'a, ParentMsg> ViewCtx<'a, ParentMsg>
Extension trait for ViewCtx to mount components
impl<'a, ParentMsg> ViewCtx<'a, ParentMsg>
Extension trait for ViewCtx to mount components
Source§impl<'a, Msg> ViewCtx<'a, Msg>
impl<'a, Msg> ViewCtx<'a, Msg>
Navigate to a page (convenience method)
Sourcepub fn router_back<P>(&mut self, to_msg: impl FnOnce(RouterMsg<P>) -> Msg)
pub fn router_back<P>(&mut self, to_msg: impl FnOnce(RouterMsg<P>) -> Msg)
Go back in router history
Sourcepub fn router_forward<P>(&mut self, to_msg: impl FnOnce(RouterMsg<P>) -> Msg)
pub fn router_forward<P>(&mut self, to_msg: impl FnOnce(RouterMsg<P>) -> Msg)
Go forward in router history
Source§impl<'a, Msg> ViewCtx<'a, Msg>
impl<'a, Msg> ViewCtx<'a, Msg>
Sourcepub fn emit_all(&mut self, msgs: impl IntoIterator<Item = Msg>)
pub fn emit_all(&mut self, msgs: impl IntoIterator<Item = Msg>)
Emit multiple messages
Sourcepub fn emit_result<T, E>(
&mut self,
result: Result<T, E>,
on_ok: impl FnOnce(T) -> Msg,
on_err: impl FnOnce(E) -> Msg,
)
pub fn emit_result<T, E>( &mut self, result: Result<T, E>, on_ok: impl FnOnce(T) -> Msg, on_err: impl FnOnce(E) -> Msg, )
Sourcepub fn emit_if_err<T, E>(
&mut self,
result: Result<T, E>,
on_err: impl FnOnce(E) -> Msg,
)
pub fn emit_if_err<T, E>( &mut self, result: Result<T, E>, on_err: impl FnOnce(E) -> Msg, )
Source§impl<'a, Msg> ViewCtx<'a, Msg>
impl<'a, Msg> ViewCtx<'a, Msg>
Button that emits a message when clicked
Sourcepub fn horizontal<R>(&mut self, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R) -> R
pub fn horizontal<R>(&mut self, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R) -> R
Horizontal layout
Sourcepub fn group<R>(&mut self, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R) -> R
pub fn group<R>(&mut self, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R) -> R
Group (framed region)
Sourcepub fn collapsing<R>(
&mut self,
heading: impl Into<String>,
f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
) -> Option<R>
pub fn collapsing<R>( &mut self, heading: impl Into<String>, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, ) -> Option<R>
Collapsing header
Sourcepub fn scroll_area<R>(
&mut self,
f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
) -> R
pub fn scroll_area<R>( &mut self, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, ) -> R
Scroll area (vertical, default settings)
Sourcepub fn scroll_area_id<R>(
&mut self,
id: impl Hash,
f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
) -> R
pub fn scroll_area_id<R>( &mut self, id: impl Hash, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, ) -> R
Scroll area with custom id (avoids ID clashes)
Sourcepub fn scroll_area_with<R>(
&mut self,
builder: impl FnOnce(ScrollArea) -> ScrollArea,
f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
) -> R
pub fn scroll_area_with<R>( &mut self, builder: impl FnOnce(ScrollArea) -> ScrollArea, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, ) -> R
Two-panel layout with right sidebar
Sourcepub fn top_panel_layout(
&mut self,
id: impl Into<Id>,
top: impl FnOnce(&mut ViewCtx<'_, Msg>),
main: impl FnOnce(&mut ViewCtx<'_, Msg>),
)
pub fn top_panel_layout( &mut self, id: impl Into<Id>, top: impl FnOnce(&mut ViewCtx<'_, Msg>), main: impl FnOnce(&mut ViewCtx<'_, Msg>), )
Top + Main panel layout
Sourcepub fn two_columns(
&mut self,
left: impl FnOnce(&mut ViewCtx<'_, Msg>),
right: impl FnOnce(&mut ViewCtx<'_, Msg>),
)
pub fn two_columns( &mut self, left: impl FnOnce(&mut ViewCtx<'_, Msg>), right: impl FnOnce(&mut ViewCtx<'_, Msg>), )
Two-column layout using allocate_ui_at_rect
Divides the available space into two equal columns. Each column gets its own ViewCtx with full emit() capability.
§Example
ctx.two_columns(
|ctx| {
ctx.ui.label("Left column");
ctx.button("Click", Msg::LeftClicked);
},
|ctx| {
ctx.ui.label("Right column");
ctx.button("Click", Msg::RightClicked);
},
);Sourcepub fn three_columns(
&mut self,
col1: impl FnOnce(&mut ViewCtx<'_, Msg>),
col2: impl FnOnce(&mut ViewCtx<'_, Msg>),
col3: impl FnOnce(&mut ViewCtx<'_, Msg>),
)
pub fn three_columns( &mut self, col1: impl FnOnce(&mut ViewCtx<'_, Msg>), col2: impl FnOnce(&mut ViewCtx<'_, Msg>), col3: impl FnOnce(&mut ViewCtx<'_, Msg>), )
Three-column layout
Divides the available space into three equal columns.
Sourcepub fn four_columns(
&mut self,
col1: impl FnOnce(&mut ViewCtx<'_, Msg>),
col2: impl FnOnce(&mut ViewCtx<'_, Msg>),
col3: impl FnOnce(&mut ViewCtx<'_, Msg>),
col4: impl FnOnce(&mut ViewCtx<'_, Msg>),
)
pub fn four_columns( &mut self, col1: impl FnOnce(&mut ViewCtx<'_, Msg>), col2: impl FnOnce(&mut ViewCtx<'_, Msg>), col3: impl FnOnce(&mut ViewCtx<'_, Msg>), col4: impl FnOnce(&mut ViewCtx<'_, Msg>), )
Four-column layout
Divides the available space into four equal columns.
Sourcepub fn columns(
&mut self,
columns: Vec<Box<dyn FnOnce(&mut ViewCtx<'_, Msg>) + '_>>,
)
pub fn columns( &mut self, columns: Vec<Box<dyn FnOnce(&mut ViewCtx<'_, Msg>) + '_>>, )
Variable-length column layout
Divides the available space into N equal columns. Use this when you need more than 4 columns or dynamic column count.
§Example
ctx.columns(vec![
Box::new(|ctx| { ctx.ui.label("Col 1"); }),
Box::new(|ctx| { ctx.ui.label("Col 2"); }),
Box::new(|ctx| { ctx.ui.label("Col 3"); }),
Box::new(|ctx| { ctx.ui.label("Col 4"); }),
Box::new(|ctx| { ctx.ui.label("Col 5"); }),
]);Sourcepub fn show_if<R>(
&mut self,
condition: bool,
f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
) -> Option<R>
pub fn show_if<R>( &mut self, condition: bool, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, ) -> Option<R>
Sourcepub fn show_if_else<R>(
&mut self,
condition: bool,
if_true: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
if_false: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
) -> R
pub fn show_if_else<R>( &mut self, condition: bool, if_true: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, if_false: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, ) -> R
Sourcepub fn enabled_if<R>(
&mut self,
enabled: bool,
f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R,
) -> R
pub fn enabled_if<R>( &mut self, enabled: bool, f: impl FnOnce(&mut ViewCtx<'_, Msg>) -> R, ) -> R
Source§impl<'a, Msg> ViewCtx<'a, Msg>
impl<'a, Msg> ViewCtx<'a, Msg>
Sourcepub fn on_shortcut(&mut self, shortcut: KeyboardShortcut, msg: Msg) -> bool
pub fn on_shortcut(&mut self, shortcut: KeyboardShortcut, msg: Msg) -> bool
Check if a keyboard shortcut was pressed and emit a message
Uses consume_shortcut internally, so the shortcut won’t trigger
other handlers after being consumed.
§Example
use egui::{Key, KeyboardShortcut, Modifiers};
// Define shortcuts
const SAVE: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::S);
const UNDO: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Z);
// In view function
ctx.on_shortcut(SAVE, Msg::Save);
ctx.on_shortcut(UNDO, Msg::Undo);Sourcepub fn on_shortcuts(&mut self, shortcuts: &[(KeyboardShortcut, Msg)])where
Msg: Clone,
pub fn on_shortcuts(&mut self, shortcuts: &[(KeyboardShortcut, Msg)])where
Msg: Clone,
Sourcepub fn on_binding(&mut self, binding: &impl InputBinding, msg: Msg) -> bool
pub fn on_binding(&mut self, binding: &impl InputBinding, msg: Msg) -> bool
Check if an input binding was triggered and emit a message.
Works with any type implementing InputBinding, including
KeyboardShortcut, DynamicShortcut, and ShortcutGroup.
§Example
use egui_cha::bindings::{DynamicShortcut, InputBinding};
let custom = DynamicShortcut::new(Modifiers::CTRL, Key::K);
ctx.on_binding(&custom, Msg::Custom);
// Also works with static shortcuts
ctx.on_binding(&shortcuts::SAVE, Msg::Save);Sourcepub fn on_action<A>(
&mut self,
bindings: &ActionBindings<A>,
action: &A,
msg: Msg,
) -> bool
pub fn on_action<A>( &mut self, bindings: &ActionBindings<A>, action: &A, msg: Msg, ) -> bool
Check if an action from ActionBindings was triggered.
This is the preferred way to handle keyboard shortcuts when using the dynamic binding system.
§Example
use egui_cha::bindings::ActionBindings;
#[derive(Clone, PartialEq, Eq, Hash)]
enum Action { Save, Undo, Redo }
let bindings = ActionBindings::new()
.with_default(Action::Save, shortcuts::SAVE)
.with_default(Action::Undo, shortcuts::UNDO);
// In view function
ctx.on_action(&bindings, &Action::Save, Msg::Save);
ctx.on_action(&bindings, &Action::Undo, Msg::Undo);