[Xwt] Allow Gradient instances to be shared among toolkits using InitForToolkit pattern

This commit is contained in:
Jérémie Laval 2015-05-08 12:32:14 -04:00
Родитель fecebb2267
Коммит 2cf5df5ef9
9 изменённых файлов: 63 добавлений и 4 удалений

Просмотреть файл

@ -265,8 +265,10 @@ namespace Xwt.CairoBackend
public override void SetPattern (object backend, object p)
{
var cb = (CairoContextBackend)backend;
var toolkit = ApplicationContext.Toolkit;
Cairo.Context ctx = cb.Context;
p = toolkit.GetSafeBackend (p);
if (p is ImagePatternBackend) {
cb.PatternAlpha = ((ImagePatternBackend)p).Image.Alpha;
p = ((ImagePatternBackend)p).GetPattern (ApplicationContext, ((CairoContextBackend)backend).ScaleFactor);

Просмотреть файл

@ -245,6 +245,8 @@ namespace Xwt.WPFBackend
public override void SetPattern (object backend, object p)
{
var c = (DrawingContext) backend;
var toolkit = ApplicationContext.Toolkit;
p = toolkit.GetSafeBackend (p);
if (p is ImagePattern)
p = ((ImagePattern)p).GetBrush (c.ScaleFactor);
c.SetPattern ((System.Windows.Media.Brush)p);

Просмотреть файл

@ -241,7 +241,8 @@ namespace Xwt.Mac
public override void SetPattern (object backend, object p)
{
CGContextBackend gc = (CGContextBackend)backend;
gc.CurrentStatus.Pattern = p;
var toolkit = ApplicationContext.Toolkit;
gc.CurrentStatus.Pattern = toolkit.GetSafeBackend (p);
}
void SetupPattern (CGContextBackend gc)

Просмотреть файл

@ -335,7 +335,7 @@ namespace Xwt.Drawing
get { return pattern; }
set {
pattern = value;
handler.SetPattern (Backend, GetBackend (value));
handler.SetPattern (Backend, value);
}
}

Просмотреть файл

@ -25,6 +25,7 @@
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using Xwt.Backends;
@ -32,11 +33,36 @@ namespace Xwt.Drawing
{
public abstract class Gradient: Pattern
{
List<KeyValuePair<double, Color>> stops;
public Gradient AddColorStop (double pos, Color color)
{
ToolkitEngine.GradientBackendHandler.AddColorStop (Backend, pos, color);
(stops ?? (stops = new List<KeyValuePair<double, Color>> ())).Add (new KeyValuePair<double, Color> (pos, color));
return this;
}
public void InitForToolkit (Toolkit t)
{
if (ToolkitEngine != t) {
var handler = t.GradientBackendHandler;
var backend = CreateGradientBackend (handler);
SetBackend (handler, backend);
if (stops != null)
foreach (var stop in stops)
handler.AddColorStop (backend, stop.Key, stop.Value);
}
}
protected override object OnCreateBackend ()
{
var handler = ToolkitEngine.GradientBackendHandler;
var backend = CreateGradientBackend (handler);
SetBackend (handler, backend);
return backend;
}
protected abstract object CreateGradientBackend (GradientBackendHandler handler);
}
}

Просмотреть файл

@ -31,9 +31,19 @@ namespace Xwt.Drawing
{
public sealed class LinearGradient: Gradient
{
double xStart, yStart, xEnd, yEnd;
public LinearGradient (double xStart, double yStart, double xEnd, double yEnd)
{
SetBackend (ToolkitEngine.GradientBackendHandler, ToolkitEngine.GradientBackendHandler.CreateLinear (xStart, yStart, xEnd, yEnd));
this.xStart = xStart;
this.xEnd = xEnd;
this.yStart = yStart;
this.yEnd = yEnd;
}
protected override object CreateGradientBackend (GradientBackendHandler handler)
{
return handler.CreateLinear (xStart, yStart, xEnd, yEnd);
}
}
}

Просмотреть файл

@ -39,6 +39,10 @@ namespace Xwt.Drawing
internal void SetBackend (DisposableResourceBackendHandler handler, object backend)
{
if (this.handler != null) {
Dispose ();
GC.ReRegisterForFinalize (this);
}
Backend = backend;
this.handler = handler;
if (handler.DisposeHandleOnUiThread)

Просмотреть файл

@ -31,9 +31,21 @@ namespace Xwt.Drawing
{
public sealed class RadialGradient: Gradient
{
double cx0, cy0, radius0, cx1, cy1, radius1;
public RadialGradient (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1)
{
SetBackend (ToolkitEngine.GradientBackendHandler, ToolkitEngine.GradientBackendHandler.CreateRadial (cx0, cy0, radius0, cx1, cy1, radius1));
this.cx0 = cx0;
this.cy0 = cy0;
this.radius0 = radius0;
this.cx1 = cx1;
this.cy1 = cy1;
this.radius1 = radius1;
}
protected override object CreateGradientBackend (GradientBackendHandler handler)
{
return handler.CreateRadial (cx0, cy0, radius0, cx1, cy1, radius1);
}
}
}

Просмотреть файл

@ -525,6 +525,8 @@ namespace Xwt
}
font.InitForToolkit (this);
} else if (obj is Gradient) {
((Gradient)obj).InitForToolkit (this);
} else if (obj is IFrontend) {
if (((IFrontend)obj).ToolkitEngine != this)
throw new InvalidOperationException ("Object belongs to a different toolkit");