Files
glassMxrph-plugins/mx-keymap/SheetPanel.qml
T
2026-11-29 15:00:14 +01:00

669 lines
27 KiB
QML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import qs.Commons
import qs.Ui
// The cheat sheet: the keyboard drawn from the repo's keymap. Overview puts
// the base layer in the middle of each key and four more layers in its
// corners, each in its own colour; a layer tab shows that layer alone, with
// keys that fall through to the base layer dimmed. Combos sit underneath;
// pointing at one lights up its keys.
Panel {
id: root
property Item anchorItem: null
property var service: null
readonly property var sheet: service ? service.sheet : null
readonly property var layers: sheet && sheet.layers ? sheet.layers : []
readonly property var layoutKeys: sheet && sheet.layout ? sheet.layout.keys : []
readonly property var base: layers.length ? layers[0].keys : []
// -1 is the overview; otherwise a layer index. Pointing at a tab previews it.
property int view: -1
property int preview: -2
readonly property int shown: preview > -2 ? preview : view
property var highlight: []
// Corner of each overview layer (layers 1..4), as in the repo's own drawing.
readonly property var places: ["bl", "tr", "tl", "br"]
// Board geometry in key units.
readonly property real unitsW: layoutKeys.reduce((m, k) => Math.max(m, k.x + 1), 0)
readonly property real unitsH: layoutKeys.reduce((m, k) => Math.max(m, k.y + 1), 0) + 0.2
readonly property real keySize: Style.space(Math.max(36, Math.min(120, Number(setting("keySize", 66)) || 66)))
readonly property real unit: Math.min(keySize, unitsW > 0 ? (card.availableCardWidth - card.padding * 2) / unitsW : keySize)
readonly property real gap: Math.max(3, Math.round(unit * 0.07))
readonly property int cardWidth: Math.max(Style.space(560), Math.ceil(unitsW * unit)) + card.padding * 2
function layerIndex(name) {
if (!name) return -1
for (let i = 0; i < layers.length; i++) if (layers[i].name === name) return i
return -1
}
// Layer colours follow the theme accent, spread around the colour wheel.
readonly property var hueSteps: [0, 0.5, 0.27, 0.77, 0.13, 0.63, 0.38, 0.88]
function layerColor(i) {
if (i <= 0) return Color.foreground
const h0 = Color.accent.hslHue < 0 ? 0.6 : Color.accent.hslHue
const h = (h0 + hueSteps[(i - 1) % hueSteps.length]) % 1
return Qt.hsla(h, 0.68, 0.72, 1)
}
function legend(k) { return k && k.type !== "trans" && k.type !== "held" ? String(k.t || "") : "" }
// What one key shows in the current view.
function cellFor(i) {
const b = base[i] || {}
const thumb = !!(layoutKeys[i] && layoutKeys[i].thumb)
const out = { t: legend(b), s: b.s || "", h: b.h || "", kind: b.type || "", corners: [], tint: -1, holdTint: -1 }
if (shown >= 0 && shown < layers.length && shown > 0) {
const k = layers[shown].keys[i] || {}
if (k.type === "trans") { out.kind = "trans"; out.s = ""; out.h = "" }
else if (k.type === "held") { out.kind = "held"; out.t = legend(b); out.s = ""; out.h = "" }
else { out.t = legend(k); out.s = k.s || ""; out.h = k.h || ""; out.kind = k.type || "" }
} else if (shown < 0 && !thumb) {
for (let j = 1; j <= Math.min(4, layers.length - 1); j++) {
const t = legend(layers[j].keys[i])
if (t && t !== out.t) out.corners.push({ place: places[j - 1], text: t, color: layerColor(j) })
}
if (out.corners.length) out.s = ""
}
out.tint = layerIndex(out.t)
out.holdTint = layerIndex(out.h)
if (out.kind === "held") out.tint = shown
return out
}
readonly property var cells: {
const list = []
for (let i = 0; i < layoutKeys.length; i++) list.push(cellFor(i))
return list
}
readonly property string shownName: shown >= 0 && shown < layers.length ? layers[shown].name : (layers.length ? layers[0].name : "")
readonly property var combos: (sheet && sheet.combos ? sheet.combos : []).filter(c => !c.layers.length || c.layers.indexOf(shownName) >= 0)
readonly property var layerCombos: sheet && sheet.layerCombos ? sheet.layerCombos : []
function select(v) {
view = Math.max(-1, Math.min(layers.length - 1, v))
preview = -2
}
function showNamed(name) {
const i = String(name).toLowerCase() === "overview" ? -1 : layers.findIndex(l => l.name.toLowerCase() === String(name).toLowerCase())
if (i >= -1) select(i)
}
Connections {
target: root.service
function onShowView(layer) { if (root.opened) root.showNamed(layer) }
}
function ago(seconds) {
if (!seconds) return ""
const s = Math.max(0, Math.round(Date.now() / 1000 - seconds))
if (s < 60) return "just now"
if (s < 3600) return Math.floor(s / 60) + " min ago"
if (s < 86400) return Math.floor(s / 3600) + " h ago"
const d = Math.floor(s / 86400)
return d === 1 ? "yesterday" : d + " days ago"
}
// Arrow glyphs are drawn small in most UI fonts; give them more room.
function isArrow(text) { return /^[←↑→↓⇡⇣⇠⇢]+$/.test(String(text)) }
function sizeFor(text) {
if (isArrow(text)) return Math.round(unit * 0.42)
const n = String(text).length
if (n <= 1) return Math.round(unit * 0.34)
if (n <= 3) return Math.round(unit * 0.25)
if (n <= 5) return Math.round(unit * 0.21)
return Math.round(unit * 0.18)
}
onOpenedChanged: {
if (!service) return
service.openPanels += opened ? 1 : -1
if (opened) {
view = setting("startView", "overview") === "base" ? 0 : -1
if (service.wantedView) showNamed(service.wantedView)
service.wantedView = ""
preview = -2
highlight = []
}
}
Component.onDestruction: if (opened && service) service.openPanels--
// Relative times stay fresh while it is open.
Timer {
id: clock
property int tick: 0
interval: 30000
repeat: true
running: root.opened
onTriggered: tick++
}
KeyboardPanel {
id: card
anchorItem: root.anchorItem
owner: root
bar: root.bar
open: root.opened
focusTarget: keys
contentWidth: card.fittedContentWidth(root.cardWidth)
contentHeight: card.fittedContentHeight(column.implicitHeight)
Item {
id: keys
anchors.fill: parent
focus: true
Keys.onPressed: function(event) {
const n = root.layers.length
if (event.key === Qt.Key_Escape) root.close()
else if (event.key === Qt.Key_Right || event.key === Qt.Key_Tab || event.key === Qt.Key_L)
root.select(root.view + 1 >= n ? -1 : root.view + 1)
else if (event.key === Qt.Key_Left || event.key === Qt.Key_Backtab || event.key === Qt.Key_H)
root.select(root.view - 1 < -1 ? n - 1 : root.view - 1)
else if (event.key === Qt.Key_0 || event.key === Qt.Key_QuoteLeft) root.select(-1)
else if (event.key >= Qt.Key_1 && event.key <= Qt.Key_9 && event.key - Qt.Key_1 < n) root.select(event.key - Qt.Key_1)
else if (event.key === Qt.Key_R && root.service) root.service.refresh(true)
else return
event.accepted = true
}
}
Column {
id: column
width: parent.width
spacing: Style.spacing.xxl
// Header: name, where it came from, actions.
Item {
width: parent.width
height: Math.max(titleCol.implicitHeight, actions.implicitHeight)
Row {
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
spacing: Style.spacing.xl
Text {
anchors.verticalCenter: parent.verticalCenter
text: "󰌌"
color: Color.accent
font.family: Style.font.family
font.pixelSize: Style.font.displayLarge
}
Column {
id: titleCol
anchors.verticalCenter: parent.verticalCenter
spacing: Style.spacing.xxs
Row {
spacing: Style.spacing.lg
Text {
id: titleText
text: "Keymap"
color: Color.foreground
font.family: Style.font.family
font.pixelSize: Style.font.heading
font.bold: true
}
Text {
anchors.baseline: titleText.baseline
text: root.sheet ? root.sheet.repo : (root.service ? root.service.repository : "")
color: Color.muted
font.family: Style.font.family
font.pixelSize: Style.font.body
}
}
Text {
text: {
clock.tick
const s = root.sheet
if (root.service && root.service.error) return root.service.error
if (!s) return root.service && root.service.busy ? "Reading the repository…" : ""
if (s.pending) return "Keymap changed, waiting for the repo to redraw it…"
const parts = []
if (s.committed) parts.push("changed " + root.ago(s.committed))
if (s.sha) parts.push(s.sha.slice(0, 7))
if (s.subject) parts.push(s.subject.replace(/^\[Draw\]\s*/, ""))
return parts.join(" · ")
}
width: Math.min(implicitWidth, column.width - actions.width - Style.space(80))
elide: Text.ElideRight
color: root.service && root.service.error ? Color.urgent : root.sheet && root.sheet.pending ? Color.accent : Color.muted
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
}
}
Row {
id: actions
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
spacing: Style.spacing.sm
Button {
iconText: "󰑐"
iconSpinning: !!root.service && root.service.busy
tooltipText: "Check the repo now (R)"
onClicked: if (root.service) root.service.refresh(true)
}
Button {
visible: !!root.sheet && !!root.sheet.web
iconText: "󰊤"
tooltipText: "Open the repository"
onClicked: {
const s = root.sheet
const url = s.web + (s.source ? "/blob/" + (s.branch || s.sha) + "/" + s.source : "")
Quickshell.execDetached(["xdg-open", url])
root.close()
}
}
Button {
iconText: "󰅖"
tooltipText: "Close"
onClicked: root.close()
}
}
}
// Layer tabs. The little key in each shows where that layer sits in the overview.
Flow {
id: tabs
width: parent.width
spacing: Style.spacing.md
visible: root.layers.length > 0
Repeater {
model: root.layers.length + 1
delegate: Rectangle {
id: tab
required property int index
readonly property int idx: index - 1
readonly property bool selected: root.view === idx
readonly property color tone: idx < 0 ? Color.accent : root.layerColor(idx)
readonly property string place: idx >= 1 && idx <= 4 ? root.places[idx - 1] : idx === 0 ? "c" : ""
width: tabRow.implicitWidth + Style.spacing.xxl * 2
height: Style.space(30)
radius: height / 2
color: selected ? Qt.alpha(tone, 0.2) : tabMouse.containsMouse ? Qt.alpha(Color.foreground, 0.07) : Qt.alpha(Color.foreground, 0.03)
border.width: 1
border.color: selected ? Qt.alpha(tone, 0.55) : Qt.alpha(Color.foreground, 0.08)
Behavior on color { ColorAnimation { duration: 120 } }
Row {
id: tabRow
anchors.centerIn: parent
spacing: Style.spacing.md
// Mini key: a dot in this layer's corner (centre for the base layer).
Rectangle {
anchors.verticalCenter: parent.verticalCenter
visible: tab.idx >= 0
width: Style.space(13)
height: width
radius: Style.space(3)
color: "transparent"
border.width: 1
border.color: Qt.alpha(tab.tone, 0.6)
Rectangle {
width: Style.space(5)
height: width
radius: width / 2
color: tab.tone
x: tab.place === "tl" || tab.place === "bl" ? Style.space(2) : tab.place === "tr" || tab.place === "br" ? parent.width - width - Style.space(2) : (parent.width - width) / 2
y: tab.place === "tl" || tab.place === "tr" ? Style.space(2) : tab.place === "bl" || tab.place === "br" ? parent.height - height - Style.space(2) : (parent.height - height) / 2
}
}
Text {
anchors.verticalCenter: parent.verticalCenter
visible: tab.idx < 0
text: "󰕰"
color: tab.selected ? tab.tone : Color.muted
font.family: Style.font.family
font.pixelSize: Style.font.title
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: tab.idx < 0 ? "Overview" : root.layers[tab.idx].name
color: tab.selected ? Color.foreground : Qt.alpha(Color.foreground, 0.75)
font.family: Style.font.family
font.pixelSize: Style.font.body
font.bold: tab.selected
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: String(tab.index)
visible: tab.index <= 9
color: Qt.alpha(Color.foreground, 0.32)
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
}
MouseArea {
id: tabMouse
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onContainsMouseChanged: {
if (containsMouse) root.preview = tab.idx
else if (root.preview === tab.idx) root.preview = -2
}
onClicked: root.select(tab.idx)
}
}
}
}
// The keyboard.
Item {
id: board
width: parent.width
height: root.layoutKeys.length ? Math.ceil(root.unitsH * root.unit) : Style.space(220)
Text {
anchors.centerIn: parent
visible: !root.layoutKeys.length
horizontalAlignment: Text.AlignHCenter
text: root.service && root.service.error ? "󰌐\n" + root.service.error : "󰌌\nReading your keymap…"
color: Color.muted
lineHeight: 1.3
font.family: Style.font.family
font.pixelSize: Style.font.title
}
Item {
id: plate
width: root.unitsW * root.unit
height: parent.height
anchors.horizontalCenter: parent.horizontalCenter
Repeater {
model: root.layoutKeys.length
delegate: Item {
id: key
required property int index
readonly property var pos: root.layoutKeys[index]
readonly property var cell: root.cells[index] || ({ t: "", s: "", h: "", kind: "", corners: [], tint: -1, holdTint: -1 })
readonly property bool dim: cell.kind === "trans" || cell.kind === "blank"
readonly property bool lit: root.highlight.indexOf(index) >= 0
readonly property color tone: cell.tint > 0 ? root.layerColor(cell.tint) : Color.foreground
readonly property bool toned: cell.tint > 0 && !dim
x: pos.x * root.unit + root.gap / 2
y: pos.y * root.unit + root.gap / 2
width: root.unit - root.gap
height: root.unit - root.gap
rotation: pos.r || 0
Rectangle {
id: tile
anchors.fill: parent
radius: Math.round(root.unit * 0.17)
gradient: Gradient {
GradientStop {
position: 0
color: key.cell.kind === "held" ? Qt.alpha(key.tone, 0.42)
: key.toned ? Qt.alpha(key.tone, 0.24)
: key.dim ? Qt.alpha(Color.foreground, 0.025) : Qt.alpha(Color.foreground, 0.09)
Behavior on color { ColorAnimation { duration: 140 } }
}
GradientStop {
position: 1
color: key.cell.kind === "held" ? Qt.alpha(key.tone, 0.28)
: key.toned ? Qt.alpha(key.tone, 0.13)
: key.dim ? Qt.alpha(Color.foreground, 0.015) : Qt.alpha(Color.foreground, 0.045)
Behavior on color { ColorAnimation { duration: 140 } }
}
}
border.width: key.lit ? 2 : 1
border.color: key.lit ? Color.accent
: key.toned || key.cell.kind === "held" ? Qt.alpha(key.tone, 0.5)
: Qt.alpha(Color.foreground, key.dim ? 0.05 : 0.1)
Behavior on border.color { ColorAnimation { duration: 140 } }
}
// Shifted / modifier hint on top.
Text {
anchors.top: parent.top
anchors.topMargin: Math.round(root.unit * 0.07)
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width - 8
horizontalAlignment: Text.AlignHCenter
visible: text !== "" && !key.dim
text: key.cell.s
elide: Text.ElideRight
color: Qt.alpha(Color.foreground, 0.5)
font.family: Style.font.family
font.pixelSize: Math.round(root.unit * 0.16)
}
// Main legend.
Text {
anchors.centerIn: parent
anchors.verticalCenterOffset: key.cell.h && !key.dim ? -Math.round(root.unit * 0.06) : 0
width: parent.width - Math.round(root.unit * 0.12)
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: key.cell.kind === "held" ? "held" : key.cell.t
wrapMode: Text.WordWrap
maximumLineCount: 2
elide: Text.ElideRight
lineHeight: 0.9
color: key.lit ? Color.accent
: key.cell.kind === "held" ? key.tone
: key.dim ? Qt.alpha(Color.foreground, 0.22)
: key.toned ? key.tone : Color.foreground
font.family: Style.font.family
font.pixelSize: key.cell.kind === "held" ? Math.round(root.unit * 0.19) : root.sizeFor(key.cell.t)
font.weight: key.dim ? Font.Normal : Font.DemiBold
font.italic: key.cell.kind === "held"
}
// Hold action underneath.
Text {
anchors.bottom: parent.bottom
anchors.bottomMargin: Math.round(root.unit * 0.07)
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width - 6
horizontalAlignment: Text.AlignHCenter
visible: text !== "" && !key.dim
text: key.cell.h
elide: Text.ElideRight
color: key.cell.holdTint > 0 ? root.layerColor(key.cell.holdTint) : Qt.alpha(Color.foreground, 0.55)
font.family: Style.font.family
font.pixelSize: Math.round(root.unit * 0.155)
font.bold: key.cell.holdTint > 0
}
// Overview: the other layers in the corners.
Repeater {
model: key.cell.corners
delegate: Text {
required property var modelData
readonly property bool atLeft: modelData.place === "tl" || modelData.place === "bl"
readonly property bool atTop: modelData.place === "tl" || modelData.place === "tr"
x: atLeft ? Math.round(root.unit * 0.08) : key.width - width - Math.round(root.unit * 0.08)
y: atTop ? Math.round(root.unit * 0.05) : key.height - height - Math.round(root.unit * 0.05)
width: Math.min(implicitWidth, key.width * 0.62)
horizontalAlignment: atLeft ? Text.AlignLeft : Text.AlignRight
text: modelData.text
elide: Text.ElideRight
color: modelData.color
font.family: Style.font.family
font.pixelSize: Math.round(root.unit * (root.isArrow(modelData.text) ? 0.25 : String(modelData.text).length > 3 ? 0.14 : 0.175))
font.weight: Font.Medium
}
}
}
}
}
}
// Combos for the layer on screen, and layers you reach by holding two.
Column {
width: parent.width
spacing: Style.spacing.lg
visible: root.setting("showCombos", true) !== false && (root.combos.length > 0 || root.layerCombos.length > 0)
Rectangle {
width: parent.width
height: 1
color: Qt.alpha(Color.foreground, 0.07)
}
Text {
text: "COMBOS" + (root.shown > 0 ? " · " + root.shownName.toUpperCase() : "")
color: Color.muted
font.family: Style.font.family
font.pixelSize: Style.font.caption
font.letterSpacing: 1.2
font.bold: true
}
Flow {
width: parent.width
spacing: Style.spacing.md
Repeater {
model: root.layerCombos
delegate: Rectangle {
id: lchip
required property var modelData
width: lrow.implicitWidth + Style.spacing.xxl * 2
height: Style.space(28)
radius: Style.space(8)
color: Qt.alpha(Color.foreground, 0.04)
border.width: 1
border.color: Qt.alpha(Color.foreground, 0.08)
Row {
id: lrow
anchors.centerIn: parent
spacing: Style.spacing.sm
Text {
text: "hold"
color: Color.muted
font.family: Style.font.family
font.pixelSize: Style.font.bodySmall
}
Repeater {
model: lchip.modelData.hold
delegate: Text {
required property var modelData
required property int index
text: (index ? "+ " : "") + modelData
color: root.layerColor(root.layerIndex(modelData))
font.family: Style.font.family
font.pixelSize: Style.font.bodySmall
font.bold: true
}
}
Text {
text: "→"
color: Color.muted
font.family: Style.font.family
font.pixelSize: Style.font.bodySmall
}
Text {
text: lchip.modelData.layer
color: root.layerColor(root.layerIndex(lchip.modelData.layer))
font.family: Style.font.family
font.pixelSize: Style.font.bodySmall
font.bold: true
}
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: root.select(root.layerIndex(lchip.modelData.layer))
}
}
}
Repeater {
model: root.combos
delegate: Rectangle {
id: chip
required property var modelData
width: crow.implicitWidth + Style.spacing.xl * 2
height: Style.space(28)
radius: Style.space(8)
color: chipMouse.containsMouse ? Qt.alpha(Color.accent, 0.14) : Qt.alpha(Color.foreground, 0.04)
border.width: 1
border.color: chipMouse.containsMouse ? Qt.alpha(Color.accent, 0.5) : Qt.alpha(Color.foreground, 0.08)
Behavior on color { ColorAnimation { duration: 100 } }
Row {
id: crow
anchors.centerIn: parent
spacing: Style.spacing.xs
Repeater {
model: chip.modelData.keys
delegate: Row {
id: part
required property var modelData
required property int index
spacing: Style.spacing.xs
Text {
anchors.verticalCenter: parent.verticalCenter
visible: part.index > 0
text: "+"
color: Qt.alpha(Color.foreground, 0.35)
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
Rectangle {
anchors.verticalCenter: parent.verticalCenter
readonly property string label: root.legend(root.base[part.modelData]) || ("#" + part.modelData)
width: Math.max(Style.space(18), capText.implicitWidth + Style.space(8))
height: Style.space(18)
radius: Style.space(4)
color: Qt.alpha(Color.foreground, 0.08)
border.width: 1
border.color: Qt.alpha(Color.foreground, 0.12)
Text {
id: capText
anchors.centerIn: parent
text: parent.label
color: Qt.alpha(Color.foreground, 0.85)
font.family: Style.font.family
font.pixelSize: Style.font.caption
font.weight: Font.DemiBold
}
}
}
}
Text {
anchors.verticalCenter: parent.verticalCenter
leftPadding: Style.spacing.xs
text: "→"
color: Color.muted
font.family: Style.font.family
font.pixelSize: Style.font.bodySmall
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: chip.modelData.legend || "?"
color: chipMouse.containsMouse ? Color.accent : Color.foreground
font.family: Style.font.family
font.pixelSize: Style.font.body
font.bold: true
}
}
MouseArea {
id: chipMouse
anchors.fill: parent
hoverEnabled: true
onContainsMouseChanged: {
if (containsMouse) root.highlight = chip.modelData.keys
else if (root.highlight === chip.modelData.keys) root.highlight = []
}
}
}
}
}
}
Text {
width: parent.width
visible: root.setting("showHints", true) !== false
horizontalAlignment: Text.AlignRight
text: "Arrow keys or 0–" + Math.min(9, root.layers.length) + " switch layers · R refresh · checks the repo every " + (root.service ? root.service.checkMinutes : 2) + " min"
color: Qt.alpha(Color.foreground, 0.32)
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
}
}
}