Repository: tauri-apps/tauri
File: crates/tauri/src/tray/mod.rs (Rust)

Code before:
      self_.inner.set_show_menu_on_left_click(enable)
    })?;
    Ok(())
  }

  /// Get tray icon rect.
  ///
  /// ## Platform-specific:
  ///
  /// - **Linux**: Unsupported, always returns `None`.
  pub fn rect(&self) -> crate::Result<Option<crate::Rect>> {
    run_item_main_thread!(self, |self_: Self| {
      self_.inner.rect().map(|rect| Rect {
        position: rect.position.into(),
        size: rect.size.into(),
      })
    })
  }

  /// Get the inner tray icon.
  ///
  /// Note that `tray-icon` crate may be updated in minor releases of Tauri.
  /// Therefore, it’s recommended to pin Tauri to at least a minor version when you’re using `with_inner_tray_icon`.
  pub fn with_inner_tray_icon<F, T>(&self, f: F) -> crate::Result<T>
  where
    F: FnOnce(tray_icon::TrayIcon) -> T + Send + 'static,
    T: Send + 'static,
  {
    run_item_main_thread!(self, |self_: Self| { f(self_.inner.clone()) })
  }
}

impl<R: Runtime> Resource for TrayIcon<R> {
  fn close(self: std::sync::Arc<Self>) {
    let mut icons = self.app_handle.manager.tray.icons.lock().unwrap();
    for (i, (tray_icon_id, _rid)) in icons.iter_mut().enumerate() {
      if tray_icon_id == &self.id {
        icons.swap_remove(i);
        return;
      }
    }
  }
}

#[cfg(test)]
mod tests {
  #[test]
  fn tray_event_json_serialization() {
    // NOTE: if this test is ever changed, you probably need to change `TrayIconEvent` in JS as well

    use super::*;

Review comment (suggestion):
I think we can avoid the clone here right? `&tray_icon::TrayIcon` should be able to do everything needed, and if people want, they could in rare cases clone it themselves

Code after:
      self_.inner.set_show_menu_on_left_click(enable)
    })?;
    Ok(())
  }

  /// Get tray icon rect.
  ///
  /// ## Platform-specific:
  ///
  /// - **Linux**: Unsupported, always returns `None`.
  pub fn rect(&self) -> crate::Result<Option<crate::Rect>> {
    run_item_main_thread!(self, |self_: Self| {
      self_.inner.rect().map(|rect| Rect {
        position: rect.position.into(),
        size: rect.size.into(),
      })
    })
  }

  /// Do something with the inner [`tray_icon::TrayIcon`] on main thread
  ///
  /// Note that `tray-icon` crate may be updated in minor releases of Tauri.
  /// Therefore, it’s recommended to pin Tauri to at least a minor version when you’re using `with_inner_tray_icon`.
  pub fn with_inner_tray_icon<F, T>(&self, f: F) -> crate::Result<T>
  where
    F: FnOnce(&tray_icon::TrayIcon) -> T + Send + 'static,
    T: Send + 'static,
  {
    run_item_main_thread!(self, |self_: Self| { f(&self_.inner) })
  }
}

impl<R: Runtime> Resource for TrayIcon<R> {
  fn close(self: std::sync::Arc<Self>) {
    let mut icons = self.app_handle.manager.tray.icons.lock().unwrap();
    for (i, (tray_icon_id, _rid)) in icons.iter_mut().enumerate() {
      if tray_icon_id == &self.id {
        icons.swap_remove(i);
        return;
      }
    }
  }
}

#[cfg(test)]
mod tests {
  #[test]
  fn tray_event_json_serialization() {
    // NOTE: if this test is ever changed, you probably need to change `TrayIconEvent` in JS as well

    use super::*;