Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

ImGuiTouch

ImGuiTouch makes Dear ImGui work on a touch screen, as on a phone: an app made for the mouse works with a finger, with no change to its widgets.

It is a library of its own (MIT): one header, a few sources, and Dear ImGui as its only dependency. It comes from ImGuiKit, which keeps the layout helpers and the widgets. Dear ImGui Bundle includes it, with Python bindings (imgui_bundle.imgui_touch).

An app run by Hello ImGui or ImmApp gets the touch layer with nothing to write: the runner sets it up, and its settings are RunnerParams.touch_config. An app with its own backend and loop sets it up once, after creating the context: imgui_touch.setup().

On a touch screen:

A widget of your own takes two fingers with one call, right after it: item_pinch() returns their gesture at each frame (its center, its spread and its move since the last frame). The widget then zooms and pans at once, as a map does on a phone.

Python
C++
from imgui_bundle import imgui, imgui_touch, ImVec2

zoom, origin = 1.0, ImVec2(0, 0)  # in your app's state: the content's scale, and its origin on the screen
imgui.invisible_button("##canvas", ImVec2(300, 200))
pinch = imgui_touch.item_pinch()  # two fingers on the canvas
if pinch.active:
    c, k = pinch.center, pinch.scale_delta
    origin = ImVec2(c.x - (c.x - origin.x) * k + pinch.center_delta.x,  # zoom around the fingers,
                    c.y - (c.y - origin.y) * k + pinch.center_delta.y)  # and follow their move
    zoom *= k

A widget that already zooms with the wheel and pans with a mouse button (an image viewer, a plot) needs no new code: set_item_pinch_as_wheel(wheel_zoom_factor) turns the pinch into wheel events (1.1 for +10% per wheel unit; PinchDelivery_.wheel_notches for a widget that reads only the wheel’s sign, as ImPlot), and set_item_two_finger_pan_as_mouse_drag(button) turns two fingers moved together into a drag. For a widget that is not one item, the set_rect_... and rect_pinch() variants take its rectangle. The demo “Touch screens (phones, tablets)” shows every gesture; on a desktop, its “Simulate a touch source” checkbox plays the finger with the mouse. The library’s own demo, imgui_touch.show_demo_window(), has the same gestures, with its settings.

Where the gestures work:

Learn more