Beispiel #1
0
        void UpdateGroup(NativeToolbarGroup group, IList <ToolbarItem> toolbarItems, double itemWidth, double itemSpacing)
        {
            int count = toolbarItems.Count;

            group.Items.Clear();
            if (count > 0)
            {
                var    subItems   = new NSToolbarItem[count];
                var    view       = new NSView();
                nfloat totalWidth = 0;
                var    currentX   = 0.0;
                for (int i = 0; i < toolbarItems.Count; i++)
                {
                    var element = toolbarItems[i];

                    var item = new NSToolbarItem(element.Text ?? "");
                    item.Activated += (sender, e) => ((IMenuItemController)element).Activate();

                    var button = new NSButton();
                    button.Title = element.Text ?? "";

                    button.SizeToFit();
                    var buttonWidth = itemWidth;
                    if (button.FittingSize.Width > itemWidth)
                    {
                        buttonWidth = button.FittingSize.Width + 10;
                    }
                    button.Frame      = new CGRect(currentX + i * itemSpacing, 0, buttonWidth, ToolbarItemHeight);
                    currentX         += buttonWidth;
                    totalWidth       += button.Frame.Width;
                    button.Activated += (sender, e) => ((IMenuItemController)element).Activate();

                    button.BezelStyle = NSBezelStyle.TexturedRounded;
                    _ = element.ApplyNativeImageAsync(ToolbarItem.IconImageSourceProperty, image =>
                    {
                        if (image != null)
                        {
                            button.Image = image;
                        }
                        button.SizeToFit();
                    });

                    button.Enabled           = item.Enabled = element.IsEnabled;
                    element.PropertyChanged -= ToolBarItemPropertyChanged;
                    element.PropertyChanged += ToolBarItemPropertyChanged;

                    view.AddSubview(button);
                    //item.Label = item.PaletteLabel = item.ToolTip = element.Text ?? "";

                    subItems[i] = item;

                    SetAccessibility(button, element);
                    group.Items.Add(new NativeToolbarGroup.Item {
                        ToolbarItem = item, Button = button, Element = element
                    });
                }
                view.Frame = new CGRect(0, 0, totalWidth + (itemSpacing * (count - 1)), ToolbarItemHeight);

                group.Group.Subitems = subItems;
                group.Group.View     = view;
            }
            else
            {
                group.Group.Subitems = new NSToolbarItem[] { };
                group.Group.View     = new NSView();
            }
        }