diff --git a/nih_plug_vizia/src/widgets.rs b/nih_plug_vizia/src/widgets.rs index bcbe075a..7ab5acc2 100644 --- a/nih_plug_vizia/src/widgets.rs +++ b/nih_plug_vizia/src/widgets.rs @@ -11,6 +11,8 @@ use std::sync::Arc; use vizia::{Context, Model}; +pub mod util; + /// Register the default theme for the widgets exported by this module. This is automatically called /// for you when using [`create_vizia_editor()`][super::create_vizia_editor()]. pub fn register_theme(cx: &mut Context) { diff --git a/nih_plug_vizia/src/widgets/util.rs b/nih_plug_vizia/src/widgets/util.rs new file mode 100644 index 00000000..3ae8ece3 --- /dev/null +++ b/nih_plug_vizia/src/widgets/util.rs @@ -0,0 +1,35 @@ +//! Utilities for writing VIZIA widgets. + +use vizia::Modifiers; + +/// An extension trait for [`Modifiers`] that adds platform-independent getters. +pub trait ModifiersExt { + /// Returns true if the Command (on macOS) or Ctrl (on any other platform) key is pressed. + fn command(&self) -> bool; + + /// Returns true if the Alt (or Option on macOS) key is pressed. + fn alt(&self) -> bool; + + /// Returns true if the Shift key is pressed. + fn shift(&self) -> bool; +} + +impl ModifiersExt for Modifiers { + fn command(&self) -> bool { + #[cfg(target_os = "macos")] + let result = self.contains(Modifiers::LOGO); + + #[cfg(not(target_os = "macos"))] + let result = self.contains(Modifiers::CTRL); + + result + } + + fn alt(&self) -> bool { + self.contains(Modifiers::ALT) + } + + fn shift(&self) -> bool { + self.contains(Modifiers::SHIFT) + } +}