Skip to content

Commit

Permalink
Editor scroll bar adjustments 🌺
Browse files Browse the repository at this point in the history
- Replaced Text::Regex abbreviated enum
- Sectioned-off calls to check min/max scroll in Editor
- Added slot for setting scrolls up and down enabled depending on scroll bar placement
- Removed top/bottom border of scroll handle
- Doubled scroll amount
- Set auto-repeat delay
  • Loading branch information
fairybow committed Jan 22, 2023
1 parent 2c090dc commit 249b4ee
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 41 deletions.
3 changes: 2 additions & 1 deletion fernanda/res/themes/window.qss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#hScrollBar::handle:horizontal, #pvScrollBar::handle:vertical, #vScrollBar::handle:vertical
{
background: @scrollHandle;
border: 2px solid @scrollBg;
border-left: 2px solid @scrollBg;
border-right: 2px solid @scrollBg;
min-height: 0px;
}

Expand Down
2 changes: 1 addition & 1 deletion fernanda/src/indicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void Indicator::updatePositions(const int cursorBlockNumber, const int cursorPos
void Indicator::updateCounts(const QString text, const int blockCount)
{
if (!hideOrShow(counts, hasLineCount, hasWordCount, hasCharCount)) return;
const auto word_count = text.split(Text::regex(Text::Re::Split), Qt::SkipEmptyParts).count();
const auto word_count = text.split(Text::regex(Text::Regex::Split), Qt::SkipEmptyParts).count();
const auto char_count = text.count();
QStringList elements;
if (hasLineCount)
Expand Down
2 changes: 1 addition & 1 deletion fernanda/src/pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ const QString Pane::rename()
bool has_input = false;
QString text = QInputDialog::getText(this, tr(nullptr), tr(nullptr), QLineEdit::Normal, nullptr, &has_input);
if (has_input && !text.isEmpty())
return text.replace(Text::regex(Text::Re::Forbidden), "_");
return text.replace(Text::regex(Text::Regex::Forbidden), "_");
return nullptr;
}

Expand Down
42 changes: 31 additions & 11 deletions fernanda/src/plaintextedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ PlainTextEdit::PlainTextEdit(QWidget* parent)
addScrollBarWidget(scrollPrevious, Qt::AlignTop);
addScrollBarWidget(scrollNext, Qt::AlignBottom);
addScrollBarWidget(scrollDown, Qt::AlignBottom);
scrollUp->setAutoRepeat(true);
scrollDown->setAutoRepeat(true);
for (const auto& button : { scrollUp, scrollDown })
{
button->setAutoRepeat(true);
button->setAutoRepeatDelay(500);
}
scrollUp->setText(Icon::draw(Icon::Name::ArrowUp));
scrollPrevious->setText(Icon::draw(Icon::Name::ArrowPrevious));
scrollNext->setText(Icon::draw(Icon::Name::ArrowNext));
Expand All @@ -31,6 +34,7 @@ PlainTextEdit::PlainTextEdit(QWidget* parent)
scrollNext->setObjectName("scrollNext");
scrollDown->setObjectName("scrollDown");
connections();
scrollButtonEnabledHandler();
updateLineNumberAreaWidth(0);
highlightCurrentLine();
}
Expand Down Expand Up @@ -77,7 +81,7 @@ int PlainTextEdit::selectedLineCount()
{
auto cursor = textCursor();
if (!cursor.hasSelection()) return 1;
return cursor.selectedText().count(Text::regex(Text::Re::ParagraphSeparator)) + 1;
return cursor.selectedText().count(Text::regex(Text::Regex::ParagraphSeparator)) + 1;
}

void PlainTextEdit::scrollNavClicked(Scroll direction)
Expand All @@ -86,24 +90,22 @@ void PlainTextEdit::scrollNavClicked(Scroll direction)
auto early_return = false;
switch (direction) {
case Scroll::Next:
if (verticalScrollBar()->sliderPosition() != verticalScrollBar()->maximum())
if (!isMaximumScroll())
{
verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMaximum);
early_return = true;
}
break;
case Scroll::Previous:
if (verticalScrollBar()->sliderPosition() != verticalScrollBar()->minimum())
if (!isMinimumScroll())
{
verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMinimum);
early_return = true;
}
break;
}
if (early_return) return;
(direction == Scroll::Next)
? askGoNext()
: askGoPrevious();
(direction == Scroll::Next) ? askGoNext() : askGoPrevious();
}

void PlainTextEdit::handleFont(StdFsPath fontPath, int sliderValue)
Expand Down Expand Up @@ -205,7 +207,7 @@ void PlainTextEdit::keyPressEvent(QKeyEvent* event)
cursor.beginEditBlock();
keyPresses(keyfilter->filter(event, chars));
cursor.endEditBlock();
if (cursor.atEnd() && verticalScrollBar()->sliderPosition() != verticalScrollBar()->maximum())
if (cursor.atEnd() && !isMaximumScroll())
verticalScrollBar()->triggerAction(QAbstractSlider::SliderToMaximum);
}

Expand Down Expand Up @@ -302,16 +304,18 @@ void PlainTextEdit::connections()
connect(this, &PlainTextEdit::blockCountChanged, this, &PlainTextEdit::updateLineNumberAreaWidth);
connect(this, &PlainTextEdit::updateRequest, this, &PlainTextEdit::updateLineNumberArea);
connect(this, &PlainTextEdit::cursorPositionChanged, this, &PlainTextEdit::highlightCurrentLine);
connect(verticalScrollBar(), &QScrollBar::rangeChanged, this, &PlainTextEdit::scrollButtonEnabledHandler);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &PlainTextEdit::scrollButtonEnabledHandler);
connect(scrollNext, &QPushButton::clicked, this, [&]() { scrollNavClicked(Scroll::Next); });
connect(scrollPrevious, &QPushButton::clicked, this, [&]() { scrollNavClicked(Scroll::Previous); });
connect(scrollUp, &QPushButton::clicked, this, [&]()
{
for (auto i = 2; i > 0; --i)
for (auto i = 4; i > 0; --i)
verticalScrollBar()->triggerAction(QAbstractSlider::SliderSingleStepSub);
});
connect(scrollDown, &QPushButton::clicked, this, [&]()
{
for (auto i = 2; i > 0; --i)
for (auto i = 4; i > 0; --i)
verticalScrollBar()->triggerAction(QAbstractSlider::SliderSingleStepAdd);
});
}
Expand Down Expand Up @@ -355,6 +359,22 @@ const QColor PlainTextEdit::highlight()
return result;
}

bool PlainTextEdit::isMinimumScroll()
{
return (verticalScrollBar()->sliderPosition() == verticalScrollBar()->minimum());
}

bool PlainTextEdit::isMaximumScroll()
{
return (verticalScrollBar()->sliderPosition() == verticalScrollBar()->maximum());
}

void PlainTextEdit::scrollButtonEnabledHandler()
{
(isMinimumScroll()) ? scrollUp->setEnabled(false) : scrollUp->setEnabled(true);
(isMaximumScroll()) ? scrollDown->setEnabled(false) : scrollDown->setEnabled(true);
}

void PlainTextEdit::updateLineNumberAreaWidth(int newBlockCount)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
Expand Down
3 changes: 3 additions & 0 deletions fernanda/src/plaintextedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public slots:
const QRect reshapeCursor(QChar currentChar);
const QColor recolorCursor(bool under = false);
const QColor highlight();
bool isMinimumScroll();
bool isMaximumScroll();

private slots:
void scrollButtonEnabledHandler();
void updateLineNumberAreaWidth(int newBlockCount);
void updateLineNumberArea(const QRect& rect, int dy);

Expand Down
2 changes: 1 addition & 1 deletion fernanda/src/story.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ bool Story::isEdited(QString key)
void Story::bak()
{
auto underscore = "_";
auto timestamp = UserData::timestamp().replace(Text::regex(Text::Re::Forbidden), underscore).replace(Text::regex(Text::Re::Space), underscore).replace(Text::regex(Text::Re::NewLine), nullptr).toLower();
auto timestamp = UserData::timestamp().replace(Text::regex(Text::Regex::Forbidden), underscore).replace(Text::regex(Text::Regex::Space), underscore).replace(Text::regex(Text::Regex::NewLine), nullptr).toLower();
timestamp.replace(QRegularExpression("(__)"), underscore).replace(QRegularExpression("(_$)"), nullptr);
auto bak_file_name = name<QString>() + ".story." + timestamp + ".bak";
auto bak_path = UserData::doThis(UserData::Operation::GetRollback) / Path::toStdFs(bak_file_name);
Expand Down
10 changes: 5 additions & 5 deletions fernanda/src/style.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace Style

inline const QString createStyleSheetFromTheme(QString styleSheet, QString themeSheet)
{
QRegularExpressionMatchIterator matches = Text::regex(Text::Re::ThemeSheetLine).globalMatch(themeSheet);
QRegularExpressionMatchIterator matches = Text::regex(Text::Regex::ThemeSheetLine).globalMatch(themeSheet);
while (matches.hasNext())
{
QRegularExpressionMatch match = matches.next();
if (!match.hasMatch()) continue;
QString variable = match.captured(0).replace(Text::regex(Text::Re::ThemeSheetValue), nullptr);
QString value = match.captured(0).replace(Text::regex(Text::Re::ThemeSheetVariable), nullptr);
QString variable = match.captured(0).replace(Text::regex(Text::Regex::ThemeSheetValue), nullptr);
QString value = match.captured(0).replace(Text::regex(Text::Regex::ThemeSheetVariable), nullptr);
styleSheet.replace(QRegularExpression(variable), value);
}
return styleSheet;
Expand All @@ -48,8 +48,8 @@ namespace Style
{
auto theme_sheet = Io::readFile(themePath);
style_sheet = style_sheet + Text::newLines() + createStyleSheetFromTheme(Io::readFile(":/themes/editor.qss"), theme_sheet);
QRegularExpressionMatch match_cursor = Text::regex(Text::Re::ThemeSheetCursor).match(theme_sheet);
QRegularExpressionMatch match_under_cursor = Text::regex(Text::Re::ThemeSheetCursorUnder).match(theme_sheet);
QRegularExpressionMatch match_cursor = Text::regex(Text::Regex::ThemeSheetCursor).match(theme_sheet);
QRegularExpressionMatch match_under_cursor = Text::regex(Text::Regex::ThemeSheetCursorUnder).match(theme_sheet);
cursor_color = match_cursor.captured(2);
under_cursor_color = match_under_cursor.captured(2);
}
Expand Down
26 changes: 13 additions & 13 deletions fernanda/src/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,41 @@ const QString Text::operator/(QString lhs, QString rhs)
return lhs + QStringLiteral("<br>") + rhs;
}

const QRegularExpression Text::regex(Re operation)
const QRegularExpression Text::regex(Regex operation)
{
QRegularExpression result;
switch (operation) {
case Re::Forbidden:
case Regex::Forbidden:
result = QRegularExpression(QStringLiteral(R"((<|>|:|\/|\\|\||\?|\*|\"))"));
break;
case Re::NewLine:
case Regex::NewLine:
result = QRegularExpression(QStringLiteral("(\\n)"));
break;
case Re::ParagraphSeparator:
case Regex::ParagraphSeparator:
result = QRegularExpression(QStringLiteral("(\U00002029)"));
break;
case Re::Space:
case Regex::Space:
result = QRegularExpression(QStringLiteral("(\\s)"));
break;
case Re::Split:
case Regex::Split:
result = QRegularExpression(QStringLiteral("(\\s|\\n|\\r|\U00002029|^)+"));
break;
case Re::ThemeSheetCursor:
case Regex::ThemeSheetCursor:
result = QRegularExpression(QStringLiteral("(@cursorColor; = )(.*)(;)"));
break;
case Re::ThemeSheetCursorUnder:
case Regex::ThemeSheetCursorUnder:
result = QRegularExpression(QStringLiteral("(@cursorUnderColor; = )(.*)(;)"));
break;
case Re::ThemeSheetLine:
case Regex::ThemeSheetLine:
result = QRegularExpression(QStringLiteral("(@.*\\n?)"));
break;
case Re::ThemeSheetValue:
case Regex::ThemeSheetValue:
result = QRegularExpression(QStringLiteral("(\\s=.*;)"));
break;
case Re::ThemeSheetVariable:
case Regex::ThemeSheetVariable:
result = QRegularExpression(QStringLiteral("(@.*=\\s)"));
break;
case Re::UrlBeginning:
case Regex::UrlBeginning:
result = QRegularExpression(QStringLiteral("(https:\\/\\/|www.)"));
break;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ const QString Text::table(QStringList columns)
const QString Text::link(const char* url, QString displayName)
{
if (displayName.isEmpty())
displayName = QString(url).replace(regex(Re::UrlBeginning), nullptr);
displayName = QString(url).replace(regex(Regex::UrlBeginning), nullptr);
return QStringLiteral("<a href='") + url + QStringLiteral("'>") + displayName + QStringLiteral("</a>");
}

Expand Down
4 changes: 2 additions & 2 deletions fernanda/src/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Text
{
enum class Re {
enum class Regex {
Forbidden,
NewLine,
ParagraphSeparator,
Expand All @@ -33,7 +33,7 @@ namespace Text
const QString operator%(QString lhs, QString rhs);
const QString operator/(QString lhs, const char* rhs);
const QString operator/(QString lhs, QString rhs);
const QRegularExpression regex(Re operation);
const QRegularExpression regex(Regex operation);
const QString multiplyThese(QString character, int defaultArgument = 1);
const QString multiSpaces(int spaces = 3);
const QString newLines(int lines = 2);
Expand Down
8 changes: 4 additions & 4 deletions fernanda/src/tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ void Tool::mousePressEvent(QMouseEvent* event)
{
if (countdown > 0 && countdown < resetCountdown())
{
if (timer.value()->isActive())
timer.value()->stop();
else
timer.value()->start(1000);
auto& timer_value = timer.value();
(timer_value->isActive())
? timer_value->stop()
: timer_value->start(1000);
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions fernanda/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#pragma once

#define VER_FILEVERSION 0,13,0,30
#define VER_FILEVERSION_STR "v0.13.0-beta30"
#define VER_FILEVERSION 0,13,1,31
#define VER_FILEVERSION_STR "v0.13.1-beta31"
#define VER_PRODUCTVERSION VER_FILEVERSION
#define VER_PRODUCTVERSION_STR VER_FILEVERSION_STR
#define VER_COMPANYNAME_STR "@fairybow"
Expand Down

0 comments on commit 249b4ee

Please sign in to comment.