Warning, /frameworks/kirigami/src/controls/AbstractApplicationItem.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 import QtQuick 2.12
0008 import QtQml 2.14
0009 import QtQuick.Templates 2.12 as T
0010 import QtQuick.Window 2.12
0011 import org.kde.kirigami 2.14 as Kirigami
0012 import "templates/private" as TP
0013 
0014 /**
0015  * @brief An item that provides the features of AbstractApplicationWindow without the window itself.
0016  *
0017  * This allows embedding into a larger application.
0018  * Unless you need extra flexibility it is recommended to use ApplicationItem instead.
0019  *
0020  * Example usage:
0021  * @code{.qml}
0022  * import org.kde.kirigami 2.4 as Kirigami
0023  *
0024  * Kirigami.AbstractApplicationItem {
0025  *  [...]
0026  *     globalDrawer: Kirigami.GlobalDrawer {
0027  *         actions: [
0028  *            Kirigami.Action {
0029  *                text: "View"
0030  *                icon.name: "view-list-icons"
0031  *                Kirigami.Action {
0032  *                        text: "action 1"
0033  *                }
0034  *                Kirigami.Action {
0035  *                        text: "action 2"
0036  *                }
0037  *                Kirigami.Action {
0038  *                        text: "action 3"
0039  *                }
0040  *            },
0041  *            Kirigami.Action {
0042  *                text: "Sync"
0043  *                icon.name: "folder-sync"
0044  *            }
0045  *         ]
0046  *     }
0047  *
0048  *     contextDrawer: Kirigami.ContextDrawer {
0049  *         id: contextDrawer
0050  *     }
0051  *
0052  *     pageStack: Kirigami.PageRow {
0053  *         ...
0054  *     }
0055  *  [...]
0056  * }
0057  * @endcode
0058  *
0059  * @inherit QtQuick.Item
0060  */
0061 Item {
0062     id: root
0063 
0064 //BEGIN properties
0065     /**
0066      * @brief This property holds the stack used to allocate the pages and to manage the
0067      * transitions between them.
0068      *
0069      * Put a container here, such as QtQuick.Controls.StackView or PageRow.
0070      */
0071     property Item pageStack
0072 
0073     /**
0074      * @brief This property exists for compatibility with Applicationwindow.
0075      *
0076      * default: ``Window.activeFocusItem``
0077      */
0078     readonly property Item activeFocusItem: Window.activeFocusItem
0079 
0080     /**
0081      * @brief This property holds the font for this item.
0082      *
0083      * default: ``Kirigami.Theme.defaultFont``
0084      */
0085     property font font: Kirigami.Theme.defaultFont
0086 
0087     /**
0088      * @brief This property holds the palette for this item.
0089      *
0090      * default: ``Kirigami.Theme.palette``
0091      */
0092     property var palette: Kirigami.Theme.palette
0093 
0094     /**
0095      * @brief This property holds the locale for this item.
0096      */
0097     property Locale locale
0098 
0099     /**
0100      * @brief This property holds an item that can be used as a menuBar for the application.
0101      * @warning This will be restricted to QQC2.MenuBar in KF6.
0102      */
0103     property Item menuBar // TODO KF6: restrict type to QQC2.MenuBar
0104 
0105     /**
0106     * @brief This property holds an item that can be used as a title for the application.
0107     *
0108     * Scrolling the main page will make it taller or shorter (through the point of going away).
0109     *
0110     * It's a behavior similar to the typical mobile web browser addressbar.
0111     *
0112     * The minimum, preferred and maximum heights of the item can be controlled with
0113     *
0114     * * ``Layout.minimumHeight``: default is 0, i.e. hidden
0115     * * ``Layout.preferredHeight``: default is Kirigami.Units.gridUnit * 1.6
0116     * * ``Layout.maximumHeight``: default is Kirigami.Units.gridUnit * 3
0117     *
0118     * To achieve a titlebar that stays completely fixed, just set the 3 sizes as the same.
0119     *
0120     * @warning This will be restricted to Kirigami.ApplicationHeader in KF6.
0121     * @property kirigami::ApplicationHeader header
0122     */
0123     property Item header // TODO KF6 restrict the type to Kirigami.ApplicationHeader
0124 
0125     /**
0126      * @brief This property holds an item that can be used as a footer for the application.
0127      */
0128     property Item footer
0129 
0130     /**
0131      * @brief This property sets whether the standard chrome of the app is visible.
0132      *
0133      * These are the action button, the drawer handles and the application header.
0134      *
0135      * default: ``true``
0136      */
0137     property bool controlsVisible: true
0138 
0139     /**
0140      * @brief This property holds the drawer for global actions.
0141      *
0142      * This drawer can be opened by sliding from the left screen edge
0143      * or by either pressing on the handle or sliding it to the right.
0144      *
0145      * @note It is recommended to use the GlobalDrawer here.
0146      * @property kirigami::OverlayDrawer globalDrawer
0147      */
0148     property OverlayDrawer globalDrawer
0149 
0150     /**
0151      * @brief This property specifies whether the application is in "widescreen" mode.
0152      *
0153      * This is enabled on desktops or horizontal tablets.
0154      *
0155      * @note Different styles can have their own logic for deciding this.
0156      */
0157     property bool wideScreen: width >= Kirigami.Units.gridUnit * 60
0158 
0159     /**
0160      * @brief This property holds the drawer for context-dependent actions.
0161      *
0162      * This drawer can be opened by sliding from the right screen edge
0163      * or by either pressing on the handle or sliding it to the left.
0164      *
0165      * @note It is recommended to use the ContextDrawer class here.
0166      *
0167      * The context drawer will display the previously defined contextual
0168      * actions of the page that is currently active in the pageStack.
0169      *
0170      * Example usage:
0171      * @code{.qml}
0172      * import org.kde.kirigami 2.4 as Kirigami
0173      *
0174      * Kirigami.ApplicationItem {
0175      *  [...]
0176      *     contextDrawer: Kirigami.ContextDrawer {
0177      *         id: contextDrawer
0178      *     }
0179      *  [...]
0180      * }
0181      * @endcode
0182      *
0183      * @code{.qml}
0184      * import org.kde.kirigami 2.4 as Kirigami
0185      *
0186      * Kirigami.Page {
0187      *   [...]
0188      *     // setting the contextual actions
0189      *     actions.contextualActions: [
0190      *         Kirigami.Action {
0191      *             icon.name: "edit"
0192      *             text: "Action text"
0193      *             onTriggered: {
0194      *                 // do stuff
0195      *             }
0196      *         },
0197      *         Kirigami.Action {
0198      *             icon.name: "edit"
0199      *             text: "Action text"
0200      *             onTriggered: {
0201      *                 // do stuff
0202      *             }
0203      *         }
0204      *     ]
0205      *   [...]
0206      * }
0207      * @endcode
0208      *
0209      *
0210      * @property kirigami::ContextDrawer contextDrawer
0211      */
0212     property OverlayDrawer contextDrawer
0213 
0214     /**
0215      * @brief This property specifies whether the application is in reachable mode,
0216      * for single hand use.
0217      *
0218      * The whole content of the application is moved down the screen to be
0219      * reachable with the thumb. If wideScreen is true, or reachableModeEnabled is false,
0220      * this property has no effect.
0221      *
0222      * @note This property should be treated as readonly. Use ``reachableModeEnabled`` instead.
0223      *
0224      * default: ``false``
0225      *
0226      * @see ::reachableModeEnabled
0227      */
0228     property bool reachableMode: false
0229 
0230     /**
0231      * @brief This property sets whether reachable mode can be used.
0232      *
0233      * default: ``true``
0234      */
0235     property bool reachableModeEnabled: true
0236 
0237     /**
0238      * @brief This property holds the list of all children of this item.
0239      * @internal
0240      * @property list<Object> __data
0241      */
0242     default property alias __data: contentItemRoot.data
0243 
0244     /**
0245      * @brief This property holds the Item of the main part of the Application UI.
0246      */
0247     readonly property Item contentItem: Item {
0248         id: contentItemRoot
0249         parent: root
0250         anchors {
0251             fill: parent
0252             topMargin: controlsVisible ? (root.header ? root.header.height : 0) + (root.menuBar ? root.menuBar.height : 0) : 0
0253             bottomMargin: controlsVisible && root.footer ? root.footer.height : 0
0254             leftMargin: root.globalDrawer && root.globalDrawer.modal === false ? root.globalDrawer.contentItem.width * root.globalDrawer.position : 0
0255             rightMargin: root.contextDrawer && root.contextDrawer.modal === false ? root.contextDrawer.contentItem.width * root.contextDrawer.position : 0
0256         }
0257 
0258         transform: Translate {
0259             Behavior on y {
0260                 NumberAnimation {
0261                     duration: Kirigami.Units.longDuration
0262                     easing.type: Easing.InOutQuad
0263                 }
0264             }
0265             y: root.reachableMode && root.reachableModeEnabled && !root.wideScreen ? root.height/2 : 0
0266             x: root.globalDrawer && root.globalDrawer.modal === true && root.globalDrawer.toString().indexOf("SplitDrawer") === 0 ? root.globalDrawer.contentItem.width * root.globalDrawer.position : 0
0267         }
0268     }
0269 
0270     /**
0271      * @brief This property holds background's color.
0272      *
0273      * default: ``Kirigami.Theme.backgroundColor``
0274      */
0275     property color color: Kirigami.Theme.backgroundColor
0276 
0277     /**
0278      * @brief This property holds the background of the Application UI.
0279      */
0280     property Item background
0281 
0282     property alias overlay: overlayRoot
0283 //END properties
0284 
0285 //BEGIN functions
0286     /**
0287      * @brief This function shows a passive notification at the bottom of the app window
0288      * lasting for few seconds, with an optional action button.
0289      *
0290      * @param message The text message to be shown to the user.
0291      * @param timeout How long to show the message:
0292      *            possible values: "short", "long" or the number of milliseconds
0293      * @param actionText Text in the action button, if any.
0294      * @param callBack A JavaScript function that will be executed when the
0295      *            user clicks the button.
0296      */
0297     function showPassiveNotification(message, timeout, actionText, callBack) {
0298         notificationsObject.showNotification(message, timeout, actionText, callBack);
0299     }
0300 
0301     /**
0302      * @brief This function hides the passive notification at specified index, if any is shown.
0303      * @param index Index of the notification to hide. Default is 0 (oldest notification).
0304     */
0305     function hidePassiveNotification(index = 0) {
0306         notificationsObject.hideNotification(index);
0307     }
0308 
0309     /**
0310      * @brief This property returns a pointer to the main instance of
0311      * AbstractApplicationItem.
0312      *
0313      * This is available to any children of this Item, including those
0314      * instantiated from separate QML files, making interoperation with
0315      * multiple files easier.
0316      *
0317      * Use this whenever you need access to properties that are available to
0318      * the main AbstractApplicationItem, such as its pageStack, globalDrawer
0319      * or header.
0320      *
0321      * @see AbstractApplicationWindow::applicationWindow()
0322      *
0323      * @returns a pointer to the instantiated AbstractApplicationItem.
0324      */
0325     function applicationWindow() {
0326         return root;
0327     }
0328 //END functions
0329 
0330 //BEGIN signals handlers
0331     onMenuBarChanged: {
0332         menuBar.parent = root.contentItem
0333         if (menuBar.z === undefined) {
0334             menuBar.z = 1;
0335         }
0336         if (menuBar instanceof T.ToolBar) {
0337             menuBar.position = T.ToolBar.Footer
0338         } else if (menuBar instanceof T.TabBar) {
0339             menuBar.position = T.TabBar.Footer
0340         } else if (menuBar instanceof T.DialogButtonBox) {
0341             menuBar.position = T.DialogButtonBox.Footer
0342         }
0343         menuBar.width = Qt.binding(() => root.contentItem.width)
0344         // FIXME: (root.header.height ?? 0) when we can depend from 5.15
0345         menuBar.y = Qt.binding(() => -menuBar.height - (root.header.height ? root.header.height : 0))
0346     }
0347 
0348     onHeaderChanged: {
0349         header.parent = root.contentItem
0350         if (header.z === undefined) {
0351             header.z = 1;
0352         }
0353         if (header instanceof T.ToolBar) {
0354             header.position = T.ToolBar.Header
0355         } else if (header instanceof T.TabBar) {
0356             header.position = T.TabBar.Header
0357         } else if (header instanceof T.DialogButtonBox) {
0358             header.position = T.DialogButtonBox.Header
0359         }
0360         header.width = Qt.binding(() => root.contentItem.width)
0361         header.y = Qt.binding(() => -header.height)
0362     }
0363 
0364     onFooterChanged: {
0365         footer.parent = root.contentItem
0366         if (footer.z === undefined) {
0367             footer.z = 1;
0368         }
0369         if (footer instanceof T.ToolBar) {
0370             footer.position = T.ToolBar.Footer
0371         } else if (footer instanceof T.TabBar) {
0372             footer.position = T.TabBar.Footer
0373         } else if (footer instanceof T.DialogButtonBox) {
0374             footer.position = T.DialogButtonBox.Footer
0375         }
0376         footer.width = Qt.binding(() => root.contentItem.width)
0377         footer.y = Qt.binding(() => root.contentItem.height)
0378     }
0379 
0380     onBackgroundChanged: {
0381         background.parent = root.contentItem
0382         if (background.z === undefined) {
0383             background.z = -1;
0384         }
0385         background.anchors.fill = background.parent
0386     }
0387 
0388     // NOTE: Don't want overscroll in landscape mode
0389     onWidthChanged: {
0390         if (width > height) {
0391             root.reachableMode = false;
0392         }
0393     }
0394 
0395     onPageStackChanged: pageStack.parent = root.contentItem;
0396 //END signals handlers
0397 
0398     LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
0399     LayoutMirroring.childrenInherit: true
0400 
0401     TP.PassiveNotificationsManager {
0402         id: notificationsObject
0403         anchors.bottom: parent.bottom
0404         anchors.horizontalCenter: parent.horizontalCenter
0405         z: 1
0406     }
0407 
0408     Item {
0409         anchors.fill: parent
0410         parent: root.parent || root
0411         z: 999999
0412         Rectangle {
0413             z: -1
0414             anchors.fill: parent
0415             color: "black"
0416             visible: contextDrawer && contextDrawer.modal
0417             parent: contextDrawer ? contextDrawer.background.parent.parent : overlayRoot
0418             opacity: contextDrawer ? contextDrawer.position * 0.6 : 0
0419         }
0420         Rectangle {
0421             z: -1
0422             anchors.fill: parent
0423             color: "black"
0424             visible: globalDrawer && globalDrawer.modal
0425             parent: globalDrawer ? globalDrawer.background.parent.parent : overlayRoot
0426             opacity: globalDrawer ? globalDrawer.position * 0.6 : 0
0427         }
0428         Item {
0429             id: overlayRoot
0430             z: -1
0431             anchors.fill: parent
0432         }
0433         Window.onWindowChanged: {
0434             if (globalDrawer) {
0435                 globalDrawer.visible = globalDrawer.drawerOpen;
0436             }
0437             if (contextDrawer) {
0438                 contextDrawer.visible = contextDrawer.drawerOpen;
0439             }
0440         }
0441     }
0442 
0443     MouseArea {
0444         parent: root
0445         z: -1
0446         anchors.fill: parent
0447         onClicked: mouse => {
0448             root.reachableMode = false;
0449         }
0450         visible: root.reachableMode && root.reachableModeEnabled
0451         Rectangle {
0452             anchors.fill: parent
0453             color: Qt.rgba(0, 0, 0, 0.3)
0454             opacity: 0.15
0455             Kirigami.Icon {
0456                 anchors.horizontalCenter: parent.horizontalCenter
0457                 y: x
0458                 width: Kirigami.Units.iconSizes.large
0459                 height: width
0460                 source: "go-up"
0461             }
0462         }
0463     }
0464 
0465     Binding {
0466         when: globalDrawer !== undefined && root.visible
0467         target: globalDrawer
0468         property: "parent"
0469         value: overlay
0470         restoreMode: Binding.RestoreBinding
0471     }
0472     Binding {
0473         when: contextDrawer !== undefined && root.visible
0474         target: contextDrawer
0475         property: "parent"
0476         value: overlay
0477         restoreMode: Binding.RestoreBinding
0478     }
0479 
0480     implicitWidth: Kirigami.Units.gridUnit * 30
0481     implicitHeight: Kirigami.Units.gridUnit * 45
0482     visible: true
0483 }