浏览代码

Merge branch 'master' into infra/artifacts_controlcatalog

Max Katz 4 年之前
父节点
当前提交
f8228b139f

+ 45 - 28
src/Avalonia.Controls/Chrome/CaptionButtons.cs

@@ -14,17 +14,21 @@ namespace Avalonia.Controls.Chrome
     public class CaptionButtons : TemplatedControl
     {
         private CompositeDisposable? _disposables;
-        private Window? _hostWindow;
 
-        public void Attach(Window hostWindow)
+        /// <summary>
+        /// Currently attached window.
+        /// </summary>
+        protected Window? HostWindow { get; private set; }
+
+        public virtual void Attach(Window hostWindow)
         {
             if (_disposables == null)
             {
-                _hostWindow = hostWindow;
+                HostWindow = hostWindow;
 
                 _disposables = new CompositeDisposable
                 {
-                    _hostWindow.GetObservable(Window.WindowStateProperty)
+                    HostWindow.GetObservable(Window.WindowStateProperty)
                     .Subscribe(x =>
                     {
                         PseudoClasses.Set(":minimized", x == WindowState.Minimized);
@@ -36,14 +40,45 @@ namespace Avalonia.Controls.Chrome
             }
         }
 
-        public void Detach()
+        public virtual void Detach()
         {
             if (_disposables != null)
             {
                 _disposables.Dispose();
                 _disposables = null;
 
-                _hostWindow = null;
+                HostWindow = null;
+            }
+        }
+
+        protected virtual void OnClose()
+        {
+            HostWindow?.Close();
+        }
+
+        protected virtual void OnRestore()
+        {
+            if (HostWindow != null)
+            {
+                HostWindow.WindowState = HostWindow.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
+            }
+        }
+
+        protected virtual void OnMinimize()
+        {
+            if (HostWindow != null)
+            {
+                HostWindow.WindowState = WindowState.Minimized;
+            }
+        }
+
+        protected virtual void OnToggleFullScreen()
+        {
+            if (HostWindow != null)
+            {
+                HostWindow.WindowState = HostWindow.WindowState == WindowState.FullScreen
+                    ? WindowState.Normal
+                    : WindowState.FullScreen;
             }
         }
 
@@ -56,31 +91,13 @@ namespace Avalonia.Controls.Chrome
             var minimiseButton = e.NameScope.Get<Panel>("PART_MinimiseButton");
             var fullScreenButton = e.NameScope.Get<Panel>("PART_FullScreenButton");
 
-            closeButton.PointerReleased += (sender, e) => _hostWindow?.Close();
+            closeButton.PointerReleased += (sender, e) => OnClose();
 
-            restoreButton.PointerReleased += (sender, e) =>
-            {
-                if (_hostWindow != null)
-                {
-                    _hostWindow.WindowState = _hostWindow.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
-                }
-            };
+            restoreButton.PointerReleased += (sender, e) => OnRestore();
 
-            minimiseButton.PointerReleased += (sender, e) =>
-            {
-                if (_hostWindow != null)
-                {
-                    _hostWindow.WindowState = WindowState.Minimized;
-                }
-            };
+            minimiseButton.PointerReleased += (sender, e) => OnMinimize();
 
-            fullScreenButton.PointerReleased += (sender, e) =>
-            {
-                if (_hostWindow != null)
-                {
-                    _hostWindow.WindowState = _hostWindow.WindowState == WindowState.FullScreen ? WindowState.Normal : WindowState.FullScreen;
-                }
-            };
+            fullScreenButton.PointerReleased += (sender, e) => OnToggleFullScreen();
         }
     }
 }

+ 11 - 1
src/Avalonia.Diagnostics/DevToolsExtensions.cs

@@ -15,7 +15,7 @@ namespace Avalonia
         /// <param name="root">The window to attach DevTools to.</param>
         public static void AttachDevTools(this TopLevel root)
         {
-            DevTools.Attach(root, new KeyGesture(Key.F12));
+            DevTools.Attach(root, new DevToolsOptions());
         }
 
         /// <summary>
@@ -27,5 +27,15 @@ namespace Avalonia
         {
             DevTools.Attach(root, gesture);
         }
+
+        /// <summary>
+        /// Attaches DevTools to a window, to be opened with the specified options.
+        /// </summary>
+        /// <param name="root">The window to attach DevTools to.</param>
+        /// <param name="options">Additional settings of DevTools.</param>
+        public static void AttachDevTools(this TopLevel root, DevToolsOptions options)
+        {
+            DevTools.Attach(root, options);
+        }
     }
 }

+ 18 - 6
src/Avalonia.Diagnostics/Diagnostics/DevTools.cs

@@ -6,6 +6,8 @@ using Avalonia.Diagnostics.Views;
 using Avalonia.Input;
 using Avalonia.Interactivity;
 
+#nullable enable 
+
 namespace Avalonia.Diagnostics
 {
     public static class DevTools
@@ -13,12 +15,20 @@ namespace Avalonia.Diagnostics
         private static readonly Dictionary<TopLevel, Window> s_open = new Dictionary<TopLevel, Window>();
 
         public static IDisposable Attach(TopLevel root, KeyGesture gesture)
+        {
+            return Attach(root, new DevToolsOptions()
+            {
+                Gesture = gesture,
+            });
+        }
+
+        public static IDisposable Attach(TopLevel root, DevToolsOptions options)
         {
             void PreviewKeyDown(object sender, KeyEventArgs e)
             {
-                if (gesture.Matches(e))
+                if (options.Gesture.Matches(e))
                 {
-                    Open(root);
+                    Open(root, options);
                 }
             }
 
@@ -28,7 +38,9 @@ namespace Avalonia.Diagnostics
                 RoutingStrategies.Tunnel);
         }
 
-        public static IDisposable Open(TopLevel root)
+        public static IDisposable Open(TopLevel root) => Open(root, new DevToolsOptions());
+
+        public static IDisposable Open(TopLevel root, DevToolsOptions options)
         {
             if (s_open.TryGetValue(root, out var window))
             {
@@ -38,15 +50,15 @@ namespace Avalonia.Diagnostics
             {
                 window = new MainWindow
                 {
-                    Width = 1024,
-                    Height = 512,
                     Root = root,
+                    Width = options.Size.Width,
+                    Height = options.Size.Height,
                 };
 
                 window.Closed += DevToolsClosed;
                 s_open.Add(root, window);
 
-                if (root is Window inspectedWindow)
+                if (options.ShowAsChildWindow && root is Window inspectedWindow)
                 {
                     window.Show(inspectedWindow);
                 }

+ 26 - 0
src/Avalonia.Diagnostics/Diagnostics/DevToolsOptions.cs

@@ -0,0 +1,26 @@
+using Avalonia.Input;
+
+namespace Avalonia.Diagnostics
+{
+    /// <summary>
+    /// Describes options used to customize DevTools.
+    /// </summary>
+    public class DevToolsOptions
+    {
+        /// <summary>
+        /// Gets or sets the key gesture used to open DevTools.
+        /// </summary>
+        public KeyGesture Gesture { get; set; } = new KeyGesture(Key.F12);
+
+        /// <summary>
+        /// Gets or sets a value indicating whether DevTools should be displayed as a child window
+        /// of the window being inspected. The default value is true.
+        /// </summary>
+        public bool ShowAsChildWindow { get; set; } = true;
+
+        /// <summary>
+        /// Gets or sets the initial size of the DevTools window. The default value is 1024x512.
+        /// </summary>
+        public Size Size { get; set; } = new Size(1024, 512);
+    }
+}