File indexing completed on 2024-05-19 05:34:08

0001 /*
0002     SPDX-FileCopyrightText: 2016 Smith AR <audoban@openmailbox.org>
0003     SPDX-FileCopyrightText: 2016-2018 Michail Vourlakos <mvourlakos@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 function horizontalMargins() {
0008     return taskFrame.margins.left + taskFrame.margins.right;
0009 }
0010 
0011 function verticalMargins() {
0012     return taskFrame.margins.top + taskFrame.margins.bottom;
0013 }
0014 
0015 function adjustMargin(height, margin) {
0016     var available = height - verticalMargins();
0017 
0018     if (available < units.iconSizes.small) {
0019         return Math.floor((margin * (units.iconSizes.small / available)) / 3);
0020     }
0021 
0022     return margin;
0023 }
0024 
0025 function launcherLayoutTasks() {
0026     return Math.round(tasksModel.launcherCount / Math.floor(preferredMinWidth() / launcherWidth()));
0027 }
0028 
0029 function launcherLayoutWidthDiff() {
0030     return (launcherLayoutTasks() * taskWidth()) - (tasksModel.launcherCount * launcherWidth());
0031 }
0032 
0033 function logicalTaskCount() {
0034     var count = (tasksModel.count - tasksModel.launcherCount) + launcherLayoutTasks();
0035 
0036     return Math.max(tasksModel.count ? 1 : 0, count);
0037 }
0038 
0039 function maxStripes() {
0040     var length = tasks.vertical ? taskList.width : taskList.height;
0041     var minimum = tasks.vertical ? preferredMinWidth() : preferredMinHeight();
0042 
0043     return Math.min(plasmoid.configuration.maxStripes, Math.max(1, Math.floor(length / minimum)));
0044 }
0045 
0046 function tasksPerStripe() {
0047     if (plasmoid.configuration.forceStripes) {
0048         return Math.ceil(logicalTaskCount() / maxStripes());
0049     } else {
0050         var length = tasks.vertical ? taskList.height : taskList.width;
0051         var minimum = tasks.vertical ? preferredMinHeight() : preferredMinWidth();
0052 
0053         return Math.floor(length / minimum);
0054     }
0055 }
0056 
0057 function calculateStripes() {
0058     var stripes = plasmoid.configuration.forceStripes ? plasmoid.configuration.maxStripes : Math.min(plasmoid.configuration.maxStripes, Math.ceil(logicalTaskCount() / tasksPerStripe()));
0059 
0060     return Math.min(stripes, maxStripes());
0061 }
0062 
0063 function full() {
0064     return (maxStripes() == calculateStripes());
0065 }
0066 
0067 function optimumCapacity(width, height) {
0068     var length = tasks.vertical ? height : width;
0069     var maximum = tasks.vertical ? preferredMaxHeight() : preferredMaxWidth();
0070 
0071     return (Math.ceil(length / maximum) * maxStripes());
0072 }
0073 
0074 function layoutWidth() {
0075     if (plasmoid.configuration.forceStripes && !tasks.vertical) {
0076         return Math.min(tasks.width, Math.max(preferredMaxWidth(), tasksPerStripe() * preferredMaxWidth()));
0077     } else {
0078         return tasks.width;
0079     }
0080 }
0081 
0082 function layoutHeight() {
0083     if (plasmoid.configuration.forceStripes && tasks.vertical) {
0084         return Math.min(tasks.height, Math.max(preferredMaxHeight(), tasksPerStripe() * preferredMaxHeight()));
0085     } else {
0086         return tasks.height;
0087     }
0088 }
0089 
0090 function preferredMinWidth() {
0091     var width = launcherWidth();
0092 
0093     if (!tasks.vertical && !tasks.iconsOnly) {
0094         width += (units.smallSpacing * 2) + (theme.mSize(theme.defaultFont).width * 12);
0095     }
0096 
0097     return width;
0098 }
0099 
0100 function preferredMaxWidth() {
0101     if (tasks.iconsOnly) {
0102         if (tasks.vertical) {
0103             return tasks.width + verticalMargins();
0104         } else {
0105             return tasks.height + horizontalMargins();
0106         }
0107     }
0108 
0109     if (plasmoid.configuration.groupingStrategy != 0 && !plasmoid.configuration.groupPopups) {
0110         return preferredMinWidth();
0111     }
0112 
0113     return Math.floor(preferredMinWidth() * 1.6);
0114 }
0115 
0116 function preferredMinHeight() {
0117     // TODO FIXME UPSTREAM: Port to proper font metrics for descenders once we have access to them.
0118     return theme.mSize(theme.defaultFont).height + 4;
0119 }
0120 
0121 function preferredMaxHeight() {
0122     return verticalMargins() + Math.min(units.iconSizes.small * 3, theme.mSize(theme.defaultFont).height * 3);
0123 }
0124 
0125 function taskWidth() {
0126     if (tasks.vertical) {
0127         return Math.floor(taskList.width / calculateStripes());
0128     } else {
0129         if (full() && Math.max(1, logicalTaskCount()) > tasksPerStripe()) {
0130             return Math.floor(taskList.width / Math.ceil(logicalTaskCount() / maxStripes()));
0131         } else {
0132             return Math.min(preferredMaxWidth(), Math.floor(taskList.width / Math.min(logicalTaskCount(), tasksPerStripe())));
0133         }
0134     }
0135 }
0136 
0137 function taskHeight() {
0138     if (tasks.vertical) {
0139         if (full() && Math.max(1, logicalTaskCount()) > tasksPerStripe()) {
0140             return Math.floor(taskList.height / Math.ceil(logicalTaskCount() / maxStripes()));
0141         } else {
0142             return Math.min(preferredMaxHeight(), Math.floor(taskList.height / Math.min(logicalTaskCount(), tasksPerStripe())));
0143         }
0144     } else {
0145         return Math.floor(taskList.height / calculateStripes());
0146     }
0147 }
0148 
0149 function launcherWidth() {
0150     var baseWidth = tasks.vertical ? preferredMinHeight() : Math.min(tasks.height, units.iconSizes.small * 3);
0151 
0152     return (baseWidth + horizontalMargins())
0153         - (adjustMargin(baseWidth, taskFrame.margins.top) + adjustMargin(baseWidth, taskFrame.margins.bottom));
0154 }
0155 
0156 function layout(container) {
0157     var item;
0158     var stripes = calculateStripes();
0159     var taskCount = tasksModel.count - tasksModel.launcherCount;
0160     var width = taskWidth();
0161     var adjustedWidth = width;
0162     var height = taskHeight();
0163 
0164     if (!tasks.vertical && stripes == 1 && taskCount)
0165     {
0166         var shrink = ((tasksModel.count - tasksModel.launcherCount) * preferredMaxWidth())
0167             + (tasksModel.launcherCount * launcherWidth()) > taskList.width;
0168         width = Math.min(shrink ? width + Math.floor(launcherLayoutWidthDiff() / taskCount) : width,
0169             preferredMaxWidth());
0170     }
0171 
0172     for (var i = 0; i < container.count; ++i) {
0173         item = container.itemAt(i);
0174 
0175         if (!item) {
0176             continue;
0177         }
0178 
0179         adjustedWidth = width;
0180 
0181         if (!tasks.vertical && !tasks.iconsOnly && (plasmoid.configuration.separateLaunchers || stripes == 1)) {
0182             if (item.m.IsLauncher === true) {
0183                 adjustedWidth = launcherWidth();
0184             } else if (stripes > 1 && i == tasksModel.launcherCount) {
0185                 adjustedWidth += launcherLayoutWidthDiff();
0186             }
0187         }
0188 
0189         item.width = adjustedWidth;
0190         item.height = height;
0191         item.visible = true;
0192     }
0193 }