Skip to main content

ContextMenuState

Struct ContextMenuState 

Source
pub struct ContextMenuState { /* private fields */ }
Expand description

Manages context menu state

Tracks the currently active menu, hover state, and handles click detection. Only one context menu can be active at a time.

Implementations§

Source§

impl ContextMenuState

Source

pub fn new() -> Self

Create a new context menu state

§Example
let mut menu = ContextMenuState::new();
Source

pub fn open(&mut self, position: (f64, f64), items: Vec<ContextMenuItem>)

Open a context menu at the specified position

If a menu is already open, it will be replaced with the new menu.

§Example
let items = vec![
    ContextMenuItem::new("copy", "Copy"),
    ContextMenuItem::new("paste", "Paste"),
];
menu.open((100.0, 200.0), items);
Source

pub fn open_for_widget( &mut self, position: (f64, f64), items: Vec<ContextMenuItem>, widget: WidgetId, )

Open a context menu with a source widget

This allows tracking which widget triggered the menu, which can be useful for context-specific actions.

§Example
let widget_id = WidgetId::new("my_widget");
menu.open_for_widget((100.0, 200.0), items, widget_id);
Source

pub fn close(&mut self)

Close the context menu

Clears the active menu and all associated state.

§Example
menu.close();
Source

pub fn is_open(&self) -> bool

Check if a context menu is currently open

§Example
if menu.is_open() {
    // Render menu
}
Source

pub fn get_active(&self) -> Option<&ContextMenuRequest>

Get the active menu request

Returns None if no menu is currently open.

§Example
if let Some(request) = menu.get_active() {
    for item in &request.items {
        println!("{}", item.label);
    }
}
Source

pub fn set_menu_rect(&mut self, rect: (f64, f64, f64, f64))

Set the menu rectangle for hit testing

This should be called after layout to enable proper click detection. The rectangle is in screen coordinates (x, y, width, height).

§Example
menu.set_menu_rect((100.0, 200.0, 150.0, 100.0));
Source

pub fn get_menu_rect(&self) -> Option<(f64, f64, f64, f64)>

Get the current menu rectangle

Source

pub fn set_hovered(&mut self, index: Option<usize>)

Set the currently hovered item

Pass None to clear hover state.

§Example
menu.set_hovered(Some(2)); // Hover third item
menu.set_hovered(None);     // Clear hover
Source

pub fn get_hovered(&self) -> Option<usize>

Get the currently hovered item index

Returns None if no item is hovered.

§Example
if let Some(index) = menu.get_hovered() {
    println!("Hovering item {}", index);
}
Source

pub fn handle_click(&mut self, index: usize) -> Option<String>

Handle a click on a menu item

Returns the item ID if the item is enabled and was clicked successfully. Returns None if the item is disabled or out of bounds.

§Example
if let Some(item_id) = menu.handle_click(0) {
    match item_id.as_str() {
        "copy" => copy_to_clipboard(),
        "paste" => paste_from_clipboard(),
        _ => {}
    }
    menu.close();
}
Source

pub fn item_count(&self) -> usize

Get the number of items in the active menu

Returns 0 if no menu is open.

§Example
let count = menu.item_count();
Source

pub fn get_item(&self, index: usize) -> Option<&ContextMenuItem>

Get a menu item by index

Returns None if the index is out of bounds or no menu is open.

§Example
if let Some(item) = menu.get_item(0) {
    println!("First item: {}", item.label);
}

Trait Implementations§

Source§

impl Clone for ContextMenuState

Source§

fn clone(&self) -> ContextMenuState

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ContextMenuState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ContextMenuState

Source§

fn default() -> ContextMenuState

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

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.