Skip to main content

SelectedItem

Struct SelectedItem 

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

A menu item that was selected by the user via ContextMenu::show or ContextMenu::show_at.

Call execute to run the associated shell command, or inspect menu_item to decide first.

The SelectedItem keeps the hidden helper window alive until it is dropped or consumed by execute, ensuring the owner HWND remains valid for commands that display UI (e.g., Properties dialog).

Implementations§

Source§

impl SelectedItem

Source

pub fn menu_item(&self) -> &MenuItem

Get the menu item metadata (label, verb, flags, etc.).

Examples found in repository?
examples/basic.rs (line 19)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let path = std::env::args()
7        .nth(1)
8        .unwrap_or_else(|| r"C:\Windows\notepad.exe".to_string());
9
10    println!("Showing context menu for: {}", path);
11
12    let items = ShellItems::from_path(&path)?;
13    let menu = ContextMenu::new(items)?;
14
15    match menu.show()? {
16        Some(selected) => {
17            println!(
18                "Selected: {} (verb: {:?})",
19                selected.menu_item().label,
20                selected.menu_item().command_string
21            );
22            selected.execute()?;
23        }
24        None => println!("No item selected."),
25    }
26
27    Ok(())
28}
More examples
Hide additional examples
examples/folder_background.rs (line 19)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let folder = std::env::args()
7        .nth(1)
8        .unwrap_or_else(|| r"C:\Windows".to_string());
9
10    println!("Showing background context menu for folder: {}", folder);
11
12    let items = ShellItems::folder_background(&folder)?;
13    let menu = ContextMenu::new(items)?;
14
15    match menu.show()? {
16        Some(selected) => {
17            println!(
18                "Selected: {} (verb: {:?})",
19                selected.menu_item().label,
20                selected.menu_item().command_string
21            );
22            selected.execute()?;
23        }
24        None => println!("No item selected."),
25    }
26
27    Ok(())
28}
examples/extended.rs (line 19)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let path = std::env::args()
7        .nth(1)
8        .unwrap_or_else(|| r"C:\Windows\notepad.exe".to_string());
9
10    println!("Showing extended context menu (Shift+right-click) for: {}", path);
11
12    let items = ShellItems::from_path(&path)?;
13    let menu = ContextMenu::new(items)?.extended(true);
14
15    match menu.show()? {
16        Some(selected) => {
17            println!(
18                "Selected: {} (verb: {:?})",
19                selected.menu_item().label,
20                selected.menu_item().command_string
21            );
22            selected.execute()?;
23        }
24        None => println!("No item selected."),
25    }
26
27    Ok(())
28}
examples/multi_select.rs (line 28)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let args: Vec<String> = std::env::args().skip(1).collect();
7    let paths: Vec<String> = if args.is_empty() {
8        vec![
9            r"C:\Windows\notepad.exe".to_string(),
10            r"C:\Windows\regedit.exe".to_string(),
11        ]
12    } else {
13        args
14    };
15
16    println!("Showing context menu for {} items:", paths.len());
17    for p in &paths {
18        println!("  - {}", p);
19    }
20
21    let items = ShellItems::from_paths(&paths)?;
22    let menu = ContextMenu::new(items)?;
23
24    match menu.show()? {
25        Some(selected) => {
26            println!(
27                "Selected: {} (verb: {:?})",
28                selected.menu_item().label,
29                selected.menu_item().command_string
30            );
31            selected.execute()?;
32        }
33        None => println!("No item selected."),
34    }
35
36    Ok(())
37}
Source

pub fn command_id(&self) -> u32

Get the raw command ID that was returned by TrackPopupMenu.

Source

pub fn execute(self) -> Result<()>

Execute the selected command with default parameters.

This is equivalent to the user clicking the menu item in Explorer. Consumes self because a command can only be invoked once.

Examples found in repository?
examples/basic.rs (line 22)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let path = std::env::args()
7        .nth(1)
8        .unwrap_or_else(|| r"C:\Windows\notepad.exe".to_string());
9
10    println!("Showing context menu for: {}", path);
11
12    let items = ShellItems::from_path(&path)?;
13    let menu = ContextMenu::new(items)?;
14
15    match menu.show()? {
16        Some(selected) => {
17            println!(
18                "Selected: {} (verb: {:?})",
19                selected.menu_item().label,
20                selected.menu_item().command_string
21            );
22            selected.execute()?;
23        }
24        None => println!("No item selected."),
25    }
26
27    Ok(())
28}
More examples
Hide additional examples
examples/folder_background.rs (line 22)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let folder = std::env::args()
7        .nth(1)
8        .unwrap_or_else(|| r"C:\Windows".to_string());
9
10    println!("Showing background context menu for folder: {}", folder);
11
12    let items = ShellItems::folder_background(&folder)?;
13    let menu = ContextMenu::new(items)?;
14
15    match menu.show()? {
16        Some(selected) => {
17            println!(
18                "Selected: {} (verb: {:?})",
19                selected.menu_item().label,
20                selected.menu_item().command_string
21            );
22            selected.execute()?;
23        }
24        None => println!("No item selected."),
25    }
26
27    Ok(())
28}
examples/extended.rs (line 22)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let path = std::env::args()
7        .nth(1)
8        .unwrap_or_else(|| r"C:\Windows\notepad.exe".to_string());
9
10    println!("Showing extended context menu (Shift+right-click) for: {}", path);
11
12    let items = ShellItems::from_path(&path)?;
13    let menu = ContextMenu::new(items)?.extended(true);
14
15    match menu.show()? {
16        Some(selected) => {
17            println!(
18                "Selected: {} (verb: {:?})",
19                selected.menu_item().label,
20                selected.menu_item().command_string
21            );
22            selected.execute()?;
23        }
24        None => println!("No item selected."),
25    }
26
27    Ok(())
28}
examples/multi_select.rs (line 31)
3fn main() -> win_context_menu::Result<()> {
4    let _com = init_com()?;
5
6    let args: Vec<String> = std::env::args().skip(1).collect();
7    let paths: Vec<String> = if args.is_empty() {
8        vec![
9            r"C:\Windows\notepad.exe".to_string(),
10            r"C:\Windows\regedit.exe".to_string(),
11        ]
12    } else {
13        args
14    };
15
16    println!("Showing context menu for {} items:", paths.len());
17    for p in &paths {
18        println!("  - {}", p);
19    }
20
21    let items = ShellItems::from_paths(&paths)?;
22    let menu = ContextMenu::new(items)?;
23
24    match menu.show()? {
25        Some(selected) => {
26            println!(
27                "Selected: {} (verb: {:?})",
28                selected.menu_item().label,
29                selected.menu_item().command_string
30            );
31            selected.execute()?;
32        }
33        None => println!("No item selected."),
34    }
35
36    Ok(())
37}
Source

pub fn execute_with(self, params: InvokeParams) -> Result<()>

Execute the selected command with custom parameters.

Use this to override the working directory or window show state.

Trait Implementations§

Source§

impl Debug for SelectedItem

Source§

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

Formats the value using the given formatter. 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> 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, 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.