public void Dispose()
 {
     WeakSubscriptionManager.Unsubscribe(
         _collection,
         nameof(INotifyCollectionChanged.CollectionChanged),
         this);
 }
Beispiel #2
0
        protected override void SubscribeAndUpdate(WeakReference reference)
        {
            object target = reference.Target;

            CurrentValue = new WeakReference(GetValue(target));

            var incc = target as INotifyCollectionChanged;

            if (incc != null)
            {
                WeakSubscriptionManager.Subscribe <NotifyCollectionChangedEventArgs>(
                    incc,
                    nameof(incc.CollectionChanged),
                    this);
            }

            var inpc = target as INotifyPropertyChanged;

            if (inpc != null)
            {
                WeakSubscriptionManager.Subscribe <PropertyChangedEventArgs>(
                    inpc,
                    nameof(inpc.PropertyChanged),
                    this);
            }
        }
        public void AddListener(INotifyCollectionChanged collection, ICollectionChangedListener listener)
        {
            collection = collection ?? throw new ArgumentNullException(nameof(collection));
            listener   = listener ?? throw new ArgumentNullException(nameof(listener));
            Dispatcher.UIThread.VerifyAccess();

            if (!_entries.TryGetValue(collection, out var listeners))
            {
                listeners = new List <WeakReference <ICollectionChangedListener> >();
                _entries.Add(collection, listeners);
                WeakSubscriptionManager.Subscribe(
                    collection,
                    nameof(INotifyCollectionChanged.CollectionChanged),
                    this);
            }

            foreach (var l in listeners)
            {
                if (l.TryGetTarget(out var target) && target == listener)
                {
                    throw new InvalidOperationException(
                              "Collection listener already added for this collection/listener combination.");
                }
            }

            listeners.Add(new WeakReference <ICollectionChangedListener>(listener));
        }
        protected override IDisposable SubscribeCore(IObserver <object> observer)
        {
            IAvaloniaObject instance;

            if (_sourceReference.TryGetTarget(out instance))
            {
                if (_count++ == 0)
                {
                    WeakSubscriptionManager.Subscribe(
                        instance,
                        nameof(instance.PropertyChanged),
                        this);
                }

                observer.OnNext(instance.GetValue(_property));

                return(Observable.Using(() => Disposable.Create(DecrementCount), _ => _changed)
                       .Subscribe(observer));
            }
            else
            {
                _changed.OnCompleted();
                observer.OnCompleted();
                return(Disposable.Empty);
            }
        }
            public Accessor(
                WeakReference reference,
                PropertyInfo property,
                Action <object> changed)
            {
                Contract.Requires <ArgumentNullException>(reference != null);
                Contract.Requires <ArgumentNullException>(property != null);

                _reference = reference;
                _property  = property;
                _changed   = changed;

                var inpc = reference.Target as INotifyPropertyChanged;

                if (inpc != null)
                {
                    WeakSubscriptionManager.Subscribe(
                        inpc,
                        nameof(inpc.PropertyChanged),
                        this);
                }
                else
                {
                    Logger.Warning(
                        LogArea.Binding,
                        this,
                        "Bound to property {Property} on {Source} which does not implement INotifyPropertyChanged",
                        property.Name,
                        reference.Target,
                        reference.Target.GetType());
                }
            }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TopLevel"/> class.
        /// </summary>
        /// <param name="impl">The platform-specific window implementation.</param>
        /// <param name="dependencyResolver">
        /// The dependency resolver to use. If null the default dependency resolver will be used.
        /// </param>
        public TopLevel(ITopLevelImpl impl, IAvaloniaDependencyResolver dependencyResolver)
        {
            if (impl == null)
            {
                throw new InvalidOperationException(
                          "Could not create window implementation: maybe no windowing subsystem was initialized?");
            }

            PlatformImpl = impl;

            dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current;
            var styler = TryGetService <IStyler>(dependencyResolver);

            _accessKeyHandler          = TryGetService <IAccessKeyHandler>(dependencyResolver);
            _inputManager              = TryGetService <IInputManager>(dependencyResolver);
            _keyboardNavigationHandler = TryGetService <IKeyboardNavigationHandler>(dependencyResolver);
            _renderInterface           = TryGetService <IPlatformRenderInterface>(dependencyResolver);
            _globalStyles              = TryGetService <IGlobalStyles>(dependencyResolver);

            Renderer = impl.CreateRenderer(this);

            if (Renderer != null)
            {
                Renderer.SceneInvalidated += SceneInvalidated;
            }

            impl.SetInputRoot(this);

            impl.Closed         = HandleClosed;
            impl.Input          = HandleInput;
            impl.Paint          = HandlePaint;
            impl.Resized        = HandleResized;
            impl.ScalingChanged = HandleScalingChanged;

            _keyboardNavigationHandler?.SetOwner(this);
            _accessKeyHandler?.SetOwner(this);

            if (_globalStyles is object)
            {
                _globalStyles.GlobalStylesAdded   += ((IStyleHost)this).StylesAdded;
                _globalStyles.GlobalStylesRemoved += ((IStyleHost)this).StylesRemoved;
            }

            styler?.ApplyStyles(this);

            ClientSize = impl.ClientSize;

            this.GetObservable(PointerOverElementProperty)
            .Select(
                x => (x as InputElement)?.GetObservable(CursorProperty) ?? Observable.Empty <Cursor>())
            .Switch().Subscribe(cursor => PlatformImpl?.SetCursor(cursor?.PlatformCursor));

            if (((IStyleHost)this).StylingParent is IResourceProvider applicationResources)
            {
                WeakSubscriptionManager.Subscribe(
                    applicationResources,
                    nameof(IResourceProvider.ResourcesChanged),
                    this);
            }
        }
Beispiel #7
0
        protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
        {
            _caretTimer.Tick += CaretTimerTick;

            disposables.Add(this.GetObservable(CaretIndexProperty)
                            .Subscribe(CaretIndexChanged));

            disposables.Add(DocumentLineTransformersProperty.Changed.Subscribe(o =>
            {
                foreach (var item in DocumentLineTransformers)
                {
                    WeakSubscriptionManager.Subscribe(item, nameof(IDocumentLineTransformer.DataChanged),
                                                      documentLineTransformerChangedSubscriber);
                }

                WeakSubscriptionManager.Subscribe(DocumentLineTransformers, nameof(DocumentLineTransformers.CollectionChanged),
                                                  documentLineTransformersChangedSubscriber);
            }));


            disposables.Add(FontSizeProperty.Changed.Subscribe(o =>
            {
                GenerateTextProperties();

                Invalidate();
            }));

            disposables.Add(FontFamilyProperty.Changed.Subscribe(o => { GenerateTextProperties(); }));

            disposables.Add(FontStyleProperty.Changed.Subscribe(o => { GenerateTextProperties(); }));

            disposables.Add(FontWeightProperty.Changed.Subscribe(o => { GenerateTextProperties(); }));
        }
        public void RemoveListener(INotifyCollectionChanged collection, ICollectionChangedListener listener)
        {
            collection = collection ?? throw new ArgumentNullException(nameof(collection));
            listener   = listener ?? throw new ArgumentNullException(nameof(listener));
            Dispatcher.UIThread.VerifyAccess();

            if (_entries.TryGetValue(collection, out var listeners))
            {
                for (var i = 0; i < listeners.Count; ++i)
                {
                    if (listeners[i].TryGetTarget(out var target) && target == listener)
                    {
                        listeners.RemoveAt(i);

                        if (listeners.Count == 0)
                        {
                            WeakSubscriptionManager.Unsubscribe(
                                collection,
                                nameof(INotifyCollectionChanged.CollectionChanged),
                                this);
                            _entries.Remove(collection);
                        }

                        return;
                    }
                }
            }

            throw new InvalidOperationException(
                      "Collection listener not registered for this collection/listener combination.");
        }
Beispiel #9
0
        public static WeakSubscriber <T> Subscribe(object target, string eventName, Action <T> onEvent)
        {
            var result = new WeakSubscriber <T>(onEvent);

            WeakSubscriptionManager.Subscribe(target, eventName, result);

            return(result);
        }
Beispiel #10
0
 public AboutWindowViewModel()
 {
     WeakSubscriptionManager.Subscribe(App.Current.LocaleManager, nameof(LocaleManager.UICultureChanged), this);
     AssemblyVersions = VersionStatistics.LoadedAssemblyVersions.Select(kvp => new AssemblyVersionViewModel(kvp.Key, kvp.Value));
     CloseCommand     = ReactiveCommand.Create(() => AttachedView.Close());
     Changelog        = File.ReadAllText("Changelog.md");
     Attributions     = new AttributionViewModel();
 }
 public Entry(INotifyCollectionChanged collection)
 {
     _collection = collection;
     Listeners   = new List <WeakReference <ICollectionChangedListener> >();
     WeakSubscriptionManager.Subscribe(
         _collection,
         nameof(INotifyCollectionChanged.CollectionChanged),
         this);
 }
 protected override void Deinitialize()
 {
     if (_sourceReference.TryGetTarget(out INotifyCollectionChanged instance))
     {
         WeakSubscriptionManager.Unsubscribe(
             instance,
             nameof(instance.CollectionChanged),
             this);
     }
 }
        public UpdateWindowViewModel(TagVersion updateVersion, string changelog)
        {
            UpdateVersion = updateVersion;
            Changelog     = changelog;
            DialogResult  = DialogResult.None;
            WeakSubscriptionManager.Subscribe(App.Current.Locales, nameof(LocaleManager.UICultureChanged), this);

            UpdateCommand = ReactiveCommand.Create(Update);
            CancelCommand = ReactiveCommand.Create(Cancel);
        }
        public void EventShoudBePassedToSubscriber()
        {
            bool handled    = false;
            var  subscriber = new Subscriber(() => handled = true);
            var  source     = new EventSource();

            WeakSubscriptionManager.Subscribe(source, "Event", subscriber);
            source.Fire();
            Assert.True(handled);
        }
        /// <summary>
        ///     Creates a new TextSegmentCollection that updates the offsets automatically.
        /// </summary>
        /// <param name="textDocument">
        ///     The document to which the text segments
        ///     that will be added to the tree belong. When the document changes, the
        ///     position of the text segments will be updated accordingly.
        /// </param>
        public TextSegmentCollection(TextDocument textDocument)
        {
            if (textDocument == null)
            {
                throw new ArgumentNullException("textDocument");
            }

            textDocument.VerifyAccess();
            isConnectedToDocument = true;
            WeakSubscriptionManager.Subscribe(textDocument, nameof(textDocument.Changed), this);
        }
Beispiel #16
0
            protected override void Dispose(bool disposing)
            {
                var inpc = _reference.Target as INotifyPropertyChanged;

                if (inpc != null)
                {
                    WeakSubscriptionManager.Unsubscribe(
                        inpc,
                        nameof(inpc.PropertyChanged),
                        this);
                }
            }
Beispiel #17
0
            private void SubscribeToChanges()
            {
                var inpc = _reference.Target as INotifyPropertyChanged;

                if (inpc != null)
                {
                    WeakSubscriptionManager.Subscribe <PropertyChangedEventArgs>(
                        inpc,
                        nameof(inpc.PropertyChanged),
                        this);
                }
            }
            public void Dispose()
            {
                var inpc = _reference.Target as INotifyPropertyChanged;

                if (inpc != null)
                {
                    WeakSubscriptionManager.Unsubscribe(
                        inpc,
                        nameof(inpc.PropertyChanged),
                        this);
                }
            }
            protected override void UnsubscribeCore()
            {
                var inpc = GetReferenceTarget() as INotifyPropertyChanged;

                if (inpc != null)
                {
                    WeakSubscriptionManager.Unsubscribe(
                        inpc,
                        nameof(inpc.PropertyChanged),
                        this);
                }
            }
Beispiel #20
0
        public AboutWindowViewModel()
        {
            WeakSubscriptionManager.Subscribe(App.Current.Locales, nameof(LocaleManager.UICultureChanged), this);
            AssemblyVersions = VersionStatistics.LoadedAssemblyVersions.Select(kvp => new AssemblyVersionViewModel(kvp.Key, kvp.Value));
            CloseCommand     = ReactiveCommand.Create(() => AttachedView.Close());

            var changelogFile = new FileInfo(Path.Combine(Program.ApplicationDirectory.FullName, "Changelog.md"));

            Changelog = changelogFile.Exists ? File.ReadAllText(changelogFile.FullName) : string.Empty;

            Attributions = new AttributionViewModel();
        }
            private void SubscribeToChanges()
            {
                var inpc = GetReferenceTarget() as INotifyPropertyChanged;

                if (inpc != null)
                {
                    WeakSubscriptionManager.Subscribe(
                        inpc,
                        nameof(inpc.PropertyChanged),
                        this);
                }
            }
 private void DecrementCount()
 {
     if (--_count == 0)
     {
         if (_sourceReference.TryGetTarget(out INotifyCollectionChanged instance))
         {
             WeakSubscriptionManager.Unsubscribe(
                 instance,
                 nameof(instance.CollectionChanged),
                 this);
         }
     }
 }
            public override void Dispose()
            {
                base.Dispose();
                var target = _reference.Target as INotifyDataErrorInfo;

                if (target != null)
                {
                    WeakSubscriptionManager.Unsubscribe(
                        target,
                        nameof(target.ErrorsChanged),
                        this);
                }
            }
Beispiel #24
0
        /// <summary>
        /// If the ItemsSource implements INotifyCollectionChanged update the visual when the collection changes.
        /// </summary>
        /// <param name="oldValue">The old ItemsSource</param>
        /// <param name="newValue">The new ItemsSource</param>
        private void SubscribeToCollectionChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            if (oldValue is INotifyCollectionChanged collection)
            {
                WeakSubscriptionManager.Unsubscribe(collection, "CollectionChanged", eventListener);
            }

            collection = newValue as INotifyCollectionChanged;
            if (collection != null)
            {
                WeakSubscriptionManager.Subscribe(collection, "CollectionChanged", eventListener);
            }
        }
            protected override void SubscribeCore(IObserver <object> observer)
            {
                var target = _reference.Target as INotifyDataErrorInfo;

                if (target != null)
                {
                    WeakSubscriptionManager.Subscribe(
                        target,
                        nameof(target.ErrorsChanged),
                        this);
                }

                base.SubscribeCore(observer);
            }
Beispiel #26
0
            protected override void UnsubscribeCore()
            {
                var target = GetReferenceTarget() as INotifyDataErrorInfo;

                if (target != null)
                {
                    WeakSubscriptionManager.Unsubscribe(
                        target,
                        nameof(target.ErrorsChanged),
                        this);
                }

                base.UnsubscribeCore();
            }
        private void DecrementCount()
        {
            if (--_count == 0)
            {
                IAvaloniaObject instance;

                if (_sourceReference.TryGetTarget(out instance))
                {
                    WeakSubscriptionManager.Unsubscribe(
                        instance,
                        nameof(instance.PropertyChanged),
                        this);
                }
            }
        }
        public ThemeViewModel(ITheme theme)
        {
            Theme = theme;

            string iconPath = Path.Combine(Program.ApplicationDirectory.FullName,
                                           "themes", "assets", "icons", theme.Name + ".png");

            if (File.Exists(iconPath))
            {
                Icon = new Bitmap(iconPath);
            }

            SelectCommand = ReactiveCommand.Create(Select);
            WeakSubscriptionManager.Subscribe(InternalEventManager, nameof(EventManager.SelectedThemeChanged), this);
            WeakSubscriptionManager.Subscribe(App.Current.Locales, nameof(LocaleManager.UICultureChanged), this);
        }
            public IndeiValidationChecker(WeakReference reference, string name, IPropertyAccessor accessor, Action <IValidationStatus> callback)
                : base(reference, name, accessor, callback)
            {
                var target = reference.Target as INotifyDataErrorInfo;

                if (target != null)
                {
                    if (target.HasErrors)
                    {
                        SendValidationCallback(new IndeiValidationStatus(target.GetErrors(name)));
                    }
                    WeakSubscriptionManager.Subscribe(
                        target,
                        nameof(target.ErrorsChanged),
                        this);
                }
            }
Beispiel #30
0
        protected override void Unsubscribe(object target)
        {
            var incc = target as INotifyCollectionChanged;

            if (incc != null)
            {
                WeakSubscriptionManager.Unsubscribe <NotifyCollectionChangedEventArgs>(
                    incc,
                    nameof(incc.CollectionChanged),
                    this);
            }

            var inpc = target as INotifyPropertyChanged;

            if (inpc != null)
            {
                WeakSubscriptionManager.Unsubscribe <PropertyChangedEventArgs>(
                    inpc,
                    nameof(inpc.PropertyChanged),
                    this);
            }
        }