Ensure cursor is visible after switching focus mode (#1478)

This commit is contained in:
Veronica Berglyd Olsen
2023-11-18 15:43:51 +01:00
parent 17fc370609
commit 208b9334e1
3 changed files with 79 additions and 5 deletions
+29 -2
View File
@@ -48,8 +48,8 @@ from PyQt5.QtGui import (
)
from PyQt5.QtWidgets import (
QAction, QFrame, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMenu,
QPlainTextEdit, QPushButton, QShortcut, QToolBar, QToolButton, QVBoxLayout, QWidget,
qApp
QPlainTextEdit, QPushButton, QShortcut, QToolBar, QToolButton, QVBoxLayout,
QWidget, qApp
)
from novelwriter import CONFIG, SHARED
@@ -508,6 +508,33 @@ class GuiDocEditor(QPlainTextEdit):
return True
def cursorIsVisible(self) -> bool:
"""Check if the cursor is visible in the editor."""
return (
0 < self.cursorRect().top()
and self.cursorRect().bottom() < self.viewport().height()
)
def ensureCursorVisibleNoCentre(self) -> None:
"""Ensure cursor is visible, but don't force it to centre."""
cT = self.cursorRect().top()
cB = self.cursorRect().bottom()
vH = self.viewport().height()
if cT < 0:
count = 0
vBar = self.verticalScrollBar()
while self.cursorRect().top() < 0 and count < 100000:
vBar.setValue(vBar.value() - 1)
count += 1
elif cB > vH:
count = 0
vBar = self.verticalScrollBar()
while self.cursorRect().bottom() > vH and count < 100000:
vBar.setValue(vBar.value() + 1)
count += 1
qApp.processEvents()
return
def updateDocMargins(self) -> None:
"""Automatically adjust the margins so the text is centred if
we have a text width set or we're in Focus Mode. Otherwise, just