Skip to content

Commit

Permalink
fix: solve the problem of the dock jumping when the height changes (#246
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 authored Apr 23, 2024
1 parent 8eac3d8 commit a00f1c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 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 @@ -313,6 +314,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
51 changes: 37 additions & 14 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: 5

cursorShape: {
if (Panel.position == Dock.Top || Panel.position == Dock.Bottom) {
Expand All @@ -366,27 +368,47 @@ Window {
}

onPressed: function(mouse) {
var launcheritem = DS.applet("org.deepin.ds.launchpad")
if (!launcheritem) {
return
var launcherItem = DS.applet("org.deepin.ds.launchpad")
if (launcherItem) {
launcherItem.rootObject.hide()
}
launcheritem.rootObject.hide()
dock.isDragging = true
oldMouseY = mouse.y
oldMouseX = mouse.x
oldMousePos = mapToGlobal(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 newPos = mapToGlobal(mouse.x, mouse.y)
var xChange = newPos.x - oldMousePos.x
var yChange = newPos.y - oldMousePos.y

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 a00f1c9

Please sign in to comment.