152 lines
5.4 KiB
QML
152 lines
5.4 KiB
QML
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
|
|
// Settings read from the JSON file named by SKYG_WORKSPACE_MANAGER.
|
|
// The file is optional. Anything missing or wrong falls back to the
|
|
// values in load(). The file is read again whenever it changes.
|
|
Item {
|
|
id: root
|
|
|
|
readonly property string configPath: Quickshell.env("SKYG_WORKSPACE_MANAGER") ?? ""
|
|
|
|
// Keys written as modifiers plus one key, like "Ctrl+N".
|
|
property string windowsKey
|
|
|
|
property bool showHints
|
|
// "{key}" in a hint is replaced by its key.
|
|
property string windowsHint
|
|
property string exitWindowsHint
|
|
// The search keys (/, n, N) are fixed, like in vim.
|
|
property string searchHint
|
|
|
|
readonly property string windowsHintText: root.fillKey(root.windowsHint, root.windowsKey)
|
|
readonly property string exitWindowsHintText: root.fillKey(root.exitWindowsHint, root.windowsKey)
|
|
|
|
Component.onCompleted: root.load({})
|
|
|
|
function fillKey(hint: string, key: string): string {
|
|
return hint.split("{key}").join(key);
|
|
}
|
|
|
|
function warn(message: string): void {
|
|
console.warn("workspace-manager config: " + message);
|
|
}
|
|
|
|
// Parsed JSON, so the values stay untyped.
|
|
function textOr(value, fallback: string, name: string): string {
|
|
if (value === undefined)
|
|
return fallback;
|
|
if (typeof value !== "string") {
|
|
root.warn(name + " should be a string");
|
|
return fallback;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function boolOr(value, fallback: bool, name: string): bool {
|
|
if (value === undefined)
|
|
return fallback;
|
|
if (typeof value !== "boolean") {
|
|
root.warn(name + " should be true or false");
|
|
return fallback;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function keyOr(value, fallback: string, name: string): string {
|
|
const text = root.textOr(value, fallback, name);
|
|
if (root.keyOf(text) < 0) {
|
|
root.warn(name + ": \"" + text + "\" is not a key this understands");
|
|
return fallback;
|
|
}
|
|
return text;
|
|
}
|
|
|
|
function load(parsed): void {
|
|
const keys = parsed.keys ?? {};
|
|
const hints = parsed.hints ?? {};
|
|
|
|
root.windowsKey = root.keyOr(keys.windows, "Tab", "keys.windows");
|
|
root.showHints = root.boolOr(hints.show, true, "hints.show");
|
|
|
|
root.windowsHint = root.textOr(hints.windows, "{key}: to see windows", "hints.windows");
|
|
root.exitWindowsHint = root.textOr(hints.exitWindows, "{key} or Esc: to exit window carousel", "hints.exitWindows");
|
|
root.searchHint = root.textOr(hints.search, "/: to search windows, n / N: next / previous match", "hints.search");
|
|
}
|
|
|
|
// ── keys ────────────────────────────────────────────────────────────
|
|
|
|
function modifiersOf(combo: string): int {
|
|
let mods = 0;
|
|
for (const part of combo.split("+")) {
|
|
const name = part.trim().toLowerCase();
|
|
if (name === "ctrl" || name === "control")
|
|
mods |= Qt.ControlModifier;
|
|
else if (name === "alt")
|
|
mods |= Qt.AltModifier;
|
|
else if (name === "shift")
|
|
mods |= Qt.ShiftModifier;
|
|
else if (name === "meta" || name === "super")
|
|
mods |= Qt.MetaModifier;
|
|
}
|
|
return mods;
|
|
}
|
|
|
|
// Qt key code of the one key that is not a modifier, or -1.
|
|
function keyOf(combo: string): int {
|
|
let code = -1;
|
|
for (const part of combo.split("+")) {
|
|
const name = part.trim().toUpperCase();
|
|
if (["CTRL", "CONTROL", "ALT", "SHIFT", "META", "SUPER"].includes(name))
|
|
continue;
|
|
if (code >= 0)
|
|
return -1;
|
|
// Qt codes for letters and digits match their ASCII codes.
|
|
if (/^[A-Z0-9]$/.test(name))
|
|
code = name.charCodeAt(0);
|
|
else if (/^F([1-9]|1[0-2])$/.test(name))
|
|
code = Qt.Key_F1 + Number(name.slice(1)) - 1;
|
|
else if (name === "SPACE")
|
|
code = Qt.Key_Space;
|
|
else if (name === "TAB")
|
|
code = Qt.Key_Tab;
|
|
else
|
|
return -1;
|
|
}
|
|
return code;
|
|
}
|
|
|
|
// Whether a key press is exactly this combo, with no extra modifiers.
|
|
function matches(event, combo: string): bool {
|
|
const code = root.keyOf(combo);
|
|
if (code < 0 || event.key !== code)
|
|
return false;
|
|
const known = Qt.ControlModifier | Qt.AltModifier | Qt.ShiftModifier | Qt.MetaModifier;
|
|
return (event.modifiers & known) === root.modifiersOf(combo);
|
|
}
|
|
|
|
FileView {
|
|
id: configFile
|
|
path: root.configPath
|
|
watchChanges: true
|
|
onFileChanged: configFile.reload()
|
|
onLoaded: {
|
|
try {
|
|
const parsed = JSON.parse(configFile.text());
|
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
throw new Error("expected a JSON object");
|
|
root.load(parsed);
|
|
} catch (error) {
|
|
root.warn(String(error));
|
|
root.load({});
|
|
}
|
|
}
|
|
onLoadFailed: {
|
|
if (root.configPath.length > 0)
|
|
root.warn("could not read " + root.configPath);
|
|
root.load({});
|
|
}
|
|
}
|
|
}
|