diff --git a/plugins/tray/fashiontrayitem.cpp b/plugins/tray/fashiontrayitem.cpp index 3c345d5c2..8ebd7eeff 100644 --- a/plugins/tray/fashiontrayitem.cpp +++ b/plugins/tray/fashiontrayitem.cpp @@ -323,14 +323,56 @@ QSize FashionTrayItem::wantedTotalSize() const } int FashionTrayItem::whereToInsert(FashionTrayWidgetWrapper *wrapper) const +{ + // 如果已经对图标进行过排序则完全按照从配置文件中获取的顺序来插入图标 + if (m_trayPlugin->traysSortedInFashionMode()) { + return whereToInsertBySortKey(wrapper); + } + + // 如果没有对图标进行过排序则使用下面的默认排序算法: + // 所有应用图标在系统图标的左侧 + // 新的应用图标在最左侧的应用图标处插入 + // 新的系统图标在最左侧的系统图标处插入 + return whereToInsertByDefault(wrapper); +} + +int FashionTrayItem::whereToInsertBySortKey(FashionTrayWidgetWrapper *wrapper) const +{ + if (m_wrapperList.isEmpty()) { + return 0; + } + + const int destSortKey = m_trayPlugin->itemSortKey(wrapper->itemKey()); + + if (destSortKey < -1) { + return 0; + } + if (destSortKey == -1) { + return m_wrapperList.size(); + } + + // 当目标插入位置为列表的大小时将从最后面追加到列表中 + int destIndex = m_wrapperList.size(); + for (int i = 0; i < m_wrapperList.size(); ++i) { + if (destSortKey > m_trayPlugin->itemSortKey(m_wrapperList.at(i)->itemKey())) { + continue; + } + destIndex = i; + break; + } + + return destIndex; +} + +int FashionTrayItem::whereToInsertByDefault(FashionTrayWidgetWrapper *wrapper) const { int index = 0; switch (wrapper->absTrayWidget()->trayTyep()) { case AbstractTrayWidget::TrayType::ApplicationTray: - index = whereToInsertAppTray(wrapper); + index = whereToInsertAppTrayByDefault(wrapper); break; case AbstractTrayWidget::TrayType::SystemTray: - index = whereToInsertSystemTray(wrapper); + index = whereToInsertSystemTrayByDefault(wrapper); break; default: Q_UNREACHABLE(); @@ -339,7 +381,7 @@ int FashionTrayItem::whereToInsert(FashionTrayWidgetWrapper *wrapper) const return index; } -int FashionTrayItem::whereToInsertAppTray(FashionTrayWidgetWrapper *wrapper) const +int FashionTrayItem::whereToInsertAppTrayByDefault(FashionTrayWidgetWrapper *wrapper) const { if (m_wrapperList.isEmpty() || wrapper->absTrayWidget()->trayTyep() != AbstractTrayWidget::TrayType::ApplicationTray) { return 0; @@ -385,7 +427,7 @@ int FashionTrayItem::whereToInsertAppTray(FashionTrayWidgetWrapper *wrapper) con return insertIndex; } -int FashionTrayItem::whereToInsertSystemTray(FashionTrayWidgetWrapper *wrapper) const +int FashionTrayItem::whereToInsertSystemTrayByDefault(FashionTrayWidgetWrapper *wrapper) const { if (m_wrapperList.isEmpty()) { return 0; @@ -429,6 +471,13 @@ int FashionTrayItem::whereToInsertSystemTray(FashionTrayWidgetWrapper *wrapper) return insertIndex; } +void FashionTrayItem::saveCurrentOrderToConfig() +{ + for (int i = 0; i < m_wrapperList.size(); ++i) { + m_trayPlugin->setSortKey(m_wrapperList.at(i)->itemKey(), i + 1); + } +} + void FashionTrayItem::onTrayAttentionChanged(const bool attention) { // 设置attention为false之后,启动timer,在timer处于Active状态期间不重设attention为true @@ -590,7 +639,11 @@ void FashionTrayItem::onItemDragStop() if (m_currentDraggingTray == wrapper) { m_currentDraggingTray = nullptr; + } else { + Q_UNREACHABLE(); } + + saveCurrentOrderToConfig(); } void FashionTrayItem::onItemRequestSwapWithDragging() @@ -601,8 +654,11 @@ void FashionTrayItem::onItemRequestSwapWithDragging() return; } - int indexOfDest = m_trayBoxLayout->indexOf(wrapper); + const int indexOfDest = m_trayBoxLayout->indexOf(wrapper); + const int indexOfDragging = m_trayBoxLayout->indexOf(m_currentDraggingTray); m_trayBoxLayout->removeWidget(m_currentDraggingTray); m_trayBoxLayout->insertWidget(indexOfDest, m_currentDraggingTray); + + m_wrapperList.insert(indexOfDest, m_wrapperList.takeAt(indexOfDragging)); } diff --git a/plugins/tray/fashiontrayitem.h b/plugins/tray/fashiontrayitem.h index 32966b82b..4f65ce824 100644 --- a/plugins/tray/fashiontrayitem.h +++ b/plugins/tray/fashiontrayitem.h @@ -62,8 +62,11 @@ public slots: QSize sizeHint() const Q_DECL_OVERRIDE; QSize wantedTotalSize() const; int whereToInsert(FashionTrayWidgetWrapper *wrapper) const; - int whereToInsertAppTray(FashionTrayWidgetWrapper *wrapper) const; - int whereToInsertSystemTray(FashionTrayWidgetWrapper *wrapper) const; + int whereToInsertBySortKey(FashionTrayWidgetWrapper *wrapper) const; + int whereToInsertByDefault(FashionTrayWidgetWrapper *wrapper) const; + int whereToInsertAppTrayByDefault(FashionTrayWidgetWrapper *wrapper) const; + int whereToInsertSystemTrayByDefault(FashionTrayWidgetWrapper *wrapper) const; + void saveCurrentOrderToConfig(); private Q_SLOTS: void onTrayAttentionChanged(const bool attention); diff --git a/plugins/tray/trayplugin.cpp b/plugins/tray/trayplugin.cpp index a3315df1d..5cc7dd77f 100644 --- a/plugins/tray/trayplugin.cpp +++ b/plugins/tray/trayplugin.cpp @@ -33,6 +33,7 @@ #include "xcb/xcb_icccm.h" #define FASHION_MODE_ITEM "fashion-mode-item" +#define FASHION_MODE_TRAYS_SORTED "fashion-mode-trays-sorted" TrayPlugin::TrayPlugin(QObject *parent) : QObject(parent), @@ -172,6 +173,10 @@ int TrayPlugin::itemSortKey(const QString &itemKey) void TrayPlugin::setSortKey(const QString &itemKey, const int order) { + if (displayMode() == Dock::DisplayMode::Fashion && !traysSortedInFashionMode()) { + m_proxyInter->saveValue(this, FASHION_MODE_TRAYS_SORTED, true); + } + // 如果是系统托盘图标则调用内部插件的相应接口 if (isSystemTrayItem(itemKey)) { return m_systemTraysController->setSystemTrayItemSortKey(itemKey, order); @@ -193,6 +198,11 @@ Dock::Position TrayPlugin::dockPosition() const return position(); } +bool TrayPlugin::traysSortedInFashionMode() +{ + return m_proxyInter->getValue(this, FASHION_MODE_TRAYS_SORTED, false).toBool(); +} + const QString TrayPlugin::getWindowClass(quint32 winId) { auto *connection = QX11Info::connection(); diff --git a/plugins/tray/trayplugin.h b/plugins/tray/trayplugin.h index 31ea3283b..9ff68ece5 100644 --- a/plugins/tray/trayplugin.h +++ b/plugins/tray/trayplugin.h @@ -61,6 +61,7 @@ class TrayPlugin : public QObject, PluginsItemInterface void setItemIsInContainer(const QString &itemKey, const bool container) Q_DECL_OVERRIDE; Dock::Position dockPosition() const; + bool traysSortedInFashionMode(); private: void loadIndicator();