Skip to content

Commit

Permalink
Skip tab switcher dialog if Ctrl is released quickly (up to 200 ms), …
Browse files Browse the repository at this point in the history
…references #52
  • Loading branch information
Emdek committed Mar 28, 2015
1 parent 33393bb commit e9daac0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
97 changes: 86 additions & 11 deletions src/ui/MainWindow.cpp
Expand Up @@ -68,6 +68,8 @@ MainWindow::MainWindow(bool isPrivate, const SessionMainWindow &session, QWidget
m_toggleEdge(NULL),
m_sidebarWidget(NULL),
m_splitter(new QSplitter(this)),
m_tabSwitcherKey(0),
m_tabSwictherTimer(0),
m_ui(new Ui::MainWindow)
{
m_ui->setupUi(this);
Expand Down Expand Up @@ -164,6 +166,33 @@ MainWindow::~MainWindow()
delete m_ui;
}

void MainWindow::timerEvent(QTimerEvent *event)
{
if (event->timerId() == m_tabSwictherTimer)
{
killTimer(m_tabSwictherTimer);

m_tabSwictherTimer = 0;

if (m_windowsManager->getWindowCount() > 1)
{
if (!m_tabSwitcher)
{
m_tabSwitcher = new TabSwitcherWidget(m_windowsManager, this);
}

m_tabSwitcher->raise();
m_tabSwitcher->resize(size());
m_tabSwitcher->show(false);
m_tabSwitcher->selectTab(m_tabSwitcherKey == Qt::Key_Tab);
}
else
{
m_windowsManager->triggerAction((m_tabSwitcherKey == Qt::Key_Tab) ? Action::ActivateTabOnRightAction : Action::ActivateTabOnLeftAction);
}
}
}

void MainWindow::closeEvent(QCloseEvent *event)
{
if (SessionsManager::isLastWindow() && !Application::getInstance()->canClose())
Expand Down Expand Up @@ -198,21 +227,36 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab)
{
if (m_windowsManager->getWindowCount() > 1)
if (m_tabSwictherTimer == 0 && m_windowsManager->getWindowCount() > 1)
{
if (!m_tabSwitcher)
{
m_tabSwitcher = new TabSwitcherWidget(m_windowsManager, this);
}

m_tabSwitcher->raise();
m_tabSwitcher->resize(size());
m_tabSwitcher->show(false);
m_tabSwitcher->selectTab(event->key() == Qt::Key_Tab);
m_tabSwitcherKey = event->key();
m_tabSwictherTimer = startTimer(200);
}
else
{
m_windowsManager->triggerAction(((event->key() == Qt::Key_Tab) ? Action::ActivateTabOnRightAction : Action::ActivateTabOnLeftAction), parentWidget());
if (m_tabSwictherTimer > 0)
{
killTimer(m_tabSwictherTimer);

m_tabSwictherTimer = 0;
}

if (m_windowsManager->getWindowCount() > 1)
{
if (!m_tabSwitcher)
{
m_tabSwitcher = new TabSwitcherWidget(m_windowsManager, this);
}

m_tabSwitcher->raise();
m_tabSwitcher->resize(size());
m_tabSwitcher->show(false);
m_tabSwitcher->selectTab(event->key() == Qt::Key_Tab);
}
else
{
m_windowsManager->triggerAction((event->key() == Qt::Key_Tab) ? Action::ActivateTabOnRightAction : Action::ActivateTabOnLeftAction);
}
}

event->accept();
Expand All @@ -223,6 +267,37 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
}
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Control && m_tabSwictherTimer > 0)
{
killTimer(m_tabSwictherTimer);

m_tabSwictherTimer = 0;

QMultiMap<qint64, quint64> map;

for (int i = 0; i < m_windowsManager->getWindowCount(); ++i)
{
Window *window = m_windowsManager->getWindowByIndex(i);

if (window)
{
map.insert(window->getLastActivity().toMSecsSinceEpoch(), window->getIdentifier());
}
}

const QList<quint64> list = map.values();

if (list.count() > 1)
{
m_windowsManager->setActiveWindowByIdentifier((m_tabSwitcherKey == Qt::Key_Tab) ? list.at(list.count() - 2) : list.first());
}
}

QMainWindow::keyReleaseEvent(event);
}

void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
if (m_tabSwitcher && m_tabSwitcher->isVisible())
Expand Down
4 changes: 4 additions & 0 deletions src/ui/MainWindow.h
Expand Up @@ -67,8 +67,10 @@ public slots:
void triggerAction(int identifier, bool checked = false);

protected:
void timerEvent(QTimerEvent *event);
void closeEvent(QCloseEvent *event);
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
void contextMenuEvent(QContextMenuEvent *event);
void createSidebar();
void createToggleEdge();
Expand Down Expand Up @@ -98,6 +100,8 @@ protected slots:
QSplitter *m_splitter;
QString m_currentBookmark;
Qt::WindowStates m_previousState;
int m_tabSwitcherKey;
int m_tabSwictherTimer;
Ui::MainWindow *m_ui;

signals:
Expand Down
2 changes: 1 addition & 1 deletion src/ui/TabSwitcherWidget.cpp
Expand Up @@ -247,7 +247,7 @@ QList<QStandardItem*> TabSwitcherWidget::createRow(Window *window) const
{
QList<QStandardItem*> items;
items.append(new QStandardItem(window->getIcon(), window->getTitle()));
items.append(new QStandardItem(window->getLastActivity().toString()));
items.append(new QStandardItem(QString::number(window->getLastActivity().toMSecsSinceEpoch())));
items[0]->setData(window->getIdentifier(), Qt::UserRole);

connect(window, SIGNAL(titleChanged(QString)), this, SLOT(setTitle(QString)));
Expand Down

0 comments on commit e9daac0

Please sign in to comment.