Commit 6a675e58 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Move ScrollBarRestorer to ktexteditor_utils

parent 9244557e
Pipeline #123903 passed with stage
in 4 minutes and 16 seconds
......@@ -36,6 +36,12 @@ target_sources(
plugin.qrc
)
target_include_directories(
externaltoolsplugin
PUBLIC
${CMAKE_SOURCE_DIR}/shared
)
if (BUILD_TESTING)
add_subdirectory(autotests)
endif()
......@@ -31,6 +31,8 @@
#include <QGuiApplication>
#include <QScrollBar>
#include <ktexteditor_utils.h>
static QString toolsConfigDir()
{
static const QString dir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String("/kate/externaltools/");
......@@ -55,46 +57,6 @@ static QVector<KateExternalTool> readDefaultTools()
return tools;
}
struct KateScrollBarRestorer {
KateScrollBarRestorer(KTextEditor::View *view)
{
// Find KateScrollBar
const auto scrollBars = view->findChildren<QScrollBar *>();
kateScrollBar = [scrollBars] {
for (auto scrollBar : scrollBars) {
if (qstrcmp(scrollBar->metaObject()->className(), "KateScrollBar") == 0) {
return scrollBar;
}
}
return static_cast<QScrollBar *>(nullptr);
}();
if (kateScrollBar) {
oldScrollValue = kateScrollBar->value();
}
}
void restore()
{
if (kateScrollBar) {
kateScrollBar->setValue(oldScrollValue);
}
restored = true;
}
~KateScrollBarRestorer()
{
if (!restored) {
restore();
}
}
private:
QPointer<QScrollBar> kateScrollBar = nullptr;
int oldScrollValue = 0;
bool restored = false;
};
K_PLUGIN_FACTORY_WITH_JSON(KateExternalToolsFactory, "externaltoolsplugin.json", registerPlugin<KateExternalToolsPlugin>();)
KateExternalToolsPlugin::KateExternalToolsPlugin(QObject *parent, const QList<QVariant> &)
......@@ -379,7 +341,7 @@ void KateExternalToolsPlugin::handleToolFinished(KateToolRunner *runner, int exi
const bool wereUpdatesEnabled = view->updatesEnabled();
view->setUpdatesEnabled(false);
KateScrollBarRestorer scrollRestorer(view);
Utils::KateScrollBarRestorer scrollRestorer(view);
// Reload doc
view->document()->documentReload();
......
......@@ -9,10 +9,53 @@
#include <KTextEditor/View>
#include <QFontDatabase>
#include <QPointer>
#include <QScrollBar>
namespace Utils
{
// A helper class that maintains scroll position
struct KateScrollBarRestorer {
KateScrollBarRestorer(KTextEditor::View *view)
{
// Find KateScrollBar
const auto scrollBars = view->findChildren<QScrollBar *>();
kateScrollBar = [scrollBars] {
for (auto scrollBar : scrollBars) {
if (qstrcmp(scrollBar->metaObject()->className(), "KateScrollBar") == 0) {
return scrollBar;
}
}
return static_cast<QScrollBar *>(nullptr);
}();
if (kateScrollBar) {
oldScrollValue = kateScrollBar->value();
}
}
void restore()
{
if (kateScrollBar) {
kateScrollBar->setValue(oldScrollValue);
}
restored = true;
}
~KateScrollBarRestorer()
{
if (!restored) {
restore();
}
}
private:
QPointer<QScrollBar> kateScrollBar = nullptr;
int oldScrollValue = 0;
bool restored = false;
};
/**
* returns the current active global font
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment