Skip to content

Commit

Permalink
Typewriter & center on scroll 🎩
Browse files Browse the repository at this point in the history
- Updated Readme
- Added UserData / MainWindow menu options for Cursor "center on scroll" and "Typewriter" positioning
- Rewrote how Splitter/SplitterHandle handle initialization
- Added Splitter::hasHover check
- Added the toggling methods for Cursor COS/Typewriter to PlainTextEdit/Editor
  • Loading branch information
fairybow committed Feb 24, 2023
1 parent 524e107 commit 47068f4
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 53 deletions.
9 changes: 8 additions & 1 deletion Fernanda/docs/Notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
<a href="https://github.com/fairybow/Fernanda/search?l=c%2B%2B"><img src="https://img.shields.io/badge/C%2B%2B20-5B5B5B?logo=c%2B%2B" alt="C++20"/></a>
<br>
<a href="https://shields.io/"><img src="https://img.shields.io/badge/omg-these%20are%20so%20cute-00b3b3"/></a>
</p>
</p>

```
auto view = viewport();
auto height = view->height();
auto y = (height / 2) - cursorRect().y();
viewport()->setGeometry(view->x(), y, view->width(), height);
```
3 changes: 3 additions & 0 deletions Fernanda/docs/To-do.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
- [x] ~~Probably should combine `elements()` and `elementsByAttribute()`~~

### Editor / PlainTextEdit / LineNumberArea
- [ ] "Ribbon" mode
- [ ] A toggleable for every relevant `QPlainTextEdit` toggleable (`centerOnScroll` for example)
- [ ] It is not clear to me that `updateLineNumberAreaWidth(int newBlockCount)` actually uses `newBlockCount` arg
- [ ] Save undo/redo stacks
- [ ] Editor spacing and kerning sliders
Expand All @@ -91,6 +93,7 @@
- [ ] Wrap for parentheses and other closables
- [ ] If a filter was just applied, backspace should function as undo
- [ ] Make thin cursor change color when there's a selection?
- [x] ~~Typewriter mode~~
- [x] ~~Avoid passing entire document for cursor underpaint lol~~
- [x] ~~Toggle chonky cursor vs regular~~
- [x] ~~Style horizontal scrollbar~~
Expand Down
23 changes: 17 additions & 6 deletions Fernanda/source/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,25 @@ void Editor::devRemoveStyle()
void Editor::toggle(bool checked, Has has)
{
switch (has) {
case Has::BlockCursor:
hasBlockCursor = checked;
plainTextEdit->cursorPositionChanged();
UserData::saveConfig(UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorBlock, checked);
break;
case Has::CursorBlink:
hasCursorBlink = checked;
startBlinker();
UserData::saveConfig(UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorBlink, checked);
break;
case Has::CursorBlock:
hasCursorBlock = checked;
plainTextEdit->cursorPositionChanged();
UserData::saveConfig(UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorBlock, checked);
break;
case Has::CursorCenterOnScroll:
askToggleCenterOnScroll(checked);
UserData::saveConfig(UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorCenterOnScroll, checked);
break;
case Has::CursorTypewriter:
hasCursorTypewriter = checked;
plainTextEdit->textChanged();
UserData::saveConfig(UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorTypewriter, checked);
break;
case Has::ExtraScrolls:
askToggleExtraScrolls(checked);
UserData::saveConfig(UserData::IniGroup::Editor, UserData::IniValue::ToggleScrollsPrevNext, checked);
Expand Down Expand Up @@ -185,6 +194,7 @@ void Editor::close(bool isFinal)

void Editor::connections()
{
connect(this, &Editor::askToggleCenterOnScroll, plainTextEdit, &PlainTextEdit::toggleCenterOnScroll);
connect(this, &Editor::askToggleLineNumberArea, plainTextEdit, &PlainTextEdit::toggleLineNumberArea);
connect(this, &Editor::askToggleScrolls, plainTextEdit, &PlainTextEdit::toggleScrolls);
connect(this, &Editor::askToggleExtraScrolls, plainTextEdit, &PlainTextEdit::toggleExtraScrolls);
Expand All @@ -205,7 +215,8 @@ void Editor::connections()
connect(plainTextEdit, &PlainTextEdit::askHasLineHighlight, this, [&]() { return hasLineHighlight; });
connect(plainTextEdit, &PlainTextEdit::askHasKeyFilter, this, [&]() { return hasKeyFilter; });
connect(plainTextEdit, &PlainTextEdit::askHasCursorBlink, this, [&]() { return hasCursorBlink; });
connect(plainTextEdit, &PlainTextEdit::askHasBlockCursor, this, [&]() { return hasBlockCursor; });
connect(plainTextEdit, &PlainTextEdit::askHasCursorBlock, this, [&]() { return hasCursorBlock; });
connect(plainTextEdit, &PlainTextEdit::askHasCursorTypewriter, this, [&]() { return hasCursorTypewriter; });
connect(plainTextEdit, &PlainTextEdit::askCursorVisible, this, [&]() { return cursorVisible; });
connect(plainTextEdit, &PlainTextEdit::cursorPositionChanged, this, [&]() { cursorPositionChanged(); });
connect(plainTextEdit, &PlainTextEdit::selectionChanged, this, [&]() { selectionChanged(); });
Expand Down
8 changes: 6 additions & 2 deletions Fernanda/source/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class Editor : public QWidget
AcceptNew
};
enum class Has {
BlockCursor,
CursorBlink,
CursorBlock,
CursorCenterOnScroll,
CursorTypewriter,
ExtraScrolls,
KeyFilter,
LineHighlight,
Expand Down Expand Up @@ -92,7 +94,8 @@ public slots:
bool hasLineHighlight = true;
bool hasKeyFilter = true;
bool hasCursorBlink = true;
bool hasBlockCursor = true;
bool hasCursorBlock = true;
bool hasCursorTypewriter = false;
bool cursorVisible = true;

void connections();
Expand All @@ -109,6 +112,7 @@ public slots:
bool askHasProject();
QAction* askTheme();
QActionGroup* askThemes();
void askToggleCenterOnScroll(bool checked);
void askToggleExtraScrolls(bool checked);
void askToggleLineNumberArea(bool checked);
void askToggleScrolls(bool checked);
Expand Down
14 changes: 11 additions & 3 deletions Fernanda/source/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ void MainWindow::makeToggleMenu()
auto load_most_recent_toggle = new QAction(tr("&Load most recent project on open"), this);
auto cursor_blink_toggle = new QAction(tr("&Blink"), this);
auto cursor_block_toggle = new QAction(tr("&Block"), this);
auto cursor_center_on_scroll_toggle = new QAction(tr("&Center on scroll"), this);
auto cursor_typewriter_toggle = new QAction(tr("&Typewriter"), this);
auto current_line_highlight_toggle = new QAction(tr("&Current line highlight"), this);
auto editor_shadow_toggle = new QAction(tr("&Editor shadow"), this);
auto editor_theme_toggle = new QAction(tr("&Editor theme"), this);
Expand All @@ -393,7 +395,7 @@ void MainWindow::makeToggleMenu()
auto indicator_toggle = new QAction(tr("&Indicator"), this);
auto preview_toggle = new QAction(tr("&Preview"), this);
auto status_bar_toggle = new QAction(tr("&Status bar"), this);
auto aot_toggle = new QAction(tr("&Always-on-top"), this);
auto aot_toggle = new QAction(tr("&Always on top"), this);
auto stay_awake_toggle = new QAction(tr("&Stay awake"), this);
auto timer_toggle = new QAction(tr("&Timer"), this);
auto window_theme_toggle = new QAction(tr("&Window theme"), this);
Expand All @@ -402,7 +404,9 @@ void MainWindow::makeToggleMenu()
UserData::saveConfig(UserData::IniGroup::Data, UserData::IniValue::ToggleLoadMostRecent, checked); // move to story?
});
connect(cursor_blink_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::CursorBlink); });
connect(cursor_block_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::BlockCursor); });
connect(cursor_block_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::CursorBlock); });
connect(cursor_center_on_scroll_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::CursorCenterOnScroll); });
connect(cursor_typewriter_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::CursorTypewriter); });
connect(current_line_highlight_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::LineHighlight); });
connect(editor_shadow_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::Shadow); });
connect(editor_theme_toggle, &QAction::toggled, this, [&](bool checked) { editor->toggle(checked, Editor::Has::Theme); });
Expand Down Expand Up @@ -436,6 +440,8 @@ void MainWindow::makeToggleMenu()
load_most_recent_toggle,
cursor_blink_toggle,
cursor_block_toggle,
cursor_center_on_scroll_toggle,
cursor_typewriter_toggle,
current_line_highlight_toggle,
editor_shadow_toggle,
editor_theme_toggle,
Expand All @@ -456,6 +462,8 @@ void MainWindow::makeToggleMenu()
loadMenuToggle(load_most_recent_toggle, UserData::IniGroup::Data, UserData::IniValue::ToggleLoadMostRecent, false);
loadMenuToggle(cursor_blink_toggle, UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorBlink, true);
loadMenuToggle(cursor_block_toggle, UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorBlock, true);
loadMenuToggle(cursor_center_on_scroll_toggle, UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorCenterOnScroll, false);
loadMenuToggle(cursor_typewriter_toggle, UserData::IniGroup::Editor, UserData::IniValue::ToggleCursorTypewriter, false);
loadMenuToggle(current_line_highlight_toggle, UserData::IniGroup::Editor, UserData::IniValue::ToggleLineHighlight, true);
loadMenuToggle(editor_shadow_toggle, UserData::IniGroup::Editor, UserData::IniValue::ToggleEditorShadow, true);
loadMenuToggle(editor_theme_toggle, UserData::IniGroup::Editor, UserData::IniValue::ToggleEditorTheme, true);
Expand All @@ -475,7 +483,7 @@ void MainWindow::makeToggleMenu()
toggle->addAction(load_most_recent_toggle);
toggle->addSeparator();
auto cursor = toggle->addMenu(tr("&Cursor"));
for (const auto& action : { cursor_blink_toggle, cursor_block_toggle })
for (const auto& action : { cursor_blink_toggle, cursor_block_toggle, cursor_center_on_scroll_toggle, cursor_typewriter_toggle })
cursor->addAction(action);
for (const auto& action : { current_line_highlight_toggle, editor_shadow_toggle, editor_theme_toggle, key_filter_toggle, line_number_area_toggle, scrolls_previous_next_toggle })
toggle->addAction(action);
Expand Down
12 changes: 10 additions & 2 deletions Fernanda/source/PlainTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void PlainTextEdit::paintEvent(QPaintEvent* event)
auto current_char = currentChar();
auto rect = reshapeCursor(current_char);
painter.fillRect(rect, recolorCursor());
if (!current_char.isNull() && askHasBlockCursor())
if (!current_char.isNull() && askHasCursorBlock())
{
painter.setPen(recolorCursor(true));
painter.drawText(rect, current_char);
Expand Down Expand Up @@ -315,6 +315,8 @@ 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(this, &PlainTextEdit::cursorPositionChanged, this, &PlainTextEdit::typewriter);
connect(this, &PlainTextEdit::textChanged, this, &PlainTextEdit::typewriter);
connect(verticalScrollBar(), &QScrollBar::rangeChanged, this, &PlainTextEdit::scrollButtonEnabledHandler);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &PlainTextEdit::scrollButtonEnabledHandler);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, [&]() { sendBlockNumber(firstVisibleBlock().blockNumber()); });
Expand All @@ -334,7 +336,7 @@ void PlainTextEdit::connections()

const QRect PlainTextEdit::reshapeCursor(QChar currentChar)
{
if (askHasBlockCursor())
if (askHasCursorBlock())
{
QFontMetrics metrics(font());
currentChar.isNull()
Expand Down Expand Up @@ -386,4 +388,10 @@ void PlainTextEdit::updateLineNumberArea(const QRect& rect, int dy)
updateLineNumberAreaWidth(0);
}

void PlainTextEdit::typewriter()
{
if (!askHasCursorTypewriter()) return;
centerCursor();
}

// PlainTextEdit.cpp, Fernanda
7 changes: 5 additions & 2 deletions Fernanda/source/PlainTextEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public slots:
void toggleScrolls(bool checked);
void toggleExtraScrolls(bool checked);

void toggleCenterOnScroll(bool checked) { setCenterOnScroll(checked); }

protected:
void resizeEvent(QResizeEvent* event) override;
void paintEvent(QPaintEvent* event) override;
Expand Down Expand Up @@ -103,6 +105,7 @@ public slots:
private slots:
void scrollButtonEnabledHandler();
void updateLineNumberArea(const QRect& rect, int dy);
void typewriter();

void updateLineNumberAreaWidth(int newBlockCount) { setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); }

Expand All @@ -111,8 +114,9 @@ private slots:
void askFontSliderZoom(Zoom direction);
void askGoNext();
void askGoPrevious();
bool askHasBlockCursor();
bool askHasCursorBlink();
bool askHasCursorBlock();
bool askHasCursorTypewriter();
bool askHasKeyFilter();
bool askHasLineHighlight();
bool askHasProject();
Expand All @@ -122,7 +126,6 @@ private slots:

class LineNumberArea : public QWidget
{

public:
LineNumberArea(PlainTextEdit* parent) : QWidget(parent), parent(parent) {}

Expand Down
39 changes: 17 additions & 22 deletions Fernanda/source/Splitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ SplitterHandle* Splitter::createHandle()
{
auto handle = new SplitterHandle(orientation(), this);
connect(handle, &SplitterHandle::askHoverExpand, this, &Splitter::hoverExpand);
connect(handle, &SplitterHandle::askIsInitialized, this, &Splitter::initialize);
connect(handle, &SplitterHandle::askStoreWidths, this, &Splitter::storeWidths);
connect(handle, &SplitterHandle::askToggleExpansion, this, &Splitter::toggleExpansion);
connect(handle, &SplitterHandle::askUnhoverAll, this, &Splitter::unhoverAll);
Expand Down Expand Up @@ -147,6 +146,16 @@ bool Splitter::eventFilter(QObject* watched, QEvent* event)
return false;
}

void Splitter::initialize()
{
for (auto& widget_info : widgets)
{
if (widget_info.width) continue;
widget_info.state = State::Collapsed;
isInitialized = true;
}
}

void Splitter::checkStates(int position, int index)
{
for (auto& widget_info : widgets)
Expand All @@ -157,11 +166,11 @@ void Splitter::checkStates(int position, int index)
auto& widget_state = widget_info.state;
(handle_index < 2)
? (position != 0)
? widget_state = State::Expanded
: widget_state = State::Collapsed
? widget_state = State::Expanded
: widget_state = State::Collapsed
: (position != (askWindowSize().width() - handleWidth()))
? widget_state = State::Expanded
: widget_state = State::Collapsed;
? widget_state = State::Expanded
: widget_state = State::Collapsed;
}
}

Expand All @@ -175,24 +184,10 @@ void Splitter::hoverExpand(SplitterHandle* handlePtr)
}
}

void Splitter::initialize()
{
if (isInitialized)
{
storeWidths();
return;
}
for (auto& widget_info : widgets)
{
if (widget_info.width) continue;
widget_info.state = State::Collapsed;
}
storeWidths();
isInitialized = true;
}

void Splitter::storeWidths()
{
if (!isInitialized)
initialize();
for (auto& widget_info : widgets)
{
if (!isExpanded(widget_info)) continue;
Expand Down Expand Up @@ -220,7 +215,7 @@ void Splitter::unhoverAll()
if (!isHoverExpanded(widget_info)) continue;
QTimer::singleShot(250, this, [&]()
{
if (!widget(widget_info.index)->underMouse() && !handle(widget_info.handleIndex)->underMouse())
if (!hasHover(widget_info))
collapse(widget_info);
});
}
Expand Down
3 changes: 2 additions & 1 deletion Fernanda/source/Splitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ class Splitter : public QSplitter
void expand(Info& widgetInfo, bool isHover = false);
void uncollapseAll();
bool eventFilter(QObject* watched, QEvent* event);
void initialize();

int toWindowX(int index, int size) { return (index < 2) ? size : askWindowSize().width() - size; }
bool match(SplitterHandle* handlePtr, Info& widgetInfo) const { return (handlePtr == handle(widgetInfo.handleIndex)); }
bool isCollapsed(Info& widgetInfo) const { return (widgetInfo.state == State::Collapsed); }
bool isExpanded(Info& widgetInfo) const { return (widgetInfo.state == State::Expanded); }
bool isHoverExpanded(Info& widgetInfo) const { return (widgetInfo.state == State::HoverExpanded); }
bool hasHover(Info& widgetInfo) const { return (widget(widgetInfo.index)->underMouse() || handle(widgetInfo.handleIndex)->underMouse()); }


private slots:
void checkStates(int position, int index);
void hoverExpand(SplitterHandle* handlePtr);
void initialize();
void storeWidths();
void toggleExpansion(SplitterHandle* handlePtr);
void unhoverAll();
Expand Down
5 changes: 2 additions & 3 deletions Fernanda/source/SplitterHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SplitterHandle : public QSplitterHandle
expanding->setDuration(100);
expanding->setEasingCurve(QEasingCurve::OutQuad);
expanding->setStartValue(splitter()->handleWidth());
expanding->setEndValue(splitter()->handleWidth() * 1.5);
expanding->setEndValue(splitter()->handleWidth() * 1.6);
connect(hoverTrigger, &QTimer::timeout, this, [&]()
{
askHoverExpand(this);
Expand Down Expand Up @@ -62,7 +62,7 @@ class SplitterHandle : public QSplitterHandle
result = true;
break;
case QEvent::MouseButtonRelease:
askIsInitialized();
askStoreWidths();
result = true;
break;
case QEvent::MouseButtonDblClick:
Expand All @@ -78,7 +78,6 @@ class SplitterHandle : public QSplitterHandle

signals:
void askHoverExpand(SplitterHandle* handlePtr);
void askIsInitialized();
void askStoreWidths();
void askToggleExpansion(SplitterHandle* handlePtr);
void askUnhoverAll();
Expand Down
6 changes: 6 additions & 0 deletions Fernanda/source/UserData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ const QString UserData::valueName(IniValue valueType)
case IniValue::ToggleCursorBlock:
result = "toggle__cursor_block";
break;
case IniValue::ToggleCursorCenterOnScroll:
result = "toggle__cursor_center_on_scroll";
break;
case IniValue::ToggleCursorTypewriter:
result = "toggle__cursor_typewriter";
break;
case IniValue::ToggleEditorShadow:
result = "toggle__editor_shadow";
break;
Expand Down
2 changes: 2 additions & 0 deletions Fernanda/source/UserData.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ namespace UserData
ToggleColorBar,
ToggleCursorBlink,
ToggleCursorBlock,
ToggleCursorCenterOnScroll,
ToggleCursorTypewriter,
ToggleEditorShadow,
ToggleEditorTheme,
ToggleIndicator,
Expand Down
4 changes: 2 additions & 2 deletions Fernanda/source/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

#pragma once

#define VER_FILEVERSION 0,24,4,53
#define VER_FILEVERSION_STR "v0.24.4-beta53"
#define VER_FILEVERSION 0,25,0,54
#define VER_FILEVERSION_STR "v0.25.0-beta54"
#define VER_PRODUCTVERSION VER_FILEVERSION
#define VER_PRODUCTVERSION_STR VER_FILEVERSION_STR
#define VER_COMPANYNAME_STR "fairybow"
Expand Down
Loading

0 comments on commit 47068f4

Please sign in to comment.