Ejemplo n.º 1
0
        public virtual void addListener(ImageStreamListener listener)
        {
            _listeners.Add(listener);
            if (_currentImage != null)
            {
                try {
                    listener.onImage(_currentImage, true);
                }
                catch (Exception exception) {
                    reportError(
                        context: new ErrorDescription("by a synchronously-called image listener"),
                        exception: exception
                        );
                }
            }

            if (_currentError != null && listener.onError != null)
            {
                try {
                    listener.onError(_currentError.exception);
                }
                catch (Exception exception) {
                    UIWidgetsError.reportError(
                        new UIWidgetsErrorDetails(
                            exception: exception,
                            library: "image resource service",
                            context: new ErrorDescription("by a synchronously-called image error listener")
                            )
                        );
                }
            }
        }
Ejemplo n.º 2
0
 public override void removeListener(ImageStreamListener listener)
 {
     base.removeListener(listener);
     if (!hasListeners)
     {
         _timer?.cancel();
         _timer = null;
     }
 }
Ejemplo n.º 3
0
        public override void addListener(ImageStreamListener listener)
        {
            if (!hasListeners && _codec != null)
            {
                _decodeNextFrameAndSchedule();
            }

            base.addListener(listener);
        }
Ejemplo n.º 4
0
        public void addListener(ImageStreamListener listener)
        {
            if (_completer != null)
            {
                _completer.addListener(listener);
                return;
            }

            if (_listeners == null)
            {
                _listeners = new List <ImageStreamListener>();
            }

            _listeners.Add(listener);
        }
Ejemplo n.º 5
0
        public void removeListener(ImageStreamListener listener)
        {
            if (_completer != null)
            {
                _completer.removeListener(listener);
                return;
            }

            D.assert(_listeners != null);
            for (int i = 0; i < _listeners.Count; i++)
            {
                if (_listeners[i] == listener)
                {
                    _listeners.RemoveAt(i);
                    break;
                }
            }
        }
Ejemplo n.º 6
0
        public virtual void removeListener(ImageStreamListener listener)
        {
            for (int i = 0; i < _listeners.Count; i += 1)
            {
                if (_listeners[i] == listener)
                {
                    _listeners.RemoveAt(i);
                    break;
                }
            }

            if (_listeners.isEmpty())
            {
                foreach (VoidCallback callback in _onLastListenerRemovedCallbacks)
                {
                    callback();
                }

                _onLastListenerRemovedCallbacks.Clear();
            }
        }
Ejemplo n.º 7
0
        public void paint(Canvas canvas, Rect rect, Path clipPath, ImageConfiguration configuration)
        {
            D.assert(canvas != null);
            D.assert(rect != null);
            D.assert(configuration != null);

            bool flipHorizontally = false;

            if (_details.matchTextDirection)
            {
                D.assert(() => {
                    // We check this first so that the assert will fire immediately, not just
                    // when the image is ready.
                    if (configuration.textDirection == null)
                    {
                        throw new UIWidgetsError(new List <DiagnosticsNode>()
                        {
                            new ErrorSummary(
                                "DecorationImage.matchTextDirection can only be used when a TextDirection is available."),
                            new ErrorDescription(
                                "When DecorationImagePainter.paint() was called, there was no text direction provided " +
                                "in the ImageConfiguration object to match."
                                ),
                            new DiagnosticsProperty <DecorationImage>("The DecorationImage was", _details,
                                                                      style: DiagnosticsTreeStyle.errorProperty),
                            new DiagnosticsProperty <ImageConfiguration>("The ImageConfiguration was", configuration,
                                                                         style: DiagnosticsTreeStyle.errorProperty)
                        });
                    }

                    return(true);
                });
                if (configuration.textDirection == TextDirection.rtl)
                {
                    flipHorizontally = true;
                }
            }

            ImageStream newImageStream = _details.image.resolve(configuration);

            if (newImageStream.key != _imageStream?.key)
            {
                ImageStreamListener listener = new ImageStreamListener(
                    _handleImage,
                    onError: _details.onError
                    );
                _imageStream?.removeListener(listener);
                _imageStream = newImageStream;
                _imageStream.addListener(listener);
            }

            if (_image == null)
            {
                return;
            }

            if (clipPath != null)
            {
                canvas.save();
                canvas.clipPath(clipPath);
            }

            painting_.paintImage(
                canvas: canvas,
                rect: rect,
                image: _image.image,
                scale: _image.scale,
                colorFilter: _details.colorFilter,
                fit: _details.fit,
                alignment: _details.alignment.resolve(configuration.textDirection),
                centerSlice: _details.centerSlice,
                repeat: _details.repeat,
                flipHorizontally: flipHorizontally,
                filterQuality: FilterQuality.low
                );

            if (clipPath != null)
            {
                canvas.restore();
            }
        }
Ejemplo n.º 8
0
        public ImageStreamCompleter putIfAbsent(object key, Func <ImageStreamCompleter> loader,
                                                ImageErrorListener onError = null)
        {
            D.assert(key != null);
            D.assert(loader != null);

            ImageStreamCompleter result = null;

            if (_pendingImages.TryGetValue(key, out var pendingImage))
            {
                result = pendingImage.completer;
                return(result);
            }

            if (_cache.TryGetValue(key, out var image))
            {
                if (image.node != null)
                {
                    _lruKeys.Remove(image.node);
                }
                _trackLiveImage(key, new _LiveImage(image.completer, image.sizeBytes, () => _liveImages.Remove(key)));
                image.node = _lruKeys.AddLast(key);
                return(image.completer);
            }

            _liveImages.TryGetValue(key, out var liveImage);
            if (liveImage != null)
            {
                _touch(key, liveImage);
                return(liveImage.completer);
            }

            try {
                result = loader();
                _trackLiveImage(key, new _LiveImage(result, null, () => _liveImages.Remove(key)));
            }
            catch (Exception ex) {
                if (onError != null)
                {
                    onError(ex);
                }
                else
                {
                    throw;
                }
            }

            bool          listenedOnce          = false;
            _PendingImage untrackedPendingImage = null;

            void listener(ImageInfo info, bool syncCall)
            {
                int          imageSize   = info?.image == null ? 0 : info.image.height * info.image.width * 4;
                _CachedImage cachedImage = new _CachedImage(result, imageSize);

                _trackLiveImage(
                    key,
                    new _LiveImage(
                        result,
                        imageSize,
                        () => _liveImages.Remove(key)
                        ),
                    debugPutOk: syncCall
                    );
                _PendingImage _pendingImage = untrackedPendingImage ?? _pendingImages.getOrDefault(key);

                _pendingImages.Remove(key);
                if (_pendingImage != null)
                {
                    _pendingImage.removeListener();
                }

                if (untrackedPendingImage == null)
                {
                    _touch(key, cachedImage);
                }

                listenedOnce = true;
            }

            ImageStreamListener streamListener = new ImageStreamListener(listener);

            if (maximumSize > 0 && maximumSizeBytes > 0)
            {
                _pendingImages[key] = new _PendingImage(result, streamListener);
            }
            else
            {
                untrackedPendingImage = new _PendingImage(result, streamListener);
            }

            result.addListener(streamListener);

            return(result);
        }