添加小屏开始菜单的拖拽功能

This commit is contained in:
youdiansaodongxi 2023-05-04 15:09:52 +08:00 committed by He Sir
parent 6d9742c5f7
commit 34f33a8dfc
9 changed files with 139 additions and 53 deletions

View File

@ -5,6 +5,7 @@ import org.ukui.menu.core 1.0
MouseArea { MouseArea {
id: control id: control
property bool isSelect: false
hoverEnabled: true hoverEnabled: true
states: State { states: State {
when: control.activeFocus when: control.activeFocus
@ -18,7 +19,7 @@ MouseArea {
anchors.fill: parent anchors.fill: parent
radius: 4 radius: 4
useStyleTransparent: false useStyleTransparent: false
alpha: control.containsPress ? 0.82 : control.containsMouse ? 0.55 : 0.00 alpha: isSelect ? 1.00 : control.containsPress ? 0.82 : control.containsMouse ? 0.55 : 0.00
ToolTip.visible: content.textTruncated && control.containsMouse ToolTip.visible: content.textTruncated && control.containsMouse
ToolTip.text: name ToolTip.text: name

View File

@ -8,6 +8,7 @@ MouseArea {
id: control id: control
property bool editStatus: false property bool editStatus: false
property bool truncate: false property bool truncate: false
property bool isSelect: false
hoverEnabled: true hoverEnabled: true
ToolTip.visible: editStatus && truncate && control.containsMouse ToolTip.visible: editStatus && truncate && control.containsMouse
ToolTip.text: name ToolTip.text: name
@ -23,7 +24,7 @@ MouseArea {
anchors.fill: parent anchors.fill: parent
radius: 4 radius: 4
useStyleTransparent: false useStyleTransparent: false
alpha: control.containsPress ? 0.82 : control.containsMouse ? 0.55 : 0.00 alpha: isSelect ? 0.55 : control.containsPress ? 0.82 : control.containsMouse ? 0.55 : 0.00
RowLayout { RowLayout {
anchors.fill: parent anchors.fill: parent

View File

@ -16,7 +16,7 @@
* *
*/ */
import QtQuick 2.0 import QtQuick 2.15
import QtQml 2.12 import QtQml 2.12
import QtQuick.Controls 2.5 import QtQuick.Controls 2.5
import QtQuick.Layouts 1.12 import QtQuick.Layouts 1.12
@ -25,6 +25,7 @@ import org.ukui.menu.core 1.0
Item { Item {
property string title: "" property string title: ""
property string idSelect: ""
signal labelItemClicked() signal labelItemClicked()
signal openFolderPageSignal(string folderId, string folderName); signal openFolderPageSignal(string folderId, string folderName);
@ -43,9 +44,10 @@ Item {
model: modelManager.getAppModel() model: modelManager.getAppModel()
delegate: Component { delegate: Component {
Loader { Loader {
width: ListView.view ? ListView.view.width : 0 id: loaderView
height: 40
focus: true focus: true
width: ListView.view ? ListView.view.width : 0
height: appListView.itemHeight
property int index: model.index property int index: model.index
property int type: model.type property int type: model.type
property string id: model.id property string id: model.id
@ -55,13 +57,13 @@ Item {
property bool recentInstall: model.recentInstall property bool recentInstall: model.recentInstall
sourceComponent: { sourceComponent: {
if (type === DataType.Normal) { if (type === DataType.Normal) {
return appItem; return appItemComponent;
} }
if (type === DataType.Folder) { if (type === DataType.Folder) {
return folderItem; return folderItemComponent;
} }
if (type === DataType.Label) { if (type === DataType.Label) {
return labelItem; return labelItemComponent;
} }
} }
} }
@ -69,10 +71,18 @@ Item {
} }
Component { Component {
id: appItem id: appItemComponent
Item {
id: appItemBase
AppControls2.AppItem { AppControls2.AppItem {
id: appItem
focus: true focus: true
width: appListView.view ? appListView.view.width : 0
height: appListView.itemHeight
acceptedButtons: Qt.LeftButton | Qt.RightButton acceptedButtons: Qt.LeftButton | Qt.RightButton
Drag.active: drag.active
Drag.hotSpot.x: width / 2
Drag.hotSpot.y: height / 2
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
appListView.model.openMenu(index); appListView.model.openMenu(index);
@ -86,6 +96,25 @@ Item {
return; return;
} }
} }
onPressAndHold: {
if (mouse.button === Qt.LeftButton) {
x = appItem.mapToItem(appListView,0,0).x;
y = appItem.mapToItem(appListView,0,0).y;
drag.target = appItem;
appItem.parent = appListView;
appItem.isSelect = true;
}
}
onReleased: {
idSelect = id;
Drag.drop();
drag.target = null;
appItem.parent = appItemBase;
x = 0;
y = 0;
appItem.isSelect = false;
}
}
Keys.onPressed: { Keys.onPressed: {
if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return) { if (event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
appManager.launchApp(id); appManager.launchApp(id);
@ -98,17 +127,32 @@ Item {
} }
Component { Component {
id: labelItem id: labelItemComponent
AppControls2.LabelItem { AppControls2.LabelItem {
focus: true focus: true;
onClicked: labelItemClicked(); onClicked: labelItemClicked();
} }
} }
Component { Component {
id: folderItem id: folderItemComponent
DropArea {
onEntered: {
folderItem.isSelect = true;
}
onExited: {
folderItem.isSelect = false;
}
onDropped: {
folderItem.isSelect = false;
if (id !== idSelect) {
appListView.model.addAppToFolder(idSelect, id);
}
}
AppControls2.FolderItem { AppControls2.FolderItem {
id: folderItem
focus: true focus: true
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
@ -140,3 +184,4 @@ Item {
} }
} }
} }
}

View File

@ -29,6 +29,7 @@ MouseArea {
property alias model: listView.model property alias model: listView.model
property alias delegate: listView.delegate property alias delegate: listView.delegate
property alias listFocus: listView.focus property alias listFocus: listView.focus
property int itemHeight: 40
hoverEnabled: true hoverEnabled: true
clip: true clip: true
onContainsMouseChanged: listView.focus = false onContainsMouseChanged: listView.focus = false
@ -49,6 +50,7 @@ MouseArea {
ListView { ListView {
id: listView id: listView
cacheBuffer: itemHeight * listView.count
spacing: 4 spacing: 4
Layout.fillHeight: true Layout.fillHeight: true
Layout.fillWidth: true Layout.fillWidth: true

View File

@ -49,7 +49,6 @@ UkuiMenuExtension {
anchors.leftMargin: 16 anchors.leftMargin: 16
anchors.topMargin: 12 anchors.topMargin: 12
anchors.bottomMargin: 6 anchors.bottomMargin: 6
clip: true
cellWidth: itemHeight + spacing; cellHeight: cellWidth cellWidth: itemHeight + spacing; cellHeight: cellWidth
property int exchangedStartIndex: 0 property int exchangedStartIndex: 0
property int spacing: 4 property int spacing: 4

View File

@ -211,6 +211,30 @@ void AppFolderHelper::addAppToNewFolder(const QString &appId, const QString &fol
Q_EMIT folderAdded(folder.id); Q_EMIT folderAdded(folder.id);
} }
void AppFolderHelper::addAppsToNewFolder(const QString &appIdA, const QString &appIdB, const QString &folderName)
{
if (appIdA.isEmpty() || appIdB.isEmpty()) {
return;
}
// TODO: max越界处理
int max = m_folders.isEmpty() ? -1 : m_folders.lastKey();
QString name = folderName;
if (name.isEmpty()) {
name = tr("New Folder %1").arg(m_folders.size() + 1);
}
Folder folder;
folder.id = ++max;
folder.name = name;
folder.apps.append(appIdA);
folder.apps.append(appIdB);
insertFolder(folder);
Q_EMIT folderAdded(folder.id);
forceSync();
}
void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folderId) void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folderId)
{ {
if (appId.isEmpty()) { if (appId.isEmpty()) {

View File

@ -74,6 +74,7 @@ public:
void addAppToFolder(const QString& appId, const int& folderId); void addAppToFolder(const QString& appId, const int& folderId);
void addAppToNewFolder(const QString& appId, const QString& folderName); void addAppToNewFolder(const QString& appId, const QString& folderName);
void addAppsToNewFolder(const QString& appIdA, const QString& appIdB, const QString& folderName);
void removeAppFromFolder(const QString& appId, const int& folderId); void removeAppFromFolder(const QString& appId, const int& folderId);
bool deleteFolder(const int& folderId); bool deleteFolder(const int& folderId);
void renameFolder(const int& folderId, const QString& folderName); void renameFolder(const int& folderId, const QString& folderName);

View File

@ -189,10 +189,11 @@ void AppModel::insertData(QVector<DataEntity> &data, int index)
void AppModel::openMenu(const int &index) void AppModel::openMenu(const int &index)
{ {
QModelIndex modelIndex = createIndex(index, 0, nullptr); if (index < 0 || index >= m_apps.size()) {
if (modelIndex.isValid()) { return;
MenuManager::instance()->showMenu(modelIndex.data(DataEntity::Id).toString());
} }
MenuManager::instance()->showMenu(m_apps.at(index));
} }
void AppModel::renameFolder(const QString &index, const QString &folderName) void AppModel::renameFolder(const QString &index, const QString &folderName)
@ -200,6 +201,16 @@ void AppModel::renameFolder(const QString &index, const QString &folderName)
AppFolderHelper::instance()->renameFolder(index.toInt(), folderName); AppFolderHelper::instance()->renameFolder(index.toInt(), folderName);
} }
void AppModel::addAppsToFolder(const QString &appIdA, const QString &appIdB, const QString &folderName)
{
AppFolderHelper::instance()->addAppsToNewFolder(appIdA, appIdB, folderName);
}
void AppModel::addAppToFolder(const QString &appId, const QString &folderId)
{
AppFolderHelper::instance()->addAppToFolder(appId, folderId.toInt());
}
QVariantList AppModel::getApps(int start, int end) QVariantList AppModel::getApps(int start, int end)
{ {
if (start < 0 || start >= m_apps.size() || end < 0 || end > m_apps.size()) { if (start < 0 || start >= m_apps.size() || end < 0 || end > m_apps.size()) {

View File

@ -43,6 +43,8 @@ public:
Q_INVOKABLE void appClicked(const int &index); Q_INVOKABLE void appClicked(const int &index);
Q_INVOKABLE void openMenu(const int &index); Q_INVOKABLE void openMenu(const int &index);
Q_INVOKABLE void renameFolder(const QString &index, const QString &folderName); Q_INVOKABLE void renameFolder(const QString &index, const QString &folderName);
Q_INVOKABLE void addAppsToFolder(const QString &appIdA, const QString &appIdB, const QString &folderName);
Q_INVOKABLE void addAppToFolder(const QString &appId, const QString &folderId);
public Q_SLOTS: public Q_SLOTS:
void toRenameFolder(QString id); void toRenameFolder(QString id);