diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc index fdd8b011cdc3e94fe22e4068b7700a06edaf2de1..f1c656a766fce075b8fbce016d456959fba34828 100644 --- a/src/UI/MainWindow.cc +++ b/src/UI/MainWindow.cc @@ -79,6 +79,22 @@ MainWindow::MainWindow() noexcept ACTION_ADD_SHORTCUT(openDocument, QKeySequence::Open); ACTION_ADD_SHORTCUT(saveFile, QKeySequence::Save); + // Custom useFakeVim action + { + editMenu->addSeparator(); + QAction *useFakeVim = new QAction(tr("Use FakeVim"), this); + useFakeVim->setStatusTip("Use FakeVim with integrated text editors"); + useFakeVim->setCheckable(true); + editMenu->addAction(useFakeVim); + connect(useFakeVim, &QAction::toggled, this, [this](bool checked) noexcept -> void { + const bool oldState = vivyApp->getUseFakeVimEditor(); + if (oldState != checked) { + vivyApp->setUseFakeVimEditor(checked); + updateFakeVimUsage(checked); + } + }); + } + // Setup the tabs to display the documents documents = new QTabWidget(this); documents->setMovable(true); @@ -150,6 +166,21 @@ MainWindow::MainWindow() noexcept newDocument(); } +void +MainWindow::updateFakeVimUsage(bool yes) noexcept +{ + const int count = documents->count(); + for (int index = 0; index < count; ++index) { + AbstractDocumentView *docView = getTab(index); + const bool documentExists = docView && docView->getDocument(); + const bool isScriptEditor = + documentExists && (docView->getDocument()->getType() == AbstractDocument::Type::Script); + + if (isScriptEditor) + static_cast<ScriptDocumentView *const>(docView)->setUseFakeVimEditor(yes); + } +} + void MainWindow::openProperties(int index) noexcept { diff --git a/src/UI/MainWindow.hh b/src/UI/MainWindow.hh index 37d225a11f494b7bdefb909e5725c177b681825b..3a5d921232f76d2a2808f3bb36eabda6627d772b 100644 --- a/src/UI/MainWindow.hh +++ b/src/UI/MainWindow.hh @@ -49,6 +49,7 @@ private: AbstractDocumentView *getCurrentDocumentView() const; int findFirstUntouchedDocument() const noexcept; + void updateFakeVimUsage(bool yes) noexcept; QString dialogOpenFileName(const QString &title, const QString &folder, const QString &filter) noexcept; diff --git a/src/UI/ScriptDocumentView.cc b/src/UI/ScriptDocumentView.cc index 51f12c7bfb2da10df66acb8f20eb05a71bb63a49..f4976576e81aa960937abd467858f9f6b8e094f2 100644 --- a/src/UI/ScriptDocumentView.cc +++ b/src/UI/ScriptDocumentView.cc @@ -23,35 +23,45 @@ ScriptDocumentView::ScriptDocumentView(std::shared_ptr<ScriptDocument> ptr, QWid setCentralWidget(editor); editor->setFocus(Qt::OtherFocusReason); - // FakeVim - { + setUseFakeVimEditor(vivyApp->getUseFakeVimEditor()); + + connect(this, &ScriptDocumentView::luaErrorFound, editor, &ScriptEditor::updateLastLuaError); + + // Same style as the editor + setStyleSheet(QStringLiteral("* {" + " background-color: #232629;" + " font-family: \"FiraCode\";" + " font-size: 10pt" + "}")); +} + +void +ScriptDocumentView::setUseFakeVimEditor(bool yes) noexcept +{ + if (yes && !isUsingFakeVim) { MainWindow *const mw = vivyApp->getMainWindow(); handler = new FakeVimHandler(editor, this); proxy = EditorProxy::connectSignals(handler, editor); + isUsingFakeVim = true; connect(proxy, &EditorProxy::handleInput, handler, &FakeVimHandler::handleInput); connect(proxy, &EditorProxy::requestQuit, this, [this, mw]() noexcept -> void { mw->closeDocument(static_cast<AbstractDocumentView *>(this)); }); + TODO(Implement the save and save + quit things) // connect(proxy, &EditorProxy::requestSave, document, &AbstractDocument::save); // TODO // connect(proxy, &EditorProxy::requestSaveAndQuit, document, &AbstractDocument::save + &MainWindow::closeTab); // TODO - // connect(proxy, &EditorProxy::requestQuit, document, &MainWindow::closeTab); // TODO initHandler(handler); - handler->handleCommand(QStringLiteral("set expandtab")); - handler->handleCommand(QStringLiteral("set shiftwidth=4")); - handler->handleCommand(QStringLiteral("set tabstop=4")); - handler->handleCommand(QStringLiteral("set autoindent")); - handler->handleCommand(QStringLiteral("set smartindent")); clearUndoRedo(editor); } - connect(this, &ScriptDocumentView::luaErrorFound, editor, &ScriptEditor::updateLastLuaError); - - // Same style as the editor - setStyleSheet(QStringLiteral("* {" - " background-color: #232629;" - " font-family: \"FiraCode\";" - " font-size: 10pt" - "}")); + else if (!yes && isUsingFakeVim) { + delete proxy; + delete handler; + clearUndoRedo(editor); + isUsingFakeVim = false; + proxy = nullptr; + handler = nullptr; + } } void @@ -61,6 +71,12 @@ ScriptDocumentView::initHandler(FakeVimHandler *handler) noexcept handler->handleCommand(QStringLiteral("set nopasscontrolkey")); handler->installEventFilter(); handler->setupWidget(); + + handler->handleCommand(QStringLiteral("set expandtab")); + handler->handleCommand(QStringLiteral("set shiftwidth=4")); + handler->handleCommand(QStringLiteral("set tabstop=4")); + handler->handleCommand(QStringLiteral("set autoindent")); + handler->handleCommand(QStringLiteral("set smartindent")); } void diff --git a/src/UI/ScriptDocumentView.hh b/src/UI/ScriptDocumentView.hh index 7b01ce08f102adc201e6817453d0d9a4179b09ad..1e94b676a88fc5e4e45f55835f05a881a9848092 100644 --- a/src/UI/ScriptDocumentView.hh +++ b/src/UI/ScriptDocumentView.hh @@ -45,6 +45,8 @@ public: QIcon getDocumentTabIcon() const noexcept override; AbstractDocument *getDocument() const noexcept override; + void setUseFakeVimEditor(bool yes) noexcept; + signals: void luaErrorFound(int, QString); @@ -56,6 +58,7 @@ private: std::shared_ptr<ScriptDocument> document{ nullptr }; QString lastLuaErrorMsg{}; int lastLuaErrorLine{ -1 }; + bool isUsingFakeVim{ false }; static void initHandler(FakeVimHandler *handler) noexcept; static void clearUndoRedo(QPlainTextEdit *editor) noexcept; diff --git a/src/UI/ScriptViews/EditorProxy.cc b/src/UI/ScriptViews/EditorProxy.cc index a5934a3ceba5fa9a09142f96016fec5210b2ba93..07b1385cb82433c00968b981e0bd5f7b2599b30c 100644 --- a/src/UI/ScriptViews/EditorProxy.cc +++ b/src/UI/ScriptViews/EditorProxy.cc @@ -20,6 +20,7 @@ EditorProxy::connectSignals(FakeVimHandler *handler, QPlainTextEdit *editor) noe int /* messageLevel */) noexcept -> void { proxy->changeStatusMessage(contents, cursorPos); }); + handler->extraInformationChanged.connect( [proxy](const QString &text) noexcept -> void { proxy->changeExtraInformation(text); }); handler->statusDataChanged.connect( diff --git a/src/VivyApplication.cc b/src/VivyApplication.cc index f3e17144af99bdce902167f4f286bf1f7d7a1a92..075638a9b25784b45ea64be5195a208210b3e330 100644 --- a/src/VivyApplication.cc +++ b/src/VivyApplication.cc @@ -99,3 +99,15 @@ VivyApplication::getCurrentDocument() const throw std::logic_error("No main window in the graphic VivyApplication"); return mainWindowPtr.get()->getCurrentDocument(); } + +bool +VivyApplication::getUseFakeVimEditor() const noexcept +{ + return useFakeVim; +} + +void +VivyApplication::setUseFakeVimEditor(bool ok) noexcept +{ + useFakeVim = ok; +} diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh index 1ba91685512f43b533e4e3cd06a511bb871c35ef..3826063a9838fcb173a0dcc0ff72b6575ce3d5ec 100644 --- a/src/VivyApplication.hh +++ b/src/VivyApplication.hh @@ -66,15 +66,19 @@ private: int fontIdBoldItalic; std::unique_ptr<MainWindow> mainWindowPtr{ nullptr }; + bool useFakeVim{ false }; public: VivyApplication(int &argc, char **argv); int exec() noexcept; - QFont getApplicationFont(Font) const noexcept; - [[nodiscard("handle-it")]] MainWindow *getMainWindow() const; - [[nodiscard("handle-it")]] AbstractDocument *getCurrentDocument() const; + [[nodiscard]] QFont getApplicationFont(Font) const noexcept; + [[nodiscard]] MainWindow *getMainWindow() const; + [[nodiscard]] AbstractDocument *getCurrentDocument() const; + [[nodiscard]] bool getUseFakeVimEditor() const noexcept; + + void setUseFakeVimEditor(bool) noexcept; void setTheme(Theme) noexcept; };