Skip to content

Commit

Permalink
fix: icon missing after drag
Browse files Browse the repository at this point in the history
get wininfo one place

Log:
  • Loading branch information
justforlxz committed Apr 11, 2024
1 parent e1c8250 commit 7c5f0ac
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 76 deletions.
40 changes: 17 additions & 23 deletions panels/dock/tray/frame/window/tray/tray_delegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,39 @@ QWidget *TrayDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem
{
Q_UNUSED(option);

TrayIconType type = index.data(TrayModel::TypeRole).value<TrayIconType>();
QString key = index.data(TrayModel::KeyRole).value<QString>();
QString servicePath = index.data(TrayModel::ServiceRole).value<QString>();
quint32 winId = index.data(TrayModel::WinIdRole).value<quint32>();
auto *model = static_cast<TrayModel*>(m_listView->model());

Check warning on line 70 in panels/dock/tray/frame/window/tray/tray_delegate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'model' shadows outer variable

Check warning on line 70 in panels/dock/tray/frame/window/tray/tray_delegate.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'model' is assigned a value that is never used.
const WinInfo info = index.data(TrayModel::InfoRole).value<WinInfo>();;

BaseTrayWidget *trayWidget = nullptr;
if(type == TrayIconType::XEmbed) {
if(info.type == TrayIconType::XEmbed) {
if (Utils::IS_WAYLAND_DISPLAY) {
static Display *display = XOpenDisplay(nullptr);
static int screenp = 0;
static xcb_connection_t *xcb_connection = xcb_connect(qgetenv("DISPLAY"), &screenp);
trayWidget = new XEmbedTrayItemWidget(winId, xcb_connection, display, parent);
trayWidget = new XEmbedTrayItemWidget(info.winId, xcb_connection, display, parent);
} else {
trayWidget = new XEmbedTrayItemWidget(winId, nullptr, nullptr, parent);
trayWidget = new XEmbedTrayItemWidget(info.winId, nullptr, nullptr, parent);
}
const TrayModel *model = qobject_cast<const TrayModel *>(index.model());
if (model)
connect(model, &TrayModel::requestUpdateIcon, trayWidget, &BaseTrayWidget::updateIcon);
} else if (type == TrayIconType::Sni) {
trayWidget = new SNITrayItemWidget(servicePath, parent);
} else if (type == TrayIconType::ExpandIcon) {
} else if (info.type == TrayIconType::Sni) {
trayWidget = new SNITrayItemWidget(info.servicePath, parent);
} else if (info.type == TrayIconType::ExpandIcon) {
ExpandIconWidget *expandWidget = new ExpandIconWidget(parent);
expandWidget->setPositon(m_position);
bool openExpand = index.data(TrayModel::ExpandRole).toBool();
if (openExpand)
if (info.expand)
expandWidget->setTrayPanelVisible(true);

trayWidget = expandWidget;
} else if (type == TrayIconType::Incicator) {
QString indicateName = key;
} else if (info.type == TrayIconType::Incicator) {
QString indicateName = info.key;
int flagIndex = indicateName.indexOf("indicator:");
if (flagIndex >= 0)
indicateName = indicateName.right(indicateName.length() - QString("indicator:").length());
IndicatorTrayItem *indicatorWidget = new IndicatorTrayItem(indicateName, parent);
TrayModel *dataModel = qobject_cast<TrayModel *>(m_listView->model());
if (IndicatorTrayItem *sourceIndicatorWidget = dataModel->indicatorWidget(key)) {
if (IndicatorTrayItem *sourceIndicatorWidget = dataModel->indicatorWidget(info.key)) {
connect(indicatorWidget, &IndicatorTrayItem::clicked, sourceIndicatorWidget, &IndicatorTrayItem::clicked);
connect(sourceIndicatorWidget, &IndicatorTrayItem::textChanged, indicatorWidget, &IndicatorTrayItem::setText);
const QByteArray pixmapData = sourceIndicatorWidget->pixmapData();
Expand All @@ -113,14 +110,11 @@ QWidget *TrayDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem
indicatorWidget->setText(text);
}
trayWidget = indicatorWidget;
} else if (type == TrayIconType::SystemItem) {
PluginsItemInterface *pluginInter = (PluginsItemInterface *)(index.data(TrayModel::PluginInterfaceRole).toULongLong());
if (pluginInter) {
const QString itemKey = QuickSettingController::instance()->itemKey(pluginInter);
SystemPluginItem *trayItem = new SystemPluginItem(pluginInter, itemKey, parent);
connect(trayItem, &SystemPluginItem::execActionFinished, this, &TrayDelegate::requestHide);
trayWidget = trayItem;
}
} else if (info.type == TrayIconType::SystemItem) {
const QString itemKey = QuickSettingController::instance()->itemKey(info.pluginInter);
SystemPluginItem *trayItem = new SystemPluginItem(info.pluginInter, itemKey, parent);
connect(trayItem, &SystemPluginItem::execActionFinished, this, &TrayDelegate::requestHide);
trayWidget = trayItem;
}

if (trayWidget)
Expand Down
53 changes: 24 additions & 29 deletions panels/dock/tray/frame/window/tray/tray_gridview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ TrayGridView *TrayGridView::getDockTrayGridView(QWidget *parent)
{
static TrayGridView *view = nullptr;
if (!view)
view = new TrayGridView(parent);
view = new TrayGridView(TrayGridView::Type::DockTray, parent);
return view;
}

TrayGridView *TrayGridView::getIconTrayGridView(QWidget *parent)
{
static TrayGridView *view = nullptr;
if (!view)
view = new TrayGridView(parent);
view = new TrayGridView(TrayGridView::Type::IconTray, parent);
return view;
}

TrayGridView::TrayGridView(QWidget *parent)
TrayGridView::TrayGridView(TrayGridView::Type type, QWidget *parent)
: DListView(parent)
, m_type(type)
, m_aniCurveType(QEasingCurve::Linear)
, m_aniDuringTime(250)
, m_dragDistance(15)
Expand Down Expand Up @@ -297,7 +298,7 @@ void TrayGridView::mouseMoveEvent(QMouseEvent *e)
return DListView::mouseMoveEvent(e);

// 如果当前拖动的位置是托盘展开按钮,则不让其拖动
TrayIconType iconType = index.data(TrayModel::Role::TypeRole).value<TrayIconType>();
TrayIconType iconType = index.data(TrayModel::Role::InfoRole).value<WinInfo>().type;
if (iconType == TrayIconType::ExpandIcon)
return DListView::mouseMoveEvent(e);

Expand Down Expand Up @@ -443,30 +444,24 @@ void TrayGridView::handleDropEvent(QDropEvent *e)
e->setDropAction(Qt::CopyAction);
e->accept();

TrayModel *dataModel = qobject_cast<TrayModel *>(model());
if (dataModel) {
WinInfo info;
info.type = static_cast<TrayIconType>(e->mimeData()->data("type").toInt());
info.key = static_cast<QString>(e->mimeData()->data("key"));
info.winId = static_cast<quint32>(e->mimeData()->data("winId").toInt());
info.servicePath = static_cast<QString>(e->mimeData()->data("servicePath"));
info.itemKey = static_cast<QString>(e->mimeData()->data("itemKey"));
info.isTypeWriting = (static_cast<QString>(e->mimeData()->data("isTypeWritting")) == "1");
info.expand = (static_cast<QString>(e->mimeData()->data("expand")) == "1");
info.pluginInter = (PluginsItemInterface *)(e->mimeData()->imageData().value<qulonglong>());
QModelIndex targetIndex = getIndexFromPos(e->pos());
int index = -1;
if (targetIndex.isValid() && targetIndex.row() < dataModel->rowCount()) {
// 如果拖动的位置是合法的位置,则让其插入到当前的位置
index = targetIndex.row();
dataModel->insertRow(index, info);
} else {
// 在其他的情况下,让其插入到最后
dataModel->addRow(info);
}

dataModel->saveConfig(index, info);
TrayModel *dockModel = TrayModel::getDockModel();
TrayModel *iconModel = TrayModel::getIconModel();
TrayModel *model = static_cast<TrayModel*>(QListView::model());
const auto itemKey = static_cast<QString>(e->mimeData()->data("itemKey"));
WinInfo info = m_type == Type::DockTray ? iconModel->getWinInfo(itemKey) : dockModel->getWinInfo(itemKey);

QModelIndex targetIndex = getIndexFromPos(e->pos());
int index = -1;
if (targetIndex.isValid() && targetIndex.row() < model->rowCount()) {
// 如果拖动的位置是合法的位置,则让其插入到当前的位置
index = targetIndex.row();
model->insertRow(index, info);
} else {
// 在其他的情况下,让其插入到最后
model->addRow(info);
}

model->saveConfig(index, info);
} else {
e->ignore();
DListView::dropEvent(e);
Expand Down Expand Up @@ -503,7 +498,7 @@ void TrayGridView::onUpdateEditorView()
bool TrayGridView::beginDrag(Qt::DropActions supportedActions)
{
QModelIndex modelIndex = indexAt(m_dragPos);
TrayIconType trayType = modelIndex.data(TrayModel::Role::TypeRole).value<TrayIconType>();
TrayIconType trayType = modelIndex.data(TrayModel::Role::InfoRole).value<WinInfo>().type;
// 展开图标不能移动
if (trayType == TrayIconType::ExpandIcon)
return false;
Expand Down Expand Up @@ -541,7 +536,7 @@ bool TrayGridView::beginDrag(Qt::DropActions supportedActions)

setState(DraggingState);

listModel->setDragKey(modelIndex.data(TrayModel::Role::KeyRole).toString());
listModel->setDragKey(modelIndex.data(TrayModel::Role::InfoRole).value<WinInfo>().key);
listModel->setDragingIndex(modelIndex);
// 删除当前的图标
WinInfo winInfo = listModel->getWinInfo(modelIndex);
Expand Down
8 changes: 7 additions & 1 deletion panels/dock/tray/frame/window/tray/tray_gridview.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class TrayGridView : public DListView
Q_OBJECT

public:
enum class Type {
DockTray,
IconTray,
};

static TrayGridView *getDockTrayGridView(QWidget *parent = Q_NULLPTR);
static TrayGridView *getIconTrayGridView(QWidget *parent = Q_NULLPTR);

Expand Down Expand Up @@ -64,14 +69,15 @@ private Q_SLOTS:
bool beginDrag(Qt::DropActions supportedActions);

private:
explicit TrayGridView(QWidget *parent = Q_NULLPTR);
explicit TrayGridView(Type type, QWidget *parent = Q_NULLPTR);

void initUi();
void createAnimation(const int pos, const bool moveNext, const bool isLastAni);
const QModelIndex getIndexFromPos(QPoint currentPoint) const;
bool mouseInDock();

private:
Type m_type;
QEasingCurve::Type m_aniCurveType;
int m_aniDuringTime;

Expand Down
35 changes: 19 additions & 16 deletions panels/dock/tray/frame/window/tray/tray_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void TrayModel::setDragKey(const QString &key)

bool TrayModel::indexDragging(const QModelIndex &index) const
{
if (index.isValid() && index.data(Role::KeyRole).toString() == m_dragKey)
if (index.isValid() && index.data(Role::InfoRole).value<WinInfo>().key == m_dragKey)
return true;

if (!m_dragModelIndex.isValid() || !m_dropModelIndex.isValid())
Expand Down Expand Up @@ -236,20 +236,8 @@ QVariant TrayModel::data(const QModelIndex &index, int role) const
const WinInfo &info = m_winInfos[itemIndex];

switch (role) {
case Role::TypeRole:
return info.type;
case Role::KeyRole:
return info.key;
case Role::WinIdRole:
return info.winId;
case Role::ServiceRole:
return info.servicePath;
case Role::PluginInterfaceRole:
return (qulonglong)(info.pluginInter);
case Role::ExpandRole:
return info.expand;
case Role::ItemKeyRole:
return info.itemKey;
case Role::InfoRole:
return QVariant::fromValue(info);
case Role::Blank:
return indexDragging(index);
default:
Expand Down Expand Up @@ -279,7 +267,7 @@ bool TrayModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, in
Q_UNUSED(row)
Q_UNUSED(column)

TrayIconType iconType = parent.data(TrayModel::Role::TypeRole).value<TrayIconType>();
TrayIconType iconType = parent.data(TrayModel::Role::InfoRole).value<WinInfo>().type;
if (iconType == TrayIconType::ExpandIcon)
return false;

Expand Down Expand Up @@ -343,6 +331,19 @@ WinInfo TrayModel::getWinInfo(const QModelIndex &index)
return m_winInfos[row];
}

WinInfo TrayModel::getWinInfo(const QString &itemKey)
{
auto find = std::find_if(m_winInfos.begin(), m_winInfos.end(), [itemKey](const WinInfo &info) {
return info.itemKey == itemKey;
});

if (find != m_winInfos.end()) {
return *find;
}

Q_UNREACHABLE();
}

void TrayModel::onXEmbedTrayAdded(quint32 winId)
{
if (!xembedCanExport(winId))
Expand Down Expand Up @@ -633,6 +634,7 @@ void TrayModel::onIndicatorAdded(const QString &indicatorName)

beginInsertRows(QModelIndex(), rowCount(), rowCount());
WinInfo info;
info.key = "indicator:" + itemKey;

Check warning on line 637 in panels/dock/tray/frame/window/tray/tray_model.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Variable 'info.key' is reassigned a value before the old one has been used.
info.type = Incicator;
info.key = itemKey;
info.itemKey = itemKey;
Expand Down Expand Up @@ -663,6 +665,7 @@ void TrayModel::onSystemTrayAdded(PluginsItemInterface *itemInter)
beginInsertRows(QModelIndex(), rowCount(), rowCount());

WinInfo info;
info.key = "systemTray:" + itemInter->pluginName();
info.type = SystemItem;
info.pluginInter = itemInter;
info.itemKey = systemItemKey(itemInter->pluginName());
Expand Down
9 changes: 2 additions & 7 deletions panels/dock/tray/frame/window/tray/tray_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ class TrayModel : public QAbstractListModel

public:
enum Role {
TypeRole = Qt::UserRole + 1,
KeyRole,
WinIdRole,
ServiceRole,
PluginInterfaceRole,
ExpandRole,
ItemKeyRole,
InfoRole = Qt::UserRole + 1,
Blank
};

Expand Down Expand Up @@ -103,6 +97,7 @@ class TrayModel : public QAbstractListModel

void clear();
WinInfo getWinInfo(const QModelIndex &index);
WinInfo getWinInfo(const QString &itemKey);
void saveConfig(int index, const WinInfo &winInfo);
void removeWinInfo(WinInfo winInfo);

Expand Down

0 comments on commit 7c5f0ac

Please sign in to comment.