"""Common web components."""

from __future__ import annotations

import typing

from qtpy.QtGui import QDesktopServices
from qtpy.QtWebEngineWidgets import QWebEnginePage

if typing.TYPE_CHECKING:
    pass


class ExternalBrowserNavigationPage(QWebEnginePage):
    """Custom :class:`~QWebEnginePage` to open links in default browser."""

    def acceptNavigationRequest(self, url, _type, isMainFrame):  # pylint: disable=invalid-name
        """Open links in external browser."""
        if _type == QWebEnginePage.NavigationTypeLinkClicked:
            QDesktopServices.openUrl(url)
            return False
        return super().acceptNavigationRequest(url, _type, isMainFrame)
