Browse Source

Merge branch 'master' into refactor/use-selectionmodel

Steven Kirk 5 năm trước cách đây
mục cha
commit
9f1e668fd7

+ 1 - 0
samples/ControlCatalog/Pages/DialogsPage.xaml

@@ -6,6 +6,7 @@
       <Button Name="OpenFile">Open File</Button>
       <Button Name="SaveFile">Save File</Button>
       <Button Name="SelectFolder">Select Folder</Button>
+      <Button Name="OpenBoth">Select Both</Button>
       <Button Name="DecoratedWindow">Decorated window</Button>
       <Button Name="DecoratedWindowDialog">Decorated window (dialog)</Button>
       <Button Name="Dialog">Dialog</Button>

+ 14 - 0
samples/ControlCatalog/Pages/DialogsPage.xaml.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using Avalonia.Controls;
+using Avalonia.Dialogs;
 using Avalonia.Markup.Xaml;
 #pragma warning disable 4014
 
@@ -58,6 +59,19 @@ namespace ControlCatalog.Pages
                     Title = "Select folder",
                 }.ShowAsync(GetWindow());
             };
+            this.FindControl<Button>("OpenBoth").Click += async delegate
+            {
+                var res = await new OpenFileDialog()
+                {
+                    Title = "Select both",
+                    AllowMultiple = true
+                }.ShowManagedAsync(GetWindow(), new ManagedFileDialogOptions
+                {
+                    AllowDirectorySelection = true
+                });
+                if (res != null)
+                    Console.WriteLine("Selected: \n" + string.Join("\n", res));
+            };
             this.FindControl<Button>("DecoratedWindow").Click += delegate
             {
                 new DecoratedWindow().Show();

+ 8 - 4
src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs

@@ -14,6 +14,7 @@ namespace Avalonia.Dialogs
 {
     internal class ManagedFileChooserViewModel : InternalViewModelBase
     {
+        private readonly ManagedFileDialogOptions _options;
         public event Action CancelRequested;
         public event Action<string[]> CompleteRequested;
 
@@ -103,8 +104,9 @@ namespace Avalonia.Dialogs
             QuickLinks.AddRange(quickSources.GetAllItems().Select(i => new ManagedFileChooserItemViewModel(i)));
         }
 
-        public ManagedFileChooserViewModel(FileSystemDialog dialog)
+        public ManagedFileChooserViewModel(FileSystemDialog dialog, ManagedFileDialogOptions options)
         {
+            _options = options;
             _disposables = new CompositeDisposable();
 
             var quickSources = AvaloniaLocator.Current
@@ -202,10 +204,12 @@ namespace Avalonia.Dialogs
                     }
                     else
                     {
-                        var invalidItems = SelectedItems.Where(i => i.ItemType == ManagedFileChooserItemType.Folder).ToList();
-                        foreach (var item in invalidItems)
+                        if (!_options.AllowDirectorySelection)
                         {
-                            SelectedItems.Remove(item);
+                            var invalidItems = SelectedItems.Where(i => i.ItemType == ManagedFileChooserItemType.Folder)
+                                .ToList();
+                            foreach (var item in invalidItems) 
+                                SelectedItems.Remove(item);
                         }
 
                         if (!_selectingDirectory)

+ 18 - 2
src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs

@@ -9,13 +9,15 @@ namespace Avalonia.Dialogs
     {
         private class ManagedSystemDialogImpl<T> : ISystemDialogImpl where T : Window, new()
         {
-            async Task<string[]> Show(SystemDialog d, Window parent)
+            async Task<string[]> Show(SystemDialog d, Window parent, ManagedFileDialogOptions options = null)
             {
-                var model = new ManagedFileChooserViewModel((FileSystemDialog)d);
+                var model = new ManagedFileChooserViewModel((FileSystemDialog)d,
+                    options ?? new ManagedFileDialogOptions());
 
                 var dialog = new T
                 {
                     Content = new ManagedFileChooser(),
+                    Title = d.Title,
                     DataContext = model
                 };
 
@@ -44,6 +46,11 @@ namespace Avalonia.Dialogs
             {
                 return (await Show(dialog, parent))?.FirstOrDefault();
             }
+            
+            public async Task<string[]> ShowFileDialogAsync(FileDialog dialog, Window parent, ManagedFileDialogOptions options)
+            {
+                return await Show(dialog, parent, options);
+            }
         }
 
         public static TAppBuilder UseManagedSystemDialogs<TAppBuilder>(this TAppBuilder builder)
@@ -61,5 +68,14 @@ namespace Avalonia.Dialogs
                 AvaloniaLocator.CurrentMutable.Bind<ISystemDialogImpl>().ToSingleton<ManagedSystemDialogImpl<TWindow>>());
             return builder;
         }
+
+        public static Task<string[]> ShowManagedAsync(this OpenFileDialog dialog, Window parent,
+            ManagedFileDialogOptions options = null) => ShowManagedAsync<Window>(dialog, parent, options);
+        
+        public static Task<string[]> ShowManagedAsync<TWindow>(this OpenFileDialog dialog, Window parent,
+            ManagedFileDialogOptions options = null) where TWindow : Window, new()
+        {
+            return new ManagedSystemDialogImpl<TWindow>().ShowFileDialogAsync(dialog, parent, options);
+        }
     }
 }

+ 7 - 0
src/Avalonia.Dialogs/ManagedFileDialogOptions.cs

@@ -0,0 +1,7 @@
+namespace Avalonia.Dialogs
+{
+    public class ManagedFileDialogOptions
+    {
+        public bool AllowDirectorySelection { get; set; }
+    }
+}

+ 3 - 1
src/Avalonia.X11/X11Window.cs

@@ -328,7 +328,9 @@ namespace Avalonia.X11
             }
             else if (ev.type == XEventName.UnmapNotify)
                 _mapped = false;
-            else if (ev.type == XEventName.Expose)
+            else if (ev.type == XEventName.Expose ||
+                     (ev.type == XEventName.VisibilityNotify &&
+                      ev.VisibilityEvent.state < 2))
             {
                 if (!_triggeredExpose)
                 {