From d8e8d80402b0907fae6d0db78f45c76e55cb8292 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 19 Mar 2022 00:38:26 +0100 Subject: [PATCH] Add helpers for keyboard modifiers in vizia --- nih_plug_vizia/src/widgets.rs | 2 ++ nih_plug_vizia/src/widgets/util.rs | 35 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 nih_plug_vizia/src/widgets/util.rs 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) + } +}