Skip to content

Commit

Permalink
feat: save sort order to config and optimize sort algorithm
Browse files Browse the repository at this point in the history
Change-Id: I00f51956127a825c36b5308f4713314e987eb186
  • Loading branch information
listenerri committed Nov 27, 2018
1 parent 72a3d2d commit 805b5b5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
66 changes: 61 additions & 5 deletions plugins/tray/fashiontrayitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -590,7 +639,11 @@ void FashionTrayItem::onItemDragStop()

if (m_currentDraggingTray == wrapper) {
m_currentDraggingTray = nullptr;
} else {
Q_UNREACHABLE();
}

saveCurrentOrderToConfig();
}

void FashionTrayItem::onItemRequestSwapWithDragging()
Expand All @@ -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));
}
7 changes: 5 additions & 2 deletions plugins/tray/fashiontrayitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions plugins/tray/trayplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions plugins/tray/trayplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 805b5b5

Please sign in to comment.