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
Changes from 4 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
33 changes: 33 additions & 0 deletions ReText/webenginepreview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ReText.syncscroll import SyncScroll
from PyQt6.QtCore import QEvent, Qt
from PyQt6.QtGui import QDesktopServices, QFontInfo, QGuiApplication, QTextDocument
from PyQt6.QtWidgets import QLabel
from PyQt6.QtWebEngineCore import (
QWebEnginePage,
QWebEngineSettings,
Expand All @@ -37,12 +38,40 @@ def interceptRequest(self, info):
info.block(True)


class UrlPopup(QLabel):

def __init__(self, window):
super().__init__(window)
self.window = window

self.setStyleSheet('''
border: 1px solid #64323232;
border-radius: 3px;
background: #FAFAFAFA;
''')
self.fontHeight = self.fontMetrics().height()
self.setVisible(False)

def pop(self, url):
if url:
self.setText(url)
windowBottom = self.window.rect().bottom()
textWidth = self.fontMetrics().horizontalAdvance(url)
self.setGeometry(-2, windowBottom-self.fontHeight-5,
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.onLinkHover)
mitya57 marked this conversation as resolved.
Show resolved Hide resolved

def setScrollPosition(self, pos):
self.runJavaScript("window.scrollTo(%s, %s);" % (pos.x(), pos.y()))
Expand Down Expand Up @@ -86,6 +115,10 @@ def acceptNavigationRequest(self, url, type, isMainFrame):
QDesktopServices.openUrl(url)
return False

def onLinkHover(self, url):
""" Show link target on mouse hover """
self.urlPopup.pop(url)


class ReTextWebEnginePreview(QWebEngineView):

Expand Down