初始化代码

This commit is contained in:
2023-08-21 16:28:25 +08:00
parent 10b05ac359
commit fc6dca918c
39 changed files with 4470 additions and 72 deletions

4
src-tauri/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/target/

3759
src-tauri/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

29
src-tauri/Cargo.toml Normal file
View 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
View File

@@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}

BIN
src-tauri/icons/128x128.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
src-tauri/icons/32x32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 974 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 903 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
src-tauri/icons/icon.icns Normal file

Binary file not shown.

BIN
src-tauri/icons/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

BIN
src-tauri/icons/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

56
src-tauri/src/lib.rs Normal file
View 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
View 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
View 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
}
}
}