Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Sep 16, 2022
1 parent 1710a24 commit fb019fc
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 3 deletions.
13 changes: 13 additions & 0 deletions core/src/libraries.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use std::env;
use tauri::{GlobalShortcutManager, Manager};

#[tauri::command]
pub fn get_args() -> Vec<String> {
let args: Vec<String> = env::args().collect();

args.into()
}

#[tauri::command]
pub fn update_tray(app: tauri::AppHandle) {
let window = app.get_window("main").unwrap();
let menu_item = app.tray_handle().get_item("toggle");

if window.is_visible().unwrap() {
menu_item.set_title("Show Authme").unwrap();
} else {
menu_item.set_title("Hide Authme").unwrap();
}
}
7 changes: 6 additions & 1 deletion core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ fn handle_tray_event(app: &AppHandle, event: SystemTrayEvent) {

window.show().unwrap();
window.unminimize().unwrap();
window.set_focus().unwrap();

menu_item.set_title("Hide Authme").unwrap();
}
};
Expand Down Expand Up @@ -97,6 +99,7 @@ fn main() {
encryption::set_encryption_key,
encryption::delete_entry,
libraries::get_args,
libraries::update_tray,
])
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
println!("{}, {argv:?}, {cwd}", app.package_info().name);
Expand Down Expand Up @@ -159,8 +162,10 @@ fn main() {
window.hide().unwrap();
menu_item.set_title("Show Authme").unwrap();
} else {
window.unminimize().unwrap();
window.show().unwrap();
window.unminimize().unwrap();
window.set_focus().unwrap();

menu_item.set_title("Hide Authme").unwrap();
}
}
Expand Down
163 changes: 163 additions & 0 deletions interface/libraries/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { globalShortcut, invoke, window } from "@tauri-apps/api"
import { exit } from "@tauri-apps/api/process"
import { getSettings, setSettings } from "interface/stores/settings"
import { navigate } from "./navigate"

const settings = getSettings()
let modify = true
let inputName: HTMLInputElement

export const shortcuts = [
{ id: "show", name: "Show Authme" },
{ id: "settings", name: "Settings" },
{ id: "exit", name: "Exit Authme" },
]
const defaultShortcuts = ["CmdOrCtrl+Shift+a", "CmdOrCtrl+Shift+s", "CmdOrCtrl+Shift+d"]

/**
* Delete specified shortcut
*/
export const deleteShortcut = (id: number) => {
const input = document.querySelector(`#shortcut${id}`)

input.value = "None"

settings.shortcuts[shortcuts[id].id] = input.value
setSettings(settings)

registerShortcuts()
}

/**
* Reset specified shortcut
*/
export const resetShortcut = (id: number) => {
const input = document.querySelector(`#shortcut${id}`)

input.value = defaultShortcuts[id]

settings.shortcuts[shortcuts[id].id] = input.value
setSettings(settings)

registerShortcuts()
}

/**
* Test if a character is ASCII
*/
const isASCII = (str: string): boolean => {
// eslint-disable-next-line no-control-regex
return /^[\x00-\x7F]*$/.test(str)
}

/**
* Detect pressed keyboard combination
* @param {KeyboardEvent} event
*/
const getKeyboardCombination = (event: KeyboardEvent) => {
let key = event.key

if (isASCII(event.key) === false) {
key = "a"
}

if (key === "Control" || key === "Shift" || key === "Alt") {
key = "a"
}

if (event.ctrlKey === true) {
inputName.value = `CmdOrCtrl+${key.toLowerCase()}`
}

if (event.altKey === true) {
inputName.value = `Alt+${key.toLowerCase()}`
}

if (event.shiftKey === true) {
inputName.value = `Shift+${key.toLowerCase()}`
}

if (event.ctrlKey === true && event.shiftKey === true) {
inputName.value = `CmdOrCtrl+Shift+${key.toLowerCase()}`
}

if (event.ctrlKey === true && event.altKey === true) {
inputName.value = `CmdOrCtrl+Alt+${key.toLowerCase()}`
}

if (event.shiftKey === true && event.altKey === true) {
inputName.value = `Shift+Alt+${key.toLowerCase()}`
}
}

/**
* Edit inputName=d shortcut
*/
export const editShortcut = (id: number) => {
let input: HTMLInputElement = document.querySelector(`#shortcut${id}`)
inputName = input

globalShortcut.unregisterAll()

if (modify === true) {
input.value = "Press any key combination"
input.style.color = "#28A443"

document.addEventListener("keydown", getKeyboardCombination)

modify = false
} else {
input = document.querySelector(`#shortcut${id}`)

if (input.value === "Press any key combination") {
input.value = "None"
}

document.removeEventListener("keydown", getKeyboardCombination)

input.style.color = "white"

settings.shortcuts[shortcuts[id].id] = input.value
setSettings(settings)

modify = true

registerShortcuts()
}
}

export const registerShortcuts = () => {
globalShortcut.unregisterAll()

globalShortcut.register(settings.shortcuts.show, async () => {
await invoke("update_tray")

const windowShown = await window.appWindow.isVisible()

if (windowShown === true) {
window.appWindow.hide()
} else {
await window.appWindow.show()
await window.appWindow.unminimize()
await window.appWindow.setFocus()
}
})

globalShortcut.register(settings.shortcuts.settings, async () => {
const windowShown = await window.appWindow.isVisible()

if (windowShown === true) {
navigate("settings")
} else {
window.appWindow.show()
}

navigate("settings")
})

globalShortcut.register(settings.shortcuts.exit, async () => {
exit()
})
}

registerShortcuts()
6 changes: 6 additions & 0 deletions interface/libraries/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ declare global {
vault: {
codes: null | string
}

shortcuts: {
show: string
settings: string
exit: string
}
}

/** Query selector element types */
Expand Down
6 changes: 6 additions & 0 deletions interface/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const defaultSettings: LibSettings = {
vault: {
codes: null,
},

shortcuts: {
show: "CmdOrCtrl+Shift+a",
settings: "CmdOrCtrl+Shift+s",
exit: "CmdOrCtrl+Shift+d",
},
}

if (build.dev === false && localStorage.settings === undefined) {
Expand Down
4 changes: 2 additions & 2 deletions interface/windows/edit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ const generateEditElements = () => {
</div>
</div>
<div class="ml-10 flex gap-3 flex-wrap sm:mt-10 sm:w-full sm:ml-0">
<button id="editCode${i}" class="button requirePassword">
<button id="editCode${i}" class="button">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z" />
</svg>
Edit
</button>
<button id="deleteCode${i}" class="button requirePassword">
<button id="deleteCode${i}" class="button">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
Expand Down
38 changes: 38 additions & 0 deletions interface/windows/settings/settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,43 @@
</div>
</div>

<div class="transparent-900 m-auto my-20 w-4/5 rounded-2xl p-10 text-left">
<h1 class="px-10 pb-10">Shortcuts</h1>

{#each shortcuts as { id, name }, i}
<div class="mx-auto flex flex-col items-center justify-center rounded-2xl px-10">
<div class="edit">
<div class="flex flex-wrap gap-3">
<div>
<h5>{name}</h5>
<input id="shortcut{i}" bind:value={$settings.shortcuts[id]} readonly class="input mt-1" type="text" />
</div>
</div>
<div class="ml-10 flex flex-wrap gap-3 sm:mt-10 sm:ml-0 sm:w-full">
<button on:click={() => editShortcut(i)} id="editShortcut{i}" class="button">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z" />
</svg>
Edit
</button>
<button on:click={() => resetShortcut(i)} id="resetShortcut{i}" class="button">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg>
Reset
</button>
<button on:click={() => deleteShortcut(i)} id="deleteShortcut{i}" class="button">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
Delete
</button>
</div>
</div>
</div>
{/each}
</div>

<div class="transparent-900 m-auto my-20 w-4/5 rounded-2xl p-10 text-left">
<h1 class="px-10">About</h1>

Expand Down Expand Up @@ -168,4 +205,5 @@
import { about, clearData, showLogs, launchOnStartup } from "./index"
import { settings } from "../../stores/settings"
import { open } from "../../libraries/navigate"
import { deleteShortcut, editShortcut, resetShortcut, shortcuts } from "../../libraries/shortcuts"
</script>

0 comments on commit fb019fc

Please sign in to comment.