Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.2.0 #3

Merged
merged 5 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pasteme",
"private": true,
"version": "0.1.1",
"version": "0.2.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
48 changes: 47 additions & 1 deletion src-tauri/Cargo.lock

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

3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pasteme"
version = "0.1.1"
version = "0.2.0"
description = "A simple clipboard manager"
authors = ["CKylinMC"]
edition = "2021"
Expand All @@ -26,6 +26,7 @@ serde_json = "1"
mouse_position = "0.1.3"
tauri-plugin-clipboard-manager = "2"
tauri-plugin-store = "2"
enigo = "0.3.0"

[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-autostart = "2"
Expand Down
22 changes: 21 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
mod tray;

use enigo::{
Direction,
Enigo, Key, Keyboard, Settings,
};
use mouse_position::mouse_position::Mouse;
use tauri::{AppHandle, Manager};
use tauri_plugin_autostart::MacosLauncher;
Expand All @@ -17,6 +21,22 @@ fn get_mouse_position() -> Vec<i32> {
}
}

#[tauri::command]
async fn input_text(text: &str) -> Result<(), String> {
let mut enigo = Enigo::new(&Settings::default()).unwrap();
enigo.text(text).unwrap();
Ok(())
}

#[tauri::command]
async fn simulate_paste() -> Result<(), String> {
let mut enigo = Enigo::new(&Settings::default()).unwrap();
let _ = enigo.key(Key::Control, Direction::Press);
let _ = enigo.key(Key::V, Direction::Click);
let _ = enigo.key(Key::Control, Direction::Release);
Ok(())
}

fn show_window(app: &AppHandle) {
let windows = app.webview_windows();

Expand Down Expand Up @@ -50,7 +70,7 @@ pub fn run() {
Ok(())
})
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![get_mouse_position])
.invoke_handler(tauri::generate_handler![get_mouse_position, input_text, simulate_paste])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "PasteMe",
"version": "0.1.1",
"version": "0.2.0",
"identifier": "in.ckyl.pasteme",
"build": {
"beforeDevCommand": "yarn dev",
Expand Down
20 changes: 19 additions & 1 deletion src/composables/useConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref } from 'vue';
import { Store } from '@tauri-apps/plugin-store';
import { tryParse } from '@/libs/utils';

export interface AIConfig {
enabled: boolean;
Expand All @@ -8,9 +9,17 @@ export interface AIConfig {
model: string;
}

export interface Snippet {
id: string;
name: string;
system: string;
prompt: string;
}

export interface Config {
globalShortcut: string;
ai: AIConfig;
snippets: Snippet[];
}

export function useConfig() {
Expand All @@ -23,20 +32,28 @@ export function useConfig() {
endpoint: 'https://api.openai.com/v1',
model: 'gpt-4o',
},
snippets: []
});

const loadConfig = async () => {
noSave.value = true;
try {
const store = await Store.load('store.bin');
config.value.globalShortcut =
config.value.globalShortcut =
(await store.get('globalShortcut')) || 'CommandOrControl+Shift+C';
config.value.ai = {
enabled: !!(await store.get('ai.enabled')),
apiKey: (await store.get('ai.apiKey')) || '',
endpoint: (await store.get('ai.endpoint')) || 'https://api.openai.com/v1',
model: (await store.get('ai.model')) || 'gpt-4o',
};
config.value.snippets = tryParse(await store.get('snippets') || "[]", [
{
id: 'intro',
name: '翻译成中文',
system: '请辅助用户完成任务,不要输出任何其他多余内容',
prompt: '请将内容翻译成中文',
}]);
await store.close();
} finally {
noSave.value = false;
Expand All @@ -51,6 +68,7 @@ export function useConfig() {
await store.set('ai.apiKey', config.value.ai.apiKey);
await store.set('ai.endpoint', config.value.ai.endpoint);
await store.set('ai.model', config.value.ai.model);
await store.set('snippets', JSON.stringify(Array.isArray(config.value.snippets) ? config.value.snippets : []));
await store.save();
await store.close();
};
Expand Down
27 changes: 15 additions & 12 deletions src/composables/usePanelWindow.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ref } from 'vue';
import { window as appWindow } from '@tauri-apps/api';
import { useConfig } from './useConfig';
import type { useClipboard } from './useClipboard';

export type PanelPage = 'index' | 'calc' | 'edit' | 'tojson' | 'askai' | 'aicreate';
export type PanelPage = 'index' | 'calc' | 'edit' | 'tojson' | 'askai' | 'aicreate' | 'snippets' | 'snippets-ai' | 'snippets-edit';

export function usePanelWindow(clipboard: ReturnType<typeof useClipboard>) {
const page = ref<PanelPage>('index');
Expand All @@ -12,27 +11,31 @@ export function usePanelWindow(clipboard: ReturnType<typeof useClipboard>) {

const gotoPage = (targetPage: PanelPage, onPageChange?: (page: PanelPage) => void) => {
page.value = targetPage;
showPreview.value = targetPage === 'index' ||
['tojson', 'askai', 'aicreate'].includes(targetPage);
showPreview.value = targetPage === 'index' ||
['tojson', 'askai', 'aicreate', 'snippets-ai'].includes(targetPage);
onPageChange?.(targetPage);
};

const setupWindowListeners = async (onHide: () => void) => {
const { loadConfig } = useConfig();
const setupWindowListeners = async (onHide: () => void, loadConfig?: ()=>Promise<void>) => {
const isBlured = ref(true);
const listeners = await Promise.all([
appWindow.getCurrentWindow().listen('tauri://blur', event => {
if (!mouseInRange.value) {
console.log('blur', event);
isBlured.value = true;
onHide();
}
}),
appWindow.getCurrentWindow().listen('tauri://focus', async () => {
showPreview.value = true;
page.value = 'index';
await Promise.all([
loadConfig(),
clipboard.refresh()
]);
if (isBlured.value) {
showPreview.value = true;
page.value = 'index';
await Promise.all([
loadConfig?.(),
clipboard.refresh()
]);
isBlured.value = false;
}
})
]);

Expand Down
10 changes: 9 additions & 1 deletion src/libs/bridges.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { invoke } from "@tauri-apps/api/core";

export async function getMousePosition(){
export async function getMousePosition() {
return await invoke("get_mouse_position") as number[];
}

export async function inputText(text: string) {
return await invoke("input_text", { text });
}

export async function simulatePaste(){
return await invoke("simulate_paste");
}
8 changes: 8 additions & 0 deletions src/libs/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export function tryParse(str: string, fallback: any = null) {
try {
return JSON.parse(str);
} catch (e) {
return fallback;
}
}
2 changes: 1 addition & 1 deletion src/pages/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function closeShortcutSetter() {
<n-card title="关于">
<n-list>
<n-list-item>
<n-thing title="版本" description="0.1.1" />
<n-thing title="版本" description="0.2.0" />
</n-list-item>
<n-list-item>
<n-thing title="作者" description="CKylinMC" />
Expand Down
Loading