Skip to content

Commit c2549e8

Browse files
committed
Add set_current_context
1 parent ebafe1a commit c2549e8

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

docs/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ <h4 id="functions">Functions</h4>
302302
<div class="api">
303303
<code><span class='keyword'>def</span> <span class='func_name'>get_current_context</span>() -> <span class='symbol'>Context</span></code>
304304
</div>
305+
<div class="api">
306+
<code><span class='keyword'>def</span> <span class='func_name'>set_current_context</span>(<span class='arg_name'>arg</span><span class='special'>:</span> <span class='symbol'>Context</span><span class='special'>,</span> /) -> <span class='keyword'>None</span></code>
307+
</div>
305308

306309
<h3 id="main">Main</h3>
307310
<h4 id="functions-1">Functions</h4>

gen/checklist.py

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"label_text_v", # varargs not relevant in Python
2525
"bullet_text_v", # varargs not relevant in Python
2626
"is_any_mouse_down", # will obsolete
27+
"get_style", # wrapped in python, using internal funcs
28+
"get_io", # wrapped in python, using internal funcs
2729
}
2830

2931

src/slimgui/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def create_context(shared_font_atlas: slimgui_ext.FontAtlas | None = None) -> Wr
4646
def get_current_context() -> WrappedContext | None:
4747
return _current_context
4848

49+
def set_current_context(ctx: WrappedContext) -> None:
50+
global _current_context
51+
_current_context = ctx
52+
slimgui_ext.set_current_context(ctx.context)
53+
4954
def destroy_context(ctx: WrappedContext | None):
5055
global _current_context
5156
prev_ctx = get_current_context()

src/slimgui/slimgui_ext.pyi

+4
Original file line numberDiff line numberDiff line change
@@ -3652,6 +3652,10 @@ def set_column_width(column_index: int, width: float) -> None:
36523652
...
36533653

36543654

3655+
def set_current_context(arg: Context, /) -> None:
3656+
...
3657+
3658+
36553659
def set_cursor_pos(local_pos: tuple[float, float]) -> None:
36563660
"""[window-local] \""""
36573661
...

src/slimgui_ext.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,9 @@ NB_MODULE(slimgui_ext, m) {
358358

359359
m.def("create_context", &ImGui::CreateContext, "shared_font_atlas"_a = nullptr, nb::rv_policy::reference);
360360
m.def("get_current_context", &ImGui::GetCurrentContext, nb::rv_policy::reference);
361+
m.def("set_current_context", &ImGui::SetCurrentContext, nb::rv_policy::reference);
361362
m.def("destroy_context", &ImGui::DestroyContext);
362-
m.def("get_style", &ImGui::GetStyle, nb::rv_policy::reference);
363+
m.def("get_style", &ImGui::GetStyle, nb::rv_policy::reference); // TODO docs should refer to slimgui/__init__.py declarations
363364
m.def("render", &ImGui::Render);
364365
m.def("new_frame", &ImGui::NewFrame);
365366
m.def("get_draw_data", &ImGui::GetDrawData, nb::rv_policy::reference);

tests/test_simple.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def frame_scope(imgui_context, null_renderer):
2424
def test_current_context(imgui_context):
2525
assert imgui_context is not None
2626
assert imgui.get_current_context() is not None
27-
assert imgui_context == imgui.get_current_context()
27+
assert imgui_context is imgui.get_current_context()
2828

2929
def test_style_access(imgui_context):
3030
styles = imgui.get_style()
@@ -37,6 +37,29 @@ def test_style_access(imgui_context):
3737
styles.window_padding = (2, 4)
3838
assert styles.window_padding == (2, 4)
3939

40+
def test_set_current_context(imgui_context):
41+
assert imgui_context is not None
42+
assert imgui.get_current_context() is not None
43+
assert imgui_context is imgui.get_current_context()
44+
45+
ctx2 = imgui.create_context()
46+
assert ctx2 is imgui.get_current_context()
47+
assert imgui_context is not imgui.get_current_context()
48+
c = imgui.get_current_context()
49+
assert c is not None
50+
assert imgui_context.context is not c.context
51+
imgui.set_current_context(imgui_context)
52+
assert imgui_context is imgui.get_current_context()
53+
c = imgui.get_current_context()
54+
assert c is not None
55+
assert imgui_context.context is c.context
56+
imgui.set_current_context(ctx2)
57+
imgui.destroy_context(None)
58+
assert imgui.get_current_context() is None
59+
imgui.set_current_context(imgui_context)
60+
assert imgui_context is imgui.get_current_context()
61+
62+
4063
def test_begin_base_window(frame_scope):
4164
visible, _ = imgui.begin("Window")
4265
imgui.end()

0 commit comments

Comments
 (0)