* [MacOS] Add ContextActions

* [MacOS] Implement ContextActions
This commit is contained in:
Rui Marinho 2017-01-27 11:46:40 +00:00 коммит произвёл GitHub
Родитель 520ff4a227
Коммит de96051a2e
4 изменённых файлов: 98 добавлений и 31 удалений

Просмотреть файл

@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.ComponentModel;
using AppKit;
using CoreGraphics;
@ -11,6 +12,7 @@ namespace Xamarin.Forms.Platform.MacOS
static readonly CGColor s_defaultHeaderViewsBackground = NSColor.LightGray.CGColor;
Cell _cell;
readonly NSTableViewCellStyle _style;
NSView _contexActionsTrackingView;
public Action<object, PropertyChangedEventArgs> PropertyChanged;
@ -29,7 +31,7 @@ namespace Xamarin.Forms.Platform.MacOS
public NSView AccessoryView { get; private set; }
public Element Element => Cell;
public virtual Element Element => Cell;
public Cell Cell
{
@ -92,14 +94,26 @@ namespace Xamarin.Forms.Platform.MacOS
nfloat labelHeights = availableHeight;
nfloat labelWidth = availableWidth - imageWidth - accessoryViewWidth;
if (DetailTextLabel != null)
{
if (!string.IsNullOrEmpty(DetailTextLabel?.StringValue))
{
labelHeights = availableHeight / 2;
DetailTextLabel.CenterTextVertically(new CGRect(imageWidth + padding, 0, labelWidth, labelHeights));
}
}
TextLabel.CenterTextVertically(new CGRect(imageWidth + padding, availableHeight - labelHeights, labelWidth,
TextLabel?.CenterTextVertically(new CGRect(imageWidth + padding, availableHeight - labelHeights, labelWidth,
labelHeights));
var topNSView = Subviews.LastOrDefault();
if (_contexActionsTrackingView != topNSView)
{
_contexActionsTrackingView.RemoveFromSuperview();
_contexActionsTrackingView.Frame = Frame;
AddSubview(_contexActionsTrackingView, NSWindowOrderingMode.Above, Subviews.LastOrDefault());
}
base.Layout();
}
@ -129,7 +143,8 @@ namespace Xamarin.Forms.Platform.MacOS
void CreateUI()
{
var style = _style;
if (style != NSTableViewCellStyle.Empty)
{
AddSubview(TextLabel = new NSTextField
{
Bordered = false,
@ -163,5 +178,51 @@ namespace Xamarin.Forms.Platform.MacOS
AddSubview(AccessoryView = accessoryView);
}
}
AddSubview(_contexActionsTrackingView = new TrackingClickNSView());
}
}
class TrackingClickNSView : NSView
{
public override void RightMouseDown(NSEvent theEvent)
{
HandleContextActions(theEvent);
base.RightMouseDown(theEvent);
}
void HandleContextActions(NSEvent theEvent)
{
var contextActionCell = (Superview as INativeElementView).Element as Cell;
var contextActionsCount = contextActionCell.ContextActions.Count;
if (contextActionsCount > 0)
{
NSMenu menu = new NSMenu();
for (int i = 0; i < contextActionsCount; i++)
{
var contextAction = contextActionCell.ContextActions[i];
var nsMenuItem = GetNSMenuItem(i, contextAction);
menu.AddItem(nsMenuItem);
}
NSMenu.PopUpContextMenu(menu, theEvent, this);
}
}
static NSMenuItem GetNSMenuItem(int i, MenuItem contextAction)
{
var menuItem = new NSMenuItem(contextAction.Text ?? "");
menuItem.Tag = i;
menuItem.Enabled = contextAction.IsEnabled;
if (menuItem.Enabled)
menuItem.Activated += (sender, e) =>
{
((IMenuItemController)contextAction).Activate();
};
if (!string.IsNullOrEmpty(contextAction.Icon))
menuItem.Image = new NSImage(contextAction.Icon);
return menuItem;
}
}
}

Просмотреть файл

@ -7,6 +7,7 @@
Value2,
Subtitle,
Image,
ImageSubtitle
ImageSubtitle,
Empty
}
}

Просмотреть файл

@ -4,13 +4,18 @@ using RectangleF = CoreGraphics.CGRect;
namespace Xamarin.Forms.Platform.MacOS
{
public class ViewCellNSView : NSView, INativeElementView
internal class ViewCellNSView : CellNSView
{
public ViewCellNSView() : base(NSTableViewCellStyle.Empty)
{
}
WeakReference<IVisualElementRenderer> _rendererRef;
ViewCell _viewCell;
public Element Element => ViewCell;
public override Element Element => ViewCell;
public ViewCell ViewCell
{

Просмотреть файл

@ -20,7 +20,7 @@ namespace Xamarin.Forms.Platform.MacOS
ITemplatedItemsView<Cell> TemplatedItemsView => Element;
public const int DefaultRowHeight = 16;
public const int DefaultRowHeight = 44;
public NSTableView NativeTableView => _table;