Skip to content

[feat] refactor tray.get_item(id) to return Option<T> #5491

@mrjackwills

Description

@mrjackwills

Describe the problem

Currently, when trying to use an unknown system tray menu item, the get_item function panics

window.app_handle().tray_handle().get_item("unknown_id") => panic!("item id not found")

Describe the solution you'd like

Ideally the get_item should return an Option<SystemTrayMenuItemHandle<R>>

Should also apply to the menu.get_item(id) function as well?

That way the user can execute, without fear of a panic

if Some(tray_entry) = window.app_handle().tray_handle().get_item("known_id") {
    tray_entry.set_title("It works!").unwrap();
}

// or

window.app_handle().tray_handle().get_item("known_id").map(|i|i.set_title("It works!").unwrap());

At first glance to me - I haven't tested this fully yet, but on my basic app it's working - it looks like the get_item function could be changed to

impl<R: Runtime> SystemTrayHandle<R> {
   /// Gets an Option<handle> to the menu item that has the specified `id`.
   pub fn get_item(&self, id: MenuIdRef<'_>) -> Option<SystemTrayMenuItemHandle<R>> {
     self.ids.lock().unwrap().iter().find(|i|i.1 == id).and_then(|i| Some(SystemTrayMenuItemHandle {
         id: *i.0,
         tray_handler: self.inner.clone(),
        })
     )
  }

Or am I missing something really obvious / completely misunderstanding the component model?

Alternatives considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions