Skip to content

Commit f6e1c72

Browse files
author
Alexander Vakrilov
committed
Merge pull request NativeScript#1207 from NativeScript/feature/action-bar-fix
FIX: Wrong tap events are fired when there are hidden ActionBarItems
2 parents 6479f06 + deb9373 commit f6e1c72

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

ui/action-bar/action-bar-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export class ActionItem extends bindable.Bindable implements dts.ActionItem {
251251
"icon", "ActionItem", new dependencyObservable.PropertyMetadata(null, null, ActionItem.onItemChanged));
252252

253253
public static visibilityProperty = new dependencyObservable.Property(
254-
"visibility", "ActionItemBase", new dependencyObservable.PropertyMetadata(enums.Visibility.visible, null, ActionItem.onItemChanged));
254+
"visibility", "ActionItem", new dependencyObservable.PropertyMetadata(enums.Visibility.visible, null, ActionItem.onItemChanged));
255255

256256
private _actionBar: ActionBar;
257257

ui/action-bar/action-bar.android.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,37 @@ import dts = require("ui/action-bar");
1010
import view = require("ui/core/view");
1111

1212
const R_ID_HOME = 0x0102002c;
13-
14-
var ACTION_ITEM_ID_OFFSET = 1000;
13+
const ACTION_ITEM_ID_OFFSET = 1000;
1514

1615
global.moduleMerge(common, exports);
1716

17+
var actionItemIdGenerator = ACTION_ITEM_ID_OFFSET;
18+
function generateItemId(): number {
19+
actionItemIdGenerator++;
20+
return actionItemIdGenerator;
21+
}
22+
1823
export class ActionItem extends common.ActionItem {
1924
private _androidPosition: dts.AndroidActionItemSettings = {
2025
position: enums.AndroidActionItemPosition.actionBar,
2126
systemIcon: undefined
2227
};
28+
private _itemId;
29+
constructor() {
30+
super();
31+
this._itemId = generateItemId();
32+
}
2333

2434
public get android(): dts.AndroidActionItemSettings {
2535
return this._androidPosition;
2636
}
2737
public set android(value: dts.AndroidActionItemSettings) {
2838
throw new Error("ActionItem.android is read-only");
2939
}
40+
41+
public _getItemId() {
42+
return this._itemId;
43+
}
3044
}
3145

3246
export class AndroidActionBarSettings implements dts.AndroidActionBarSettings {
@@ -126,14 +140,24 @@ export class ActionBar extends common.ActionBar {
126140
}
127141

128142
public _onAndroidItemSelected(itemId: number): boolean {
129-
var menuItem = this.actionItems.getItemAt(itemId - ACTION_ITEM_ID_OFFSET);
130-
if (menuItem) {
131-
menuItem._raiseTap();
143+
// Handle home button
144+
if (this.navigationButton && itemId === R_ID_HOME) {
145+
this.navigationButton._raiseTap();
132146
return true;
133147
}
148+
149+
// Find item with the right ID;
150+
var menuItem: dts.ActionItem = undefined;
151+
var items = this.actionItems.getItems();
152+
for (let i = 0; i < items.length; i++) {
153+
if ((<ActionItem>items[i])._getItemId() === itemId) {
154+
menuItem = items[i];
155+
break;
156+
}
157+
}
134158

135-
if (this.navigationButton && itemId === R_ID_HOME) {
136-
this.navigationButton._raiseTap();
159+
if (menuItem) {
160+
menuItem._raiseTap();
137161
return true;
138162
}
139163

@@ -211,8 +235,8 @@ export class ActionBar extends common.ActionBar {
211235

212236
menu.clear();
213237
for (var i = 0; i < items.length; i++) {
214-
var item = items[i];
215-
var menuItem = menu.add(android.view.Menu.NONE, i + ACTION_ITEM_ID_OFFSET, android.view.Menu.NONE, item.text + "");
238+
var item = <ActionItem>items[i];
239+
var menuItem = menu.add(android.view.Menu.NONE, item._getItemId(), android.view.Menu.NONE, item.text + "");
216240

217241
if (item.android.systemIcon) {
218242
// Try to look in the system resources.

ui/enums/enums.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@
359359
*/
360360
module NavigationBarVisibility {
361361
/**
362-
* NavigationBar will be visible if there if frame backstack canGoBack is true or Page have optionsMenu with menuItems.
362+
* NavigationBar will be visible if there if frame backstack canGoBack is true or if the page Action Bar is not empty.
363363
*/
364364
export var auto: string;
365365

0 commit comments

Comments
 (0)