Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
no need to connect context
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsn committed Sep 21, 2022
1 parent d891b1f commit d470f5b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/VL.ImGui.Generator/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static string CreateSource(SourceProductionContext context, ClassDeclara
nodeDecl = "return c.Node(inputs, outputs);";
break;
case Mode.ImmediateMode:
nodeDecl = "return c.Node(inputs, outputs, () => { if (ctx != null) s.Update(ctx); });";
nodeDecl = "return c.Node(inputs, outputs, () => { s.Update(ctx); });";
break;
default:
break;
Expand Down
10 changes: 10 additions & 0 deletions src/VL.ImGui/Core/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ namespace VL.ImGui
{
using ImGui = ImGuiNET.ImGui;

public static class ContextHelpers
{
public static Context Validate(this Context c) => c ?? Context.Current;
}


public class Context : IDisposable
{
private readonly IntPtr _context;
private readonly List<Widget> _widgetsToReset = new List<Widget>();

[ThreadStatic]
internal static Context Current;

public Context()
{
_context = ImGui.CreateContext();
Expand All @@ -32,6 +41,7 @@ public virtual void NewFrame()

public Frame MakeCurrent()
{
Current = this;
return new Frame(_context);
}

Expand Down
18 changes: 11 additions & 7 deletions src/VL.ImGui/Core/Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ internal virtual void Reset() { }

internal void Update(Context context)
{
try
context = context.Validate();
if (context != null)
{
Style?.Set();
UpdateCore(context);
}
finally
{
Style?.Reset();
try
{
Style?.Set();
UpdateCore(context);
}
finally
{
Style?.Reset();
}
}
}
}
Expand Down

0 comments on commit d470f5b

Please sign in to comment.