Skip to content

Commit

Permalink
Simplify inserting/removing tray menu actions
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Nov 17, 2021
1 parent 0a64dfe commit 386e5d0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 40 deletions.
14 changes: 6 additions & 8 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,15 +941,12 @@ void MainWindow::updateTrayMenuCommands()
QAction *clipboardAction = m_trayMenu->addAction( iconClipboard(), clipboardLabel );
connect(clipboardAction, &QAction::triggered,
this, &MainWindow::showClipboardContent);
m_trayMenu->addCustomAction(clipboardAction);

m_trayMenu->markItemInClipboard(m_clipboardData);

int i = m_trayMenu->actions().size();
addCommandsToTrayMenu(m_clipboardData);
QList<QAction *> actions = m_trayMenu->actions();
for ( ; i < actions.size(); ++i )
m_trayMenu->addCustomAction(actions[i]);
QList<QAction*> customActions;
customActions.append(clipboardAction);
addCommandsToTrayMenu(m_clipboardData, &customActions);
m_trayMenu->setCustomActions(customActions);
}

void MainWindow::updateIcon()
Expand Down Expand Up @@ -1539,7 +1536,7 @@ void MainWindow::addCommandsToItemMenu(ClipboardBrowser *c)
runMenuCommandFilters(&m_itemMenuMatchCommands, data);
}

void MainWindow::addCommandsToTrayMenu(const QVariantMap &clipboardData)
void MainWindow::addCommandsToTrayMenu(const QVariantMap &clipboardData, QList<QAction*> *actions)
{
if ( m_trayMenuCommands.isEmpty() ) {
interruptMenuCommandFilters(&m_trayMenuMatchCommands);
Expand All @@ -1561,6 +1558,7 @@ void MainWindow::addCommandsToTrayMenu(const QVariantMap &clipboardData)
QString name = command.name;
QMenu *currentMenu = createSubMenus(&name, m_trayMenu);
auto act = new CommandAction(command, name, currentMenu);
actions->append(act);

addMenuMatchCommand(&m_trayMenuMatchCommands, command.matchCmd, act);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ class MainWindow final : public QMainWindow

QVector<Command> commandsForMenu(const QVariantMap &data, const QString &tabName, const QVector<Command> &allCommands);
void addCommandsToItemMenu(ClipboardBrowser *c);
void addCommandsToTrayMenu(const QVariantMap &clipboardData);
void addCommandsToTrayMenu(const QVariantMap &clipboardData, QList<QAction*> *actions);
void addMenuMatchCommand(MenuMatchCommands *menuMatchCommands, const QString &matchCommand, QAction *act);
void runMenuCommandFilters(MenuMatchCommands *menuMatchCommands, QVariantMap &data);
void interruptMenuCommandFilters(MenuMatchCommands *menuMatchCommands);
Expand Down
58 changes: 31 additions & 27 deletions src/gui/traymenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@

namespace {

const char propertyCustomAction[] = "CopyQ_tray_menu_custom_action";
const char propertyClipboardItemAction[] = "CopyQ_tray_menu_clipboard_item";

const QIcon iconClipboard() { return getIcon("clipboard", IconPaste); }

bool canActivate(const QAction &action)
Expand Down Expand Up @@ -81,7 +78,7 @@ TrayMenu::TrayMenu(QWidget *parent)
{
m_clipboardItemActionsSeparator = addSeparator();
m_customActionsSeparator = addSeparator();
initSingleShotTimer( &m_timerUpdateActiveAction, 0, this, &TrayMenu::updateActiveAction );
initSingleShotTimer( &m_timerUpdateActiveAction, 0, this, &TrayMenu::doUpdateActiveAction );
setAttribute(Qt::WA_InputMethodEnabled);
}

Expand All @@ -92,7 +89,7 @@ void TrayMenu::addClipboardItemAction(const QVariantMap &data, bool showImages)
setSearchMenuItem( m_viMode ? tr("Press '/' to search") : tr("Type to search") );

QAction *act = addAction(QString());
act->setProperty(propertyClipboardItemAction, true);
m_clipboardActions.append(act);

act->setData(data);

Expand Down Expand Up @@ -147,13 +144,16 @@ void TrayMenu::addClipboardItemAction(const QVariantMap &data, bool showImages)
}

connect(act, &QAction::triggered, this, &TrayMenu::onClipboardItemActionTriggered);

updateActiveAction();
}

void TrayMenu::clearClipboardItems()
{
clearActionsWithProperty(propertyClipboardItemAction);
const auto actions = m_clipboardActions;
m_clipboardActions = {};
for (QAction *action : actions) {
removeAction(action);
delete action;
}

m_clipboardItemActionCount = 0;

Expand All @@ -164,18 +164,25 @@ void TrayMenu::clearClipboardItems()

void TrayMenu::clearCustomActions()
{
clearActionsWithProperty(propertyCustomAction);
const auto actions = m_customActions;
m_customActions = {};
for (QAction *action : actions) {
removeAction(action);
delete action;
}
}

void TrayMenu::addCustomAction(QAction *action)
void TrayMenu::setCustomActions(QList<QAction*> actions)
{
action->setProperty(propertyCustomAction, true);
insertAction(m_customActionsSeparator, action);
updateActiveAction();
clearCustomActions();
m_customActions = actions;
insertActions(m_customActionsSeparator, actions);
}

void TrayMenu::clearAllActions()
{
m_clipboardActions = {};
m_customActions = {};
clear();
m_clipboardItemActionCount = 0;
m_searchText.clear();
Expand Down Expand Up @@ -273,6 +280,9 @@ void TrayMenu::showEvent(QShowEvent *event)
if ( !m_searchAction.isNull() )
m_searchAction->setVisible(true);

if ( m_timerUpdateActiveAction.isActive() )
doUpdateActiveAction();

QMenu::showEvent(event);
}

Expand All @@ -286,8 +296,8 @@ void TrayMenu::hideEvent(QHideEvent *event)

void TrayMenu::actionEvent(QActionEvent *event)
{
delayedUpdateActiveAction();
QMenu::actionEvent(event);
m_timerUpdateActiveAction.start();
}

void TrayMenu::leaveEvent(QEvent *event)
Expand All @@ -305,16 +315,6 @@ void TrayMenu::inputMethodEvent(QInputMethodEvent *event)
event->ignore();
}

void TrayMenu::clearActionsWithProperty(const char *property)
{
for ( auto action : actions() ) {
if ( action->property(property).toBool() ) {
removeAction(action);
delete action;
}
}
}

void TrayMenu::search(const QString &text)
{
if (m_searchText == text)
Expand Down Expand Up @@ -364,11 +364,15 @@ void TrayMenu::onClipboardItemActionTriggered()
close();
}

void TrayMenu::updateActiveAction()
void TrayMenu::delayedUpdateActiveAction()
{
if ( isVisible() && activeAction() != nullptr )
return;
if ( !isVisible() || activeAction() == nullptr )
m_timerUpdateActiveAction.start();
}

void TrayMenu::doUpdateActiveAction()
{
m_timerUpdateActiveAction.stop();
const auto action = firstEnabledAction(this);
if (action != nullptr)
setActiveAction(action);
Expand Down
10 changes: 6 additions & 4 deletions src/gui/traymenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TrayMenu final : public QMenu
void clearCustomActions();

/** Add custom action. */
void addCustomAction(QAction *action);
void setCustomActions(QList<QAction*> actions);

/** Clear clipboard item actions and curstom actions. */
void clearAllActions();
Expand Down Expand Up @@ -83,11 +83,10 @@ class TrayMenu final : public QMenu
void inputMethodEvent(QInputMethodEvent *event) override;

private:
void clearActionsWithProperty(const char *property);

void onClipboardItemActionTriggered();

void updateActiveAction();
void delayedUpdateActiveAction();
void doUpdateActiveAction();

void setSearchMenuItem(const QString &text);

Expand All @@ -105,6 +104,9 @@ class TrayMenu final : public QMenu
QTimer m_timerUpdateActiveAction;

bool m_rowIndexFromOne = true;

QList<QAction*> m_clipboardActions;
QList<QAction*> m_customActions;
};

#endif // TRAYMENU_H

0 comments on commit 386e5d0

Please sign in to comment.