Fix draggable and window position (#14)

This commit is contained in:
DX 2023-11-26 21:42:16 +03:30 committed by GitHub
parent a7fc110a27
commit bf94546f47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View file

@ -5,6 +5,8 @@ import com.parch 1.0
ApplicationWindow {
width: 900
height: 500
x: (Screen.width - width) / 2 // Center the window horizontally
y: (Screen.height - height) / 2 // Center the window vertically
flags: Qt.FramelessWindowHint
visible: true
id: parch
@ -21,6 +23,40 @@ ApplicationWindow {
parch.close()
}
}
MouseArea {
anchors.fill: parent;
property variant clickPos: "1,1"
onPressed: {
clickPos = Qt.point(mouse.x, mouse.y)
}
onPositionChanged: {
var delta = Qt.point(mouse.x - clickPos.x, mouse.y - clickPos.y)
var new_x = parch.x + delta.x
var new_y = parch.y + delta.y
// Ensure the new position is within the screen boundaries
var screenWidth = Screen.width
var screenHeight = Screen.height
if (new_x < 0) {
new_x = 0
} else if (new_x + parch.width > screenWidth) {
new_x = screenWidth - parch.width
}
if (new_y < 0) {
new_y = 0
} else if (new_y + parch.height > screenHeight) {
new_y = screenHeight - parch.height
}
// Update the window position
parch.x = new_x
parch.y = new_y
}
}
Rectangle {
anchors.centerIn: parent
width: 900

View file

@ -16,15 +16,14 @@ int main(int argc, char *argv[])
// Register the ProcessHandler class with the QML engine
qmlRegisterType<ProcessHandler>("com.parch", 1, 0, "ProcessHandler");
// Load the QML content from the resource
QResource::registerResource(":/resources.rcc");
// Load the QML file
engine.load(QUrl("qrc:/ParchLinux.qml"));
// Set App Icon
// Set App Icon
app.setWindowIcon(QIcon("icon.png"));
return app.exec();
}
}