// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL import QtQuick import QtQuick.Controls as QQC2 import QtQuick.Layouts import org.kde.kirigami as Kirigami import org.kde.kirigamiaddons.formcard as FormCard import koutnet.app // The themes tab: a tile for every picture in the themes folder, and a button to // open the folder, because a list of pictures is a list of files and a person // looking at one wants to know where to put the next. // // Everything it shows comes from ThemeLibrary, which reads the folder, and the // settings hold nothing here but the path of the picture chosen - the same // setting a wallpaper picked by hand writes, so a theme and a wallpaper are one // thing to the window and the tab cannot show one and use the other. // // The tab does not write that setting itself: it says which picture was picked // and SettingsPage puts it where it keeps everything else. QQC2.ScrollView { id: root contentWidth: availableWidth // The pictures as ThemeLibrary read them off the folder: { name, label, // path, url }. Held as a property rather than read where it is used, so the // gallery can be shown a list of its own - which is how the test drives it // without a folder of photographs to read. property var themes: ThemeLibrary.themes // The folder they came from, which is shown at the foot of the grid and is // the one piece of information a person needs to add a theme of their own. property string directory: ThemeLibrary.directory // The path of the picture in use, empty when there is none. The check on // the tile is this, and nothing else: the picture in use is one setting, // and a second copy of it in this tab is a second answer to the question. property string selectedPath: "" // The width the cards and the heading are drawn at, handed in by the page // rather than worked out here: this is a page of settings, and a card that // grew its own idea of how wide it should be is a card that does not line up // with the ones on the tabs next door. Zero means the page has not said, and // the gallery then fills the window - which is the right answer in a window // narrow enough that the page's own width is the width. property real kFormWidth: 0 signal chosen(string path) // How close together the tiles sit, and how wide a tile wants to be at the // least. Ten grid units is about as wide as a name stays legible under a // picture, and narrow enough that a window with the conversation list // beside it still gets three or four of them to a row. readonly property real kSpacing: Kirigami.Units.largeSpacing readonly property real kMinTileWidth: Kirigami.Units.gridUnit * 10 readonly property real cardWidth: root.kFormWidth > 0 ? root.kFormWidth : root.width ColumnLayout { width: root.availableWidth spacing: 0 ThemedFormHeader { maximumWidth: root.cardWidth title: i18nc("@title:settings section", "Themes") } ThemedFormCard { maximumWidth: root.cardWidth ColumnLayout { Layout.fillWidth: true spacing: Kirigami.Units.largeSpacing * 2 // Not a layout. A layout measures its own width off the widths // of the children it is told to size, and here those widths are // worked out of the width of the layout, so the two take each // other's measure and the grid settles at the width of a single // tile - a card with one thumbnail in the middle of it, and the // same in every window. The tiles are put where they go by hand // here, and the row is then the width the card gave the grid. Item { id: grid Layout.fillWidth: true // Measured off its own width, which is the width the card // gives it: the tiles are as wide as the card is, and a // count worked out from the window instead would leave the // last row hanging over the edge of the card in a wide // window and squashing the tiles in a narrow one. readonly property int tileCount: Math.max(1, Math.floor((width + root.kSpacing) / (root.kMinTileWidth + root.kSpacing))) // As many tiles to a row as fit at their narrowest, and each // one then taking up what is left over, so that a row ends at // the edge of the card instead of stopping short of it or // hanging one tile over. Hence the count first and the width // from it: a width first and a count from that leaves tiles // that do not divide the row. readonly property real tileWidth: Math.max(root.kMinTileWidth, (width - (tileCount - 1) * root.kSpacing) / tileCount) // A row is as tall as the tile in it, and a tile is as tall // as its picture and the name under it, so the height of // that tile is the one place the figure is said. readonly property real tileHeight: noneTile.height // Twice the gap between the tiles: the caption sits in a // row's own height, so the rows are further apart than the // tiles in them are. readonly property real rowSpacing: root.kSpacing * 2 // The tile with no picture is a tile in the first row, so it // is counted among the rows and the themes begin at the // second slot of the first one. readonly property int rowCount: Math.max(1, Math.ceil((themeRow.count + 1) / tileCount)) implicitHeight: rowCount * (tileHeight + rowSpacing) - rowSpacing // The way back to a window with no picture behind it, first // in the grid: it is the choice that is not a theme, and a // row of pictures with nothing at the start of it is a row // of pictures that all point one way. ThemeTile { id: noneTile objectName: "tile-none" x: 0 y: 0 width: grid.tileWidth label: i18nc("@label:checkbox no wallpaper", "No picture") selected: root.selectedPath.length === 0 onClicked: root.chosen("") } Repeater { id: themeRow model: root.themes delegate: ThemeTile { required property var modelData required property int index // The grid numbers its tiles the way it lays them // out, and the first of those is the one with no // picture, so a theme sits one past its own index // and is named after the slot it lands in. readonly property int slot: index + 1 objectName: "tile-" + slot width: grid.tileWidth x: (slot % grid.tileCount) * (grid.tileWidth + root.kSpacing) y: Math.floor(slot / grid.tileCount) * (grid.tileHeight + grid.rowSpacing) sourceUrl: modelData.url label: modelData.label // Compared as paths, and through FsUtils because the // setting holds whatever the wallpaper file dialog // last put there - a file:// URL from the dialog, a // bare path from here - and the two name the same // picture and would otherwise be told apart by // nothing but the punctuation. selected: FsUtils.toLocalPath(root.selectedPath) === modelData.path onClicked: root.chosen(modelData.path) } } } // Nothing in the folder. Said as a folder and not as a shrug, // because the one thing to do about it is to put a picture // there, and the button below opens exactly that. Kirigami.PlaceholderMessage { Layout.fillWidth: true visible: root.themes.length === 0 text: i18nc("@title:settings section", "No themes") explanation: i18nc("@info:whatsthis no themes in the folder", "There are no pictures in the themes folder yet. Put one there and it appears here, named after the file.") } // The folder, and the two buttons that are about it: a folder is // somewhere a person can put a picture, so it is worth showing // and worth opening, and worth reading again afterwards for a // picture that was put there while this tab was open. RowLayout { Layout.fillWidth: true spacing: Kirigami.Units.largeSpacing QQC2.Label { Layout.fillWidth: true text: root.themes.length === 0 ? i18nc("@info:status no themes yet", "No themes in %1", root.directory) : i18nc("@info:status %1 themes, in %2", "%1 themes in %2", root.themes.length, root.directory) // The folder is the long part of that line and the count // is the part being read, so the tail is what gives way // when the window is narrow. elide: Text.ElideMiddle color: AppPalette.disabledText horizontalAlignment: Text.AlignLeft verticalAlignment: Text.AlignVCenter } QQC2.Button { text: i18nc("@action:button", "Reload") icon.name: "view-refresh" onClicked: ThemeLibrary.reload() } QQC2.Button { text: i18nc("@action:button", "Open folder") icon.name: "document-open-folder" // A folder path is not a URL: a space in a folder name // has to be escaped before the desktop will take it, and // FsUtils is the helper that does the escaping the // dialogs here do it with. onClicked: Qt.openUrlExternally(FsUtils.toSourceUrl(root.directory)) } } } } } }