Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show link on hover #634

Merged
merged 8 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ReText/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
'restDirectives': {'light': '#800080', 'dark': '#d070d0'},
'restRoles': {'light': '#800000', 'dark': '#d07070'},
'whitespaceOnEnd': {'light': '#80e1e1a5', 'dark': '#8096966e'},
# Preview
'urlPopupArea': {'light': '#fafafafa', 'dark': '#fa323232'},
'urlPopupBorder': {'light': '#64323232', 'dark': '#64fafafa'},
}

colorValues = {}
Expand Down
45 changes: 44 additions & 1 deletion ReText/webenginepreview.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from ReText import globalSettings
from ReText.editor import getColor
from ReText.syncscroll import SyncScroll
from PyQt6.QtCore import QEvent, Qt
from PyQt6.QtGui import QDesktopServices, QFontInfo, QGuiApplication, QTextDocument
from PyQt6.QtGui import QDesktopServices, QFontInfo, QGuiApplication, QTextDocument, QColor
from PyQt6.QtWidgets import QLabel
from PyQt6.QtWebEngineCore import (
QWebEnginePage,
QWebEngineSettings,
Expand All @@ -37,12 +39,53 @@ def interceptRequest(self, info):
info.block(True)


def str_rgba(color: QColor):
""" Todo: More elegant use of QColor with alpha in stylesheet """
return "rgba({r}, {g}, {b}, {a})".format(
r = color.red(),
g = color.green(),
b = color.blue(),
a = color.alpha())

class UrlPopup(QLabel):
def __init__(self, window):
super().__init__(window)
self.window = window

self.setStyleSheet('''
border: 1px solid {:s};
border-radius: 3px;
background: {:s};
'''.format(str_rgba(getColor('urlPopupBorder')),
str_rgba(getColor('urlPopupArea'))))
self.fontHeight = self.fontMetrics().height()
self.setVisible(False)

def pop(self, url: str):
""" Show link target on mouse hover

QWebEnginePage emits signal 'linkHovered' which provides
url: str -- target of link hovered (or empty on mouse-release)
"""
if url:
self.setText(url)
windowBottom = self.window.rect().bottom()
textWidth = self.fontMetrics().horizontalAdvance(url)
self.setGeometry(-2, windowBottom-self.fontHeight-6,
textWidth+10, self.fontHeight+10)
self.setVisible(True)
else:
self.setVisible(False)


class ReTextWebEnginePage(QWebEnginePage):
def __init__(self, parent, tab):
QWebEnginePage.__init__(self, parent)
self.tab = tab
self.interceptor = ReTextWebEngineUrlRequestInterceptor(self)
self.setUrlRequestInterceptor(self.interceptor)
self.urlPopup = UrlPopup(self.tab.p)
self.linkHovered.connect(self.urlPopup.pop)

def setScrollPosition(self, pos):
self.runJavaScript("window.scrollTo(%s, %s);" % (pos.x(), pos.y()))
Expand Down