Warning, /network/neochat/src/qml/LinkPreviewDelegate.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2022 Bharadwaj Raju <bharadwaj.raju777@protonmail.com>
0002 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-or-later OR LicenseRef-KDE-Accepted-GPL
0004 
0005 import QtQuick
0006 import QtQuick.Controls as QQC2
0007 import QtQuick.Layouts
0008 
0009 import org.kde.kirigami as Kirigami
0010 
0011 import org.kde.neochat
0012 
0013 Loader {
0014     id: root
0015 
0016     /**
0017      * @brief The link preview properties.
0018      *
0019      * This is a list or object containing the following:
0020      *  - url - The URL being previewed.
0021      *  - loaded - Whether the URL preview has been loaded.
0022      *  - title - the title of the URL preview.
0023      *  - description - the description of the URL preview.
0024      *  - imageSource - a source URL for the preview image.
0025      */
0026     required property var linkPreviewer
0027 
0028     /**
0029      * @brief Standard height for the link preview.
0030      *
0031      * When the content of the link preview is larger than this it will be
0032      * elided/hidden until maximized.
0033      */
0034     property var defaultHeight: Kirigami.Units.gridUnit * 3 + Kirigami.Units.smallSpacing * 2
0035 
0036     /**
0037      * @brief Whether the loading indicator should animate if visible.
0038      */
0039     property bool indicatorEnabled: false
0040 
0041     visible: active
0042     sourceComponent: linkPreviewer && linkPreviewer.loaded ? linkPreviewComponent : loadingComponent
0043 
0044     Component {
0045         id: linkPreviewComponent
0046         QQC2.Control {
0047             id: componentRoot
0048             property bool truncated: linkPreviewDescription.truncated || !linkPreviewDescription.visible
0049 
0050             leftPadding: 0
0051             rightPadding: 0
0052             topPadding: 0
0053             bottomPadding: 0
0054 
0055             contentItem: RowLayout {
0056                 spacing: Kirigami.Units.smallSpacing
0057 
0058                 Rectangle {
0059                     Layout.fillHeight: true
0060                     width: Kirigami.Units.smallSpacing
0061                     color: Kirigami.Theme.highlightColor
0062                 }
0063                 Image {
0064                     visible: root.linkPreviewer.imageSource
0065                     Layout.maximumHeight: root.defaultHeight
0066                     Layout.maximumWidth: root.defaultHeight
0067                     source: root.linkPreviewer.imageSource
0068                     fillMode: Image.PreserveAspectFit
0069                 }
0070                 ColumnLayout {
0071                     id: column
0072                     spacing: Kirigami.Units.smallSpacing
0073                     Kirigami.Heading {
0074                         id: linkPreviewTitle
0075                         Layout.fillWidth: true
0076                         level: 3
0077                         wrapMode: Text.Wrap
0078                         textFormat: Text.RichText
0079                         text: "<style>
0080                             a {
0081                                 text-decoration: none;
0082                             }
0083                             </style>
0084                             <a href=\"" + root.linkPreviewer.url + "\">" + (maximizeButton.checked ? root.linkPreviewer.title : titleTextMetrics.elidedText).replace("&ndash;", "—") + "</a>"
0085                         onLinkActivated: RoomManager.resolveResource(link, "join")
0086 
0087                         TextMetrics {
0088                             id: titleTextMetrics
0089                             text: root.linkPreviewer.title
0090                             font: linkPreviewTitle.font
0091                             elide: Text.ElideRight
0092                             elideWidth: (linkPreviewTitle.width - Kirigami.Units.largeSpacing * 2.5) * 3
0093                         }
0094                     }
0095                     QQC2.Label {
0096                         id: linkPreviewDescription
0097                         Layout.fillWidth: true
0098                         Layout.maximumHeight: maximizeButton.checked ? -1 : root.defaultHeight - linkPreviewTitle.height - column.spacing
0099                         visible: linkPreviewTitle.height + column.spacing <= root.defaultHeight || maximizeButton.checked
0100                         text: linkPreviewer.description
0101                         wrapMode: Text.Wrap
0102                         elide: Text.ElideRight
0103                     }
0104                 }
0105             }
0106 
0107             QQC2.Button {
0108                 id: maximizeButton
0109                 anchors.right: parent.right
0110                 anchors.bottom: parent.bottom
0111                 visible: componentRoot.hovered && (componentRoot.truncated || checked)
0112                 checkable: true
0113                 text: checked ? i18n("Shrink preview") : i18n("Expand preview")
0114                 icon.name: checked ? "go-up" : "go-down"
0115                 display: QQC2.AbstractButton.IconOnly
0116 
0117                 QQC2.ToolTip {
0118                     text: maximizeButton.text
0119                     visible: hovered
0120                     delay: Kirigami.Units.toolTipDelay
0121                 }
0122             }
0123         }
0124     }
0125 
0126     Component {
0127         id: loadingComponent
0128         RowLayout {
0129             id: componentRoot
0130             property bool truncated: false
0131 
0132             Rectangle {
0133                 Layout.fillHeight: true
0134                 width: Kirigami.Units.smallSpacing
0135                 color: Kirigami.Theme.highlightColor
0136             }
0137             QQC2.BusyIndicator {
0138                 running: root.indicatorEnabled
0139             }
0140             Kirigami.Heading {
0141                 Layout.fillWidth: true
0142                 Layout.minimumHeight: root.defaultHeight
0143                 level: 2
0144                 text: i18n("Loading URL preview")
0145             }
0146         }
0147     }
0148 }