Skip to content

Commit

Permalink
Merge branch 'main' into feat/tauri-emit-listen-update
Browse files Browse the repository at this point in the history
  • Loading branch information
clearlysid authored Jul 21, 2024
2 parents 69f98b4 + b21a06a commit 54e9822
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 73 deletions.
12 changes: 1 addition & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ cocoa = "0.25"
objc = "0.2"

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.58.0", features = [
"Win32_UI_WindowsAndMessaging",
"Win32_Foundation",
"Win32_Graphics_Dwm",
"Win32_UI_Controls",
"UI_WindowManagement",
"Foundation",
"Win32_UI",
"Win32_UI_Input",
"Win32_UI_Input_KeyboardAndMouse",
] }
enigo = "0.1.3"

[build-dependencies]
tauri-plugin = { version = "2.0.0-beta", features = ["build"] }
6 changes: 0 additions & 6 deletions examples/tauri-app/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@ rust-version = "1.70"

[build-dependencies]
tauri-build = { version = "2.0.0-beta", default-features = false , features = [] }
cxx-build = "1.0"

[dependencies]
tauri = { version = "2.0.0-beta", features = [] }
tauri-plugin-decorum = { path = "../../../" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
cxx = "1.0.119"
enigo = "0.2.0"
anyhow = "1.0.82"
window-vibrancy = "0.5.0"
windows = "0.56.0"
65 changes: 14 additions & 51 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,21 @@
use tauri;

#[cfg(target_os = "windows")]
fn emulate_win_z() -> Result<(), anyhow::Error> {
use windows::Win32::UI::Input::KeyboardAndMouse::{
SendInput, INPUT, INPUT_KEYBOARD, KEYBD_EVENT_FLAGS, KEYEVENTF_KEYUP, VIRTUAL_KEY, VK_MENU,
VK_RWIN,
};
// Press Win + Z
unsafe {
let mut inputs: [INPUT; 4] = std::mem::zeroed();
inputs[0].r#type = INPUT_KEYBOARD;
inputs[0].Anonymous.ki.wVk = VK_RWIN;
inputs[0].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS(0);

inputs[1].r#type = INPUT_KEYBOARD;
inputs[1].Anonymous.ki.wVk = VIRTUAL_KEY('Z' as u16);
inputs[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS(0);

inputs[2].r#type = INPUT_KEYBOARD;
inputs[2].Anonymous.ki.wVk = VIRTUAL_KEY('Z' as u16);
inputs[2].Anonymous.ki.dwFlags = KEYEVENTF_KEYUP;

inputs[3].r#type = INPUT_KEYBOARD;
inputs[3].Anonymous.ki.wVk = VK_RWIN;
inputs[3].Anonymous.ki.dwFlags = KEYEVENTF_KEYUP;

SendInput(&inputs, std::mem::size_of::<INPUT>() as _);
}

// Wait 50 ms
std::thread::sleep(std::time::Duration::from_millis(50));
#[tauri::command]
pub async fn show_snap_overlay() {
#[cfg(target_os = "windows")]
{
use enigo::{Enigo, Key, KeyboardControllable};

// Press Alt to hide the ugly numbers
unsafe {
let mut inputs: [INPUT; 2] = std::mem::zeroed();
inputs[0].r#type = INPUT_KEYBOARD;
inputs[0].Anonymous.ki.wVk = VK_MENU;
inputs[0].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS(0);
// press win + z using enigo
let mut enigo = Enigo::new();
enigo.key_down(Key::Meta);
enigo.key_click(Key::Layout('z'));
enigo.key_up(Key::Meta);

inputs[1].r#type = INPUT_KEYBOARD;
inputs[1].Anonymous.ki.wVk = VK_MENU;
inputs[1].Anonymous.ki.dwFlags = KEYEVENTF_KEYUP;
// Wait 50 ms
std::thread::sleep(std::time::Duration::from_millis(50));

SendInput(&inputs, std::mem::size_of::<INPUT>() as _);
// Press Alt to hide the ugly numbers
enigo.key_click(Key::Alt);
}

Ok(())
}

#[tauri::command]
pub async fn show_snap_overlay() -> Result<(), String> {
#[cfg(not(target_os = "windows"))]
return Ok(());

#[cfg(target_os = "windows")]
emulate_win_z().map_err(|e| e.to_string())
}
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl<'a> WebviewWindowExt for WebviewWindow {

/// Set the window background to transparent.
/// This helper function is different from Tauri's default
/// as it doesn't make use of the `transparent` window attribute.
/// and doesn't need macOS Private APIs.
/// as it doesn't use the `transparent` flag or macOS Private APIs.
#[cfg(target_os = "macos")]
fn make_transparent(&self) -> Result<&WebviewWindow, Error> {
use cocoa::{
Expand Down Expand Up @@ -158,18 +157,20 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
Err(e) => println!("decorum error: {:?}", e),
}
})
.on_window_ready(|win| {
.on_window_ready(|_win| {
#[cfg(target_os = "macos")]
traffic::setup_traffic_light_positioner(win);
traffic::setup_traffic_light_positioner(_win);
return;
})
.build()
}

#[cfg(target_os = "macos")]
fn is_main_thread() -> bool {
std::thread::current().name() == Some("main")
}

#[cfg(target_os = "macos")]
fn ensure_main_thread<F>(
win: &WebviewWindow,
main_action: F,
Expand Down
2 changes: 1 addition & 1 deletion src/traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use objc::{msg_send, sel, sel_impl};
use rand::{distributions::Alphanumeric, Rng};
use tauri::{Runtime, Window, Emitter};
use tauri::{Emitter, Runtime, Window};

const WINDOW_CONTROL_PAD_X: f64 = 8.0;
const WINDOW_CONTROL_PAD_Y: f64 = 12.0;
Expand Down

0 comments on commit 54e9822

Please sign in to comment.