初始化代码
4
src-tauri/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
3759
src-tauri/Cargo.lock
generated
Normal file
29
src-tauri/Cargo.toml
Normal file
@@ -0,0 +1,29 @@
|
||||
[package]
|
||||
name = "milky-warp"
|
||||
version = "1.0.0"
|
||||
description = "Milky Warp"
|
||||
authors = ["hugoattal"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/hugoattal/milky-warp"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "1.3", features = [] }
|
||||
|
||||
[patch.crates-io]
|
||||
tao = { git = "https://github.com/tauri-apps/tao", branch = "v0.16" }
|
||||
|
||||
[dependencies]
|
||||
tauri = { version = "1.3", features = ["global-shortcut-all", "protocol-asset", "shell-open", "system-tray", "window-hide", "window-set-ignore-cursor-events", "window-set-position", "window-set-size", "window-show"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
enigo = "0.1.2"
|
||||
screenshots = "0.5.4"
|
||||
winapi = { version = "0.3.9", features = ["winuser"] }
|
||||
|
||||
[features]
|
||||
# this feature is used for production builds or when `devPath` points to the filesystem
|
||||
# DO NOT REMOVE!!
|
||||
custom-protocol = ["tauri/custom-protocol"]
|
3
src-tauri/build.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
}
|
BIN
src-tauri/icons/128x128.png
Normal file
After Width: | Height: | Size: 3.4 KiB |
BIN
src-tauri/icons/128x128@2x.png
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
src-tauri/icons/32x32.png
Normal file
After Width: | Height: | Size: 974 B |
BIN
src-tauri/icons/Square107x107Logo.png
Normal file
After Width: | Height: | Size: 2.8 KiB |
BIN
src-tauri/icons/Square142x142Logo.png
Normal file
After Width: | Height: | Size: 3.8 KiB |
BIN
src-tauri/icons/Square150x150Logo.png
Normal file
After Width: | Height: | Size: 3.9 KiB |
BIN
src-tauri/icons/Square284x284Logo.png
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
src-tauri/icons/Square30x30Logo.png
Normal file
After Width: | Height: | Size: 903 B |
BIN
src-tauri/icons/Square310x310Logo.png
Normal file
After Width: | Height: | Size: 8.4 KiB |
BIN
src-tauri/icons/Square44x44Logo.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
src-tauri/icons/Square71x71Logo.png
Normal file
After Width: | Height: | Size: 2.0 KiB |
BIN
src-tauri/icons/Square89x89Logo.png
Normal file
After Width: | Height: | Size: 2.4 KiB |
BIN
src-tauri/icons/StoreLogo.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
src-tauri/icons/icon.icns
Normal file
BIN
src-tauri/icons/icon.ico
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
src-tauri/icons/icon.png
Normal file
After Width: | Height: | Size: 14 KiB |
56
src-tauri/src/lib.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
#[cfg(target_os = "windows")]
|
||||
extern crate winapi;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn get_screen_index_from_cursor_pos(cursor_pos: (i32, i32)) -> u32 {
|
||||
use winapi::shared::windef::{POINT, HMONITOR, HDC, LPRECT};
|
||||
use winapi::shared::minwindef::{LPARAM, BOOL};
|
||||
use winapi::um::winuser::{MonitorFromPoint, MONITOR_DEFAULTTONEAREST, EnumDisplayMonitors};
|
||||
|
||||
let point = POINT { x: cursor_pos.0, y: cursor_pos.1 };
|
||||
|
||||
let monitor = unsafe { MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST) };
|
||||
let mut index = 0;
|
||||
|
||||
static mut GLOBAL_MONITOR: HMONITOR = std::ptr::null_mut();
|
||||
static mut GLOBAL_INDEX: u32 = 0;
|
||||
|
||||
unsafe {
|
||||
GLOBAL_MONITOR = monitor;
|
||||
GLOBAL_INDEX = index;
|
||||
}
|
||||
|
||||
unsafe extern "system" fn monitor_enum_proc(hmonitor: HMONITOR, _: HDC, _: LPRECT, _: LPARAM) -> BOOL {
|
||||
if hmonitor == GLOBAL_MONITOR {
|
||||
return 0;
|
||||
}
|
||||
GLOBAL_INDEX += 1;
|
||||
1
|
||||
}
|
||||
|
||||
unsafe {
|
||||
EnumDisplayMonitors(
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null_mut(),
|
||||
Some(monitor_enum_proc),
|
||||
0,
|
||||
)
|
||||
};
|
||||
|
||||
unsafe {
|
||||
index = GLOBAL_INDEX;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn get_screen_index_from_cursor_pos(cursor_pos: (i32, i32)) -> u32 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn get_screen_index_from_cursor_pos(cursor_pos: (i32, i32)) -> u32 {
|
||||
return 0;
|
||||
}
|
78
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use std::fs;
|
||||
|
||||
use enigo::Enigo;
|
||||
use enigo::MouseControllable;
|
||||
|
||||
use screenshots::Screen;
|
||||
|
||||
static mut SCREENSHOT_PATH: String = String::new();
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
||||
#[tauri::command]
|
||||
fn get_mouse_location() -> String {
|
||||
let enigo = Enigo::new();
|
||||
let cursor_location: (i32, i32) = enigo.mouse_location();
|
||||
format!("{};{}", cursor_location.0, cursor_location.1)
|
||||
}
|
||||
|
||||
fn capture_screen() -> String {
|
||||
let enigo = Enigo::new();
|
||||
let cursor_location: (i32, i32) = enigo.mouse_location();
|
||||
let screen_index = milky_warp::get_screen_index_from_cursor_pos(cursor_location);
|
||||
|
||||
let display = Screen::all().expect("Couldn't find primary display.");
|
||||
let image = display[screen_index as usize].capture().expect("Couldn't capture display.");
|
||||
let buffer = image.buffer();
|
||||
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let file_path = temp_dir.join("screenshot.png");
|
||||
|
||||
fs::write(file_path.clone(), buffer).unwrap();
|
||||
|
||||
file_path.to_str().unwrap().to_owned()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn update_screenshot() -> String {
|
||||
unsafe {
|
||||
SCREENSHOT_PATH = capture_screen();
|
||||
SCREENSHOT_PATH.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_screenshot_path() -> String {
|
||||
unsafe {
|
||||
SCREENSHOT_PATH.clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn make_tray() -> tauri::SystemTray {
|
||||
let tray_menu = tauri::SystemTrayMenu::new()
|
||||
.add_item(tauri::CustomMenuItem::new("quit".to_string(), "Quit"));
|
||||
|
||||
return tauri::SystemTray::new()
|
||||
.with_menu(tray_menu);
|
||||
}
|
||||
|
||||
fn handle_tray_event(app: &tauri::AppHandle, event: tauri::SystemTrayEvent) {
|
||||
if let tauri::SystemTrayEvent::MenuItemClick { id, .. } = event {
|
||||
if id.as_str() == "quit" {
|
||||
app.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
update_screenshot();
|
||||
|
||||
tauri::Builder::default()
|
||||
.system_tray(make_tray())
|
||||
.on_system_tray_event(handle_tray_event)
|
||||
.invoke_handler(tauri::generate_handler![get_mouse_location, get_screenshot_path, update_screenshot])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
72
src-tauri/tauri.conf.json
Normal file
@@ -0,0 +1,72 @@
|
||||
{
|
||||
"build": {
|
||||
"beforeDevCommand": "pnpm dev",
|
||||
"beforeBuildCommand": "pnpm build",
|
||||
"devPath": "http://localhost:1420",
|
||||
"distDir": "../dist",
|
||||
"withGlobalTauri": false
|
||||
},
|
||||
"package": {
|
||||
"productName": "milky-warp",
|
||||
"version": "../package.json"
|
||||
},
|
||||
"tauri": {
|
||||
"allowlist": {
|
||||
"all": false,
|
||||
"shell": {
|
||||
"all": false,
|
||||
"open": true
|
||||
},
|
||||
"window": {
|
||||
"setPosition": true,
|
||||
"setIgnoreCursorEvents": true,
|
||||
"setSize": true,
|
||||
"show": true,
|
||||
"hide": true
|
||||
},
|
||||
"protocol": {
|
||||
"asset": true,
|
||||
"assetScope": ["**"]
|
||||
},
|
||||
"globalShortcut": {
|
||||
"all": true
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"icon": [
|
||||
"icons/32x32.png",
|
||||
"icons/128x128.png",
|
||||
"icons/128x128@2x.png",
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
],
|
||||
"identifier": "com.milky-warp.app",
|
||||
"targets": "all"
|
||||
},
|
||||
"security": {
|
||||
"csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost"
|
||||
},
|
||||
"updater": {
|
||||
"active": false
|
||||
},
|
||||
"windows": [
|
||||
{
|
||||
"fullscreen": false,
|
||||
"resizable": false,
|
||||
"title": "Milky Warp",
|
||||
"width": 256,
|
||||
"height": 256,
|
||||
"transparent": true,
|
||||
"decorations": false,
|
||||
"alwaysOnTop": true,
|
||||
"center": true,
|
||||
"visible": false
|
||||
}
|
||||
],
|
||||
"systemTray": {
|
||||
"iconPath": "icons/icon.png",
|
||||
"iconAsTemplate": true
|
||||
}
|
||||
}
|
||||
}
|