Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwi committed Dec 1, 2024
1 parent e135ad1 commit 463fb4d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 12 deletions.
18 changes: 17 additions & 1 deletion src-tauri/src/frontend_commands/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::utils;
use crate::{
common::{Base64Png, Base64PngExt},
utils,
};
use serde::Serialize;
use std::fs;
use std::path::Path;

#[tauri::command]
pub fn create_dir(path: String) -> Result<bool, String> {
Expand Down Expand Up @@ -81,6 +85,18 @@ pub fn read_dir(path: String) -> Result<Vec<Entry>, String> {
Ok(files)
}

#[tauri::command]
pub fn save_base64_image(path: String, data: Base64Png) -> Result<bool, String> {
let buffer = data.to_buffer().unwrap();
let path = Path::new(path.as_str());
let parent_path = path.parent().unwrap();
let _ = utils::fs::create_dir(parent_path.to_str().unwrap().to_string());
if let Err(error) = buffer.save(path) {
return Err(error.to_string());
}
Ok(true)
}

#[derive(Debug, Serialize, Clone)]
pub struct Entry {
name: String,
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn run() {
frontend_commands::fs::exists,
frontend_commands::fs::write_file,
frontend_commands::fs::read_file,
frontend_commands::fs::save_base64_image,
frontend_commands::capture::snapshot,
frontend_commands::capture::display_size,
frontend_commands::find::find_peak,
Expand Down Expand Up @@ -69,6 +70,7 @@ pub fn run() {
frontend_commands::fs::exists,
frontend_commands::fs::write_file,
frontend_commands::fs::read_file,
frontend_commands::fs::save_base64_image,
frontend_commands::capture::snapshot,
frontend_commands::capture::display_size,
frontend_commands::find::find_image,
Expand Down
7 changes: 4 additions & 3 deletions src-tauri/src/python_commands/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{common::Point, input};
use crate::input;
use pyo3::prelude::*;

#[pyfunction]
Expand Down Expand Up @@ -50,8 +50,9 @@ pub fn move_rel(x: i32, y: i32) -> PyResult<()> {
}

#[pyfunction]
pub fn location() -> PyResult<Point> {
Ok(input::location())
pub fn location() -> PyResult<(f64, f64)> {
let point = input::location();
Ok((point.x, point.y))
}

#[pyfunction]
Expand Down
5 changes: 5 additions & 0 deletions src/components/NewProject.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
resourceDir,
scriptDir,
defaultScriptFile,
moduleDir,
} from "./../stores/app";
import { sep } from "@tauri-apps/api/path";
import { createDir, createFile, exists } from "./../utils/fs";
Expand All @@ -29,9 +30,13 @@ async function save() {
msgError(`project already exists`);
return;
}
const module = await moduleDir(form.name);
const resouce = await resourceDir(form.name);
const script = await scriptDir(form.name);
const defaultFile = await defaultScriptFile(form.name);
await createDir(module);
//copy default moudles into module folder
//todo
await createDir(project);
await createDir(resouce);
await createDir(script);
Expand Down
71 changes: 63 additions & 8 deletions src/components/monitor/FindImage.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<script setup>
import { ref, onMounted, reactive, watch } from "vue";
import { ref, onMounted, reactive, watch, computed } from "vue";
import {
drawBase64ImageOnCanvas,
generateRandomString,
} from "../../utils/common";
import { invoke } from "@tauri-apps/api/core";
import { msgError } from "../../utils/msg";
import { useStore } from "vuex";
import { resourceDir } from "../../stores/app";
import { sep } from "@tauri-apps/api/path";
import { cropBase64Image } from "../../utils/common";
import { msgError, msgSuccess } from "../../utils/msg";
const props = defineProps(["form", "projectPath"]);
const emits = defineEmits(["close", "form"]);
Expand All @@ -28,6 +31,9 @@ const canvasRef = ref(null);
const hiddenCanvasRef = ref(null);
const magnifyingGlassCanvasRef = ref(null);
const filePath = ref(null);
const saved = ref(false);
const form = reactive({
name: null,
threshold: 0.99,
Expand Down Expand Up @@ -215,6 +221,45 @@ async function findImages() {
});
}
async function buildFilePath() {
if (form.name == null) {
return "";
}
const relativeFilePath = form.name
.replace(/[\u3000 ]/g, "")
.replace(/[/\\]/g, await sep());
return (
(await resourceDir(store.getters.currentProjectName)) +
(await sep()) +
relativeFilePath +
".png"
);
}
async function save() {
if (form.name == null) {
return;
}
const data = await cropBase64Image(
canvasRef.value.toDataURL("image/png"),
dataExtSideLength / 2,
dataExtSideLength / 2,
form.captured.size.width,
form.captured.size.height
);
invoke("save_base64_image", {
path: filePath.value,
data,
})
.then((result) => {
saved.value = result;
msgSuccess("save successed, you can copy the code now");
})
.catch((error) => {
msgError(error);
});
}
watch(props.form, () => {
Object.assign(form, props.form);
Object.assign(originForm, props.form);
Expand All @@ -223,9 +268,12 @@ watch(props.form, () => {
form.findArea.end.y = form.monitor.size.height;
});
watch(form, async () => {
filePath.value = await buildFilePath();
});
onMounted(async () => {
form.name = generateRandomString(3);
console.log("a", props.projectPath);
});
</script>
<template>
Expand Down Expand Up @@ -329,13 +377,13 @@ onMounted(async () => {
</el-form-item>
<el-form-item prop="imagePath" style="margin-bottom: 0px">
<el-input
v-model="store.getters.currentProjectPath"
v-model="filePath"
autocapitalize="off"
autocorrect="off"
spellcheck="false"
disabled
>
<template #prepend>image path</template>
<template #prepend>fullpath</template>
</el-input>
</el-form-item>
</div>
Expand Down Expand Up @@ -432,7 +480,14 @@ onMounted(async () => {
<div class="item">
<div class="title">
<span>Code</span>
<el-button type="primary" @click=""> copy </el-button>
<el-button
type="primary"
@click=""
:disabled="!saved"
title="save image before copy"
>
copy
</el-button>
</div>
<div>
<el-input
Expand All @@ -448,8 +503,8 @@ onMounted(async () => {
</el-form>
</el-main>
<el-footer>
<el-button @click="close">Cancel</el-button>
<el-button type="primary" @click=""> Save </el-button>
<el-button @click="close">Close</el-button>
<el-button type="primary" @click="save"> Save </el-button>
</el-footer>
</el-container>
</template>
Expand Down
6 changes: 6 additions & 0 deletions src/stores/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { sep } from "@tauri-apps/api/path";
export const projectsDir = await invoke("projects_dir");
export const scriptDirName = "scripts";
export const resourceDirName = "resources";
export const moduleDirName = "modules";
export const defaultScriptName = "main.py";

export const moduleDir = async (projectName) => {
return (
projectsDir + (await sep()) + projectName + (await sep()) + moduleDirName
);
};
export const resourceDir = async (projectName) => {
return (
projectsDir + (await sep()) + projectName + (await sep()) + resourceDirName
Expand Down

0 comments on commit 463fb4d

Please sign in to comment.