Warning, /system/mycroft-gui/import/qml/AutoFitLabel.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * Copyright 2018 by Marco Martin <mart@kde.org>
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *    http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  *
0016  */
0017 import QtQuick 2.15
0018 import QtQuick.Layouts 1.15
0019 import QtQuick.Controls 2.15
0020 import QtQuick.Window 2.15
0021 import org.kde.kirigami 2.19 as Kirigami
0022 
0023 Label {
0024     id: root
0025     
0026     Layout.alignment: Qt.AlignCenter
0027 
0028     horizontalAlignment: Text.AlignHCenter
0029     verticalAlignment: Text.AlignVCenter
0030     fontSizeMode: Text.Fit
0031     minimumPixelSize: 5
0032     font.pixelSize: 250
0033 
0034     renderType: height > 40
0035         ? Text.QtRendering
0036         : (Screen.devicePixelRatio % 1 !== 0 ? Text.QtRendering : Text.NativeRendering)
0037 
0038     Timer {
0039         id: sizeModeChangeTimer
0040         interval: 50
0041         running: false
0042         repeat: false
0043 
0044         onTriggered: {
0045             if (fontSizeMode == 1) {
0046                 if(paintedHeight > root.height) {
0047                     // Text Height is bigger than implicit height switch font size mode to fit
0048                     root.fontSizeMode = 3
0049                     opacity = 1
0050                 } else {
0051                     opacity = 1
0052                 }
0053             }
0054         }
0055     }
0056 
0057     onTextChanged: {
0058         var wordlist = text.split(" ")
0059         var wordcount = text.split(" ").length
0060         if(wordcount <= 1) {
0061             fontSizeMode = Text.HorizontalFit
0062             opacity = 0
0063             // Do text height check if we are in horizontal fit mode to make sure text is not bigger than available height
0064             sizeModeChangeTimer.start()
0065         } else {
0066             // Check how many characters are in the longest word in the wordlist
0067             var longestWord = wordlist.reduce(function(a, b) {
0068                 return a.length > b.length ? a : b;
0069             });
0070             var longestWordLength = longestWord.length
0071 
0072             // If char count in longest word less than 2 sylabels, use horizontal fit
0073             if(longestWordLength < 2) {
0074                 fontSizeMode = Text.HorizontalFit   
0075             } else {
0076                 fontSizeMode = Text.VerticalFit
0077             }
0078         }
0079     }
0080 }