|
| 1 | +import QtQuick |
| 2 | + |
| 3 | +///! Rectangle capable of clipping content inside its border. |
| 4 | +/// > [!WARNING] This type requires at least Qt 6.7. |
| 5 | +/// |
| 6 | +/// This is a specialized version of @@QtQuick.Rectangle that clips content |
| 7 | +/// inside of its border, including rounded rectangles. It costs more than |
| 8 | +/// @@QtQuick.Rectangle, so it should not be used unless you need to clip |
| 9 | +/// items inside of it to the border. |
| 10 | +Item { |
| 11 | + id: root |
| 12 | + |
| 13 | + /// If content should be displayed underneath the border. |
| 14 | + /// |
| 15 | + /// Defaults to false, does nothing if the border is opaque. |
| 16 | + property bool contentUnderBorder: false; |
| 17 | + /// If the content item should be resized to fit inside the border. |
| 18 | + /// |
| 19 | + /// Defaults to `!contentUnderBorder`. Most useful when combined with |
| 20 | + /// `anchors.fill: parent` on an item passed to the ClippingRectangle. |
| 21 | + property bool contentInsideBorder: !root.contentUnderBorder; |
| 22 | + /// If the rectangle should be antialiased. |
| 23 | + /// |
| 24 | + /// Defaults to true if any corner has a non-zero radius, otherwise false. |
| 25 | + property /*bool*/alias antialiasing: rectangle.antialiasing; |
| 26 | + /// The background color of the rectangle, which goes under its content. |
| 27 | + property /*color*/alias color: shader.backgroundColor; |
| 28 | + /// See @@QtQuick.Rectangle.border. |
| 29 | + property clippingRectangleBorder border; |
| 30 | + /// Radius of all corners. Defaults to 0. |
| 31 | + property /*real*/alias radius: rectangle.radius |
| 32 | + /// Radius of the top left corner. Defaults to @@radius. |
| 33 | + property /*real*/alias topLeftRadius: rectangle.topLeftRadius |
| 34 | + /// Radius of the top right corner. Defaults to @@radius. |
| 35 | + property /*real*/alias topRightRadius: rectangle.topRightRadius |
| 36 | + /// Radius of the bottom left corner. Defaults to @@radius. |
| 37 | + property /*real*/alias bottomLeftRadius: rectangle.bottomLeftRadius |
| 38 | + /// Radius of the bottom right corner. Defaults to @@radius. |
| 39 | + property /*real*/alias borromRightRadius: rectangle.bottomRightRadius |
| 40 | + |
| 41 | + /// Visual children of the ClippingRectangle's @@contentItem. (`list<Item>`). |
| 42 | + /// |
| 43 | + /// See @@QtQuick.Item.children for details. |
| 44 | + default property alias children: contentItem.children; |
| 45 | + /// The item containing the rectangle's content. |
| 46 | + /// There is usually no reason to use this directly. |
| 47 | + readonly property alias contentItem: contentItem; |
| 48 | + |
| 49 | + Rectangle { |
| 50 | + id: rectangle |
| 51 | + anchors.fill: root |
| 52 | + color: "#ffff0000" |
| 53 | + border.color: "#ff00ff00" |
| 54 | + border.pixelAligned: root.border.pixelAligned |
| 55 | + border.width: root.border.width |
| 56 | + layer.enabled: true |
| 57 | + visible: false |
| 58 | + } |
| 59 | + |
| 60 | + Item { |
| 61 | + id: contentItemContainer |
| 62 | + anchors.fill: root |
| 63 | + layer.enabled: true |
| 64 | + visible: false |
| 65 | + |
| 66 | + Item { |
| 67 | + id: contentItem |
| 68 | + anchors.fill: parent |
| 69 | + anchors.margins: root.contentInsideBorder ? root.border.width : 0 |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + ShaderEffect { |
| 74 | + id: shader |
| 75 | + anchors.fill: root |
| 76 | + fragmentShader: `qrc:/Quickshell/Widgets/shaders/cliprect${root.contentUnderBorder ? "-ub" : ""}.frag.qsb` |
| 77 | + property Rectangle rect: rectangle; |
| 78 | + property color backgroundColor; |
| 79 | + property color borderColor: root.border.color; |
| 80 | + property Item content: contentItemContainer; |
| 81 | + } |
| 82 | +} |
0 commit comments