Exemplo n.º 1
0
        private static IList <IMainMenuItem> OnMenuChanged(IAvaloniaObject targetLocation, IList <IMainMenuItem> viewModel)
        {
            NativeMenuItemBase item;

            foreach (var m in viewModel)
            {
                NativeMenuItem topLevelItem = new NativeMenuItem(m.ItemName.Replace("_", ""));
                topLevelItem.Menu = new NativeMenu();
                foreach (var subItem in m.SubItems)
                {
                    if (subItem.ItemName == "Separator")
                    {
                        item = new NativeMenuItemSeperator();
                    }
                    else
                    {
                        item = new NativeMenuItem(subItem.ItemName.Replace("_", ""));
                        if (subItem is IMenuCommandItem cmd)
                        {
                            ((NativeMenuItem)item).Command = cmd.ItemCommand;
                        }
                    }

                    topLevelItem.Menu.Add(item);
                }

                (targetLocation as NativeMenu).Add(topLevelItem);
            }
            return(viewModel);
        }
Exemplo n.º 2
0
        private static IList <IMainMenuItem> OnMenuChanged(IAvaloniaObject targetLocation, IList <IMainMenuItem> viewModel)
        {
            var systemWideControlModifier = AvaloniaLocator.Current
                                            .GetService <PlatformHotkeyConfiguration>()?.CommandModifiers ?? KeyModifiers.Control;

            NativeMenuItemBase item;

            foreach (var m in viewModel)
            {
                NativeMenuItem topLevelItem = new NativeMenuItem(m.ItemName.Replace("_", ""));
                topLevelItem.Menu = new NativeMenu();
                foreach (var subItem in m.SubItems)
                {
                    if (subItem.ItemName == "Separator")
                    {
                        item = new NativeMenuItemSeperator();
                    }
                    else
                    {
                        var nativeMenuItem = new NativeMenuItem(subItem.ItemName.Replace("_", ""));
                        if (subItem is IMenuCommandItem cmd)
                        {
                            nativeMenuItem.Command = cmd.ItemCommand;
                            if (cmd.Shortcut.HasValue && Enum.TryParse(cmd.Shortcut.Value.Key, out Key key))
                            {
                                var keyGesture = new KeyGesture(key, cmd.Shortcut.Value.Control ? systemWideControlModifier : KeyModifiers.None);
                                nativeMenuItem.Gesture = keyGesture;
                            }
                        }
                        if (subItem is ICheckableMenuItem checkable)
                        {
                            nativeMenuItem.ToggleType = NativeMenuItemToggleType.CheckBox;
                            checkable.ToObservable(o => o.IsChecked)
                            .SubscribeAction(@is => nativeMenuItem.IsChecked = @is);
                        }
                        item = nativeMenuItem;
                    }

                    topLevelItem.Menu.Add(item);
                }

                ((NativeMenu)targetLocation).Add(topLevelItem);
            }
            return(viewModel);
        }