Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Last active October 7, 2019 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TinkerWorX/3cd85ca6844aaf312508f1e851ddd676 to your computer and use it in GitHub Desktop.
Save TinkerWorX/3cd85ca6844aaf312508f1e851ddd676 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
namespace Ultralight.Net
{
public class ClConfig : IDisposable
{
[StructLayout(LayoutKind.Sequential)]
private struct C_Config
{
public ClFaceWinding FaceWinding;
public bool IsImagesEnabled;
public bool IsJavaScriptEnabled;
public bool UseBgraForOffscreenRendering;
public double DeviceScaleHint;
}
[DllImport("x86/" + Ultralight.ULTRALIGHT_DLL, EntryPoint = nameof(ulCreateConfig))]
private static extern IntPtr ulCreateConfigX86();
[DllImport("x64/" + Ultralight.ULTRALIGHT_DLL, EntryPoint = nameof(ulCreateConfig))]
private static extern IntPtr ulCreateConfigX64();
private static IntPtr ulCreateConfig() => Marshal.SizeOf<IntPtr>() switch
{
4 => ulCreateConfigX86(),
8 => ulCreateConfigX64(),
_ => throw new InvalidOperationException(),
};
[DllImport("x64/" + Ultralight.ULTRALIGHT_DLL, EntryPoint = nameof(ulDestroyConfig))]
private static extern void ulDestroyConfigX86(IntPtr config);
[DllImport("x64/" + Ultralight.ULTRALIGHT_DLL, EntryPoint = nameof(ulDestroyConfig))]
private static extern void ulDestroyConfigX64(IntPtr config);
private static void ulDestroyConfig(IntPtr config)
{
switch (Marshal.SizeOf<IntPtr>())
{
case 4: ulDestroyConfigX86(config); break;
case 8: ulDestroyConfigX64(config); break;
default: throw new InvalidOperationException();
}
}
private unsafe C_Config* config;
public ClConfig()
{
unsafe { this.config = (C_Config*)ulCreateConfig(); }
}
public IntPtr Handle { get { AssertNotDisposed(); unsafe { return new IntPtr(this.config); } } }
public bool IsDisposed { get; private set; }
~ClConfig() { this.Dispose(false); }
private void AssertNotDisposed()
{
if (this.IsDisposed)
throw new ObjectDisposedException(nameof(ClConfig));
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
AssertNotDisposed();
ulDestroyConfig(this.Handle);
unsafe { this.config = null; }
this.IsDisposed = true;
}
/// <summary>
/// The winding order for front-facing triangles. <see cref="Ultralight.Net.FaceWinding"/>
/// </summary>
public ClFaceWinding FaceWinding
{
get { AssertNotDisposed(); unsafe { return this.config->FaceWinding; } }
set { AssertNotDisposed(); unsafe { this.config->FaceWinding = value; } }
}
/// <summary>
/// Whether or not images should be enabled.
/// </summary>
public bool IsImagesEnabled
{
get { AssertNotDisposed(); unsafe { return this.config->IsImagesEnabled; } }
set { AssertNotDisposed(); unsafe { this.config->IsImagesEnabled = value; } }
}
/// <summary>
/// Whether or not JavaScript should be enabled
/// </summary>
public bool IsJavaScriptEnabled
{
get { AssertNotDisposed(); unsafe { return this.config->IsJavaScriptEnabled; } }
set { AssertNotDisposed(); unsafe { this.config->IsJavaScriptEnabled = value; } }
}
/// <summary>
/// When using the default, offscreen GPU driver, whether or not we should use BGRA byte order (instead of RGBA). <see cref="Ultralight.Net.Bitmap"/>
/// </summary>
public bool UseBgraForOffscreenRendering
{
get { AssertNotDisposed(); unsafe { return this.config->UseBgraForOffscreenRendering; } }
set { AssertNotDisposed(); unsafe { this.config->UseBgraForOffscreenRendering = value; } }
}
/// <summary>
/// The amount that the application DPI has been scaled (200% = 2.0). Used for scaling device coordinates to pixels and oversampling raster shapes.
/// </summary>
public double DeviceScaleHint
{
get { AssertNotDisposed(); unsafe { return this.config->DeviceScaleHint; } }
set { AssertNotDisposed(); unsafe { this.config->DeviceScaleHint = value; } }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment