Merge lp:~tpeeters/ubuntu-ui-toolkit/buttonAction into lp:ubuntu-ui-toolkit

Proposed by Tim Peeters
Status: Merged
Approved by: Zsombor Egri
Approved revision: 522
Merged at revision: 518
Proposed branch: lp:~tpeeters/ubuntu-ui-toolkit/buttonAction
Merge into: lp:ubuntu-ui-toolkit
Diff against target: 270 lines (+94/-32)
6 files modified
components.api (+1/-0)
modules/Ubuntu/Components/AbstractButton.qml (+20/-7)
modules/Ubuntu/Components/Button.qml (+20/-9)
modules/Ubuntu/Components/Toolbar.qml (+15/-16)
tests/unit/tst_components/tst_abstractbutton.qml (+16/-0)
tests/unit/tst_components/tst_button.qml (+22/-0)
To merge this branch: bzr merge lp:~tpeeters/ubuntu-ui-toolkit/buttonAction
Reviewer Review Type Date Requested Status
Zsombor Egri Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+165563@code.launchpad.net

Commit message

Add action property to AbstractButton, and use it in the toolbar.

Description of the change

Add action property to AbstractButton, and use it in the toolbar.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Zsombor Egri (zsombi) wrote :

looks cool :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'components.api'
2--- components.api 2013-05-22 11:27:49 +0000
3+++ components.api 2013-05-28 12:40:33 +0000
4@@ -1,5 +1,6 @@
5 modules/Ubuntu/Components/AbstractButton.qml
6 Item
7+ property Action action
8 signal clicked()
9 signal pressAndHold()
10 property bool pressed
11
12=== modified file 'modules/Ubuntu/Components/AbstractButton.qml'
13--- modules/Ubuntu/Components/AbstractButton.qml 2013-04-17 11:06:44 +0000
14+++ modules/Ubuntu/Components/AbstractButton.qml 2013-05-28 12:40:33 +0000
15@@ -31,35 +31,48 @@
16 id: button
17
18 /*!
19- \preliminary
20+ If an action is specified, the button's clicked signal will trigger the action.
21+ Subclasses of AbstractButton can use other properties of action (for example
22+ the text and iconSource).
23+ */
24+ property Action action: null
25+
26+ visible: action ? action.visible : true
27+ enabled: action ? action.enabled : true
28+
29+ /*!
30 This handler is called when there is a mouse click on the button
31- and the button is not disabled.
32+ and the button is not disabled. If \l action is defined,
33+ the action will be triggered.
34 */
35 signal clicked()
36
37+ /*!
38+ If \l action was set, action.triggered() is automatically called with
39+ the AbstractButton as parameter.
40+ */
41+ onClicked: if (action) action.triggered(button)
42+
43 Keys.onEnterPressed: clicked()
44 Keys.onReturnPressed: clicked()
45
46 /*!
47- \preliminary
48 This handler is called when there is a long press.
49 */
50 signal pressAndHold()
51
52 /*!
53- \preliminary
54 True if the user presses a mouse button in the button's mouse area.
55 */
56 property bool pressed: mouseArea.pressed
57
58 /*!
59- \preliminary
60 True if the mouse cursor hovers over the button's mouse area.
61 */
62 property bool hovered: __acceptEvents && mouseArea.containsMouse
63
64-
65- /*! \internal
66+ /*!
67+ \internal
68 Disable or enable signal emition by default.
69 Some classes want to emit the signal by themselves (ListItem.Standard)
70 */
71
72=== modified file 'modules/Ubuntu/Components/Button.qml'
73--- modules/Ubuntu/Components/Button.qml 2013-05-24 09:12:56 +0000
74+++ modules/Ubuntu/Components/Button.qml 2013-05-28 12:40:33 +0000
75@@ -49,6 +49,22 @@
76 }
77 }
78 \endqml
79+ An \l Action can be used to specify \l clicked(), \l iconSource and \l text. Example:
80+ \qml
81+ Item {
82+ Action {
83+ id: action1
84+ text: "Click me"
85+ onTriggered: print("action!")
86+ iconSource: "icon.png"
87+ }
88+ Button {
89+ anchors.centerIn: parent
90+ action: action1
91+ color: "green"
92+ }
93+ }
94+ \endqml
95 */
96 AbstractButton {
97 id: button
98@@ -59,31 +75,26 @@
99 implicitHeight: units.gu(4)
100
101 /*!
102- \preliminary
103 The foreground color of the button in idle state.
104 */
105 property color color: "transparent"
106
107 /*!
108- \preliminary
109 The source URL of the icon to display inside the button.
110 Leave this value blank for a text-only button.
111- \qmlproperty url iconSource
112+ If \l action is set, the default iconSource is that of the action.
113 */
114- property url iconSource: ""
115+ property url iconSource: action ? action.iconSource : ""
116
117 /*!
118- \preliminary
119 The text to display in the button. If an icon was defined,
120 the text will be shown next to the icon, otherwise it will
121 be centered. Leave blank for an icon-only button.
122- \qmlproperty string text
123+ If \l action is set, the default text is that of the action.
124 */
125- property string text
126+ property string text: action ? action.text : undefined
127
128 /*!
129- \preliminary
130-
131 The position of the icon relative to the text. Options
132 are "left" and "right". The default value is "left".
133
134
135=== modified file 'modules/Ubuntu/Components/Toolbar.qml'
136--- modules/Ubuntu/Components/Toolbar.qml 2013-05-02 19:18:09 +0000
137+++ modules/Ubuntu/Components/Toolbar.qml 2013-05-28 12:40:33 +0000
138@@ -122,15 +122,12 @@
139
140 Component {
141 id: toolButtonComponent
142- Item {
143+ Button {
144 id: toolButton
145+ // Disable the mouse area so swipes on the button will not be blocked
146+ // from going to the toolbar. The panel will take care calling the button's clicked().
147+ __mouseArea.visible: false
148 Theming.ItemStyle.class: "toolbar-button"
149- property string text: action && action.text ? action.text : ""
150- property url iconSource: action && action.iconSource ? action.iconSource : ""
151- signal clicked()
152- onClicked: action.triggered(toolButton)
153- enabled: action && action.enabled
154- visible: action && action.visible
155 width: units.gu(5)
156 height: toolbar.height
157 }
158@@ -138,22 +135,20 @@
159
160 Loader {
161 id: backButton
162- property Action action: toolbar.tools && toolbar.tools.back ? toolbar.tools.back : null
163- sourceComponent: action ? action.itemHint ? action.itemHint : toolButtonComponent : null
164+ property Action backAction: toolbar.tools ? toolbar.tools.back : null
165+ sourceComponent: backAction ? backAction.itemHint ? backAction.itemHint : toolButtonComponent : null
166 anchors {
167 left: parent.left
168 leftMargin: units.gu(2)
169 verticalCenter: parent.verticalCenter
170 }
171 onStatusChanged: {
172- if (item && status == Loader.Ready && action && action.itemHint) {
173- if (item.hasOwnProperty("clicked")) item.clicked.connect(backButton.itemTriggered);
174- if (item.hasOwnProperty("accepted")) item.accepted.connect(backButton.itemTriggered);
175- if (item.hasOwnProperty("triggered")) item.accepted.connect(backButton.itemTtriggered);
176+ if (item && status == Loader.Ready) {
177+ if (item.hasOwnProperty("action")) item.action = backAction;
178 }
179 }
180- signal itemTriggered()
181- onItemTriggered: action.triggered(item)
182+ // ensure the item's action is up-to-date (which is not the case without this line):
183+ onBackActionChanged: if (item && item.hasOwnProperty("action")) item.action = backAction;
184 }
185
186 Row {
187@@ -170,8 +165,12 @@
188 model: internal.visibleTools ? internal.visibleTools.children : 0
189 Loader {
190 sourceComponent: modelData.itemHint ? modelData.itemHint : toolButtonComponent
191- property Action action: modelData
192 anchors.verticalCenter: toolButtonsContainer.verticalCenter
193+ onStatusChanged: {
194+ if (item && status == Loader.Ready) {
195+ if (item.hasOwnProperty("action")) item.action = modelData
196+ }
197+ }
198 }
199 }
200 }
201
202=== modified file 'tests/unit/tst_components/tst_abstractbutton.qml'
203--- tests/unit/tst_components/tst_abstractbutton.qml 2012-11-21 21:23:04 +0000
204+++ tests/unit/tst_components/tst_abstractbutton.qml 2013-05-28 12:40:33 +0000
205@@ -21,6 +21,16 @@
206 TestCase {
207 name: "AbstractButtonAPI"
208
209+ function test_action() {
210+ compare(absButton.action, null,"Action is null by default")
211+ absButton.action = action1
212+ compare(absButton.action, action1, "Action can be set")
213+ var numTriggers = action1.triggerCount
214+ absButton.clicked()
215+ compare(action1.triggerCount, numTriggers+1, "Button clicked triggers action")
216+ absButton.action = null
217+ }
218+
219 function test_hovered() {
220 compare(absButton.hovered,false,"Hovered is boolean and false by default")
221 }
222@@ -46,4 +56,10 @@
223 target: parent
224 }
225 }
226+
227+ Action {
228+ id: action1
229+ property int triggerCount: 0
230+ onTriggered: triggerCount++
231+ }
232 }
233
234=== modified file 'tests/unit/tst_components/tst_button.qml'
235--- tests/unit/tst_components/tst_button.qml 2013-05-06 13:11:21 +0000
236+++ tests/unit/tst_components/tst_button.qml 2013-05-28 12:40:33 +0000
237@@ -28,6 +28,18 @@
238 compare(button.text,newText,"Can set/get text")
239 }
240
241+ function test_action() {
242+ compare(actionButton.action, null, "Action is null by default")
243+ actionButton.action = action1
244+ compare(actionButton.action, action1, "action can be set")
245+ var newText = "Hello action!"
246+ action1.text = newText
247+ compare(actionButton.text, newText, "action can be used to define text")
248+ var newIcon = "../../../examples/ubuntu-ui-toolkit-gallery/small_avatar.png"
249+ action1.iconSource = newIcon
250+ compare(actionButton.iconSource, Qt.resolvedUrl(newIcon), "action can be used to define iconSource")
251+ }
252+
253 function test_iconPosition() {
254 compare(button.iconPosition,"left","The default value for iconPosition is 'left'")
255 var newIconPosition = "right"
256@@ -73,4 +85,14 @@
257 target: parent
258 }
259 }
260+
261+ // Use a new button for action tests, because other tests override
262+ // the button's default text and iconSource so they are no longer
263+ // automatically taken from the action.
264+ Button {
265+ id: actionButton
266+ }
267+ Action {
268+ id: action1
269+ }
270 }

Subscribers

People subscribed via source and target branches

to status/vote changes: