# imgui_touch, for AI assistants

imgui_touch makes Dear ImGui work on a touch screen, as on a phone (C++, 1.93 or later, docking or master; MIT): the swipe, the long press, the pinch, the phone's keyboard, with no change to the widgets. Repository: https://github.com/pthom/imgui_touch

## Where the truth is

- `imgui_touch.h`: the whole API, one header. Each function has its doc comment there. Read it to check that a name exists and how to call it.
- `imgui_touch_demo.cpp`: the usual way to call each function (`ImGuiTouch::ShowDemoWindow()`).
- `docs/dear_imgui_integration.md`: how the layer works inside, and what it reads from Dear ImGui's internals.

## Build

Copy `imgui_touch*.h` and `imgui_touch*.cpp` beside Dear ImGui (leave out `imgui_touch_demo.cpp`, or define `IMGUI_TOUCH_DISABLE_DEMO`), or with CMake: `add_subdirectory(imgui_touch)` then link `imgui_touch` (it uses an existing `imgui` target). C++11, no STL.

## Setup (namespace ImGuiTouch)

Set up once, after `ImGui::CreateContext()`; it then runs at each frame through Dear ImGui's context hooks (nothing to call per frame):

```cpp
ImGuiTouch::Setup();                    // swipe, long press, pinch
#ifdef __EMSCRIPTEN__
ImGuiTouch::SetupEmscripten();          // after the backend's init: the browser's fingers and pointer type
ImGuiTouch::SetupEmscriptenKeyboard();  // the phone's keyboard for text fields
#endif
```

The traps:
- A widget that a finger drags (a canvas, a knob): `ImGuiTouch::SetItemTakesDrags()` right after it (`ImGuiTouchAxis_Any` by default). Otherwise a drag scrolls the window, and the widget gets the finger only after a hold.
- A widget dragged along one axis (a slider): `ImGuiTouch::SetItemTakesDrags(ImGuiTouchAxis_X)` right after it: a drag along that axis moves it, a drag across scrolls the window. Dear ImGui's own sliders and drags do it with no call when the build has `IMGUI_HAS_ITEM_DRAG_AXIS_CALLBACK` (Dear ImGui Bundle's fork); with upstream Dear ImGui, call it after them.
- Two fingers on a widget (otherwise they scale the font), one call right after it: `ImGuiTouch::ItemPinch()` returns the gesture (`ImGuiTouchPinch`: `Center`, `CenterDelta`, `ScaleDelta`, `Scale`, `Started`, `Ended`, `Kind`) for a widget that zooms and pans itself; for an existing widget that zooms with the wheel, `ImGuiTouch::SetItemPinchAsWheel(wheel_zoom_factor, delivery)` (1.1 = +10% per wheel unit; `ImGuiTouchPinchDelivery_WheelNotches` if it reads only the wheel's sign) and `ImGuiTouch::SetItemTwoFingerPanAsMouseDrag(button)`. `RectPinch()`, `SetRectPinchAsWheel()`, `SetRectTwoFingerPanAsMouseDrag()` for a rectangle (a plot). An item that calls `ItemPinch()` needs an id (an `InvisibleButton`).
- Widgets inside a canvas that zooms and pans them (a node editor): the canvas wraps them in `ImGuiTouch::PushClaimTransform(scale, offset)` and `PopClaimTransform()` (on the screen: local * scale + offset). Otherwise their claims (the pinch, the drags) are where the widgets would be without the zoom.
- In a browser, the canvas must have the id "canvas".
- A link, in a browser: `if (!ImGuiTouch::SetItemOpensUrl(url) && ImGui::IsItemClicked()) open it yourself`. The page opens it in a new tab from the gesture's own handler (the only place WebKit allows one); true means the page takes it. `ImGui::TextLinkOpenURL` alone opens in a new tab on Chrome and Firefox, in the same tab on Safari and on iOS.
- Which backends report fingers, the touch source and the keyboard, and what to add for the others: the README, "What each backend reports". In short: a browser (with the Emscripten setup) is complete; SDL, Win32 and Android report the touch source but no fingers (no pinch); GLFW on Linux and macOS cannot tell a finger.
- A native app (iOS, Android) reports each finger with `ImGuiTouch::AddFingerEvent(id, pos, phase)` (for the pinch; `ImGuiTouchPhase_Cancelled` when the system takes the touch over), and its keyboard with `ImGuiTouch::SetKeyboardInsets()`: see `examples/sdl3_touch_demo/main.cpp` (the fingers from SDL3) and `examples/ios_touch_demo/main.mm` (UIKit).
- An app that sleeps when idle keeps drawing while `ImGuiTouch::IsAnimating()` is true (a fling, a bounce, a long press's ring).
- To try it on a desktop: `ImGuiTouch::GetConfig().SimulateTouch = true` (the mouse plays a finger).
- With Dear ImGui's Test Engine: call `ImGuiTouch::Setup()` before `ImGuiTestEngine_Start()` (hooks run in the order they were added).
- Hello ImGui sets all of this up itself: its settings are `RunnerParams::touchConfig`.

## Python

Dear ImGui Bundle binds it as `imgui_bundle.imgui_touch` (snake_case names). Its guide for AI assistants: https://imgui-bundle.pages.dev/llms.txt
