| 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 |
| 18 |
import Lomiri.Components.Styles 2.0 as Style |
| 19 |
|
| 20 |
Style.ActionBarStyle { |
| 21 |
id: actionBarStyle |
| 22 |
implicitHeight: units.gu(5) |
| 23 |
|
| 24 |
clip: true // avoid showing the icons that are scrolled out of the view. |
| 25 |
backgroundColor: theme.palette.normal.background |
| 26 |
buttons { |
| 27 |
foregroundColor: theme.palette.normal.backgroundText |
| 28 |
pressedForegroundColor: buttons.foregroundColor |
| 29 |
disabledForegroundColor: theme.palette.disabled.backgroundText |
| 30 |
backgroundColor: "transparent" // background is already colored |
| 31 |
pressedBackgroundColor: theme.palette.highlighted.background |
| 32 |
disabledBackgroundColor: buttons.backgroundColor |
| 33 |
} |
| 34 |
|
| 35 |
property QtObject scrollButtons: Style.ActionItemProperties { |
| 36 |
foregroundColor: actionBarStyle.buttons.foregroundColor |
| 37 |
pressedForegroundColor: actionBarStyle.buttons.pressedForegroundColor |
| 38 |
backgroundColor: actionBarStyle.backgroundColor // must be opaque to hide the icon buttons |
| 39 |
pressedBackgroundColor: actionBarStyle.buttons.pressedBackgroundColor |
| 40 |
property real width: units.gu(4) |
| 41 |
property int fadeDuration: LomiriAnimation.FastDuration |
| 42 |
} |
| 43 |
|
| 44 |
/*! |
| 45 |
The default action delegate if the styled item does |
| 46 |
not provide a delegate. |
| 47 |
*/ |
| 48 |
// FIXME: This ListItem { AbstractButton { } } construction can be avoided |
| 49 |
// when StyledItem supports cursor keys navigation, see bug #1573616. |
| 50 |
defaultDelegate: ListItem { |
| 51 |
width: units.gu(4) |
| 52 |
height: actionBarStyle.height |
| 53 |
enabled: modelData.enabled |
| 54 |
objectName: modelData.objectName + "_button" |
| 55 |
onClicked: button.trigger() |
| 56 |
AbstractButton { |
| 57 |
id: button |
| 58 |
anchors.fill: parent |
| 59 |
style: IconButtonStyle { |
| 60 |
foregroundColor: button.pressed ? |
| 61 |
actionBarStyle.buttons.pressedForegroundColor : |
| 62 |
button.enabled ? |
| 63 |
actionBarStyle.buttons.foregroundColor : |
| 64 |
actionBarStyle.buttons.disabledForegroundColor |
| 65 |
backgroundColor: button.pressed ? |
| 66 |
actionBarStyle.buttons.pressedBackgroundColor : |
| 67 |
button.enabled ? |
| 68 |
actionBarStyle.buttons.backgroundColor : |
| 69 |
actionBarStyle.buttons.disabledBackgroundColor |
| 70 |
} |
| 71 |
action: modelData |
| 72 |
} |
| 73 |
divider.visible: false |
| 74 |
} |
| 75 |
|
| 76 |
Rectangle { |
| 77 |
color: actionBarStyle.backgroundColor |
| 78 |
id: listViewContainer |
| 79 |
anchors.fill: parent |
| 80 |
|
| 81 |
property var visibleActions: getVisibleActions(styledItem.actions) |
| 82 |
function getVisibleActions(actions) { |
| 83 |
var visibleActionList = []; |
| 84 |
for (var i = 0; i < actions.length; i++) { |
| 85 |
var action = actions[i]; |
| 86 |
if (action && action.hasOwnProperty("visible") && action.visible) { |
| 87 |
visibleActionList.push(action); |
| 88 |
} |
| 89 |
} |
| 90 |
return visibleActionList; |
| 91 |
} |
| 92 |
|
| 93 |
LomiriListView { |
| 94 |
id: actionsListView |
| 95 |
objectName: "actions_listview" |
| 96 |
anchors { |
| 97 |
right: parent.right |
| 98 |
top: parent.top |
| 99 |
bottom: parent.bottom |
| 100 |
rightMargin: scrollButtons.width |
| 101 |
} |
| 102 |
displayMarginBeginning: scrollButtons.width |
| 103 |
displayMarginEnd: scrollButtons.width |
| 104 |
leftMargin: -scrollButtons.width |
| 105 |
rightMargin: -scrollButtons.width |
| 106 |
width: listViewContainer.width - 2*scrollButtons.width |
| 107 |
layoutDirection: Qt.RightToLeft |
| 108 |
|
| 109 |
orientation: ListView.Horizontal |
| 110 |
boundsBehavior: Flickable.StopAtBounds |
| 111 |
delegate: styledItem.delegate |
| 112 |
model: listViewContainer.visibleActions |
| 113 |
|
| 114 |
highlight: Rectangle { |
| 115 |
color: "transparent" |
| 116 |
border { |
| 117 |
color: theme.palette.selected.focus |
| 118 |
width: 2 |
| 119 |
} |
| 120 |
visible: actionsListView.activeFocus |
| 121 |
width: actionsListView.currentItem.width |
| 122 |
height: actionsListView.currentItem.height |
| 123 |
} |
| 124 |
|
| 125 |
// This gives the correct behavior when scrollButtons.width is |
| 126 |
// larger/smaller/equal compared to the width of the delegate. |
| 127 |
onCurrentIndexChanged: positionViewAtIndex(currentIndex, ListView.Contain) |
| 128 |
|
| 129 |
SmoothedAnimation { |
| 130 |
objectName: "actions_scroll_animation" |
| 131 |
id: contentXAnim |
| 132 |
target: actionsListView |
| 133 |
property: "contentX" |
| 134 |
duration: LomiriAnimation.FastDuration |
| 135 |
} |
| 136 |
|
| 137 |
// direction == 1 to show more icons on the left |
| 138 |
// direction == -1 to show more icons on the right |
| 139 |
function scroll(direction) { |
| 140 |
if (contentXAnim.running) contentXAnim.stop(); |
| 141 |
var newContentX = actionsListView.contentX - (actionsListView.width * direction); |
| 142 |
contentXAnim.from = actionsListView.contentX; |
| 143 |
// make sure we don't overshoot bounds |
| 144 |
contentXAnim.to = MathUtils.clamp( |
| 145 |
newContentX, |
| 146 |
originX + scrollButtons.width, |
| 147 |
actionsListView.originX + actionsListView.contentWidth |
| 148 |
- actionsListView.width - scrollButtons.width); |
| 149 |
contentXAnim.start(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
Component { |
| 154 |
id: scrollButtonComponent |
| 155 |
AbstractButton { |
| 156 |
id: scrollButton |
| 157 |
width: actionBarStyle.scrollButtons.width |
| 158 |
enabled: opacity === 1.0 |
| 159 |
onClicked: actionsListView.scroll(scrollDirection) |
| 160 |
opacity: buttonOpacity |
| 161 |
objectName: buttonName |
| 162 |
Behavior on opacity { |
| 163 |
LomiriNumberAnimation { |
| 164 |
duration: actionBarStyle.scrollButtons.fadeDuration |
| 165 |
} |
| 166 |
} |
| 167 |
Rectangle { |
| 168 |
anchors.fill: parent |
| 169 |
color: scrollButton.pressed ? actionBarStyle.scrollButtons.pressedBackgroundColor |
| 170 |
: actionBarStyle.scrollButtons.backgroundColor |
| 171 |
} |
| 172 |
Icon { |
| 173 |
anchors.centerIn: parent |
| 174 |
width: units.gu(1) |
| 175 |
height: units.gu(1) |
| 176 |
name: scrollDirection === 1 ? "toolkit_arrow-left" : "toolkit_arrow-right" |
| 177 |
color: scrollButton.pressed ? actionBarStyle.scrollButtons.pressedForegroundColor |
| 178 |
: actionBarStyle.scrollButtons.foregroundColor |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
Loader { |
| 183 |
anchors { |
| 184 |
left: parent.left |
| 185 |
top: parent.top |
| 186 |
bottom: parent.bottom |
| 187 |
} |
| 188 |
sourceComponent: scrollButtonComponent |
| 189 |
property real buttonOpacity: actionsListView.atXBeginning ? 0.0 : 1.0 |
| 190 |
property int scrollDirection: 1 |
| 191 |
property string buttonName: "leading_scroll_button" |
| 192 |
} |
| 193 |
Loader { |
| 194 |
anchors { |
| 195 |
right: parent.right |
| 196 |
top: parent.top |
| 197 |
bottom: parent.bottom |
| 198 |
} |
| 199 |
sourceComponent: scrollButtonComponent |
| 200 |
property real buttonOpacity: actionsListView.atXEnd ? 0.0 : 1.0 |
| 201 |
property int scrollDirection: -1 |
| 202 |
property string buttonName: "trailing_scroll_button" |
| 203 |
} |
| 204 |
} |
| 205 |
} |