Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Refactor slider code, fix mouse wheel for kf5. #72

Open
wants to merge 5 commits into
base: kf5
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 64 additions & 28 deletions plasmoid/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import org.kde.plasma.extras 2.0 as PlasmaExtras

Item {
id: root
// Do never apply new values if one slider is not released yet
property bool valuesLock: false
CatEricka marked this conversation as resolved.
Show resolved Hide resolved
property bool outsideSysTray: !(plasmoid.containmentDisplayHints & PlasmaCore.Types.ContainmentDrawsPlasmoidHeading)

// https://github.com/Zren/plasma-applet-commandoutput/blob/master/package/contents/ui/main.qml
Expand Down Expand Up @@ -58,13 +56,10 @@ Item {
return;
}

// if the lock is held, simply do nothing and wait for the next refresh
if (!valuesLock) {
monitorModel.clear();
const response = JSON.parse(stdout);
for (let instance of response.value) {
monitorModel.append(instance);
}
monitorModel.clear();
const response = JSON.parse(stdout);
for (let instance of response.value) {
monitorModel.append(instance);
}

commandSuccess(cmd);
Expand Down Expand Up @@ -219,12 +214,37 @@ Item {
from: 0
to: 100
value: brightness
snapMode: PlasmaComponents.Slider.SnapOnRelease
stepSize: plasmoid.configuration.stepSize || 1

onValueChanged: function() {
if (brightness != value) {
CatEricka marked this conversation as resolved.
Show resolved Hide resolved

// Round value in step
brightness = roundNumberInStep(value, stepSize)
CatEricka marked this conversation as resolved.
Show resolved Hide resolved
value = brightness
// Fire command
brightnessChangedDebounceTimer.restart()
}
}

function roundNumberInStep(number, step) {
const halfStep = step / 2

const integerPart = (Math.floor(number / step) * step)
const diffPart = number - integerPart

if (diffPart >= halfStep) {
return integerPart + step
} else {
return integerPart
}
}
CatEricka marked this conversation as resolved.
Show resolved Hide resolved

Timer {
id: mouseWheelScrollingDebounceTimer
id: brightnessChangedDebounceTimer

// How long does it take to trigger when the mouse wheel stops scrolling
// How long does it take to trigger when the mouse wheel stops scrolling or slider stops sliding
interval: 400

// will only be triggered once after restart() called
Expand All @@ -233,29 +253,45 @@ Item {
triggeredOnStart: false

onTriggered: {
valuesLock = false
executable.exec(plasmoid.configuration.executable + ` set-brightness ${bus_id} ${brightness}`)
}
}

onMoved: () => {
// Should also be locked during mouse wheel scrolling.
valuesLock = true
brightness = value
// Backport of https://invent.kde.org/plasma/libplasma/-/commit/aea3d4b131070d8388edf84c9c2a32f7b4203617
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood this correctly, this code doesn't need to be implemented for the KF6 version because it's built into PlasmaComponents.Slider?

Copy link
Contributor Author

@CatEricka CatEricka Mar 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? It depends on when the distro updates KF6 versions.

wheelEnabled: false
MouseArea {
property int wheelDelta: 0

// Handle mouse wheel debounce only when the slider is not pressed.
if (!pressed) {
mouseWheelScrollingDebounceTimer.restart()
anchors {
fill: parent
leftMargin: slider.leftPadding
rightMargin: slider.rightPadding
}
}

onPressedChanged: function() {
if (pressed) {
valuesLock = true
} else {
// Slider is released
valuesLock = false
executable.exec(plasmoid.configuration.executable + ` set-brightness ${bus_id} ${brightness}`)
LayoutMirroring.enabled: false

acceptedButtons: Qt.NoButton

onWheel: wheel => {
const lastValue = slider.value
// We want a positive delta to increase the slider for up/right scrolling,
// independently of the scrolling inversion setting
// The x-axis is also inverted (scrolling right produce negative values)
const delta = (wheel.angleDelta.y || -wheel.angleDelta.x) * (wheel.inverted ? -1 : 1)
wheelDelta += delta;
// magic number 120 for common "one click"
// See: https://doc.qt.io/qt-5/qml-qtquick-wheelevent.html#angleDelta-prop

while (wheelDelta >= 120) {
wheelDelta -= 120;
slider.increase();
}
while (wheelDelta <= -120) {
wheelDelta += 120;
slider.decrease();
}
if (lastValue !== sliders.value) {
slider.moved();
}
}
}
}
Expand Down