Skip to content

Commit

Permalink
fix: macos volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Nov 25, 2023
1 parent dd73c28 commit d19bc99
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tauri = { version = "1.4", features = ["shell-open", "devtools"] }
enigo = { version = "0.2.0-rc2" }
env_logger = "0.10"
log = "0.4.20"
cfg-if = "1.0.0"


[target.'cfg(any(windows, target_os = "macos"))'.dependencies]
Expand Down
55 changes: 53 additions & 2 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,67 @@ use enigo::{Direction, Enigo, Key, Keyboard, Settings};
use log::debug;
use tauri::Manager;

cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
use log::error;
use std::process::Command;

fn osx_volume_up() {
let output = Command::new("osascript")
.args(&[
"-e",
"set Outputvol to output volume of (get volume settings)",
"-e",
"set volume output volume (Outputvol + 6.25)",
])
.output()
.expect("Failed to execute command");

if !output.status.success() {
error!("{output:?}");
}
}

fn osx_volume_down() {
let output = Command::new("osascript")
.args(&[
"-e",
"set Outputvol to output volume of (get volume settings)",
"-e",
"set volume output volume (Outputvol - 6.25)",
])
.output()
.expect("Failed to execute command");

if !output.status.success() {
error!("{output:?}");
}
}
}
}

#[tauri::command]
async fn press(key: String) -> Result<(), String> {
let mut enigo = Enigo::new(&Settings::default()).unwrap();
debug!("Pressing {}", key);
match key.as_str() {
"VOL_UP" => {
enigo.key(Key::VolumeUp, Direction::Click).unwrap();
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
osx_volume_up();
} else {
enigo.key(Key::VolumeUp, Direction::Click).unwrap();
}
}
}
"VOL_DN" => {
enigo.key(Key::VolumeDown, Direction::Click).unwrap();
cfg_if::cfg_if! {
if #[cfg(target_os = "macos")] {
osx_volume_down();
} else {
enigo.key(Key::VolumeDown, Direction::Click).unwrap();
}
}
}
"PG_UP" => {
enigo.key(Key::PageUp, Direction::Click).unwrap();
Expand Down

0 comments on commit d19bc99

Please sign in to comment.