This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters