From a297199e59d9d233b4819d887dfac8c4e2413f23 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Wed, 30 Sep 2015 16:27:08 +0300 Subject: [PATCH] Fixed GTK/Cairo render --- src/Gtk/Perspex.Cairo/CairoPlatform.cs | 6 ++++- src/Gtk/Perspex.Cairo/RenderTarget.cs | 23 ++++--------------- src/Gtk/Perspex.Gtk/WindowImpl.cs | 14 +++++------ src/Perspex.Application/Application.cs | 2 +- .../Platform/ITopLevelRenderer.cs | 4 +++- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/Gtk/Perspex.Cairo/CairoPlatform.cs b/src/Gtk/Perspex.Cairo/CairoPlatform.cs index ba0d8881c1..a4bf08c4ea 100644 --- a/src/Gtk/Perspex.Cairo/CairoPlatform.cs +++ b/src/Gtk/Perspex.Cairo/CairoPlatform.cs @@ -38,7 +38,11 @@ namespace Perspex.Cairo public IRenderTarget CreateRenderer(IPlatformHandle handle, double width, double height) { - return new RenderTarget(handle, width, height); + if (handle.HandleDescriptor != "GtkWindow") + throw new NotSupportedException(string.Format( + "Don't know how to create a Cairo renderer from a '{0}' handle", + handle.HandleDescriptor)); + return new RenderTarget((Gtk.Window)handle, width, height); } public IRenderTargetBitmapImpl CreateRenderTargetBitmap(int width, int height) diff --git a/src/Gtk/Perspex.Cairo/RenderTarget.cs b/src/Gtk/Perspex.Cairo/RenderTarget.cs index b8de2e2166..333c558e8a 100644 --- a/src/Gtk/Perspex.Cairo/RenderTarget.cs +++ b/src/Gtk/Perspex.Cairo/RenderTarget.cs @@ -17,19 +17,18 @@ namespace Perspex.Cairo /// public class RenderTarget : IRenderTarget { - private readonly IPlatformHandle _handle; private readonly Surface _surface; - private Gdk.Window _window; + private Gtk.Window _window; /// /// Initializes a new instance of the class. /// - /// The window handle. + /// The window. /// The width of the window. /// The height of the window. - public RenderTarget(IPlatformHandle handle, double width, double height) + public RenderTarget(Gtk.Window window, double width, double height) { - _handle = handle; + _window = window; } public RenderTarget(ImageSurface surface) @@ -56,19 +55,7 @@ namespace Perspex.Cairo { if(_surface != null) return new DrawingContext(_surface); - - switch (_handle.HandleDescriptor) - { - case "GdkWindow": - if (_window == null) - _window = new Gdk.Window(_handle.Handle); - - return new DrawingContext(_window); - default: - throw new NotSupportedException(string.Format( - "Don't know how to create a Cairo renderer from a '{0}' handle", - _handle.HandleDescriptor)); - } + return new DrawingContext(_window.GdkWindow); } public void Dispose() => _surface?.Dispose(); diff --git a/src/Gtk/Perspex.Gtk/WindowImpl.cs b/src/Gtk/Perspex.Gtk/WindowImpl.cs index c6076a85f8..7b4b149dc3 100644 --- a/src/Gtk/Perspex.Gtk/WindowImpl.cs +++ b/src/Gtk/Perspex.Gtk/WindowImpl.cs @@ -14,12 +14,10 @@ namespace Perspex.Gtk { using Gtk = global::Gtk; - public class WindowImpl : Gtk.Window, IWindowImpl + public class WindowImpl : Gtk.Window, IWindowImpl, IPlatformHandle { private IInputRoot _inputRoot; - - private IPlatformHandle _windowHandle; - + private Size _clientSize; private Gtk.IMContext _imContext; @@ -46,7 +44,6 @@ namespace Perspex.Gtk Events = EventMask.PointerMotionMask | EventMask.ButtonPressMask | EventMask.ButtonReleaseMask; - _windowHandle = new PlatformHandle(Handle, "GtkWindow"); _imContext = new Gtk.IMMulticontext(); _imContext.Commit += ImContext_Commit; } @@ -73,7 +70,8 @@ namespace Perspex.Gtk } } - IPlatformHandle ITopLevelImpl.Handle => _windowHandle; + IPlatformHandle ITopLevelImpl.Handle => this; + public string HandleDescriptor => "GtkWindow"; public Action Activated { get; set; } @@ -94,7 +92,9 @@ namespace Perspex.Gtk public void Invalidate(Rect rect) { - base.GdkWindow.InvalidateRect (new Rectangle ((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height), true); + if (base.GdkWindow != null) + base.GdkWindow.InvalidateRect( + new Rectangle((int) rect.X, (int) rect.Y, (int) rect.Width, (int) rect.Height), true); } public Point PointToScreen(Point point) diff --git a/src/Perspex.Application/Application.cs b/src/Perspex.Application/Application.cs index 88f3a23ffe..334f147b43 100644 --- a/src/Perspex.Application/Application.cs +++ b/src/Perspex.Application/Application.cs @@ -177,7 +177,7 @@ namespace Perspex /// The value of Environment.OSVersion.Platform. protected void InitializeSubsystems(int platformID) { - if (platformID == 4 || platformID == 6) + if (true) { InitializeSubsystem("Perspex.Cairo"); InitializeSubsystem("Perspex.Gtk"); diff --git a/src/Perspex.Controls/Platform/ITopLevelRenderer.cs b/src/Perspex.Controls/Platform/ITopLevelRenderer.cs index fa71860f7d..0f58dac76e 100644 --- a/src/Perspex.Controls/Platform/ITopLevelRenderer.cs +++ b/src/Perspex.Controls/Platform/ITopLevelRenderer.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Perspex.Platform; using Perspex.Rendering; +using Perspex.Threading; namespace Perspex.Controls.Platform { @@ -42,7 +43,8 @@ namespace Perspex.Controls.Platform viewport.Resize((int) clientSize.Width, (int) clientSize.Height); })); resources.Add(queueManager.RenderNeeded.Subscribe(_ - => topLevel.PlatformImpl.Invalidate(new Rect(topLevel.ClientSize)))); + => + Dispatcher.UIThread.InvokeAsync(() => topLevel.PlatformImpl.Invalidate(new Rect(topLevel.ClientSize))))); topLevel.PlatformImpl.Paint = rect => {