Skip to content

Commit

Permalink
#46: calculate preferred size
Browse files Browse the repository at this point in the history
  • Loading branch information
antroids committed Aug 2, 2024
1 parent 867c9e1 commit 74af47b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
43 changes: 41 additions & 2 deletions package/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ PlasmoidItem {
property bool leftEdgeLocation: plasmoid.location === PlasmaCore.Types.LeftEdge

signal invokeKWinShortcut(string shortcut)
signal widgetElementsLayoutUpdated

Plasmoid.constraintHints: Plasmoid.CanFillArea
Layout.fillWidth: !vertical && plasmoid.configuration.widgetFillWidth
Expand All @@ -55,6 +56,7 @@ PlasmoidItem {

onLoaded: function () {
Utils.copyLayoutConstraint(item, widgetElementLoader);
widgetElementLoader.Layout.preferredWidthChanged.connect(root.widgetElementsLayoutUpdated);
item.modelData = modelData;
}
sourceComponent: {
Expand Down Expand Up @@ -126,8 +128,9 @@ PlasmoidItem {
property var modelData

height: root.elementHeight
Layout.alignment: root.widgetAlignment
width: height
Layout.alignment: root.widgetAlignment
Layout.preferredWidth: width
source: tasksModel.activeWindow.icon || "window"
enabled: tasksModel.hasActiveWindow && !!tasksModel.activeWindow.icon

Expand Down Expand Up @@ -166,8 +169,9 @@ PlasmoidItem {
property var modelData

height: root.elementHeight
Layout.alignment: root.widgetAlignment
width: height / 3
Layout.alignment: root.widgetAlignment
Layout.preferredWidth: width
color: "transparent"
enabled: tasksModel.hasActiveWindow
}
Expand All @@ -193,6 +197,7 @@ PlasmoidItem {
Layout.maximumWidth: !hideEmpty ? plasmoid.configuration.windowTitleMaximumWidth : 0
Layout.alignment: root.widgetAlignment
Layout.fillWidth: plasmoid.configuration.widgetFillWidth
Layout.preferredWidth: textMetrics.width + leftPadding + rightPadding + 1 // Magic number
text: titleText(windowTitleSource) || plasmoid.configuration.windowTitleUndefined
font.pointSize: plasmoid.configuration.windowTitleFontSize
font.bold: plasmoid.configuration.windowTitleFontBold
Expand All @@ -202,6 +207,12 @@ PlasmoidItem {
wrapMode: Text.WrapAnywhere
enabled: tasksModel.hasActiveWindow

TextMetrics {
id: textMetrics
font: windowTitleLabel.font
text: windowTitleLabel.text
}

Connections {
target: plasmoid.configuration

Expand Down Expand Up @@ -309,6 +320,9 @@ PlasmoidItem {
Layout.maximumWidth: root.vertical ? widgetRow.Layout.maximumHeight : widgetRow.Layout.maximumWidth
Layout.maximumHeight: root.vertical ? widgetRow.Layout.maximumWidth : widgetRow.Layout.maximumHeight

Layout.preferredWidth: root.vertical ? widgetRow.Layout.preferredHeight : widgetRow.Layout.preferredWidth
Layout.preferredHeight: root.vertical ? widgetRow.Layout.preferredWidth : widgetRow.Layout.preferredHeight

RowLayout {
id: widgetRow

Expand Down Expand Up @@ -363,6 +377,7 @@ PlasmoidItem {
}

Repeater {
id: widgetElementsMaximizedRepeater
property var elements: plasmoid.configuration.overrideElementsMaximized ? plasmoid.configuration.widgetElementsMaximized : []

onElementsChanged: function () {
Expand All @@ -381,6 +396,30 @@ PlasmoidItem {
});
}
}

Connections {
target: root

function onWidgetElementsLayoutUpdated() {
widgetRow.updatePreferredWidth();
}
}

function updatePreferredWidth() {
var repeater = widgetElementsRepeater.visible ? widgetElementsRepeater : widgetElementsMaximizedRepeater;
var preferredWidth = (repeater.count - 1) * widgetRow.spacing;
for (var i = 0; i < repeater.count; i++) {
var item = repeater.itemAt(i);
preferredWidth += Utils.calculateItemPreferredWidth(item);
}
if (preferredWidth < widgetRow.Layout.minimumWidth) {
widgetRow.Layout.preferredWidth = widgetRow.Layout.minimumWidth;
} else if (preferredWidth > widgetRow.Layout.maximumWidth) {
widgetRow.Layout.preferredWidth = widgetRow.Layout.maximumWidth;
} else {
widgetRow.Layout.preferredWidth = preferredWidth;
}
}
}
}
}
12 changes: 12 additions & 0 deletions package/contents/ui/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ function copyLayoutConstraint(from, to) {
})
}

function calculateItemPreferredWidth(item) {
var preferredWidth = 0;

if (item && item.Layout) {
preferredWidth += item.Layout.preferredWidth || 0;
preferredWidth += item.Layout.leftMargin || 0;
preferredWidth += item.Layout.rightMargin || 0;
}

return preferredWidth;
}

function widgetElementModelFromName(name) {
switch (name) {
case "windowCloseButton":
Expand Down
2 changes: 1 addition & 1 deletion package/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Id": "com.github.antroids.application-title-bar",
"Name": "Application Title Bar",
"License": "GPL-3.0+",
"Version": "0.6.8",
"Version": "0.6.9",
"Website": "https://github.com/antroids/application-title-bar",
"FormFactors": [
"desktop"
Expand Down

0 comments on commit 74af47b

Please sign in to comment.