Exemple #1
0
        internal static SelectorMatch Evaluate(
            ILogical logical, IChildIndexProvider childIndexProvider,
            int step, int offset, bool reversed)
        {
            var index = childIndexProvider.GetChildIndex(logical);

            if (index < 0)
            {
                return(SelectorMatch.NeverThisInstance);
            }

            if (reversed)
            {
                if (childIndexProvider.TryGetTotalCount(out var totalCountValue))
                {
                    index = totalCountValue - index;
                }
                else
                {
                    return(SelectorMatch.NeverThisInstance);
                }
            }
            else
            {
                // nth child index is 1-based
                index += 1;
            }

            var n = Math.Sign(step);

            var diff  = index - offset;
            var match = diff == 0 || (Math.Sign(diff) == n && diff % step == 0);

            return(match ? SelectorMatch.AlwaysThisInstance : SelectorMatch.NeverThisInstance);
        }
Exemple #2
0
        private static SelectorMatch MatchDescendent(IStyleable control, Selector previous)
        {
            ILogical c = (ILogical)control;
            List <IObservable <bool> > descendentMatches = new List <IObservable <bool> >();

            while (c != null)
            {
                c = c.LogicalParent;

                if (c is IStyleable)
                {
                    var match = previous.Match((IStyleable)c);

                    if (match.ImmediateResult != null)
                    {
                        if (match.ImmediateResult == true)
                        {
                            return(SelectorMatch.True);
                        }
                    }
                    else
                    {
                        descendentMatches.Add(match.ObservableResult);
                    }
                }
            }

            return(new SelectorMatch(StyleActivator.Or(descendentMatches)));
        }
Exemple #3
0
        /// <summary>
        /// returns the items control from the container
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public static ItemsControl ItemsControlFromItemContainer(AvaloniaObject container)
        {
#warning needs to be checked

            ILogical uiLogical = container as ILogical;
            if (uiLogical == null)
            {
                return(null);
            }

            // ui appeared in items collection
            ItemsControl ic = LogicalExtensions.GetLogicalParent(uiLogical) as ItemsControl;
            if (ic != null)
            {
                // this is the right ItemsControl as long as the item
                // is (or is eligible to be) its own container
                //IGeneratorHost host = ic as IGeneratorHost;
                //if (host.IsItemItsOwnContainer(ui))
                //    return ic;
                //else
                //    return null;
            }

            IVisual uiVisual = container as IVisual;

            uiVisual = VisualTree.VisualExtensions.GetVisualParent <IVisual>(uiVisual);

            return(GetItemsOwner(uiLogical as AvaloniaObject));
        }
Exemple #4
0
 private static void ValidateLogicalChild(ILogical c)
 {
     if (c == null)
     {
         throw new ArgumentException("Cannot add null to LogicalChildren.");
     }
 }
        public override void Prepare(string[] splits)
        {
            if (splits.Length < 2)
            {
                throw new Exception("Expected at least 2 tokens in an if-statement");
            }

            if (!Enum.TryParse(splits[1], out eLogicType logic))
            {
                throw new Exception($"Unable to recognize logical check type named {splits[1]}");
            }

            this._logical = logic switch
            {
                eLogicType.incareer_exists => new InCareerExistsLogicalCheck(),
                eLogicType.texture_exists => new TextureExistsLogicalCheck(),
                eLogicType.string_exists => new StringExistsLogicalCheck(),
                eLogicType.file_exists => new FileExistsLogicalCheck(),
                eLogicType.directory_exists => new DirectoryExistsLogicalCheck(),
                eLogicType.collection_value_equals => new CollectionValueEqualsLogicalCheck(),
                eLogicType.incareer_value_equals => new InCareerValueEqualsLogicalCheck(),
                eLogicType.texture_value_equals => new TextureValueEqualsLogicalCheck(),
                eLogicType.string_value_equals => new StringValueEqualsLogicalCheck(),
                _ => new CollectionExistsLogicalCheck(),
            };

            this._logical.Parse(splits);
        }
Exemple #6
0
        /// <summary>
        /// returns the children by control
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="forceUsingTheVisualTreeHelper"></param>
        /// <returns></returns>
        public static IEnumerable <IControl> GetChildObjects(this IControl parent, bool forceUsingTheVisualTreeHelper = false)
        {
            if (parent == null)
            {
                yield break;
            }

            ILogical parentLogical = parent as ILogical;

            if (!forceUsingTheVisualTreeHelper && (parentLogical != null))
            {
                foreach (var item in LogicalExtensions.GetLogicalChildren(parentLogical))
                {
                    IControl avalonia = item as IControl;
                    if (avalonia != null)
                    {
                        yield return(item as IControl);
                    }
                }
            }
            else
            {
                IVisual visual = parent as IVisual;

                foreach (var item in VisualTree.VisualExtensions.GetVisualChildren(visual))
                {
                    IControl avalonia = item as IControl;
                    if (avalonia != null)
                    {
                        yield return(item as IControl);
                    }
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Sets the control's logical parent.
        /// </summary>
        /// <param name="parent">The parent.</param>
        void ISetLogicalParent.SetParent(ILogical parent)
        {
            var old = Parent;

            if (parent != old)
            {
                if (old != null && parent != null)
                {
                    throw new InvalidOperationException("The Control already has a parent.");
                }

                InheritanceParent = parent as PerspexObject;
                _parent           = (IControl)parent;

                var root = FindStyleRoot(old);

                if (root != null)
                {
                    var e = new LogicalTreeAttachmentEventArgs(root);
                    OnDetachedFromLogicalTree(e);
                }

                root = FindStyleRoot(this);

                if (root != null)
                {
                    var e = new LogicalTreeAttachmentEventArgs(root);
                    OnAttachedToLogicalTree(e);
                }

                RaisePropertyChanged(ParentProperty, old, _parent, BindingPriority.LocalValue);
            }
        }
Exemple #8
0
        public void RemoveLogical(ILogical logical)
        {
            if (logical == null)
            {
                throw new ArgumentException("Logical cannot be null!");
            }
            if (!Loaded)
            {
                throw new InvalidOperationException("This world object has not been loaded! Please call the \"Load()\" method before adding jobs!");
            }
            if (IsDisposing)
            {
                throw new InvalidOperationException("This world object is currently disposing, can not remove logical item..");
            }

            if (!lFetch)
            {
                logics.Remove(logical);
            }
            else
            {
                if (lCache.Contains(logical))
                {
                    lCache.Remove(logical);
                }
                else
                {
                    if (logics.Contains(logical))
                    {
                        lToRemove.Add(logical);
                    }
                }
            }
        }
        public static IEnumerable <ILogical> GetSelfAndLogicalAncestors(this ILogical logical)
        {
            yield return(logical);

            foreach (var ancestor in logical.GetLogicalAncestors())
            {
                yield return(ancestor);
            }
        }
Exemple #10
0
                static void SetButtonText(ToggleButton button, string text)
                {
                    ILogical textBlock = button.GetLogicalDescendants().First(x => x is TextBlock);

                    if (textBlock != null && textBlock is TextBlock block)
                    {
                        block.Text = text;
                    }
                }
 /// <summary>
 /// Initializes a new instance of the <see cref="LogicalTreeAttachmentEventArgs"/> class.
 /// </summary>
 /// <param name="root">The root of the logical tree.</param>
 /// <param name="source">The control being attached/detached.</param>
 /// <param name="parent">The <see cref="Parent"/>.</param>
 public LogicalTreeAttachmentEventArgs(
     ILogicalRoot root,
     ILogical source,
     ILogical?parent)
 {
     Root   = root ?? throw new ArgumentNullException(nameof(root));
     Source = source ?? throw new ArgumentNullException(nameof(source));
     Parent = parent;
 }
        /// <summary>
        /// Enumerates an <see cref="ILogical"/> and its descendants in the logical tree.
        /// </summary>
        /// <param name="logical">The logical.</param>
        /// <returns>The logical and its ancestors.</returns>
        public static IEnumerable <ILogical> GetSelfAndLogicalDescendants(this ILogical logical)
        {
            yield return(logical);

            foreach (var descendent in logical.GetLogicalDescendants())
            {
                yield return(descendent);
            }
        }
        public static INameScope FindNameScope(this ILogical control)
        {
            Contract.Requires <ArgumentNullException>(control != null);

            return(control.GetSelfAndLogicalAncestors()
                   .OfType <StyledElement>()
                   .Select(x => (x as INameScope) ?? NameScope.GetNameScope(x))
                   .FirstOrDefault(x => x != null));
        }
Exemple #14
0
 public LogicalTreeNode(IAvaloniaObject avaloniaObject, TreeNode?parent)
     : base(avaloniaObject, parent)
 {
     Children = avaloniaObject switch
     {
         ILogical logical => new LogicalTreeNodeCollection(this, logical),
         Controls.Application host => new ApplicationHostLogical(this, host),
         _ => TreeNodeCollection.Empty
     };
 }
        /// <summary>
        /// Registers the <paramref name="root"/> control and all of it's children to the <see cref="BindingContext"/> of the specified <paramref name="key"/> in <see cref="Bindings"/>.
        /// </summary>
        /// <param name="root">The root of the avalonia logical sub tree that should be registered.</param>
        /// <param name="key">The <see cref="Type"/> linked by the <see cref="Bindings"/> to the target <see cref="BindingContext"/>.</param>
        public void RegisterControls(ILogical root, Type key)
        {
            Queue <IEnumerable <ILogical> > logicals = new Queue <IEnumerable <ILogical> >();

            logicals.Enqueue(new List <ILogical>()
            {
                root
            });
            RegisterControls(logicals, key);
        }
Exemple #16
0
        public static INameScope?FindNameScope(this ILogical control)
        {
            _ = control ?? throw new ArgumentNullException(nameof(control));

            var scope = control.GetSelfAndLogicalAncestors()
                        .OfType <StyledElement>()
                        .Select(x => (x as INameScope) ?? NameScope.GetNameScope(x))
                        .FirstOrDefault(x => x != null);

            return(scope);
        }
Exemple #17
0
            private void OwnerChanged(ILogical c)
            {
                _propertySubscription?.Dispose();
                _propertySubscription = null;

                if (c is TabControl tabControl)
                {
                    _propertySubscription = tabControl.GetObservable(_ownerProperty)
                                            .Subscribe(x => PublishNext(x));
                }
            }
Exemple #18
0
        /// <summary>
        /// Sets the control's logical parent.
        /// </summary>
        /// <param name="parent">The parent.</param>
        void ISetLogicalParent.SetParent(ILogical parent)
        {
            var old = Parent;

            if (old != null && parent != null)
            {
                throw new InvalidOperationException("The Control already has a parent.");
            }

            SetAndRaise(ParentProperty, ref _parent, (IControl)parent);
        }
Exemple #19
0
 public NthChildActivator(
     ILogical control,
     IChildIndexProvider provider,
     int step, int offset, bool reversed)
 {
     _control  = control;
     _provider = provider;
     _step     = step;
     _offset   = offset;
     _reversed = reversed;
 }
        internal static int CalculateDistanceFromLogicalParent <T>(ILogical logical, int @default = -1) where T : class, ILogical
        {
            var result = 0;

            while (logical != null && !(logical is T))
            {
                ++result;
                logical = logical.LogicalParent;
            }

            return(logical != null ? result : @default);
        }
Exemple #21
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LogicalTreeAttachmentEventArgs"/> class.
        /// </summary>
        /// <param name="root">The root of the logical tree.</param>
        /// <param name="source">The control being attached/detached.</param>
        /// <param name="parent">The <see cref="Parent"/>.</param>
        public LogicalTreeAttachmentEventArgs(
            IStyleRoot root,
            ILogical source,
            ILogical parent)
        {
            Contract.Requires <ArgumentNullException>(root != null);
            Contract.Requires <ArgumentNullException>(source != null);

            Root   = root;
            Source = source;
            Parent = parent;
        }
 public static bool TryFind <T>(this ILogical scope, string name, out T result) where T : class
 {
     try
     {
         result = scope.Find <T>(name);
     }
     catch
     {
         result = null;
     }
     return(result != null);
 }
        public static IEnumerable <ILogical> GetLogicalDescendants(this ILogical logical)
        {
            foreach (ILogical child in logical.LogicalChildren)
            {
                yield return(child);

                foreach (ILogical descendant in child.GetLogicalDescendants())
                {
                    yield return(descendant);
                }
            }
        }
        /// <summary>
        /// Registers the <paramref name="root"/> control and all of it's children to a new <see cref="BindingContext"/>.
        /// </summary>
        /// <param name="root">The root of the avalonia logical sub tree that should be registered to the new <see cref="BindingContext"/>.</param>
        public static BindingContext RegisterControlsToBindingContext(ILogical root)
        {
            BindingContext bindingContext            = new BindingContext();
            Queue <IEnumerable <ILogical> > logicals = new Queue <IEnumerable <ILogical> >();

            logicals.Enqueue(new List <ILogical>()
            {
                root
            });
            RegisterControls(logicals, bindingContext);
            return(bindingContext);
        }
        public static IEnumerable <ILogical> GetLogicalSiblings(this ILogical logical)
        {
            ILogical parent = logical.LogicalParent;

            if (parent != null)
            {
                foreach (ILogical sibling in parent.LogicalChildren)
                {
                    yield return(sibling);
                }
            }
        }
Exemple #26
0
        private static int CalculateDistanceFromLogicalParent <T>(ILogical logical, int @default = -1) where T : class
        {
            var result = 0;

            while (logical != null && logical.GetType() != typeof(T))
            {
                ++result;
                logical = logical.LogicalParent;
            }

            return(logical != null ? result : @default);
        }
Exemple #27
0
        /// <summary>
        /// Enumerates the ancestors of an <see cref="ILogical"/> in the logical tree.
        /// </summary>
        /// <param name="logical">The logical.</param>
        /// <returns>The logical's ancestors.</returns>
        public static IEnumerable <ILogical> GetLogicalAncestors(this ILogical logical)
        {
            _ = logical ?? throw new ArgumentNullException(nameof(logical));

            ILogical?l = logical.LogicalParent;

            while (l != null)
            {
                yield return(l);

                l = l.LogicalParent;
            }
        }
        public static IEnumerable <ILogical> GetLogicalAncestors(this ILogical logical)
        {
            Contract.Requires <ArgumentNullException>(logical != null);

            logical = logical.LogicalParent;

            while (logical != null)
            {
                yield return(logical);

                logical = logical.LogicalParent;
            }
        }
        /// <summary>
        /// Finds first descendant of given type.
        /// </summary>
        /// <typeparam name="T">Descendant type.</typeparam>
        /// <param name="logical">The logical.</param>
        /// <param name="includeSelf">If given logical should be included in search.</param>
        /// <returns>First descendant of given type.</returns>
        public static T FindLogicalDescendantOfType <T>(this ILogical logical, bool includeSelf = false) where T : class
        {
            if (logical is null)
            {
                return(null);
            }

            if (includeSelf && logical is T result)
            {
                return(result);
            }

            return(FindDescendantOfTypeCore <T>(logical));
        }
Exemple #30
0
        /// <summary>
        /// Requests that the visual children of the panel use another control as their logical
        /// parent.
        /// </summary>
        /// <param name="logicalParent">
        /// The logical parent for the visual children of the panel.
        /// </param>
        /// <param name="children">
        /// The <see cref="ILogical.LogicalChildren"/> collection to modify.
        /// </param>
        void IReparentingControl.ReparentLogicalChildren(ILogical logicalParent, IPerspexList<ILogical> children)
        {
            Contract.Requires<ArgumentNullException>(logicalParent != null);
            Contract.Requires<ArgumentNullException>(children != null);

            _childLogicalParent = logicalParent;
            RedirectLogicalChildren(children);

            foreach (var control in Children)
            {
                ((ISetLogicalParent)control).SetParent(null);
                ((ISetLogicalParent)control).SetParent((IControl)logicalParent);
                children.Add(control);
            }
        }
Exemple #31
0
        /// <summary>
        /// Requests that the visual children of the panel use another control as their logical
        /// parent.
        /// </summary>
        /// <param name="logicalParent">
        /// The logical parent for the visual children of the panel.
        /// </param>
        /// <param name="children">
        /// The <see cref="ILogical.LogicalChildren"/> collection to modify.
        /// </param>
        void IReparentingControl.ReparentLogicalChildren(ILogical logicalParent, IPerspexList <ILogical> children)
        {
            Contract.Requires <ArgumentNullException>(logicalParent != null);
            Contract.Requires <ArgumentNullException>(children != null);

            _childLogicalParent = logicalParent;
            RedirectLogicalChildren(children);

            foreach (var control in Children)
            {
                ((ISetLogicalParent)control).SetParent(null);
                ((ISetLogicalParent)control).SetParent((IControl)logicalParent);
                children.Add(control);
            }
        }
Exemple #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Panel"/> class.
 /// </summary>
 public Panel()
 {
     _children.CollectionChanged += ChildrenChanged;
     _childLogicalParent = this;
 }
 public LogicalTreeNode(ILogical logical, TreeNode parent)
     : base((Control)logical, parent)
 {
     Children = logical.LogicalChildren.CreateDerivedCollection(x => new LogicalTreeNode(x, this));
 }
Exemple #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Panel"/> class.
 /// </summary>
 public Panel()
 {
     this.children.CollectionChanged += this.ChildrenChanged;
     this.childLogicalParent = this;
 }
Exemple #35
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Panel"/> class.
 /// </summary>
 public Panel()
 {
     this.childLogicalParent = this;
 }
Exemple #36
0
 public LogicalTreeNode(ILogical logical)
     : base((Control)logical)
 {
     Children = logical.LogicalChildren.CreateDerivedCollection(x => new LogicalTreeNode(x));
 }