aboutsummaryrefslogtreecommitdiffstats
path: root/config/quickshell/qs-hyprview-trixie/layouts/JustifiedLayout.qml
blob: 655147b553675e57c9cd1f008aaaf62417f7f07f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
pragma Singleton
import Quickshell

Singleton {
    id: root

    function doLayout(windowList, outerWidth, outerHeight) {
        var N = windowList.length
        if (N === 0)
            return []

        var containerWidth  = outerWidth  * 0.9
        var containerHeight = outerHeight * 0.9

        // Gap: 0.8% of screen, clamped between 12px and 32px
        var rawGap = Math.min(outerWidth * 0.08, outerHeight * 0.08)
        var gap = Math.max(12, Math.min(32, rawGap))

        var maxThumbHeight = outerHeight * 0.3

        if (containerWidth <= 0 || containerHeight <= 0) {
            return windowList.map(function(item) {
                return {
                    win: item.win,
                    x: 0,
                    y: 0,
                    width: 0,
                    height: 0
                }
            })
        }

        var targetRowH = maxThumbHeight
        var rows = []
        var currentRow = []
        var sumAspect = 0

        function flushRow() {
            if (currentRow.length === 0)
                return

            var n = currentRow.length
            var rowHeight = maxThumbHeight
            if (sumAspect > 0) {
                var totalGapWidth = gap * (n - 1)
                var hFit = (containerWidth - totalGapWidth) / sumAspect
                if (hFit < rowHeight)
                    rowHeight = hFit
            }

            if (rowHeight > maxThumbHeight)
                rowHeight = maxThumbHeight
            if (rowHeight <= 0)
                rowHeight = 1

            rows.push({
                items: currentRow.slice(),
                height: rowHeight,
                sumAspect: sumAspect
            })

            currentRow = []
            sumAspect = 0
        }

        for (var i = 0; i < N; ++i) {
            var item = windowList[i]
            var w0 = item.width > 0 ? item.width : 1
            var h0 = item.height > 0 ? item.height : 1
            var a = w0 / h0
            item.aspect = a

            if (currentRow.length > 0 &&
                ((sumAspect + a) * targetRowH + gap * currentRow.length) > containerWidth) {
                flushRow()
            }

            currentRow.push(item)
            sumAspect += a
        }

        if (currentRow.length > 0) {
            flushRow()
        }

        var totalRawHeight = 0
        for (var r = 0; r < rows.length; ++r) {
            totalRawHeight += rows[r].height
        }
        if (rows.length > 1) {
            totalRawHeight += gap * (rows.length - 1)
        }

        var sV = 1.0
        var availH = containerHeight
        if (totalRawHeight > 0 && totalRawHeight > availH) {
            sV = availH / totalRawHeight
        }
        if (sV <= 0)
            sV = 0.1
        if (sV > 1.0)
            sV = 1.0

        var gridTotalHeightScaled = totalRawHeight * sV
        var yAcc = (outerHeight - gridTotalHeightScaled) / 2
        if (!isFinite(yAcc) || yAcc < 0)
            yAcc = 0

        var result = []

        for (var r2 = 0; r2 < rows.length; ++r2) {
            var row = rows[r2]
            var rowHeightScaled = row.height * sV

            var rowWidthNoGapsScaled = 0
            for (var j = 0; j < row.items.length; ++j) {
                rowWidthNoGapsScaled += row.items[j].aspect * rowHeightScaled
            }
            var totalRowWidthScaled = rowWidthNoGapsScaled + gap * (row.items.length - 1)

            var xAcc = (outerWidth - totalRowWidthScaled) / 2
            if (!isFinite(xAcc))
                xAcc = 0

            for (var j2 = 0; j2 < row.items.length; ++j2) {
                var it2 = row.items[j2]
                var wScaled = it2.aspect * rowHeightScaled
                var hScaled = rowHeightScaled

                result.push({
                    win: it2.win,
                    x: xAcc,
                    y: yAcc,
                    width: wScaled,
                    height: hScaled
                })

                xAcc += wScaled + gap
            }

            yAcc += rowHeightScaled
            if (r2 < rows.length - 1) {
                yAcc += gap * sV
            }
        }

        return result
    }
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage