Ejemplo n.º 1
0
        public void Receive(DependencyObject value)
        {
            var control = (UIElement) value;
            control.AllowDrop = true;

            var window = Window.GetWindow(value);
            DragAdorner adorner=null;

            var createAdorner = Observable.FromEventPattern<DragEventHandler, DragEventArgs>
                (h => control.PreviewDragEnter += h, h => control.PreviewDragEnter -= h)
                .Select(ev => ev.EventArgs)
                .Subscribe(e =>
                {
                    var files = (string[])e.Data.GetData(DataFormats.FileDrop);
                    var container = new FileDropContainer(files);
                    var contentPresenter = new ContentPresenter {Content = container};

                    adorner = new DragAdorner(control, contentPresenter);
                });
            
            var clearAdorner = Observable.FromEventPattern<DragEventHandler, DragEventArgs>(h => control.PreviewDragLeave += h, h => control.PreviewDragLeave -= h).ToUnit()
                .Merge(Observable.FromEventPattern<DragEventHandler, DragEventArgs>(h => control.PreviewDrop += h, h => control.PreviewDrop -= h).ToUnit())
                .Subscribe(e =>
                {
                    if (adorner == null) return;
                    adorner.Detatch();
                    adorner = null;
                });

            var updatePositionOfAdornment = Observable.FromEventPattern<DragEventHandler, DragEventArgs>
                    (h => control.PreviewDragOver += h, h => control.PreviewDragOver -= h)
                    .Select(ev => ev.EventArgs)
                    .Where(_=>adorner!=null)
                    .Subscribe(e => adorner.MousePosition = e.GetPosition(window));
            
            var dropped = Observable.FromEventPattern<DragEventHandler, DragEventArgs>
                (h => control.Drop += h, h => control.Drop -= h)
                .Select(ev => ev.EventArgs)
                .SelectMany(e =>
                {
                    if (!e.Data.GetDataPresent(DataFormats.FileDrop))
                        return Enumerable.Empty<FileInfo>();

                    // Note that you can have more than one file.
                    var files = (string[]) e.Data.GetData(DataFormats.FileDrop);
                    return files.Select(f => new FileInfo(f));
                })
                .SubscribeSafe(_fileDropped);


            _cleanUp.Disposable = Disposable.Create(() =>
            {
                updatePositionOfAdornment.Dispose();
                clearAdorner.Dispose();
                createAdorner.Dispose();
                dropped.Dispose();
                _fileDropped.OnCompleted();
            });
        }
Ejemplo n.º 2
0
        public void FileDropContainerShouldReturnFileNames()
        {
            var input = new[] { @"c:\temp\file1.txt", @"c:\temp\file2.txt" };
            var expectedResult = new[] { @"file1.txt", @"file2.txt" };
            var container = new FileDropContainer(input);

            container.Files.Should().BeEquivalentTo(expectedResult);
        }
Ejemplo n.º 3
0
        public void FileDropContainerShouldHandleEmpty()
        {
            var input = new string[0];

            var container = new FileDropContainer(input);

            container.Files.Should().BeEmpty();
        }
Ejemplo n.º 4
0
        public void Receive(DependencyObject value)
        {
            if (_isLoaded || null == value)
            {
                return;
            }

            _isLoaded = true;

            var control = value as UIElement;

            if (null == control)
            {
                return;
            }

            control.AllowDrop = true;

            var         window  = Window.GetWindow(value);
            DragAdorner adorner = null;

            var createAdorner = Observable.FromEventPattern <DragEventHandler, DragEventArgs>
                                    (h => control.PreviewDragEnter += h, h => control.PreviewDragEnter -= h)
                                .Select(ev => ev.EventArgs)
                                .Subscribe(e =>
            {
                var files            = (string[])e.Data.GetData(DataFormats.FileDrop);
                var container        = new FileDropContainer(files);
                var contentPresenter = new ContentPresenter {
                    Content = container
                };

                adorner = new DragAdorner(control, contentPresenter);
            });

            var clearAdorner = Observable.FromEventPattern <DragEventHandler, DragEventArgs>(h => control.PreviewDragLeave += h, h => control.PreviewDragLeave -= h).ToUnit()
                               .Merge(Observable.FromEventPattern <DragEventHandler, DragEventArgs>(h => control.PreviewDrop += h, h => control.PreviewDrop -= h).ToUnit())
                               .Subscribe(e =>
            {
                if (adorner == null)
                {
                    return;
                }
                adorner.Detatch();

                adorner = null;
            });

            var updatePositionOfAdornment = Observable.FromEventPattern <DragEventHandler, DragEventArgs>
                                                (h => control.PreviewDragOver += h, h => control.PreviewDragOver -= h)
                                            .Select(ev => ev.EventArgs)
                                            .Where(_ => adorner != null)
                                            .Subscribe(e => adorner.MousePosition = e.GetPosition(window));

            var dropped = Observable.FromEventPattern <DragEventHandler, DragEventArgs>
                              (h => control.Drop += h, h => control.Drop -= h)
                          .Select(ev => ev.EventArgs)
                          .SelectMany(e =>
            {
                if (!e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    return(Enumerable.Empty <FileInfo>());
                }

                // Note that you can have more than one file.
                var files = (string[])e.Data.GetData(DataFormats.FileDrop);
                return(files.Select(f => new FileInfo(f)));
            })
                          .SubscribeSafe(_fileDropped);

            _cleanUp.Disposable = Disposable.Create(() =>
            {
                updatePositionOfAdornment.Dispose();
                clearAdorner.Dispose();
                createAdorner.Dispose();
                dropped.Dispose();
                _fileDropped.OnCompleted();
            });
        }
Ejemplo n.º 5
0
        public void FileDropContainerShouldHandleNull()
        {
            var container = new FileDropContainer(null);

            container.Files.Should().BeEmpty();
        }