Skip to content
Snippets Groups Projects
Commit f808d426 authored by Andreas Traczyk's avatar Andreas Traczyk Committed by Sébastien Blin
Browse files

misc: move viewmanager into a separate file

Change-Id: Id5659ede0fc870926b4b627a5bbe12c67d4f9224
parent 759e3ea3
No related branches found
No related tags found
No related merge requests found
......@@ -51,6 +51,7 @@ ApplicationWindow {
property LayoutManager layoutManager: LayoutManager {
appContainer: appContainer
}
property ViewManager viewManager: ViewManager {}
property ViewCoordinator viewCoordinator: ViewCoordinator {
resources: {
"WelcomePage": "mainview/components/WelcomePage.qml",
......@@ -60,6 +61,7 @@ ApplicationWindow {
"WizardView": "wizardview/WizardView.qml",
"SettingsView": "settingsview/SettingsView.qml",
}
viewManager: root.viewManager
}
property bool windowSettingsLoaded: false
......
......@@ -29,6 +29,8 @@ import net.jami.Models 1.1
QtObject {
id: root
required property QtObject viewManager
signal requestAppWindowWizardView
// A map of view names to file paths for QML files that define each view.
......@@ -139,76 +141,6 @@ QtObject {
return viewManager.views[viewManager.viewPaths[viewName]]
}
// A private object used to manage the lifecycle of views.
property QtObject viewManager: QtObject {
id: viewManager
// A map of path strings to view objects.
property variant views: ({})
// A map of view names to path strings.
property variant viewPaths: ({})
// The number of views.
property int nViews: 0
function createView(path, parent=null, cb=null, props={}) {
if (views[path] !== undefined) {
// an instance of <path> already exists
return views[path]
}
const component = Qt.createComponent(Qt.resolvedUrl(path))
if (component.status === Component.Ready) {
const obj = component.createObject(parent, props)
if (obj === null) {
print("error creating object")
return null
}
views[path] = obj
// Set the view name to the object name if it has one.
const viewName = obj.objectName.toString() !== '' ?
obj.objectName :
path.replace(/^.*[\\\/]/, '').replace(/\.[^/.]+$/, "")
viewPaths[viewName] = path
nViews = Object.keys(views).length
if (cb !== null) {
cb(obj)
}
return views[path]
}
print("error creating component", path)
console.error(component.errorString())
Qt.exit(1)
return null
}
function destroyView(path) {
if (views[path] === undefined) {
print(path, "instance does not exist", Object.keys(views))
return false
}
views[path].destroy()
views[path] = undefined
// QObject::destroy is queued, and we can't connect to its completion,
// so we queue the resulting mutation to our view storage.
Qt.callLater(function() {
delete views[path]
// Remove the view name from the viewPaths map.
for (var viewName in viewPaths) {
if (viewPaths[viewName] === path) {
delete viewPaths[viewName]
break
}
}
nViews = Object.keys(views).length
})
return true
}
function hasView(viewName) {
return nViews && viewPaths[viewName] !== undefined
}
}
// Sets object references, onInitialized is a good time to present views.
function setRootView(obj) {
rootView = obj
......
/*
* Copyright (C) 2023 Savoir-faire Linux Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import QtQuick
QtObject {
id: viewManager
// A map of path strings to view objects.
property variant views: ({})
// A map of view names to path strings.
property variant viewPaths: ({})
// The number of views.
property int nViews: 0
function createView(path, parent=null, cb=null, props={}) {
if (views[path] !== undefined) {
// an instance of <path> already exists
return views[path]
}
const component = Qt.createComponent(Qt.resolvedUrl(path))
if (component.status === Component.Ready) {
const obj = component.createObject(parent, props)
if (obj === null) {
print("error creating object")
return null
}
views[path] = obj
// Set the view name to the object name if it has one.
const viewName = obj.objectName.toString() !== '' ?
obj.objectName :
path.replace(/^.*[\\\/]/, '').replace(/\.[^/.]+$/, "")
viewPaths[viewName] = path
nViews = Object.keys(views).length
if (cb !== null) {
cb(obj)
}
return views[path]
}
print("error creating component", path)
console.error(component.errorString())
Qt.exit(1)
return null
}
function destroyView(path) {
if (views[path] === undefined) {
print(path, "instance does not exist", Object.keys(views))
return false
}
views[path].destroy()
views[path] = undefined
// QObject::destroy is queued, and we can't connect to its completion,
// so we queue the resulting mutation to our view storage.
Qt.callLater(function() {
delete views[path]
// Remove the view name from the viewPaths map.
for (var viewName in viewPaths) {
if (viewPaths[viewName] === path) {
delete viewPaths[viewName]
break
}
}
nViews = Object.keys(views).length
})
return true
}
function hasView(viewName) {
return nViews && viewPaths[viewName] !== undefined
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment