Skip to content

Commit

Permalink
Show link on hover (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
red-kite authored Apr 9, 2024
1 parent 29abe3f commit 6941d00
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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

0 comments on commit 6941d00

Please sign in to comment.