window_customizer.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. use tauri::{plugin::Plugin, Manager, Runtime, Window};
  2. pub struct PinchZoomDisablePlugin;
  3. impl Default for PinchZoomDisablePlugin {
  4. fn default() -> Self {
  5. Self
  6. }
  7. }
  8. impl<R: Runtime> Plugin<R> for PinchZoomDisablePlugin {
  9. fn name(&self) -> &'static str {
  10. "Does not matter here"
  11. }
  12. fn window_created(&mut self, window: Window<R>) {
  13. let Some(webview_window) = window.get_webview_window(window.label()) else {
  14. return;
  15. };
  16. let _ = webview_window.with_webview(|_webview| {
  17. #[cfg(target_os = "linux")]
  18. unsafe {
  19. use gtk::glib::ObjectExt;
  20. use gtk::GestureZoom;
  21. use webkit2gtk::glib::gobject_ffi;
  22. if let Some(data) = _webview.inner().data::<GestureZoom>("wk-view-zoom-gesture") {
  23. gobject_ffi::g_signal_handlers_destroy(data.as_ptr().cast());
  24. }
  25. }
  26. #[cfg(target_os = "macos")]
  27. unsafe {
  28. use objc2::rc::Retained;
  29. use objc2_web_kit::WKWebView;
  30. // Get the WKWebView pointer and disable magnification gestures
  31. // This prevents Cmd+Ctrl+scroll and pinch-to-zoom from changing the zoom level
  32. let wk_webview: Retained<WKWebView> =
  33. Retained::retain(_webview.inner().cast()).unwrap();
  34. wk_webview.setAllowsMagnification(false);
  35. }
  36. });
  37. }
  38. }