Ejemplo n.º 1
0
        public void SelectionPredicateDynamicSerializationTest()
        {
            const string path = "SelectionPredicateDynamicSave.json";

            SelectionPredicate expected = new SelectionPredicate {
                NeedSelected           = true,
                NeedLocked             = true,
                NeedGeneratedNotByThis = true,
                NeedGeneratedByThis    = false,
                MinRelevancy           = 0.66
            };

            ProjectManager.SaveJson(path, expected);

            dynamic actual = LoadJsonDynamic(path, (dynamic)expected);

            Assert.AreEqual(expected, (SelectionPredicate)actual);
        }
Ejemplo n.º 2
0
        public void SelectionPredicateSerializationTest()
        {
            const string path = "SelectionPredicateSave.json";

            SelectionPredicate expected = new SelectionPredicate {
                NeedSelected           = true,
                NeedLocked             = true,
                NeedGeneratedNotByThis = true,
                NeedGeneratedByThis    = false,
                MinRelevancy           = 0.66
            };

            ProjectManager.SaveJson(path, expected);

            SelectionPredicate actual = ProjectManager.LoadJson <SelectionPredicate>(path);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        private StackPanel GetSettingControl(PropertyInfo prop, object settings)
        {
            if (!prop.CanWrite || !prop.CanRead)
            {
                return(null);
            }
            if (!SettingsTypes.Contains(prop.PropertyType))
            {
                return(null);
            }

            var value = prop.GetValue(settings);

            if (value == null)
            {
                return(null);
            }

            var horizontalPanel = new StackPanel {
                Orientation = Orientation.Horizontal, Margin = new Thickness(10)
            };

            var name = new TextBlock {
                Width = 150
            };

            if (prop.GetCustomAttribute(typeof(DisplayNameAttribute)) is DisplayNameAttribute n)
            {
                name.Text = n.DisplayName;
            }
            else
            {
                name.Text = prop.Name;
            }
            if (prop.GetCustomAttribute(typeof(DescriptionAttribute)) is DescriptionAttribute d)
            {
                name.ToolTip = d.Description;
            }
            horizontalPanel.Children.Add(name);

            switch (value)
            {
            case bool _:
                var toggleButton = new ToggleButton {
                    Cursor = Cursors.Hand
                };
                Binding toggleBinding = new Binding(prop.Name)
                {
                    Source = settings
                };
                toggleButton.SetBinding(ToggleButton.IsCheckedProperty, toggleBinding);
                horizontalPanel.Children.Add(toggleButton);
                break;

            case double _:
                var doubleTextBox = new TextBox {
                    Width = 100
                };
                Binding doubleBinding = new Binding(prop.Name)
                {
                    Source    = settings,
                    Converter = new DoubleToStringConverter()
                };
                doubleTextBox.SetBinding(TextBox.TextProperty, doubleBinding);
                horizontalPanel.Children.Add(doubleTextBox);
                break;

            case string _:
                var stringTextBox = new TextBox {
                    Width = 100
                };
                Binding stringBinding = new Binding(prop.Name)
                {
                    Source = settings
                };
                stringTextBox.SetBinding(TextBox.TextProperty, stringBinding);
                horizontalPanel.Children.Add(stringTextBox);
                break;

            case SelectionPredicateCollection c:
                var list = new ListView {
                    Width = 270, ItemsSource = c.Predicates
                };
                list.ItemTemplate = FindResource("SelectionPredicateTemplate") as DataTemplate;
                var cm = new ContextMenu();
                cm.Items.Add(new MenuItem {
                    Header  = "Duplicate",
                    ToolTip = "Duplicate selected predicates.",
                    Command = new CommandImplementation(_ => {
                        var itemsToDupe = new SelectionPredicate[list.SelectedItems.Count];
                        var i           = 0;
                        foreach (var listSelectedItem in list.SelectedItems)
                        {
                            itemsToDupe[i++] = (SelectionPredicate)listSelectedItem;
                        }
                        foreach (var listSelectedItem in itemsToDupe)
                        {
                            c.Predicates.Insert(list.Items.IndexOf(listSelectedItem) + 1, (SelectionPredicate)listSelectedItem.Clone());
                        }
                    })
                });
                cm.Items.Add(new MenuItem {
                    Header  = "Remove",
                    ToolTip = "Remove all selected predicates",
                    Command = new CommandImplementation(_ =>
                                                        c.Predicates.RemoveAll(o => list.SelectedItems.Contains(o)))
                });
                list.ContextMenu = cm;

                var addButton = new Button {
                    Style             = FindResource("MaterialDesignFloatingActionMiniLightButton") as Style,
                    ToolTip           = "Add a new selection predicate.",
                    VerticalAlignment = VerticalAlignment.Bottom,
                    Content           = new PackIcon {
                        Kind = PackIconKind.Plus, Width = 24, Height = 24
                    },
                    Margin  = new Thickness(5),
                    Command = new CommandImplementation(_ => c.Predicates.Add(new SelectionPredicate()))
                };

                var removeButton = new Button {
                    Style             = FindResource("MaterialDesignFloatingActionMiniLightButton") as Style,
                    ToolTip           = "Remove all selected predicates or the last predicate if nothing is selected.",
                    VerticalAlignment = VerticalAlignment.Bottom,
                    Content           = new PackIcon {
                        Kind = PackIconKind.Minus, Width = 24, Height = 24
                    },
                    Margin  = new Thickness(5),
                    Command = new CommandImplementation(_ => {
                        if (list.SelectedItems.Count == 0 && c.Predicates.Count > 0)
                        {
                            c.Predicates.RemoveAt(c.Predicates.Count - 1);
                            return;
                        }
                        c.Predicates.RemoveAll(o => list.SelectedItems.Contains(o));
                    })
                };

                horizontalPanel.Children.Add(list);
                horizontalPanel.Children.Add(addButton);
                horizontalPanel.Children.Add(removeButton);
                break;
            }

            return(horizontalPanel);
        }