Esempio n. 1
0
        public void Ok_MakeLazy_IsLazy()
        {
            var lazy  = new LazyResult <int, string>(() => new Success <int, string>(5));
            var lazy2 = lazy.MakeLazy();

            Assert.AreEqual(lazy, lazy2);
        }
Esempio n. 2
0
 protected BrushStamp(SizeInt32 size, double opacity, bool antialiased)
 {
     this.Size           = size;
     this.Opacity        = opacity;
     this.Antialiased    = antialiased;
     this.lazyMaskBitmap = new LazyResult <IBitmap <ColorAlpha8>, int>(_ => this.CreateMaskBitmap(), 0);
 }
        /// <summary>
        /// Executes the request asynchronously without parsing the response,
        /// and calls the specified method once finished.
        /// </summary>
        /// <remarks>The returned stream is encoded in UTF-8.</remarks>
        public void FetchAsyncAsStream([Optional] ExecuteRequestDelegate <Stream> methodToCall)
        {
            GetAsyncResponse(
                (IAsyncRequestResult state) =>
            {
                var result = new LazyResult <Stream>(
                    () =>
                {
                    // Retrieve and convert the response.
                    IResponse response = state.GetResponse();
                    response.ThrowIfNull("response");
                    return(response.Stream);
                });

                // Only invoke the method if it was set.
                if (methodToCall != null)
                {
                    methodToCall(result);
                }
                else
                {
                    result.GetResult();     // Resolve the result in any case.
                }
            });
        }
        private void OnListTasks(LazyResult <Tasks.v1.Data.Tasks> response)
        {
            Tasks.v1.Data.Tasks result = null;

            try {
                result = response.GetResult();
            } catch (Exception ex) {
                AppDelegate.FinishActivity();
                ReloadComplete();
                ShowError(ex);
                return;
            }

            BeginInvokeOnMainThread(() => {
                var section = Root.First();
                section.Clear();

                if (result.Items != null)
                {
                    section.AddAll(result.Items.Select(t =>
                                                       new StyledStringElement(t.Title, () => OnTap(t))
                    {
                        Accessory = (t.Status == "completed") ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None
                    }
                                                       ).Cast <Element>());
                }

                ReloadComplete();

                AppDelegate.FinishActivity();
            });
        }
        private void OnTaskListResponse(LazyResult<TaskLists> response)
        {
            TaskLists lists = null;

            try {
                lists = response.GetResult();
            } catch (Exception ex) {
                BeginInvokeOnMainThread (ReloadComplete);
                AppDelegate.FinishActivity();
                ShowError (ex);
            }

            if (lists == null || lists.Items == null) {
                AppDelegate.FinishActivity();
                return;
            }

            Section section = new Section();
            section.AddAll (lists.Items.Select (l =>
                new StringElement (l.Title, () => {
                    var tasks = new TasksViewController (this.service, l);
                    NavigationController.PushViewController (tasks, true);
                })
            ).Cast<Element>());

            BeginInvokeOnMainThread (() => {
                Root.Clear();
                Root.Add (section);

                ReloadComplete();

                AppDelegate.FinishActivity();
            });
        }
 public SelectionGeometryCache(IDirect2DFactory factory, PaintDotNet.Canvas.SelectionSnapshot selectionSnapshot)
 {
     this.factory           = factory;
     this.selectionSnapshot = selectionSnapshot;
     this.geometry          = LazyResult.New <IGeometry>(() => this.factory.CreateGeometry(selectionSnapshot.GeometryList.Value), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
     this.pixelatedGeometry = LazyResult.New <IGeometry>(() => GeometryHelpers.ToDirect2DGeometryDestructive(this.factory, ScansHelpers.ConvertNonOverlappingScansToPolygons(selectionSnapshot.PixelatedScans.Value), FillMode.Alternate, FigureBegin.Filled, FigureEnd.Closed), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
 }
Esempio n. 7
0
        private void OnEventResponse(LazyResult <Event> _event)
        {
            var realEvent = _event.GetResult();

            ViewModel.EventTitle = realEvent.Summary;
            ViewModel.StartDate  = DateTime.Parse(realEvent.Start.DateTime);
            ViewModel.EndDate    = DateTime.Parse(realEvent.End.DateTime);
        }
 public PaintBucketToolContentRenderer(IRenderer <ColorBgra> sampleSource, PaintBucketToolChanges changes) : base(sampleSource.Width, sampleSource.Height, true)
 {
     Validate.IsNotNull <PaintBucketToolChanges>(changes, "changes");
     this.sampleSource  = sampleSource;
     this.changes       = changes;
     this.brush         = new PdnLegacyBrush(this.changes.BrushType, this.changes.HatchStyle, this.changes.ForegroundColor, this.changes.BackgroundColor).EnsureFrozen <PdnLegacyBrush>();
     this.brushRenderer = this.brush.CreateRenderer(base.Width, base.Height);
     this.lazyStencil   = LazyResult.New <IRenderer <ColorAlpha8> >(() => this.CreateMaskRenderer(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
 }
Esempio n. 9
0
 public BrushStrokeRenderCache(BrushStrokeRenderData renderData, BrushStamp stamp, int tileEdgeLog2)
 {
     Validate.Begin().IsNotNull <BrushStrokeRenderData>(renderData, "renderData").IsNotNull <BrushStamp>(stamp, "stamp").Check();
     this.renderData              = renderData;
     this.stamp                   = stamp;
     this.lazyStampMaskDevBitmap  = LazyResult.New <DeviceBitmap>(() => new DeviceBitmap(this.stamp.MaskBitmap).EnsureFrozen <DeviceBitmap>(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
     this.tileOffsetToTileDataMap = new ConcurrentDictionary <PointInt32, TileData>();
     this.tileMathHelper          = new TileMathHelper(TransactedToolChanges.MaxMaxRenderBounds.Size, tileEdgeLog2);
 }
Esempio n. 10
0
 public SelectionSnapshot(Result <PaintDotNet.Rendering.GeometryList> lazyGeometryList, Result <IReadOnlyList <RectInt32> > lazyPixelatedScans, Matrix3x2Double interimTransform, RectDouble fastMaxBounds, bool isEmpty, int geometryVersion)
 {
     Validate.Begin().IsNotNull <Result <PaintDotNet.Rendering.GeometryList> >(lazyGeometryList, "lazyGeometryList").IsNotNull <Result <IReadOnlyList <RectInt32> > >(lazyPixelatedScans, "lazyPixelatedScans").Check();
     this.lazyGeometryList   = lazyGeometryList;
     this.lazyPixelatedScans = lazyPixelatedScans;
     this.interimTransform   = interimTransform;
     this.fastMaxBounds      = fastMaxBounds;
     this.isEmpty            = isEmpty;
     this.geometryVersion    = geometryVersion;
     this.isRectilinear      = LazyResult.New <bool>(() => this.GeometryList.Value.IsRectilinear, LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
     this.isPixelated        = LazyResult.New <bool>(() => this.GeometryList.Value.IsPixelated, LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
 }
Esempio n. 11
0
        public void Success_Ok()
        {
            var called = 0;
            Func <Result <int, string> > factory = () =>
            {
                called++;
                return(new Success <int, string>(5));
            };

            var result = LazyResult.Create(factory).Bind(i => (i + 5).ToString(CultureInfo.InvariantCulture));

            Assert.AreEqual(called, 0);
            Assert.That(result.Unwrap(), Is.EqualTo("10"));
            Assert.AreEqual(called, 1);
        }
Esempio n. 12
0
        /// <summary>
        /// Initialize a new MediaUpload object from a JsonDictionary describing its configuration.
        /// </summary>
        /// <param name="dict"></param>
        public MediaUpload(JsonDictionary dict)
        {
            if (dict == null)
                throw new ArgumentNullException("dict");
            information = dict;

            accepts = new LazyResult<string[]>(() =>
            {
                var acceptList = information[AcceptsPath] as IEnumerable<object>;
                return acceptList == null ?
                    new string[] { } : acceptList.Select(x => x as string).ToArray();
            });

            var protocols = information[ProtocolsPath] as JsonDictionary;
        }
Esempio n. 13
0
        public void Failure_Ok()
        {
            var called       = 0;
            var errorMessage = "Error!";
            Func <Result <int, string> > factory = () =>
            {
                called++;
                return(new Failure <int, string>(errorMessage));
            };

            var result = LazyResult.Create(factory).Bind(i => (i + 5).ToString(CultureInfo.InvariantCulture));

            Assert.AreEqual(called, 0);
            Assert.That(result.UnwrapError(), Is.EqualTo(errorMessage));
            Assert.AreEqual(called, 1);
        }
Esempio n. 14
0
        public void IsLazyTest()
        {
            bool hasBeenFetched = false;
            Func<bool> fetchFunction = () => (hasBeenFetched = true);

            // Try fetching the result.
            var result = new LazyResult<bool>(fetchFunction);
            Assert.IsFalse(hasBeenFetched);
            Assert.IsTrue(result.GetResult());
            Assert.IsTrue(hasBeenFetched);
            
            // Try a second fetch. Confirm that a cached result will be used.
            hasBeenFetched = false;
            Assert.IsTrue(result.GetResult());
            Assert.IsFalse(hasBeenFetched);
        }
Esempio n. 15
0
        public void TwoCalls_Failure_Ok()
        {
            var called = 0;
            Func <Result <string, int> > factory = () =>
            {
                called++;
                return(new Failure <string, int>(5));
            };

            var result = LazyResult.Create(factory)
                         .Bind <object, string>(_ => throw new Exception("Should not hit this!"), i => (i + 5).ToString(CultureInfo.InvariantCulture));

            Assert.AreEqual(called, 0);
            Assert.That(result.UnwrapError(), Is.EqualTo("10"));
            Assert.AreEqual(called, 1);
        }
Esempio n. 16
0
        public void Failure_Ok()
        {
            var called = 0;
            Func <Result <int, string> > factory = () =>
            {
                called++;
                return(new Failure <int, string>("Failure"));
            };

            var failure       = LazyResult.Create(factory);
            var failureResult = failure.BindToResult(this.testFunc);

            Assert.AreEqual(called, 0);
            Assert.That(failureResult.UnwrapError(), Is.EqualTo("Failure"));
            Assert.AreEqual(called, 1);
        }
Esempio n. 17
0
        /// <summary>
        /// Initialize a new MediaUpload object from a JsonDictionary describing its configuration.
        /// </summary>
        /// <param name="dict"></param>
        public MediaUpload(JsonDictionary dict)
        {
            if (dict == null)
            {
                throw new ArgumentNullException("dict");
            }
            information = dict;

            accepts = new LazyResult <string[]>(() =>
            {
                var acceptList = information[AcceptsPath] as IEnumerable <object>;
                return(acceptList == null ?
                       new string[] { } : acceptList.Select(x => x as string).ToArray());
            });

            var protocols = information[ProtocolsPath] as JsonDictionary;
        }
Esempio n. 18
0
        public void Success_BindsToFailure_Ok()
        {
            var called = 0;
            Func <Result <int, string> > factory = () =>
            {
                called++;
                return(new Success <int, string>(6));
            };

            var success = LazyResult.Create(factory);

            var successResult = success.BindToResult(this.testFunc);

            Assert.AreEqual(called, 0);
            Assert.That(successResult.UnwrapError(), Is.EqualTo("Oops"));
            Assert.AreEqual(called, 1);
        }
Esempio n. 19
0
        public void IsLazyTest()
        {
            bool        hasBeenFetched = false;
            Func <bool> fetchFunction  = () => (hasBeenFetched = true);

            // Try fetching the result.
            var result = new LazyResult <bool>(fetchFunction);

            Assert.IsFalse(hasBeenFetched);
            Assert.IsTrue(result.GetResult());
            Assert.IsTrue(hasBeenFetched);

            // Try a second fetch. Confirm that a cached result will be used.
            hasBeenFetched = false;
            Assert.IsTrue(result.GetResult());
            Assert.IsFalse(hasBeenFetched);
        }
Esempio n. 20
0
 private void EnsureCachesInitialized()
 {
     if (!this.areCachesInitialized)
     {
         object sync = this.sync;
         lock (sync)
         {
             if (!this.areCachesInitialized)
             {
                 this.lazyFlattenedPathGeometry = this.lazyFlattenedPathGeometry ?? LazyResult.New <PathGeometry>(() => this.geometry.GetFlattenedPathGeometry().EnsureFrozen <PathGeometry>(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.lazyFlattenedPolyPoly     = this.lazyFlattenedPolyPoly ?? LazyResult.New <IList <PointDouble[]> >(() => (from p in PathGeometryUtil.EnumeratePathGeometryPolygons(this.lazyFlattenedPathGeometry.Value) select p.ToArrayEx <PointDouble>()).ToArrayEx <PointDouble[]>(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.lazyGeometrySource        = this.lazyGeometrySource ?? LazyResult.New <GeometrySource>(() => GeometrySource.Create(this.geometry), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.perThreadD2DGeometry      = this.perThreadD2DGeometry ?? new ThreadLocal <IGeometry>(() => this.lazyGeometrySource.Value.ToDirect2DGeometry(Direct2DFactory.PerThread));
                 this.lazyGeometryBounds        = this.lazyGeometryBounds ?? LazyResult.New <RectDouble>(new Func <RectDouble>(this.ComputeBounds), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.areCachesInitialized      = true;
             }
         }
     }
 }
Esempio n. 21
0
        public void Result_Ok()
        {
            var calls = 0;

            var factory = new Func <Result <int, object> >(() =>
            {
                calls += 1;
                return(new Success <int, object>(calls));
            });

            var result = LazyResult.Create(factory);

            Assert.AreEqual(calls, 0);

            Assert.AreEqual(result.Unwrap(), 1);
            Assert.AreEqual(calls, 1);

            Assert.AreEqual(result.Unwrap(), 1);
            Assert.AreEqual(calls, 1);
        }
 protected override void Dispose(bool disposing)
 {
     this.selectionSnapshot = null;
     if (disposing)
     {
         LazyResult <IGeometry> geometry = this.geometry;
         this.geometry = null;
         if ((geometry != null) && geometry.IsEvaluated)
         {
             geometry.Value.Dispose();
         }
         LazyResult <IGeometry> pixelatedGeometry = this.pixelatedGeometry;
         this.pixelatedGeometry = null;
         if ((pixelatedGeometry != null) && pixelatedGeometry.IsEvaluated)
         {
             pixelatedGeometry.Value.Dispose();
         }
     }
     base.Dispose(disposing);
 }
Esempio n. 23
0
        public ShapesToolChanges CloneWithNewTransform(Matrix3x2Double newTransform)
        {
            if (newTransform == this.transform)
            {
                return(this);
            }
            if (!(this.transform.GetScale() == newTransform.GetScale()))
            {
                return(new ShapesToolChanges(base.DrawingSettingsValues, this.ShapeInfoSettingPath, this.AllShapePropertyValues, this.MouseStartPoint, this.MouseEndPoint, this.ShouldApplySnapping, this.ShouldApplyConstraint, this.IsEditingStartPoint, this.IsEditingEndPoint, this.WhichUserColor, newTransform, this.RotationAnchorOffset));
            }
            Matrix3x2Double   relativeTx = this.transform.Inverse * newTransform;
            ShapesToolChanges changes    = (ShapesToolChanges)base.MemberwiseClone();

            using (changes.UseChangeScope())
            {
                changes.transform       = newTransform;
                changes.shapeRenderData = LazyResult.New <PaintDotNet.Shapes.ShapeRenderData>(() => PaintDotNet.Shapes.ShapeRenderData.Transform(this.shapeRenderData.Value, relativeTx), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                changes.InvalidateCachedMaxRenderBounds();
            }
            return(changes);
        }
        /// <summary>
        /// Executes the request asynchronously and optionally calls the specified method once finished.
        /// </summary>
        public void FetchAsync([Optional] ExecuteRequestDelegate <TResponse> methodToCall)
        {
            GetAsyncResponse(
                (IAsyncRequestResult state) =>
            {
                var result = new LazyResult <TResponse>(() =>
                {
                    // Retrieve and convert the response.
                    IResponse response = state.GetResponse();
                    return(FetchObject(response));
                });

                // Only invoke the method if it was set.
                if (methodToCall != null)
                {
                    methodToCall(result);
                }
                else
                {
                    result.GetResult();
                }
            });
        }
Esempio n. 25
0
        private void Initialize()
        {
            if ((this.allShapePropertyValuesMap == null) && (this.allShapePropertyValuesArray != null))
            {
                this.allShapePropertyValuesMap = ShapePropertyValuesUtil.ToReadOnlyMap(this.allShapePropertyValuesArray);
            }
            PaintDotNet.UI.Media.DashStyle style = DashStyleUtil.ToMedia(this.PenDashStyle);
            this.outlinePen = new Pen {
                Brush     = SolidColorBrushCache.Get((ColorRgba128Float)Colors.White),
                Thickness = this.PenWidth,
                LineJoin  = PenLineJoin.Miter,
                DashStyle = style
            }.EnsureFrozen <Pen>();
            RectDouble   baseBounds = this.BaseBounds;
            VectorDouble txScale    = this.Transform.GetScale();
            VectorDouble num3       = (VectorDouble)(this.EndPoint - this.StartPoint);
            PointDouble  endPoint   = new PointDouble(this.StartPoint.X + (num3.X * txScale.X), this.StartPoint.Y + (num3.Y * txScale.Y));
            Dictionary <string, object> settingValues = (from gsp in this.Shape.RenderSettingPaths select KeyValuePairUtil.Create <string, object>(gsp, base.GetDrawingSettingValue(gsp))).ToDictionary <string, object>();

            PaintDotNet.Shapes.ShapeRenderParameters renderParams = new PaintDotNet.Shapes.ShapeRenderParameters(this.StartPoint, endPoint, txScale, settingValues, null);
            this.shapePropertySchema = this.Shape.CreatePropertyCollection(renderParams);
            foreach (Property property in this.shapePropertySchema)
            {
                property.ValueChanged += delegate(object s, ValueEventArgs <object> e) {
                    throw new ReadOnlyException();
                };
                property.ReadOnlyChanged += delegate(object s, ValueEventArgs <bool> e) {
                    throw new ReadOnlyException();
                };
            }
            this.shapeRenderParams = new PaintDotNet.Shapes.ShapeRenderParameters(this.StartPoint, endPoint, txScale, settingValues, this.ShapePropertyValues);
            this.shapeRenderData   = LazyResult.New <PaintDotNet.Shapes.ShapeRenderData>(delegate {
                PaintDotNet.Shapes.ShapeRenderData renderData = this.Shape.CreateRenderData(this.shapeRenderParams);
                Matrix3x2Double matrix = Matrix3x2Double.ScalingAt(1.0 / Math.Abs(txScale.X), 1.0 / Math.Abs(txScale.Y), this.StartPoint.X, this.StartPoint.Y);
                return(PaintDotNet.Shapes.ShapeRenderData.Transform(PaintDotNet.Shapes.ShapeRenderData.Transform(renderData, matrix), this.transform));
            }, LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
        }
        private void OnTaskListResponse(LazyResult <TaskLists> response)
        {
            TaskLists lists = null;

            try {
                lists = response.GetResult();
            } catch (Exception ex) {
                BeginInvokeOnMainThread(ReloadComplete);
                AppDelegate.FinishActivity();
                ShowError(ex);
            }

            if (lists == null || lists.Items == null)
            {
                AppDelegate.FinishActivity();
                return;
            }

            Section section = new Section();

            section.AddAll(lists.Items.Select(l =>
                                              new StringElement(l.Title, () => {
                var tasks = new TasksViewController(this.service, l);
                NavigationController.PushViewController(tasks, true);
            })
                                              ).Cast <Element>());

            BeginInvokeOnMainThread(() => {
                Root.Clear();
                Root.Add(section);

                ReloadComplete();

                AppDelegate.FinishActivity();
            });
        }
Esempio n. 27
0
        protected override GeometryList CreateSelectionGeometry(MagicWandToolChanges changes, AsyncSelectionToolCreateGeometryContext context, CancellationToken cancellationToken)
        {
            GeometryList          list;
            Result <BitVector2D>  lazyBaseStencil;
            IRenderer <ColorBgra> sampleSource = ((MagicWandToolCreateGeometryContext)context).SampleSource;
            byte       x         = (byte)Math.Round((double)(changes.Tolerance * 255.0), MidpointRounding.AwayFromZero);
            byte       tolerance = ByteUtil.FastScale(x, x);
            PointInt32 pt        = changes.OriginPointInt32;

            if (!sampleSource.Bounds <ColorBgra>().Contains(pt))
            {
                switch (changes.SelectionCombineMode)
                {
                case SelectionCombineMode.Replace:
                case SelectionCombineMode.Intersect:
                    return(new GeometryList());

                case SelectionCombineMode.Union:
                case SelectionCombineMode.Exclude:
                case SelectionCombineMode.Xor:
                    return(changes.BaseGeometry);
                }
                throw ExceptionUtil.InvalidEnumArgumentException <SelectionCombineMode>(changes.SelectionCombineMode, "changes.SelectionCombineMode");
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }
            Func <bool>       isCancellationRequestedFn = () => cancellationToken.IsCancellationRequested;
            ColorBgra         basis             = sampleSource.GetPointSlow(pt);
            int               width             = ((sampleSource.Width + 0x1f) / 0x20) * 0x20;
            BitVector2D       newStencil        = new BitVector2D(width, sampleSource.Height);
            BitVector2DStruct newStencilWrapper = new BitVector2DStruct(newStencil);

            if (((changes.SelectionCombineMode != SelectionCombineMode.Replace) && sampleSource.Bounds <ColorBgra>().Contains(changes.BaseGeometry.Bounds.Int32Bound)) && changes.BaseGeometry.IsPixelated)
            {
                lazyBaseStencil = LazyResult.New <BitVector2D>(() => PixelatedGeometryListToBitVector2D(changes.BaseGeometry, newStencil.Width, newStencil.Height, cancellationToken), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                ThreadPool.QueueUserWorkItem(delegate(object _) {
                    lazyBaseStencil.EnsureEvaluated();
                });
            }
            else
            {
                lazyBaseStencil = null;
            }
            FloodMode floodMode = changes.FloodMode;

            if (floodMode != FloodMode.Local)
            {
                if (floodMode != FloodMode.Global)
                {
                    throw ExceptionUtil.InvalidEnumArgumentException <FloodMode>(changes.FloodMode, "changes.FloodMode");
                }
            }
            else
            {
                RectInt32 num4;
                FloodFillAlgorithm.FillStencilFromPoint <BitVector2DStruct>(sampleSource, newStencilWrapper, pt, tolerance, isCancellationRequestedFn, out num4);
                goto Label_0293;
            }
            TileMathHelper tileMathHelper = new TileMathHelper(sampleSource.Width, sampleSource.Height, 7);

            Work.ParallelForEach <PointInt32>(WaitType.Pumping, tileMathHelper.EnumerateTileOffsets(), delegate(PointInt32 tileOffset) {
                if (!cancellationToken.IsCancellationRequested)
                {
                    RectInt32 clipRect = tileMathHelper.GetTileSourceRect(tileOffset);
                    FloodFillAlgorithm.FillStencilByColor <BitVector2DStruct>(sampleSource, newStencilWrapper, basis, tolerance, isCancellationRequestedFn, clipRect);
                }
            }, WorkItemQueuePriority.Normal, null);
Label_0293:
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }
            if (changes.SelectionCombineMode == SelectionCombineMode.Replace)
            {
                list = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken);
            }
            else if (lazyBaseStencil == null)
            {
                GeometryList rhs = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken);
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                list = GeometryList.Combine(changes.BaseGeometry, changes.SelectionCombineMode.ToGeometryCombineMode(), rhs);
            }
            else
            {
                BitVector2D other = lazyBaseStencil.Value;
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                switch (changes.SelectionCombineMode)
                {
                case SelectionCombineMode.Replace:
                    throw new InternalErrorException();

                case SelectionCombineMode.Union:
                    newStencil.Or(other);
                    break;

                case SelectionCombineMode.Exclude:
                    newStencil.Invert();
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }
                    newStencil.And(other);
                    break;

                case SelectionCombineMode.Intersect:
                    newStencil.And(other);
                    break;

                case SelectionCombineMode.Xor:
                    newStencil.Xor(other);
                    break;

                default:
                    throw ExceptionUtil.InvalidEnumArgumentException <SelectionCombineMode>(changes.SelectionCombineMode, "changes.SelectionCombineMode");
                }
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                list = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken);
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }
            list.Freeze();
            return(list);
        }
Esempio n. 28
0
 public void IsSuccess_Ok()
 {
     Assert.AreEqual(LazyResult.Create(() => new Success <int, object>(4)).IsSuccess, true);
     Assert.AreEqual(LazyResult.Create(() => new Failure <object, int>(4)).IsSuccess, false);
 }
Esempio n. 29
0
        public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
        {
            GeometryList cachedClippingMask = historyWorkspace.Selection.GetCachedClippingMask();

            if (historyWorkspace.Selection.IsEmpty || (cachedClippingMask.Bounds.Area < 1.0))
            {
                return(null);
            }
            Document  oldDocument   = historyWorkspace.Document;
            RectInt32 b             = cachedClippingMask.Bounds.GetInt32Bound(1E-05);
            RectInt32 oldClipBounds = RectInt32.Intersect(oldDocument.Bounds(), b);
            Document  document      = new Document(oldClipBounds.Width, oldClipBounds.Height);

            document.ReplaceMetadataFrom(oldDocument);
            RectInt32 newClipBounds = new RectInt32(0, 0, oldClipBounds.Width, oldClipBounds.Height);
            SelectionRenderingQuality                   quality = historyWorkspace.ToolSettings.Selection.RenderingQuality.Value;
            Result <IRenderer <ColorAlpha8> >           oldClipMaskRendererLazy = historyWorkspace.Selection.GetCachedLazyClippingMaskRenderer(quality);
            LazyResult <ClippedRenderer <ColorAlpha8> > newClipMaskRendererLazy = LazyResult.New <ClippedRenderer <ColorAlpha8> >(() => new ClippedRenderer <ColorAlpha8>(oldClipMaskRendererLazy.Value, oldClipBounds), LazyThreadSafetyMode.ExecutionAndPublication, new VistaCriticalSection(0));
            SelectionData selectionData = historyWorkspace.Selection.Save();

            System.Threading.Tasks.Task <SelectionHistoryMemento>       task  = System.Threading.Tasks.Task.Factory.StartNew <SelectionHistoryMemento>(() => new SelectionHistoryMemento(null, null, historyWorkspace, selectionData));
            System.Threading.Tasks.Task <ReplaceDocumentHistoryMemento> task2 = System.Threading.Tasks.Task.Factory.StartNew <ReplaceDocumentHistoryMemento>(() => new ReplaceDocumentHistoryMemento(null, null, historyWorkspace, oldDocument), TaskCreationOptions.LongRunning);
            int count = oldDocument.Layers.Count;

            System.Threading.Tasks.Task <BitmapLayer>[] items = new System.Threading.Tasks.Task <BitmapLayer> [count];
            for (int i = 0; i < count; i++)
            {
                int iP = i;
                items[iP] = System.Threading.Tasks.Task.Factory.StartNew <BitmapLayer>(delegate {
                    if (iP == 0)
                    {
                        newClipMaskRendererLazy.EnsureEvaluated();
                    }
                    BitmapLayer layer      = (BitmapLayer)oldDocument.Layers[iP];
                    Surface croppedSurface = layer.Surface.CreateWindow(oldClipBounds);
                    BitmapLayer newLayer   = RetryManager.RunMemorySensitiveOperation <BitmapLayer>(() => new BitmapLayer(croppedSurface));
                    newLayer.LoadProperties(layer.SaveProperties());
                    Parallel.ForEach <RectInt32>(newClipBounds.GetTiles(TilingStrategy.Tiles, 7), delegate(RectInt32 newTileRect) {
                        ISurface <ColorBgra> surface = newLayer.Surface.CreateWindow(newTileRect);
                        IRenderer <ColorAlpha8> mask = new ClippedRenderer <ColorAlpha8>(newClipMaskRendererLazy.Value, newTileRect);
                        surface.MultiplyAlphaChannel(mask);
                    });
                    return(newLayer);
                });
            }
            List <TupleStruct <System.Threading.Tasks.Task, double> > collection = new List <TupleStruct <System.Threading.Tasks.Task, double> >();

            collection.AddTuple <System.Threading.Tasks.Task, double>(task, 0.1);
            collection.AddTuple <System.Threading.Tasks.Task, double>(task2, 1.0);
            for (int j = 0; j < items.Length; j++)
            {
                collection.AddTuple <System.Threading.Tasks.Task, double>(items[j], 0.1);
            }
            PaintDotNet.Threading.Tasks.Task <Unit> task3 = historyWorkspace.TaskManager.CreateFrameworkTasksWrapper(collection);
            historyWorkspace.WaitWithProgress(task3, HistoryMementoImage, HistoryMementoName, PdnResources.GetString("Effects.ApplyingDialog.Description"));
            document.Layers.AddRange <Layer>(items.Select <System.Threading.Tasks.Task <BitmapLayer>, BitmapLayer>(t => t.Result));
            SelectionHistoryMemento       result   = task.Result;
            ReplaceDocumentHistoryMemento memento2 = task2.Result;

            base.EnterCriticalRegion();
            historyWorkspace.Document = document;
            HistoryMemento[] mementos = new HistoryMemento[] { result, memento2 };
            return(HistoryMemento.Combine(HistoryMementoName, HistoryMementoImage, mementos));
        }
        private void OnListTasks(LazyResult<Tasks.v1.Data.Tasks> response)
        {
            Tasks.v1.Data.Tasks result = null;

            try {
                result = response.GetResult();
            } catch (Exception ex) {
                AppDelegate.FinishActivity();
                ReloadComplete();
                ShowError (ex);
                return;
            }

            BeginInvokeOnMainThread (() => {
                var section = Root.First();
                section.Clear();

                if (result.Items != null) {
                    section.AddAll (result.Items.Select (t =>
                        new StyledStringElement (t.Title, () => OnTap (t)) {
                            Accessory = (t.Status == "completed") ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None
                        }
                    ).Cast<Element>());
                }

                ReloadComplete();

                AppDelegate.FinishActivity();
            });
        }