Skip to content

Commit

Permalink
Extract SteamInputScrollableGridView.qml (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik authored Jul 13, 2023
1 parent 76006df commit 51a5f65
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 182 deletions.
207 changes: 28 additions & 179 deletions resources/qml/DirectoryView/DirectoryView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -27,92 +27,19 @@ Item {
id: fs_model
showHidden: showHiddenSwitch.position === 1.0
onModelReset: view.currentIndex = 0
}

function cdIndex(index) {
const item = fs_model.get(index)

if (item.isDir) {
fs_model.path = item.path
return
}
root.fileOpened(item.path)
}

SteamInputControl {
id: inputControl
objectName: "DirectoryView"
controller: steam_input.lastController

enabled: visible

actionSet: "deckfm"
actionSetLayers: ["file_manager"]

pressHandlers: {
"down": view.moveCurrentIndexDown,
"up": view.moveCurrentIndexUp,
"left": view.moveCurrentIndexLeft,
"right": view.moveCurrentIndexRight,
"file_manager_open": () => root.cdIndex(view.currentIndex),
"file_manager_go_back": fs_model.goUp,
"file_manager_go_home": fs_model.goHome
}

property double scrollPos: 0

analogHandlers: {
"scroll": e => {
if ((e.analogY <= 0 && view.atYEnd) || (e.analogY >= 0
&& view.atYBeginning)) {
view.stopScrollMomentum()
return
}

if (e.analogY >= 0 && view.atYBeginning) {
view.stopScrollMomentum()
return
}

scrollPos += Math.abs(e.analogY)
function cdIndex(index) {
const item = fs_model.get(index)

if (scrollPos >= view.cellHeight) {
scrollPos = 0
view.scrollHaptic()
}
view.contentY -= e.analogY
view.returnToBounds()
if (item.isDir) {
fs_model.path = item.path
return
}
root.fileOpened(item.path)
}
}

Keys.onPressed: {
event.accepted = true

switch (event.key) {
case Qt.Key_Return:
root.cdIndex(view.currentIndex)
break
case Qt.Key_Escape:
fs_model.goUp()
break
case Qt.Key_Home:
fs_model.goHome()
break
case Qt.Key_Up:
view.moveCurrentIndexUp()
break
case Qt.Key_Down:
view.moveCurrentIndexDown()
break
case Qt.Key_Left:
view.moveCurrentIndexLeft()
break
case Qt.Key_Right:
view.moveCurrentIndexRight()
break
}
}
onActiveFocusChanged: view.forceActiveFocus()

ColumnLayout {
anchors.fill: parent
Expand Down Expand Up @@ -155,7 +82,7 @@ Item {
}
}

GridView {
Core.SteamInputScrollableGridView {
id: view

model: fs_model
Expand All @@ -165,116 +92,38 @@ Item {

cellWidth: Math.max(parent.width / 4, 300)
cellHeight: 70
clip: true
keyNavigationWraps: false

boundsBehavior: Flickable.StopAtBounds

ScrollBar.vertical: ScrollBar {}

onCurrentIndexChanged: view.scrollHaptic()
actionSetLayers: ["file_manager"]

function stopScrollMomentum() {
if (!inputControl.controller) {
return
}

const a = inputControl.controller.actionSet.actions["scroll"]
inputControl.controller.stopAnalogActionMomentum(a)
pressHandlers: {
"file_manager_open": () => fs_model.cdIndex(view.currentIndex),
"file_manager_go_back": fs_model.goUp,
"file_manager_go_home": fs_model.goHome
}

function stopScrollHaptic() {
if (!inputControl.controller) {
return
}

inputControl.controller.triggerRepeatedHapticPulse(2900, 1200,
2)
}

function scrollHaptic() {
if (!inputControl.controller) {
return
}

inputControl.controller.triggerRepeatedHapticPulse(500, 1, 1)
}

onAtYBeginningChanged: {
if (atYBeginning) {
stopScrollMomentum()
stopScrollHaptic()
}
}

onAtYEndChanged: {
if (atYEnd) {
stopScrollMomentum()
stopScrollHaptic()
}
}

populate: Transition {
id: _transition

SequentialAnimation {
PropertyAction {
property: "opacity"
value: 0.0
}

PauseAnimation {
duration: 10 * _transition.ViewTransition.index
}

NumberAnimation {
property: "opacity"
from: 0.0
to: 1.0
duration: 200
easing.type: Easing.InOutQuad
}
}
}

add: _transition

delegate: FileDelegate {
width: view.cellWidth
height: view.cellHeight

onClicked: {
root.cdIndex(index)
fs_model.cdIndex(index)
}
}

highlight: Rectangle {
id: fileHighlight

color: Material.primary
visible: root.activeFocus

readonly property bool belowView: {
(-view.contentY + fileHighlight.y) < 0
}

readonly property bool aboveView: {
(-view.contentY + fileHighlight.y) > view.height - view.cellHeight
}

readonly property bool inView: {
!belowView && !aboveView
}

Connections {
target: view

function onMovingChanged() {
if (!view.moving && !fileHighlight.inView) {
const y = fileHighlight.belowView ? view.contentY + view.cellHeight : view.contentY + view.height - view.cellHeight
view.currentIndex = view.indexAt(fileHighlight.x, y)
}
}
Keys.onPressed: {
switch (event.key) {
case Qt.Key_Return:
event.accepted = true
fs_model.cdIndex(view.currentIndex)
break
case Qt.Key_Escape:
event.accepted = true
fs_model.goUp()
break
case Qt.Key_Home:
event.accepted = true
fs_model.goHome()
break
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions resources/qml/GamesView/GamesView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ Item {
id: root

property bool showFooter: true
property var hintActions: ["scroll", "file_manager_open", "file_manager_go_back", "file_manager_go_home"]
property var hintActions: ["scroll"]

GridView {
onActiveFocusChanged: view.forceActiveFocus()

Core.SteamInputScrollableGridView {
id: view
anchors.fill: parent

Expand Down
4 changes: 3 additions & 1 deletion resources/qml/MainWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ ApplicationWindow {
anchors.fill: parent

SteamInputControl {
objectName: "global"
global: true

controller: steam_input.lastController
Expand All @@ -106,6 +105,7 @@ ApplicationWindow {
"debug": debugOverlay.toggle
}
}

Drawer {
id: drawer
y: header.height
Expand All @@ -125,7 +125,9 @@ ApplicationWindow {
id: directoryView

onFileOpened: JS.openFile(path)
visible: stackView.currentItem == directoryView
}

GamesView.GamesView {
id: gamesView
}
Expand Down
35 changes: 35 additions & 0 deletions resources/qml/SteamInputControl.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import QtQuick 2.15

import Steamworks.SteamInput 1.0 as SI

SI.SteamInputControl {
enabled: visible
actionSet: "deckfm"

controller: steam_input.lastController

function scrollHaptic() {
if (!controller) {
return
}

controller.triggerRepeatedHapticPulse(500, 1, 1)
}

function stopScrollMomentum() {
if (!controller) {
return
}

const a = controller.actionSet.actions["scroll"]
controller.stopAnalogActionMomentum(a)
}

function stopScrollHaptic() {
if (!controller) {
return
}

controller.triggerRepeatedHapticPulse(2900, 1200, 2)
}
}
Loading

0 comments on commit 51a5f65

Please sign in to comment.