Kirigami-addons

ConfigWindow.qml
1// SPDX-FileCopyrightText: 2020 Tobias Fella <fella@posteo.de>
2// SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
3// SPDX-FileCopyrightText: 2021 Felipe Kinoshita <kinofhek@gmail.com>
4// SPDX-FileCopyrightText: 2021 Marco Martin <mart@kde.org>
5// SPDX-License-Identifier: LGPL-2.0-or-later
6
7import QtQml
8import QtQuick
9import QtQuick.Controls as QQC2
10import QtQuick.Layouts
11import org.kde.kirigami as Kirigami
12import org.kde.kirigamiaddons.delegates as Delegates
13import org.kde.kirigamiaddons.settings
14
15Kirigami.ApplicationWindow {
16 id: root
17
18 required property string defaultModule
19 required property list<ConfigurationModule> modules
20
21 // Do not use Map, it crashes very frequently
22 property var pageCache: Object.create(null)
23
24 pageStack {
25 columnView.columnWidth: Kirigami.Units.gridUnit * 13
26
27 popHiddenPages: true
28
29 globalToolBar {
30 style: Kirigami.ApplicationHeaderStyle.ToolBar
31 showNavigationButtons: root.pageStack.depth > 1 ? Kirigami.ApplicationHeaderStyle.ShowBackButton : Kirigami.ApplicationHeaderStyle.NoNavigationButtons
32 }
33 }
34
35 contentItem.Keys.onEscapePressed: root.close()
36
37 globalDrawer: Kirigami.OverlayDrawer {
38 id: drawer
39 edge: Qt.application.layoutDirection === Qt.RightToLeft ? Qt.RightEdge : Qt.LeftEdge
40
41 modal: false
42 Kirigami.OverlayZStacking.layer: Kirigami.OverlayZStacking.Drawer
43 z: Kirigami.OverlayZStacking.z
44 drawerOpen: true
45 width: Kirigami.Units.gridUnit * 13
46 Kirigami.Theme.colorSet: Kirigami.Theme.View
47 Kirigami.Theme.inherit: false
48
49 leftPadding: 0
50 rightPadding: 0
51 topPadding: 0
52 bottomPadding: 0
53
54 contentItem: ColumnLayout {
55 spacing: 0
56
57 QQC2.ToolBar {
58 Layout.fillWidth: true
59 Layout.preferredHeight: pageStack.globalToolBar.preferredHeight
60
61 leftPadding: 3
62 rightPadding: 3
63 topPadding: 3
64 bottomPadding: 3
65
66 visible: !Kirigami.Settings.isMobile
67
68 contentItem: Kirigami.SearchField {
69 Layout.fillWidth: true
70 onTextChanged: listview.filterText = text.toLowerCase();
71 }
72 }
73
74 QQC2.ScrollView {
75 Layout.fillWidth: true
76 Layout.fillHeight: true
77
78 ListView {
79 id: listview
80
81 property string filterText: ""
82 property bool initDone: false
83
84 currentIndex: -1
85
86 onWidthChanged: if (!initDone) {
87 let module = getModuleByName(defaultModule);
88 if (module) {
89 root.pageStack.push(pageForModule(module));
90 listview.currentIndex = modules.findIndex(module => module.moduleId == defaultModule);
91 } else {
92 root.pageStack.push(pageForModule(modules[0]));
93 listview.currentIndex = 0;
94 if (root.pageStack.currentItem.title.length === 0) {
95 root.pageStack.currentItem.title = modules[0].text;
96 }
97 }
98 initDone = true;
99 }
100 model: {
101 const isFiltering = filterText.length !== 0;
102 let filteredModules = [];
103 for (const module of root.modules) {
104 const modulePassesFilter = module.text.toLowerCase().includes(filterText);
105 if (module.visible && (isFiltering ? modulePassesFilter : true)) {
106 filteredModules.push(module);
107 }
108 }
109 return filteredModules;
110 }
111 delegate: Delegates.RoundedItemDelegate {
112 id: settingDelegate
113
114 required property int index
115 required property ConfigurationModule modelData
116
117 text: modelData.text
118 icon.name: modelData.icon.name
119 icon.source: modelData.icon.source
120 checked: ListView.view.currentIndex === settingDelegate.index
121
122 onClicked: {
123 const page = pageForModule(modelData);
124 if (ListView.view.currentIndex === settingDelegate.index) {
125 return;
126 }
127 root.pageStack.replace(page);
128
129 ListView.view.currentIndex = settingDelegate.index;
130 if (root.pageStack.currentItem.title.length === 0) {
131 root.pageStack.currentItem.title = text;
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139
140 function pageForModule(module: ConfigurationModule): var {
141 if (pageCache[module.moduleId]) {
142 return pageCache[module.moduleId];
143 } else {
144 const component = module.page();
145 if (!component) {
146 console.error("Unable to create component for moduleId", module.moduleId);
147 return null;
148 }
149
150 if (component.status === Component.Error) {
151 console.error(`Unable to create component for moduleId: "${module.moduleId}". Error: ${component.errorString()}`);
152 return null;
153 }
154
155 const page = component.createObject(root, module.initialProperties());
156 if (page.title.length === 0) {
157 page.title = module.text;
158 }
159 pageCache[module.moduleId] = page;
160 return page;
161 }
162 }
163
164 function getModuleByName(moduleId: string): ConfigurationModule {
165 return modules.find(module => module.moduleId == moduleId) ?? null;
166 }
167}
This object holds the information of configuration module.
string text
This property holds the name of the module.
KGuiItem find()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.