Skip to content

Commit

Permalink
fix: solve the problem of the dock jumping when the height changes
Browse files Browse the repository at this point in the history
1.makes the change height data smooth without jumps
2.let the mouse close to the window by grabbing mouse

Log: solve the problem of the dock jumping when the height changes
Influence: adjust dock size effect
  • Loading branch information
yixinshark committed Apr 23, 2024
1 parent 0af8eaf commit 18e8b0f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
11 changes: 11 additions & 0 deletions panels/dock/dockpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QQuickWindow>
#include <QLoggingCategory>
#include <QGuiApplication>
#include <QQuickItem>
#include <DGuiApplicationHelper>

#define SETTINGS DockSettings::instance()
Expand Down Expand Up @@ -312,6 +313,16 @@ void DockPanel::loadDockPlugins()
}
}

void DockPanel::setMouseGrabEnabled(QQuickItem *item, bool enabled)
{
if (!item) return;

auto window = item->window();
if (!window) return;

window->setMouseGrabEnabled(enabled);
}

D_APPLET_CLASS(DockPanel)

}
Expand Down
1 change: 1 addition & 0 deletions panels/dock/dockpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class DockPanel : public DS_NAMESPACE::DPanel, public QDBusContext
bool debugMode() const;

Q_INVOKABLE void openDockSettings() const;
Q_INVOKABLE void setMouseGrabEnabled(QQuickItem *item, bool enabled);

private Q_SLOTS:
void onWindowGeometryChanged();
Expand Down
43 changes: 33 additions & 10 deletions panels/dock/package/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,10 @@ Window {

MouseArea {
id: dragArea
property int oldMouseY: 0;
property int oldMouseX: 0;
property point oldMousePos: Qt.point(0, 0)
property int oldDockSize: 0
property list<int> recentDeltas: []
property int averageCount: 6

cursorShape: {
if (Panel.position == Dock.Top || Panel.position == Dock.Bottom) {
Expand All @@ -372,21 +374,41 @@ Window {
}
launcheritem.rootObject.hide()
dock.isDragging = true
oldMouseY = mouse.y
oldMouseX = mouse.x
oldMousePos = Qt.point(mouse.x, mouse.y)
oldDockSize = dockSize
recentDeltas = []
Panel.setMouseGrabEnabled(dragArea, true);
}

onPositionChanged: function(mouse) {
var yChange = mouse.y - oldMouseY
var xChange = mouse.x - oldMouseX
var yChange = mouse.y - oldMousePos.y
var xChange = mouse.x - oldMousePos.x

if (Panel.position === Dock.Bottom || Panel.position === Dock.Top) {
recentDeltas.push(yChange)
} else {
recentDeltas.push(xChange)
}

if (recentDeltas.length > averageCount) {
recentDeltas.shift()
}
// Taking the average makes the data smooth without jumps
var changeAverage = recentDeltas.reduce(function(acc, val) { return acc + val; }, 0) / recentDeltas.length;

var newDockSize = 0
if (Panel.position == Dock.Bottom) {
dockSize = Math.min(Math.max(dockSize - yChange, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
newDockSize = Math.min(Math.max(oldDockSize - changeAverage, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
} else if (Panel.position == Dock.Top) {
dockSize = Math.min(Math.max(dockSize + yChange, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
newDockSize = Math.min(Math.max(oldDockSize + changeAverage, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
} else if (Panel.position == Dock.Left) {
dockSize = Math.min(Math.max(dockSize + xChange, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
newDockSize = Math.min(Math.max(oldDockSize + changeAverage, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
} else {
dockSize = Math.min(Math.max(dockSize - xChange, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
newDockSize = Math.min(Math.max(oldDockSize - changeAverage, Dock.MIN_DOCK_SIZE), Dock.MAX_DOCK_SIZE)
}

if (newDockSize !== dockSize) {
dockSize = newDockSize
}

pressedAndDragging(true)
Expand All @@ -397,6 +419,7 @@ Window {
Applet.dockSize = dockSize
itemIconSizeBase = dockItemMaxSize
pressedAndDragging(false)
Panel.setMouseGrabEnabled(dragArea, false);
}

function anchorToTop() {
Expand Down

0 comments on commit 18e8b0f

Please sign in to comment.