Skip to content

Commit

Permalink
add show_context_menu function
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Jul 31, 2023
1 parent 37497a1 commit 80f4f2e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
27 changes: 27 additions & 0 deletions core/tauri-runtime/src/window/dpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,30 @@ impl<P: Pixel> From<LogicalPosition<P>> for Position {
Position::Logical(position.cast())
}
}

impl<P> From<LogicalPosition<P>> for crate::menu::LogicalPosition<P> {
fn from(value: LogicalPosition<P>) -> Self {
Self {
x: value.x,
y: value.y,
}
}
}

impl<P> From<PhysicalPosition<P>> for crate::menu::PhysicalPosition<P> {
fn from(value: PhysicalPosition<P>) -> Self {
Self {
x: value.x,
y: value.y,
}
}
}

impl From<Position> for crate::menu::Position {
fn from(value: Position) -> Self {
match value {
Position::Physical(p) => Self::Physical(p.into()),
Position::Logical(p) => Self::Logical(p.into()),
}
}
}
23 changes: 22 additions & 1 deletion core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! The Tauri window types and functions.

use tauri_runtime::menu::MenuEvent;
use tauri_runtime::menu::{ContextMenu, MenuEvent};
pub use tauri_utils::{config::Color, WindowEffect as Effect, WindowEffectState as EffectState};
use url::Url;

Expand Down Expand Up @@ -1209,6 +1209,27 @@ impl<R: Runtime> Window<R> {
Ok(false)
}

/// Shows the specified menu as a context menu at the specified position.
/// If a position was not provided, the cursor position will be used.
///
/// The position is relative to the window's top-left corner.
pub fn show_context_menu<P: Into<Position>>(
&self,
menu: &dyn ContextMenu,
position: Option<P>,
) -> crate::Result<()> {
let position = position.map(|p| p.into());

#[cfg(target_os = "windows")]
menu.show_context_menu_for_hwnd(self.hwnd()? as _, position);
#[cfg(target_os = "linux")]
menu.show_context_menu_for_gtk_window(&self.gtk_window()?, position);
#[cfg(target_os = "macos")]
menu.show_context_menu_for_nsview(self.ns_view() as _, position);

Ok(())
}

/// Executes a closure, providing it with the webview handle that is specific to the current platform.
///
/// The closure is executed on the main thread.
Expand Down

0 comments on commit 80f4f2e

Please sign in to comment.