Skip to content

Commit

Permalink
Use NavigationStyle for Vi/Emacs option
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Sep 25, 2024
1 parent 7c40fe0 commit 2810029
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/app/clipboardserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ void ClipboardServer::loadSettings(AppConfig *appConfig)
m_sharedData->editor = appConfig->option<Config::editor>();
m_sharedData->maxItems = appConfig->option<Config::maxitems>();
m_sharedData->textWrap = appConfig->option<Config::text_wrap>();
m_sharedData->viMode = appConfig->option<Config::vi>();
m_sharedData->navigationStyle = appConfig->option<Config::vi>();
m_sharedData->saveOnReturnKey = !appConfig->option<Config::edit_ctrl_return>();
m_sharedData->moveItemOnReturnKey = appConfig->option<Config::move>();
m_sharedData->showSimpleItems = appConfig->option<Config::show_simple_items>();
Expand Down
7 changes: 2 additions & 5 deletions src/common/appconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef APPCONFIG_H
#define APPCONFIG_H

#include "common/navigationstyle.h"
#include "common/settings.h"

class QString;
Expand Down Expand Up @@ -120,14 +121,10 @@ struct confirm_exit : Config<bool> {
static Value defaultValue() { return true; }
};

struct vi : Config<bool> {
struct vi : Config<NavigationStyle> {
static QString name() { return QStringLiteral("vi"); }
};

struct emacs : Config<bool> {
static QString name() { return "emacs"; }
};

struct save_filter_history : Config<bool> {
static QString name() { return QStringLiteral("save_filter_history"); }
};
Expand Down
8 changes: 8 additions & 0 deletions src/common/navigationstyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once

enum class NavigationStyle {
Default,
Vi,
Emacs
};
4 changes: 2 additions & 2 deletions src/gui/clipboardbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,13 +1378,13 @@ void ClipboardBrowser::keyPressEvent(QKeyEvent *event)
return;

// translate keys for vi mode
if (m_sharedData->viMode && handleViKey(event, this)) {
if (m_sharedData->navigationStyle == NavigationStyle::Vi && handleViKey(event, this)) {
d.updateIfNeeded();
return;
}

// translate keys for emacs mode
if (m_sharedData->emacsMode && handleEmacsKey(event, this))
if (m_sharedData->navigationStyle == NavigationStyle::Emacs && handleEmacsKey(event, this))
return;

const Qt::KeyboardModifiers mods = event->modifiers();
Expand Down
4 changes: 2 additions & 2 deletions src/gui/clipboardbrowsershared.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef CLIPBOARDBROWSERSHARED_H
#define CLIPBOARDBROWSERSHARED_H

#include "common/navigationstyle.h"
#include "gui/menuitems.h"
#include "gui/theme.h"

Expand All @@ -17,8 +18,7 @@ struct ClipboardBrowserShared {
QString editor;
int maxItems = 100;
bool textWrap = true;
bool viMode = false;
bool emacsMode = false;
NavigationStyle navigationStyle = NavigationStyle::Default;
bool saveOnReturnKey = false;
bool moveItemOnReturnKey = false;
bool showSimpleItems = false;
Expand Down
13 changes: 6 additions & 7 deletions src/gui/configurationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ void ConfigurationManager::initOptions()
bind<Config::move>(m_tabHistory->checkBoxMove);
bind<Config::check_clipboard>(m_tabGeneral->checkBoxClip);
bind<Config::confirm_exit>(m_tabGeneral->checkBoxConfirmExit);
bind<Config::vi>(m_tabGeneral->radioButtonViMode);
bind<Config::emacs>(m_tabGeneral->radioButtonEmacsMode);
bind<Config::vi>(m_tabGeneral->comboBoxNavigationStyle);
bind<Config::save_filter_history>(m_tabGeneral->checkBoxSaveFilterHistory);
bind<Config::autocompletion>(m_tabGeneral->checkBoxAutocompleteCommands);
bind<Config::always_on_top>(m_tabGeneral->checkBoxAlwaysOnTop);
Expand Down Expand Up @@ -362,11 +361,6 @@ void ConfigurationManager::bind(const QString &optionKey, QCheckBox *obj, bool d
m_options[optionKey] = Option(defaultValue, "checked", obj);
}

void ConfigurationManager::bind(const QString &optionKey, QRadioButton *obj, bool defaultValue)
{
m_options[optionKey] = Option(defaultValue, "checked", obj);
}

void ConfigurationManager::bind(const QString &optionKey, QSpinBox *obj, int defaultValue)
{
m_options[optionKey] = Option(defaultValue, "value", obj);
Expand All @@ -382,6 +376,11 @@ void ConfigurationManager::bind(const QString &optionKey, QComboBox *obj, int de
m_options[optionKey] = Option(defaultValue, "currentIndex", obj);
}

void ConfigurationManager::bind(const QString &optionKey, QComboBox *obj, NavigationStyle defaultValue)
{
m_options[optionKey] = Option(static_cast<int>(defaultValue), "currentIndex", obj);
}

void ConfigurationManager::bind(const QString &optionKey, const QVariant &defaultValue, const char *description)
{
m_options[optionKey] = Option(defaultValue, description);
Expand Down
4 changes: 2 additions & 2 deletions src/gui/configurationmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class Option;
class ShortcutsWidget;
class QAbstractButton;
class QCheckBox;
class QRadioButton;
class QComboBox;
class QLineEdit;
class QListWidgetItem;
class QSpinBox;
enum class NavigationStyle;

/**
* Configuration dialog.
Expand Down Expand Up @@ -107,10 +107,10 @@ class ConfigurationManager final : public QDialog
void bind();

void bind(const QString &optionKey, QCheckBox *obj, bool defaultValue);
void bind(const QString &optionKey, QRadioButton *obj, bool defaultValue);
void bind(const QString &optionKey, QSpinBox *obj, int defaultValue);
void bind(const QString &optionKey, QLineEdit *obj, const QString &defaultValue);
void bind(const QString &optionKey, QComboBox *obj, int defaultValue);
void bind(const QString &optionKey, QComboBox *obj, NavigationStyle defaultValue);
void bind(const QString &optionKey, const QVariant &defaultValue, const char *description);

void updateTabComboBoxes();
Expand Down
18 changes: 6 additions & 12 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ void MainWindow::onSearchShowRequest(const QString &text)

enterSearchMode();

if (!m_options.viMode || text != "/") {
if (m_options.navigationStyle != NavigationStyle::Vi || text != "/") {
ui->searchBar->setText(text);
ui->searchBar->end(false);
}
Expand Down Expand Up @@ -2530,7 +2530,7 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
if (m_options.hideTabs && key == Qt::Key_Alt)
setHideTabs(false);

if (m_options.viMode) {
if (m_options.navigationStyle == NavigationStyle::Vi) {
if (modifiers == Qt::ControlModifier && key == Qt::Key_BracketLeft) {
onEscape();
return;
Expand All @@ -2557,7 +2557,7 @@ void MainWindow::keyPressEvent(QKeyEvent *event)
}
}

if (m_options.emacsMode) {
if (m_options.navigationStyle == NavigationStyle::Emacs) {
if ((modifiers == Qt::ControlModifier && key == Qt::Key_G)
|| (key == Qt::Key_Escape)) {
onEscape();
Expand Down Expand Up @@ -2714,15 +2714,9 @@ void MainWindow::loadSettings(QSettings &settings, AppConfig *appConfig)
setAlwaysOnTop(this, alwaysOnTop);
setAlwaysOnTop(m_commandDialog.data(), alwaysOnTop);

// Vi mode
m_options.viMode = appConfig->option<Config::vi>();
m_trayMenu->setViModeEnabled(m_options.viMode);
m_menu->setViModeEnabled(m_options.viMode);

// Emacs mode
m_options.emacsMode = appConfig->option<Config::emacs>();
m_trayMenu->setEmacsModeEnabled(m_options.emacsMode);
m_menu->setEmacsModeEnabled(m_options.emacsMode);
m_options.navigationStyle = appConfig->option<Config::vi>();
m_trayMenu->setNavigationStyle(m_options.navigationStyle);
m_menu->setNavigationStyle(m_options.navigationStyle);

// Number search
m_trayMenu->setNumberSearchEnabled(m_sharedData->numberSearch);
Expand Down
5 changes: 2 additions & 3 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "common/clipboardmode.h"
#include "common/command.h"
#include "common/navigationstyle.h"
#include "gui/clipboardbrowsershared.h"
#include "gui/menuitems.h"
#include "item/persistentdisplayitem.h"
Expand Down Expand Up @@ -34,7 +35,6 @@ class Theme;
class TrayMenu;
class ToolBar;
class QModelIndex;
struct MainWindowOptions;
struct NotificationButton;

Q_DECLARE_METATYPE(QPersistentModelIndex)
Expand Down Expand Up @@ -71,8 +71,7 @@ struct MainWindowOptions {
bool activatePastes() const { return itemActivationCommands & ActivatePastes; }

bool confirmExit = true;
bool viMode = false;
bool emacsMode = false;
NavigationStyle navigationStyle = NavigationStyle::Default;
bool trayCommands = false;
bool trayCurrentTab = false;
QString trayTabName;
Expand Down
27 changes: 7 additions & 20 deletions src/gui/traymenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ TrayMenu::TrayMenu(QWidget *parent)
: QMenu(parent)
, m_clipboardItemActionCount(0)
, m_omitPaste(false)
, m_viMode(false)
, m_emacsMode(false)
, m_numberSearch(false)
{
m_clipboardItemActionsSeparator = addSeparator();
Expand Down Expand Up @@ -107,7 +105,7 @@ QAction *TrayMenu::addClipboardItemAction(const QVariantMap &data, bool showImag
{
// Show search text at top of the menu.
if ( m_clipboardItemActionCount == 0 && m_searchText.isEmpty() ) {
if (m_viMode) {
if (m_navigationStyle == NavigationStyle::Vi) {
setSearchMenuItem( tr("Press '/' to search") );
} else {
setSearchMenuItem( tr("Type to search") );
Expand Down Expand Up @@ -213,20 +211,9 @@ void TrayMenu::clearAllActions()
m_searchText.clear();
}

void TrayMenu::setViModeEnabled(bool enabled)
void TrayMenu::setNavigationStyle(NavigationStyle style)
{
if (enabled) {
m_emacsMode = false;
}
m_viMode = enabled;
}

void TrayMenu::setEmacsModeEnabled(bool enabled)
{
if (enabled) {
m_viMode = false;
}
m_emacsMode = enabled;
m_navigationStyle = style;
}

void TrayMenu::setNumberSearchEnabled(bool enabled)
Expand All @@ -242,9 +229,9 @@ void TrayMenu::keyPressEvent(QKeyEvent *event)

bool not_searching = m_searchText.isEmpty();

if ( not_searching && m_viMode && handleViKey(event, this) ) {
if ( not_searching && m_navigationStyle == NavigationStyle::Vi && handleViKey(event, this) ) {
return;
} else if ( not_searching && m_emacsMode && handleEmacsKey(event, this) ) {
} else if ( not_searching && m_navigationStyle == NavigationStyle::Emacs && handleEmacsKey(event, this) ) {
return;
} else {
// Movement in tray menu.
Expand Down Expand Up @@ -277,7 +264,7 @@ void TrayMenu::keyPressEvent(QKeyEvent *event)
default:
// Type text for search.
if ( (m_clipboardItemActionCount > 0 || !m_searchText.isEmpty())
&& (!m_viMode || !m_searchText.isEmpty() || key == Qt::Key_Slash)
&& (m_navigationStyle != NavigationStyle::Vi || !m_searchText.isEmpty() || key == Qt::Key_Slash)
&& !event->modifiers().testFlag(Qt::AltModifier)
&& !event->modifiers().testFlag(Qt::ControlModifier) )
{
Expand Down Expand Up @@ -363,7 +350,7 @@ void TrayMenu::search(const QString &text)
return;

m_searchText = text;
emit searchRequest(m_viMode ? m_searchText.mid(1) : m_searchText);
emit searchRequest((m_navigationStyle == NavigationStyle::Vi) ? m_searchText.mid(1) : m_searchText);
}

void TrayMenu::markItemInClipboard(const QVariantMap &clipboardData)
Expand Down
11 changes: 4 additions & 7 deletions src/gui/traymenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef TRAYMENU_H
#define TRAYMENU_H

#include "common/navigationstyle.h"

#include <QMenu>
#include <QPointer>
#include <QTimer>
Expand Down Expand Up @@ -36,11 +38,7 @@ class TrayMenu final : public QMenu
/** Clear clipboard item actions and curstom actions. */
void clearAllActions();

/** Handle Vi shortcuts. */
void setViModeEnabled(bool enabled);

/** Handle Emacs shortcuts. */
void setEmacsModeEnabled(bool enabled);
void setNavigationStyle(NavigationStyle style);

/** Enable searching for numbers. */
void setNumberSearchEnabled(bool enabled);
Expand Down Expand Up @@ -85,9 +83,8 @@ class TrayMenu final : public QMenu
int m_clipboardItemActionCount;

bool m_omitPaste;
bool m_viMode;
bool m_emacsMode;
bool m_numberSearch;
NavigationStyle m_navigationStyle = NavigationStyle::Default;

QString m_searchText;

Expand Down
Loading

0 comments on commit 2810029

Please sign in to comment.