Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 223
Registered: ‎04-26-2012
My Device: Z10
My Carrier: Vodafone
Accepted Solution

(bb10) how to cancel a context menu opening

I'm having a strange behaviour with context menus.

I have two lists in my app with two contextual menus, one menu for each list.

 

I have some logic to determine if context menu has to be launched or not, and if not, I prevent ContextMenuEvent.OPENING event, so menu is not launched. Fine. But after that, context menu stops working for the other list.

 

I think something in ContexMenu classes must be left in a bad state after preventDefault on OPENING event.

 

What will be then the best way to stop opening one context menu and still work for other components?

 

See the sample...

package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	import qnx.fuse.ui.actionbar.ActionBar;
	import qnx.fuse.ui.core.Action;
	import qnx.fuse.ui.core.ActionBase;
	import qnx.fuse.ui.core.ActionSet;
	import qnx.fuse.ui.core.TabAction;
	import qnx.fuse.ui.events.ContextMenuEvent;
	import qnx.fuse.ui.events.DragEvent;
	import qnx.fuse.ui.listClasses.List;
	import qnx.ui.data.DataProvider;
	
	public class TestPush extends Sprite
	{
		private var actionBarOverflow:Sprite;
		private var content:Sprite = new Sprite();
		
		private var slideX:Number = 0;
		
		public function TestPush()
		{
			
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener( Event.RESIZE, function( e:Event ):void
			{
				actBar.y = stage.stageHeight - actBar.height;
			} );
			
			content.addChild( new Bitmap( new BitmapData( stage.stageWidth, stage.stageHeight, false, 0xffffff ) ) );
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			var dp:DataProvider=new DataProvider();
			dp.addItem({"label":"item1"});
			dp.addItem({"label":"item2"});
			dp.addItem({"label":"item3"});
			dp.addItem({"label":"item4"});
			dp.addItem({"label":"item5"});
			dp.addItem({"label":"item6"});
			dp.addItem({"label":"item7"});
			dp.addItem({"label":"item8"});
			dp.addItem({"label":"item9"});
			dp.addItem({"label":"item10"});
			var list:List=new List();
			list.dataProvider=dp;
			
			actionBarOverflow = new Sprite();
			
			var actBar:ActionBar=new ActionBar();
			actBar.showTabsFirstOnBar(false);
			actBar.reserveActionSpace(true);
			actBar.backButton = null; 
			
			actBar.tabOverflowParent = actionBarOverflow;
			actBar.width=stage.stageWidth;
			actBar.showTabsFirstOnBar(false);
			actBar.addAction(new TabAction("TEST"));
			actBar.addAction(new TabAction("TEST2"));
			actBar.addAction(new Action("Test1"));
			actBar.addAction(new Action("Test2"));
			actBar.addAction(new Action("Test3"));

			var list2:List=new List();
			list2.dataProvider=dp;

			
			addChild(actionBarOverflow);
			addChild( content );
			content.addChild(list);
			content.addChild(list2);
			content.addChild(actBar);

			actBar.x=0;
			actBar.y=stage.stageHeight-actBar.height;

			list.width=list2.width=stage.stageWidth;
			list.x=list2.x=0;
			list.height=(stage.stageHeight-actBar.height)/2;
			list2.height=(stage.stageHeight-actBar.height)/2;
			list.y=0;
			list2.y=list.height;
			
			list.addEventListener(ContextMenuEvent.OPENING, contextOpening);

			
			var actionsForBar:Vector.<ActionBase> = new Vector.<ActionBase>;
			actionsForBar.push(new ActionBase("Action 1"));
			actionsForBar.push(new ActionBase("Action 2"));
			var actionset:ActionSet=new ActionSet(actionsForBar);
			var contextactions:Vector.<ActionSet>=new Vector.<ActionSet>();
			contextactions.push(actionset);

			var actionsForBar2:Vector.<ActionBase> = new Vector.<ActionBase>;
			actionsForBar2.push(new ActionBase("Action 3"));
			actionsForBar2.push(new ActionBase("Action 4"));
			var actionset2:ActionSet=new ActionSet(actionsForBar2);
			var contextactions2:Vector.<ActionSet>=new Vector.<ActionSet>();
			contextactions2.push(actionset2);

			
			list.contextActions=contextactions;
			list2.contextActions=contextactions2;

			
			actBar.addEventListener( DragEvent.DRAG_MOVE, onDragMove );
		}
		
		private function contextOpening(e:ContextMenuEvent):void{
			trace("CANCEL OPENING");
			e.preventDefault();
		}
		
		private function onDragMove( event:DragEvent ):void
		{
			slideX += event.deltaX;
			content.x = Math.round( slideX + stage.stageWidth );
		}
	}
}

 

My apps for Playbook: PlayEpub Book Reader with epub and FB2 support, Google Reader, ReaditLater - Net Files Share Explorer Get access to your network shared files- FFSync Sync your Firefox bookmarks and passwords
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: (bb10) how to cancel a context menu opening

Why do you want to prevent the user from opening the context menu?
Developer
Posts: 223
Registered: ‎04-26-2012
My Device: Z10
My Carrier: Vodafone

Re: (bb10) how to cancel a context menu opening

Because depending on the pressed item I want to show it or not Smiley Tongue

i.e. a file browser. I want to show a context menu on file but don't want on folders.

My apps for Playbook: PlayEpub Book Reader with epub and FB2 support, Google Reader, ReaditLater - Net Files Share Explorer Get access to your network shared files- FFSync Sync your Firefox bookmarks and passwords
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: (bb10) how to cancel a context menu opening

[ Edited ]

Another way to solve this is to have two different ListItemComponents in your listview - one for files, and the other for folders. You can have a different context menu for each list item type by putting the ActionSet inside the ListItemComponent, rather than in the ListView:

 

ListView {
  listItemComponents: [

    ListItemComponent {
      type: "file"

      contextActions: [
          ActionSet {
             ActionItem {
                // will only show for "file" item
             }
          }
      ]
    }

    ListItemComponent {
      type: "folder"
    }
  }
}

 

All you need to do now is make sure your datamodel returns the correct itemtype for each item. The itemType() callback function in QML is a good place to start looking

Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: (bb10) how to cancel a context menu opening

Well, if you cannot get that to work, you can also disable items in the menu.
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: (bb10) how to cancel a context menu opening

AIR does not use QML.
Developer
Posts: 223
Registered: ‎04-26-2012
My Device: Z10
My Carrier: Vodafone

Re: (bb10) how to cancel a context menu opening

Although strobejb's could be a solution, it implies lots of changes on my code at this moment, but I think I found a good solution.

Instead of dealing with ContextMenu I forget it and override onLongPress method extending the list.

 

        override protected function onLongPress():void{

        }

 

And if I have to display context menu I can do from withing the method with:

showContextMenu()

 

 

My apps for Playbook: PlayEpub Book Reader with epub and FB2 support, Google Reader, ReaditLater - Net Files Share Explorer Get access to your network shared files- FFSync Sync your Firefox bookmarks and passwords
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: (bb10) how to cancel a context menu opening

yes sorry - please ignore my solution, I posted by mistake & didn't realise this was the AIR forum. Possibly a similar technique might exist in AIR though?
Highlighted
Developer
Posts: 223
Registered: ‎04-26-2012
My Device: Z10
My Carrier: Vodafone

Re: (bb10) how to cancel a context menu opening

I'm pretty sure don't, context actions are asigned to list, not to items, but I think you could have an approach defining ContextActions on the ItemRenderer for each one so as ContextMEnu is launched for the renderer and not for the list.

 

HAven't tested, but should work.

My apps for Playbook: PlayEpub Book Reader with epub and FB2 support, Google Reader, ReaditLater - Net Files Share Explorer Get access to your network shared files- FFSync Sync your Firefox bookmarks and passwords