Exemplo n.º 1
0
        private async void SetType()
        {
            using (await AsyncWork.RequestAsyncWork(this)) {
                ITypeInfo selectedType = null;
                var       args         = new TypeRequestedEventArgs();
                TypeRequested?.Invoke(this, args);
                if (args.SelectedType == null)
                {
                    return;
                }

                try {
                    selectedType = await args.SelectedType;
                    if (selectedType == null)
                    {
                        return;
                    }
                } catch (OperationCanceledException) {
                    return;
                }

                await SetValueAsync(new ValueInfo <ITypeInfo> {
                    Value  = selectedType,
                    Source = ValueSource.Local
                });
            }
        }
        private async Task <IReadOnlyList <PropertyViewModel> > GetViewModelsAsync(IPropertyInfo property)
        {
            PropertyViewModel baseVm = CreateViewModel(property);
            var vms = new List <PropertyViewModel> {
                baseVm
            };

            if (baseVm.HasVariations)
            {
                using (await AsyncWork.RequestAsyncWork(this)) {
                    var variants = await GetVariationsAsync(property);

                    baseVm.HasVariantChildren = variants.Count > 0;
                    if (baseVm.HasVariantChildren)
                    {
                        foreach (PropertyVariation variant in variants)
                        {
                            vms.Add(CreateViewModel(property, variant));
                        }
                    }
                }
            }

            return(vms);
        }
Exemplo n.º 3
0
        private async Task <IReadOnlyList <PropertyViewModel> > GetViewModelsAsync(IPropertyInfo property, IEnumerable <PropertyVariation> variantsToSkip = null)
        {
            PropertyViewModel        baseVm = GetViewModel(property);
            List <PropertyViewModel> vms    = new List <PropertyViewModel> ();

            vms.Add(baseVm);

            HashSet <PropertyVariation> skipped =
                (variantsToSkip != null) ? new HashSet <PropertyVariation> (variantsToSkip) : null;

            if (baseVm.HasVariations)
            {
                using (await AsyncWork.RequestAsyncWork(this)) {
                    var variants = await GetVariationsAsync(property);

                    for (int i = 0; i < variants.Length; i++)
                    {
                        foreach (PropertyVariation variant in variants[i])
                        {
                            if (skipped != null && skipped.Contains(variant))
                            {
                                continue;
                            }

                            vms.Add(GetViewModel(property, variant));
                        }
                    }
                }
            }

            return(vms);
        }
Exemplo n.º 4
0
        protected override async Task UpdateCurrentValueAsync()
        {
            if (this.predefinedValues == null)
            {
                return;
            }

            using (await AsyncWork.RequestAsyncWork(this)) {
                await base.UpdateCurrentValueAsync();

                var newValues = new Dictionary <string, bool?> (this.predefinedValues.PredefinedValues.Count);

                ValueInfo <IReadOnlyList <TValue> >[] values = await Task.WhenAll(Editors.Select (ed => ed.GetValueAsync <IReadOnlyList <TValue> > (Property, Variation)).ToArray());

                foreach (ValueInfo <IReadOnlyList <TValue> > valueInfo in values)
                {
                    if (valueInfo.Value == null || valueInfo.Source == ValueSource.Unset)
                    {
                        foreach (var kvp in this.predefinedValues.PredefinedValues)
                        {
                            newValues[kvp.Key] = null;
                        }

                        continue;
                    }

                    foreach (var kvp in this.predefinedValues.PredefinedValues)
                    {
                        bool currentValue = valueInfo.Value.Contains(kvp.Value);
                        if (newValues.TryGetValue(kvp.Key, out bool?presentValue))
                        {
                            if (presentValue.HasValue && presentValue.Value != currentValue)
                            {
                                newValues[kvp.Key] = null;
                            }
                        }
                        else
                        {
                            newValues[kvp.Key] = currentValue;
                        }
                    }
                }

                this.fromUpdate = true;
                foreach (var vm in Choices)
                {
                    if (newValues.TryGetValue(vm.Name, out bool?value))
                    {
                        vm.IsFlagged = value;
                    }
                    else
                    {
                        vm.IsFlagged = false;
                    }
                }
                this.fromUpdate = false;
            }
        }
Exemplo n.º 5
0
        private async void CreateInstance()
        {
            IsCreateInstancePending = true;

            try {
                using (await AsyncWork.RequestAsyncWork(this)) {
                    ITypeInfo selectedType = null;

                    var types = await AssignableTypes.Task;
                    // If there's only one assignable type, we'll skip selection
                    if (types.Count == 1)
                    {
                        var kvp = types.First();
                        if (kvp.Value.Count == 1)
                        {
                            var group = kvp.Value.First();
                            if (!group.Skip(1).Any())
                            {
                                selectedType = group.First();
                            }
                        }
                    }

                    if (selectedType == null)
                    {
                        var args = new TypeRequestedEventArgs();
                        TypeRequested?.Invoke(this, args);
                        if (args.SelectedType == null)
                        {
                            return;
                        }

                        try {
                            selectedType = await args.SelectedType;
                            if (selectedType == null)
                            {
                                return;
                            }
                        } catch (OperationCanceledException) {
                            return;
                        }
                    }

                    await SetValueAsync(new ValueInfo <object> {
                        Value           = await TargetPlatform.EditorProvider.CreateObjectAsync(selectedType),
                        ValueDescriptor = selectedType,
                        Source          = ValueSource.Local
                    });
                }
            } finally {
                IsCreateInstancePending = false;
            }
        }
        protected override async Task UpdateCurrentValueAsync()
        {
            if (Event == null)
            {
                return;
            }
            if (Editors.Count == 0)
            {
                SetCurrentMethods(null);
                return;
            }

            using (await AsyncWork.RequestAsyncWork(this)) {
                // Right now we only show events if one item is selected, but there's no technical reason
                // we can't attach the same event to the same handler across multiple objects, so the ground
                // work is done.
                IReadOnlyList <string>[] methodLists = await Task.WhenAll(Editors.OfType <IObjectEventEditor>().Select(ed => ed.GetHandlersAsync(Event)));

                bool disagree = false;
                IReadOnlyList <string> methods = methodLists[0];
                for (int i = 1; i < methodLists.Length; i++)
                {
                    IReadOnlyList <string> methodList = methodLists[i];
                    if (methodList.Count != methods.Count)
                    {
                        disagree = true;
                        break;
                    }

                    for (int x = 0; x < methodList.Count; i++)
                    {
                        if (methodList[x] != methods[x])
                        {
                            disagree = true;
                            break;
                        }
                    }

                    if (disagree)
                    {
                        break;
                    }
                }

                MultipleValues = disagree;
                SetCurrentMethods((!disagree) ? methods : null);
            }
        }
        private async void UpdateMaxMin()
        {
            bool isDefault = true;
            T    max = default(T), min = default(T);

            if (this.selfConstraint != null)
            {
                isDefault = false;
                max       = this.selfConstraint.MaxValue;
                min       = this.selfConstraint.MinValue;
            }

            if (this.clampProperties != null && Editors.Count > 0)
            {
                bool doMax = this.clampProperties.MaximumProperty != null;
                bool doMin = this.clampProperties.MinimumProperty != null;

                using (await AsyncWork.RequestAsyncWork(this)) {
                    // TODO: max/min property get error case
                    foreach (IObjectEditor editor in Editors)
                    {
                        if (doMax)
                        {
                            ValueInfo <T> maxinfo = await editor.GetValueAsync <T> (this.clampProperties.MaximumProperty);

                            max = (isDefault) ? maxinfo.Value : Min(max, maxinfo.Value);
                        }

                        if (doMin)
                        {
                            ValueInfo <T> mininfo = await editor.GetValueAsync <T> (this.clampProperties.MinimumProperty);

                            min = (isDefault) ? mininfo.Value : Max(min, mininfo.Value);
                        }
                    }
                }
            }

            MaximumValue = max;
            MinimumValue = min;
        }
Exemplo n.º 8
0
        protected override async Task UpdateCurrentValueAsync()
        {
            if (ValueModel == null)
            {
                return;
            }

            using (await AsyncWork.RequestAsyncWork(this)) {
                await base.UpdateCurrentValueAsync();

                ValueType = CurrentValue?.ValueDescriptor as ITypeInfo;

                if (CurrentValue?.Value != null)
                {
                    ValueModel.SelectedObjects.Reset(new[] { CurrentValue.Value });
                }
                else
                {
                    ValueModel.SelectedObjects.Clear();
                }

                SetCanDelve(ValueModel.SelectedObjects.Count > 0);
            }
        }
Exemplo n.º 9
0
        private async void OnVariationsChanged(object sender, EventArgs e)
        {
            using (await AsyncWork.RequestAsyncWork(this)) {
                PropertyViewModel pvm = (PropertyViewModel)sender;
                var variations        = (await GetVariationsAsync(pvm.Property)).SelectMany(vs => vs).Distinct();
                var properties        = this.editors
                                        .OfType <PropertyViewModel> ()
                                        .Where(evm => Equals(evm.Property, pvm.Property) && evm.Variation != null)
                                        .ToDictionary(evm => evm.Variation);

                List <PropertyViewModel> toAdd = new List <PropertyViewModel> ();
                foreach (PropertyVariation variation in variations)
                {
                    if (!properties.Remove(variation))
                    {
                        toAdd.Add(GetViewModel(pvm.Property, variation));
                    }
                }

                if (properties.Count > 0)
                {
                    var toRemove = new List <PropertyViewModel> ();
                    foreach (var kvp in properties)
                    {
                        toRemove.Add(kvp.Value);
                    }

                    RemoveProperties(toRemove);
                }

                if (toAdd.Count > 0)
                {
                    AddProperties(toAdd);
                }
            }
        }
Exemplo n.º 10
0
        private async Task PushValuesAsync(FlaggableChoiceViewModel <TValue> changedChoice)
        {
            SetError(null);

            using (await AsyncWork.RequestAsyncWork(this)) {
                try {
                    // Snapshot current choices so we don't catch updates mid-push for multi-editors
                    var currentChoices = Choices.ToDictionary(c => c, c => c.IsFlagged);

                    foreach (IObjectEditor editor in Editors)
                    {
                        ValueInfo <IReadOnlyList <TValue> > value = await editor.GetValueAsync <IReadOnlyList <TValue> > (Property, Variation);

                        HashSet <TValue> current;
                        if (value.Value == null || value.Source == ValueSource.Unset)
                        {
                            current = new HashSet <TValue> ();
                        }
                        else
                        {
                            current = new HashSet <TValue> (value.Value);
                        }

                        foreach (var choice in currentChoices)
                        {
                            if (!choice.Value.HasValue)
                            {
                                continue;
                            }

                            if (choice.Value.Value)
                            {
                                current.Add(choice.Key.Value);
                            }
                            else
                            {
                                current.Remove(choice.Key.Value);
                            }
                        }

                        IReadOnlyList <TValue> values = current.ToArray();
                        if (this.validator != null)
                        {
                            if (!this.validator.IsValid(values))
                            {
                                // Some combinables simply don't have a valid "none", but if we're going from indeterminate we still need to
                                // update the value, so we'll flip the changed value to true in that case so we don't go right back to indeterminate
                                if (values.Count == 0)
                                {
                                    changedChoice.IsFlagged = true;
                                    // We're explicitly triggering a change and need the update here so we need to update our snapshot.
                                    currentChoices = Choices.ToDictionary(c => c, c => c.IsFlagged);
                                }

                                continue;
                            }
                        }

                        if (this.coerce != null)
                        {
                            values = this.coerce.CoerceValue(values);
                        }

                        await editor.SetValueAsync(Property, new ValueInfo <IReadOnlyList <TValue> > {
                            Source = ValueSource.Local,
                            Value  = values
                        });
                    }
                } catch (Exception ex) {
                    if (ex is AggregateException aggregate)
                    {
                        aggregate = aggregate.Flatten();
                        ex        = aggregate.InnerExceptions[0];
                    }

                    SetError(ex.ToString());
                }
            }
        }
        private async void OnVariantsChanged(object sender, EventArgs e)
        {
            IPropertyInfo property = sender as IPropertyInfo;

            if (property == null)
            {
                property = ((PropertyViewModel)sender).Property;
            }

            using (await AsyncWork.RequestAsyncWork(this)) {
                var variationsTask = GetVariationsAsync(property);

                PropertyViewModel baseVm = null;
                var properties           = new Dictionary <PropertyVariation, PropertyViewModel> ();
                foreach (PropertyViewModel pvm in this.editors.OfType <PropertyViewModel> ())
                {
                    if (!Equals(property, pvm.Property))
                    {
                        continue;
                    }

                    if (pvm.Variation == null)
                    {
                        baseVm = pvm;
                    }
                    else
                    {
                        properties.Add(pvm.Variation, pvm);
                    }
                }

                if (baseVm == null)
                {
                    throw new InvalidOperationException("Base property view model couldn't be found");
                }

                var variations = await variationsTask;
                baseVm.HasVariantChildren = variations.Count > 0;

                List <PropertyViewModel> toAdd = new List <PropertyViewModel> ();
                foreach (PropertyVariation variation in variations)
                {
                    if (!properties.Remove(variation))
                    {
                        toAdd.Add(CreateViewModel(property, variation));
                    }
                }

                if (properties.Count > 0)
                {
                    var toRemove = new List <PropertyViewModel> ();
                    foreach (var kvp in properties)
                    {
                        toRemove.Add(kvp.Value);
                    }

                    RemoveProperties(toRemove);
                }

                if (toAdd.Count > 0)
                {
                    AddProperties(toAdd);
                }
            }
        }