/// <summary> /// Adds items to the menu. /// </summary> /// <param name="itemsToAdd">Items to add.</param> public void AddItems(params KSStapleMenuItem[] itemsToAdd) { Debug.Assert(itemsToAdd != null, "Provide valid items for initialization!"); if (this.items == null) { this.items = new ObservableCollection <KSStapleMenuItem> (); } this.items.CollectionChanged -= this.HandleItemsCollectionChanged; for (int i = 0; i < itemsToAdd.Length; ++i) { KSStapleMenuItem currentItem = itemsToAdd[i]; this.items.Add(currentItem); currentItem.SizeElements(this.ItemSize); currentItem.OnLongPress += this.HandleItemLongPress; currentItem.OnSelect += this.HandleItemSelect; var containerView = new KSMenuItemHostView(this, currentItem.Id); // Make container wide enough to hold all items. containerView.Frame = new RectangleF(0, 0, this.ItemSize.Width * currentItem.NumberOfElements, this.ItemSize.Height); // Add element views to container view. for (int elementIndex = 0; elementIndex < currentItem.NumberOfElements; elementIndex++) { var itemView = currentItem.GetViewForIndex(elementIndex); containerView.AddSubview(itemView); if (this.Mode == STAPLEMENU_MODE.Right) { itemView.Center = new PointF(containerView.Bounds.Width - itemView.Bounds.Width / 2f, containerView.Bounds.Height / 2f); } else { itemView.Center = new PointF(itemView.Bounds.Width / 2f, containerView.Bounds.Height / 2f); } } this.AddSubview(containerView); } this.items.CollectionChanged += this.HandleItemsCollectionChanged; }
/// <summary> /// Expands an item. This automatically collapses a previously opened item. /// </summary> /// <param name="id">Identifier of the item to expand.</param> /// <param name="animated">If set to <c>true</c>, animate.</param> public void ExpandItem(string id, bool animated) { if (string.Compare(id, this.ExpandedItemId, StringComparison.OrdinalIgnoreCase) == 0) { return; } if (this.timer != null) { this.timer.Invalidate(); this.timer = null; } // Collapse current open item before expanding another one. this.Collapse(animated); KSMenuItemHostView hostView = null; foreach (KSMenuItemHostView subview in this.Subviews) { if (string.Compare(id, subview.ItemId, StringComparison.OrdinalIgnoreCase) == 0) { hostView = subview; break; } } if (hostView == null) { return; } ; this.ExpandedItemId = id; if (animated) { UIView.Animate(ANIMATION_EXPAND_SECONDS, 0f, UIViewAnimationOptions.CurveEaseOut, delegate { hostView.ExpandSubElements(); }, delegate { float newWidth = hostView.Bounds.Width; if (this.Mode == STAPLEMENU_MODE.Right) { this.Frame = new RectangleF(this.Superview.Bounds.Width - newWidth, this.Frame.Y, newWidth, this.Bounds.Height); } else { this.Frame = new RectangleF(0, this.Frame.X, newWidth, this.Bounds.Height); } }); } else { hostView.ExpandSubElements(); float newWidth = hostView.Bounds.Width; if (this.Mode == STAPLEMENU_MODE.Right) { this.Frame = new RectangleF(this.Superview.Bounds.Width - newWidth, this.Frame.Y, newWidth, this.Bounds.Height); } else { this.Frame = new RectangleF(0, this.Frame.X, newWidth, this.Bounds.Height); } } // Create a timer that collapses the expanded item after a while. this.timer = NSTimer.CreateTimer(AUTO_CLOSE_TIMEOUT_SECS, () => this.Collapse(animated)); NSRunLoop.Current.AddTimer(this.timer, NSRunLoopMode.Default); }
/// <summary> /// Collapses the current expanded item (if any). /// </summary> public void Collapse(bool animated) { if (this.timer != null) { this.timer.Invalidate(); this.timer = null; } if (this.ExpandedItemId == null) { return; } KSMenuItemHostView hostView = null; foreach (KSMenuItemHostView subview in this.Subviews) { if (string.Compare(this.ExpandedItemId, subview.ItemId, StringComparison.OrdinalIgnoreCase) == 0) { hostView = subview; break; } } this.ExpandedItemId = null; if (hostView == null) { return; } ; if (animated) { UIView.Animate(ANIMATION_COLLAPSE_SECONDS, 0f, UIViewAnimationOptions.CurveEaseIn, delegate { hostView.CollapseSubElements(); }, delegate { float newWidth = this.ItemSize.Width; if (this.Mode == STAPLEMENU_MODE.Right) { this.Frame = new RectangleF(this.Superview.Bounds.Width - newWidth, this.Frame.Y, newWidth, this.Bounds.Height); } else { this.Frame = new RectangleF(0, this.Frame.X, newWidth, this.Bounds.Height); } }); } else { hostView.CollapseSubElements(); float newWidth = this.ItemSize.Width; if (this.Mode == STAPLEMENU_MODE.Right) { this.Frame = new RectangleF(this.Superview.Bounds.Width - newWidth, this.Frame.Y, newWidth, this.Bounds.Height); } else { this.Frame = new RectangleF(0, this.Frame.X, newWidth, this.Bounds.Height); } } }
/// <summary> /// Adds items to the menu. /// </summary> /// <param name="itemsToAdd">Items to add.</param> public void AddItems(params KSStapleMenuItem[] itemsToAdd) { Debug.Assert (itemsToAdd != null, "Provide valid items for initialization!"); if (this.items == null) { this.items = new ObservableCollection<KSStapleMenuItem> (); } this.items.CollectionChanged -= this.HandleItemsCollectionChanged; for(int i = 0; i < itemsToAdd.Length; ++i) { KSStapleMenuItem currentItem = itemsToAdd[i]; this.items.Add (currentItem); currentItem.SizeElements(this.ItemSize); currentItem.OnLongPress += this.HandleItemLongPress; currentItem.OnSelect += this.HandleItemSelect; var containerView = new KSMenuItemHostView(this, currentItem.Id); // Make container wide enough to hold all items. containerView.Frame = new RectangleF(0, 0, this.ItemSize.Width * currentItem.NumberOfElements, this.ItemSize.Height); // Add element views to container view. for(int elementIndex = 0; elementIndex < currentItem.NumberOfElements; elementIndex++) { var itemView = currentItem.GetViewForIndex(elementIndex); containerView.AddSubview(itemView); if(this.Mode == STAPLEMENU_MODE.Right) { itemView.Center = new PointF(containerView.Bounds.Width - itemView.Bounds.Width /2f, containerView.Bounds.Height / 2f); } else { itemView.Center = new PointF(itemView.Bounds.Width /2f, containerView.Bounds.Height / 2f); } } this.AddSubview(containerView); } this.items.CollectionChanged += this.HandleItemsCollectionChanged; }