| 1 |
/* |
| 2 |
* Copyright 2016 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
import QtQuick 2.4 |
| 17 |
import Lomiri.Components 1.3 |
| 18 |
import Lomiri.Components.Popups 1.3 |
| 19 |
import Lomiri.Components.Styles 1.3 as Style |
| 20 |
|
| 21 |
Style.ActionBarStyle { |
| 22 |
id: actionBarStyle |
| 23 |
implicitWidth: actionsContainer.implicitWidth |
| 24 |
implicitHeight: units.gu(5) |
| 25 |
|
| 26 |
overflowIconName: "contextual-menu" |
| 27 |
|
| 28 |
// Unused with the standard action icon buttons, but may be used with a custom delegate. |
| 29 |
overflowText: "More" |
| 30 |
|
| 31 |
backgroundColor: theme.palette.normal.background |
| 32 |
buttons { |
| 33 |
foregroundColor: theme.palette.normal.backgroundText |
| 34 |
pressedForegroundColor: buttons.foregroundColor |
| 35 |
disabledForegroundColor: theme.palette.disabled.backgroundText |
| 36 |
backgroundColor: "transparent" // action bar background is already colored |
| 37 |
pressedBackgroundColor: theme.palette.highlighted.background |
| 38 |
disabledBackgroundColor: buttons.backgroundColor |
| 39 |
} |
| 40 |
|
| 41 |
/*! |
| 42 |
The default action delegate if the styled item does |
| 43 |
not provide a delegate. |
| 44 |
*/ |
| 45 |
defaultDelegate: Component { |
| 46 |
AbstractButton { |
| 47 |
id: button |
| 48 |
style: IconButtonStyle { |
| 49 |
foregroundColor: button.pressed ? |
| 50 |
actionBarStyle.buttons.pressedForegroundColor : |
| 51 |
button.enabled ? |
| 52 |
actionBarStyle.buttons.foregroundColor : |
| 53 |
actionBarStyle.buttons.disabledForegroundColor |
| 54 |
backgroundColor: button.pressed ? |
| 55 |
actionBarStyle.buttons.pressedBackgroundColor : |
| 56 |
button.enabled ? |
| 57 |
actionBarStyle.buttons.backgroundColor : |
| 58 |
actionBarStyle.buttons.disabledBackgroundColor |
| 59 |
} |
| 60 |
objectName: action.objectName + "_button" |
| 61 |
height: parent ? parent.height : undefined |
| 62 |
action: modelData |
| 63 |
activeFocusOnTab: true |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
defaultNumberOfSlots: 3 |
| 68 |
|
| 69 |
Rectangle { |
| 70 |
id: background |
| 71 |
anchors.fill: parent |
| 72 |
color: actionBarStyle.backgroundColor |
| 73 |
} |
| 74 |
|
| 75 |
Component { |
| 76 |
id: fadeInComponent |
| 77 |
SequentialAnimation { |
| 78 |
id: fadeIn |
| 79 |
property alias target: opacityAnimation.target |
| 80 |
ScriptAction { |
| 81 |
script: fadeIn.target.opacity = 0.0; |
| 82 |
} |
| 83 |
LomiriNumberAnimation { |
| 84 |
id: opacityAnimation |
| 85 |
from: 0.0 |
| 86 |
to: 1.0 |
| 87 |
property: "opacity" |
| 88 |
alwaysRunToEnd: true |
| 89 |
duration: LomiriAnimation.BriskDuration |
| 90 |
} |
| 91 |
ScriptAction { |
| 92 |
script: fadeIn.destroy() |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
Row { |
| 98 |
id: actionsContainer |
| 99 |
|
| 100 |
property var visibleActions: getVisibleActions(styledItem.actions) |
| 101 |
function getVisibleActions(actions) { |
| 102 |
var visibleActionList = []; |
| 103 |
for (var i in actions) { |
| 104 |
var action = actions[i]; |
| 105 |
if (action && action.hasOwnProperty("visible") && action.visible) { |
| 106 |
visibleActionList.push(action); |
| 107 |
} |
| 108 |
} |
| 109 |
return visibleActionList; |
| 110 |
} |
| 111 |
function getReversedActions(actions) { |
| 112 |
var newlist = []; |
| 113 |
for (var i=actions.length-1; i >= 0; i--) { |
| 114 |
newlist.push(actions[i]); |
| 115 |
} |
| 116 |
return newlist; |
| 117 |
} |
| 118 |
|
| 119 |
property var directActions: getReversedActions(visibleActions.slice(0, numberOfSlots.used)) |
| 120 |
property var barActions: overflowAction.visible |
| 121 |
? directActions.concat(overflowAction) |
| 122 |
: directActions |
| 123 |
property var overflowActions: visibleActions.slice(numberOfSlots.used, |
| 124 |
numberOfSlots.requested) |
| 125 |
|
| 126 |
QtObject { |
| 127 |
id: numberOfSlots |
| 128 |
property int requested: actionsContainer.visibleActions.length |
| 129 |
property int available: styledItem.numberOfSlots |
| 130 |
property int overflow: overflowAction.visible ? 1 : 0 |
| 131 |
// when numberOfSlots < 1, show the overflow button, but set used |
| 132 |
// to 0 so that all actions will appear in the overflow panel. |
| 133 |
property int used: Math.min(Math.max(0, available - overflow), requested) |
| 134 |
} |
| 135 |
|
| 136 |
anchors { |
| 137 |
top: parent.top |
| 138 |
bottom: parent.bottom |
| 139 |
} |
| 140 |
|
| 141 |
Repeater { |
| 142 |
id: actionsRepeater |
| 143 |
objectName: "actions_repeater" |
| 144 |
model: actionsContainer.barActions |
| 145 |
delegate: styledItem.delegate |
| 146 |
property int previousCount: count |
| 147 |
onCountChanged: { |
| 148 |
// after all itemAdded signals |
| 149 |
previousCount = count; |
| 150 |
} |
| 151 |
|
| 152 |
function fadeIn(item) { |
| 153 |
var fadeObject = fadeInComponent.createObject(actionBarStyle, |
| 154 |
{"target": item}); |
| 155 |
fadeObject.target = item; |
| 156 |
fadeObject.start(); |
| 157 |
} |
| 158 |
onItemAdded: { |
| 159 |
if (count <= previousCount) return; // no items added |
| 160 |
if (index == 0) fadeIn(item); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
Action { |
| 165 |
id: overflowAction |
| 166 |
iconSource: actionBarStyle.overflowIconSource |
| 167 |
iconName: actionBarStyle.overflowIconName |
| 168 |
text: actionBarStyle.overflowText |
| 169 |
objectName: "overflow_action" |
| 170 |
visible: numberOfSlots.requested > numberOfSlots.available |
| 171 |
onTriggered: { |
| 172 |
var overflowButton = actionsRepeater.itemAt(actionsRepeater.count - 1); |
| 173 |
PopupUtils.open(actionsOverflowPopoverComponent, overflowButton); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
Component { |
| 178 |
id: actionsOverflowPopoverComponent |
| 179 |
OverflowPanel { |
| 180 |
id: actionsOverflowPopover |
| 181 |
objectName: "actions_overflow_panel" |
| 182 |
|
| 183 |
// Ensure the popover closes when actions change and |
| 184 |
// the list item below may be destroyed before its |
| 185 |
// onClicked is executed. See bug |
| 186 |
// https://bugs.launchpad.net/lomiri-ui-toolkit/+bug/1326963 |
| 187 |
Connections { |
| 188 |
target: styledItem |
| 189 |
function onActionsChanged() { |
| 190 |
actionsOverflowPopover.hide(); |
| 191 |
} |
| 192 |
} |
| 193 |
actions: actionsContainer.overflowActions |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |