Use normal QtQuick.Shortcut

This commit is contained in:
miruka 2019-12-08 12:42:40 -04:00
parent 793db51a60
commit c2f5a5bcbc
6 changed files with 41 additions and 120 deletions

View File

@ -121,14 +121,14 @@ class UISettings(JSONConfigFile):
"toggleDebugConsole": ["Alt+Shift+C"], "toggleDebugConsole": ["Alt+Shift+C"],
"reloadConfig": ["Alt+Shift+R"], "reloadConfig": ["Alt+Shift+R"],
"zoomIn": ["Ctrl+Plus", "Ctrl+Shift+Plus"], "zoomIn": ["Ctrl++"],
"zoomOut": ["Ctrl+Minus", "Ctrl+Shift+Minus"], "zoomOut": ["Ctrl+-"],
"zoomReset": ["Ctrl+Equal", "Ctrl+Shift+Backspace"], "zoomReset": ["Ctrl+="],
"scrollUp": ["Alt+Up", "Alt+K"], "scrollUp": ["Alt+Up", "Alt+K"],
"scrollDown": ["Alt+Down", "Alt+J"], "scrollDown": ["Alt+Down", "Alt+J"],
"scrollPageUp": ["Alt+Ctrl+Up", "Alt+Ctrl+K", "PageUp"], "scrollPageUp": ["Alt+Ctrl+Up", "Alt+Ctrl+K", "PgUp"],
"scrollPageDown": ["Alt+Ctrl+Down", "Alt+Ctrl+J", "PageDown"], "scrollPageDown": ["Alt+Ctrl+Down", "Alt+Ctrl+J", "PgDown"],
"scrollToTop": "scrollToTop":
["Alt+Ctrl+Shift+Up", "Alt+Ctrl+Shift+K", "Home"], ["Alt+Ctrl+Shift+Up", "Alt+Ctrl+Shift+K", "Home"],
"scrollToBottom": "scrollToBottom":

View File

@ -41,7 +41,5 @@ ScrollView {
event.modifiers & Qt.AltModifier || event.modifiers & Qt.AltModifier ||
event.modifiers & Qt.MetaModifier event.modifiers & Qt.MetaModifier
) event.accepted = true ) event.accepted = true
Keys.forwardTo: mainUI.shortcuts
} }
} }

View File

@ -1,10 +1,5 @@
import QtQuick 2.12 import QtQuick 2.12
QtObject { Shortcut {
signal pressed(var event) context: Qt.ApplicationShortcut
signal held(var event)
signal released(var event)
property bool enabled: true
property var sequences: "" // shortcut string array of shortcut strings
} }

View File

@ -1,65 +0,0 @@
import QtQuick 2.12
Item {
id: shortcutHandler
Keys.onPressed: {
let shortcut = match(event)
if (! shortcut) return
event.accepted = true
event.isAutoRepeat ? shortcut.held(event) : shortcut.pressed(event)
}
Keys.onReleased: {
if (event.isAutoRepeat) return
let shortcut = match(event)
if (! shortcut) return
event.accepted = true
if (shortcut && ! event.isAutoRepeat) shortcut.released(event)
}
readonly property var modifierDict: ({
Ctrl: Qt.ControlModifier,
Shift: Qt.ShiftModifier,
Alt: Qt.AltModifier,
Meta: Qt.MetaModifier,
})
function match(event) {
for (let i = 0; i < shortcutHandler.resources.length; i++) {
let shortcut = shortcutHandler.resources[i]
if (! shortcut.enabled) continue
if (typeof(shortcut.sequences) == "string") {
shortcut.sequences = [shortcut.sequences]
}
for (let i = 0; i < shortcut.sequences.length; i++) {
if (sequenceMatches(event, shortcut.sequences[i])) {
return shortcut
}
}
}
return null
}
function sequenceMatches(event, sequence) {
let [key, ...mods] = sequence.split("+").reverse()
key = key.charAt(0).toUpperCase() + key.slice(1)
if (event.key !== Qt["Key_" + key]) return false
for (let [name, code] of Object.entries(modifierDict)) {
if (mods.includes(name) && ! (event.modifiers & code)) return false
if (! mods.includes(name) && event.modifiers & code) return false
}
return true
}
}

View File

@ -49,6 +49,4 @@ TextField {
event.modifiers & Qt.AltModifier || event.modifiers & Qt.AltModifier ||
event.modifiers & Qt.MetaModifier event.modifiers & Qt.MetaModifier
) event.accepted = true ) event.accepted = true
Keys.forwardTo: mainUI.shortcuts
} }

View File

@ -3,7 +3,9 @@ import QtQuick.Controls 2.12
import "Base" import "Base"
import "utils.js" as Utils import "utils.js" as Utils
HShortcutHandler { Item {
visible: false
// Flickable or ListView that should be affected by scroll shortcuts // Flickable or ListView that should be affected by scroll shortcuts
property Item flickTarget property Item flickTarget
@ -19,40 +21,40 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: debugMode enabled: debugMode
sequences: settings.keys.startPythonDebugger sequences: settings.keys.startPythonDebugger
onPressed: py.call("APP.pdb") onActivated: py.call("APP.pdb")
} }
HShortcut { HShortcut {
enabled: debugMode && debugConsole enabled: debugMode && debugConsole
sequences: settings.keys.toggleDebugConsole sequences: settings.keys.toggleDebugConsole
onPressed: debugConsole.visible = ! debugConsole.visible onActivated: debugConsole.visible = ! debugConsole.visible
} }
HShortcut { HShortcut {
sequences: settings.keys.reloadConfig sequences: settings.keys.reloadConfig
onPressed: py.loadSettings(() => { mainUI.pressAnimation.start() }) onActivated: py.loadSettings(() => { mainUI.pressAnimation.start() })
} }
HShortcut { HShortcut {
sequences: settings.keys.zoomIn sequences: settings.keys.zoomIn
onPressed: theme.uiScale += 0.1 onActivated: theme.uiScale += 0.1
} }
HShortcut { HShortcut {
sequences: settings.keys.zoomOut sequences: settings.keys.zoomOut
onPressed: theme.uiScale = Math.max(0.1, theme.uiScale - 0.1) onActivated: theme.uiScale = Math.max(0.1, theme.uiScale - 0.1)
} }
HShortcut { HShortcut {
sequences: settings.keys.zoomReset sequences: settings.keys.zoomReset
onPressed: theme.uiScale = 1 onActivated: theme.uiScale = 1
} }
// Pages // Pages
HShortcut { HShortcut {
sequences: settings.keys.goToLastPage sequences: settings.keys.goToLastPage
onPressed: mainUI.pageLoader.showPrevious() onActivated: mainUI.pageLoader.showPrevious()
} }
// Page scrolling // Page scrolling
@ -60,50 +62,44 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: flickTarget enabled: flickTarget
sequences: settings.keys.scrollUp sequences: settings.keys.scrollUp
onPressed: Utils.smartVerticalFlick(flickTarget, -335) onActivated: Utils.smartVerticalFlick(flickTarget, -335)
onHeld: pressed(event)
} }
HShortcut { HShortcut {
enabled: flickTarget enabled: flickTarget
sequences: settings.keys.scrollDown sequences: settings.keys.scrollDown
onPressed: Utils.smartVerticalFlick(flickTarget, 335) onActivated: Utils.smartVerticalFlick(flickTarget, 335)
onHeld: pressed(event)
} }
HShortcut { HShortcut {
enabled: flickTarget enabled: flickTarget
sequences: settings.keys.scrollPageUp sequences: settings.keys.scrollPageUp
onPressed: Utils.smartVerticalFlick( onActivated: Utils.smartVerticalFlick(
flickTarget, -2.3 * flickTarget.height, 8, flickTarget, -2.3 * flickTarget.height, 8,
) )
onHeld: pressed(event)
// Ensure only a slight slip after releasing the key // Ensure only a slight slip after releasing the key
onReleased: Utils.smartVerticalFlick(flickTarget, -335) // onReleased: Utils.smartVerticalFlick(flickTarget, -335)
} }
HShortcut { HShortcut {
enabled: flickTarget enabled: flickTarget
sequences: settings.keys.scrollPageDown sequences: settings.keys.scrollPageDown
onPressed: Utils.smartVerticalFlick( onActivated: Utils.smartVerticalFlick(
flickTarget, 2.3 * flickTarget.height, 8, flickTarget, 2.3 * flickTarget.height, 8,
) )
onHeld: pressed(event) // onReleased: Utils.smartVerticalFlick(flickTarget, 335)
onReleased: Utils.smartVerticalFlick(flickTarget, 335)
} }
HShortcut { HShortcut {
enabled: flickTarget enabled: flickTarget
sequences: settings.keys.scrollToTop sequences: settings.keys.scrollToTop
onPressed: Utils.flickToTop(flickTarget) onActivated: Utils.flickToTop(flickTarget)
onHeld: pressed(event)
} }
HShortcut { HShortcut {
enabled: flickTarget enabled: flickTarget
sequences: settings.keys.scrollToBottom sequences: settings.keys.scrollToBottom
onPressed: Utils.flickToBottom(flickTarget) onActivated: Utils.flickToBottom(flickTarget)
onHeld: pressed(event)
} }
@ -112,7 +108,7 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: tabsTarget enabled: tabsTarget
sequences: settings.keys.previousTab sequences: settings.keys.previousTab
onPressed: tabsTarget.setCurrentIndex( onActivated: tabsTarget.setCurrentIndex(
Utils.numberWrapAt(tabsTarget.currentIndex - 1, tabsTarget.count), Utils.numberWrapAt(tabsTarget.currentIndex - 1, tabsTarget.count),
) )
} }
@ -120,7 +116,7 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: tabsTarget enabled: tabsTarget
sequences: settings.keys.nextTab sequences: settings.keys.nextTab
onPressed: tabsTarget.setCurrentIndex( onActivated: tabsTarget.setCurrentIndex(
Utils.numberWrapAt(tabsTarget.currentIndex + 1, tabsTarget.count), Utils.numberWrapAt(tabsTarget.currentIndex + 1, tabsTarget.count),
) )
} }
@ -131,54 +127,53 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.focusSidePane sequences: settings.keys.focusSidePane
onPressed: mainUI.sidePane.setFocus() onActivated: mainUI.sidePane.toggleFocus()
context: Qt.ApplicationShortcut
} }
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.clearRoomFilter sequences: settings.keys.clearRoomFilter
onPressed: mainUI.sidePane.toolBar.roomFilter = "" onActivated: mainUI.sidePane.toolBar.roomFilter = ""
} }
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.addNewAccount sequences: settings.keys.addNewAccount
onPressed: mainUI.sidePane.toolBar.addAccountButton.clicked() onActivated: mainUI.sidePane.toolBar.addAccountButton.clicked()
} }
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.addNewChat sequences: settings.keys.addNewChat
onPressed: mainUI.sidePane.sidePaneList.addNewChat() onActivated: mainUI.sidePane.sidePaneList.addNewChat()
} }
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.accountSettings sequences: settings.keys.accountSettings
onPressed: mainUI.sidePane.sidePaneList.accountSettings() onActivated: mainUI.sidePane.sidePaneList.accountSettings()
} }
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.toggleCollapseAccount sequences: settings.keys.toggleCollapseAccount
onPressed: mainUI.sidePane.sidePaneList.toggleCollapseAccount() onActivated: mainUI.sidePane.sidePaneList.toggleCollapseAccount()
} }
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.goToPreviousRoom sequences: settings.keys.goToPreviousRoom
onPressed: mainUI.sidePane.sidePaneList.previous() onActivated: mainUI.sidePane.sidePaneList.previous()
onHeld: pressed(event)
} }
HShortcut { HShortcut {
enabled: mainUI.accountsPresent enabled: mainUI.accountsPresent
sequences: settings.keys.goToNextRoom sequences: settings.keys.goToNextRoom
onPressed: mainUI.sidePane.sidePaneList.next() onActivated: mainUI.sidePane.sidePaneList.next()
onHeld: pressed(event)
} }
@ -187,7 +182,7 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: window.uiState.page == "Chat/Chat.qml" enabled: window.uiState.page == "Chat/Chat.qml"
sequences: settings.keys.clearRoomMessages sequences: settings.keys.clearRoomMessages
onPressed: Utils.makePopup( onActivated: Utils.makePopup(
"Popups/ClearMessagesPopup.qml", "Popups/ClearMessagesPopup.qml",
mainUI, mainUI,
{ {
@ -200,7 +195,7 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: window.uiState.page == "Chat/Chat.qml" enabled: window.uiState.page == "Chat/Chat.qml"
sequences: settings.keys.sendFile sequences: settings.keys.sendFile
onPressed: Utils.makeObject( onActivated: Utils.makeObject(
"Dialogs/SendFilePicker.qml", "Dialogs/SendFilePicker.qml",
mainUI, mainUI,
{ {
@ -215,7 +210,7 @@ HShortcutHandler {
HShortcut { HShortcut {
enabled: window.uiState.page == "Chat/Chat.qml" enabled: window.uiState.page == "Chat/Chat.qml"
sequences: settings.keys.sendFileFromPathInClipboard sequences: settings.keys.sendFileFromPathInClipboard
onPressed: Utils.sendFile( onActivated: Utils.sendFile(
window.uiState.pageProperties.userId, window.uiState.pageProperties.userId,
window.uiState.pageProperties.roomId, window.uiState.pageProperties.roomId,
Clipboard.text.trim(), Clipboard.text.trim(),