Skip to content

Commit

Permalink
AppWindowParams: add EmscriptenKeyboardElement
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Nov 23, 2024
1 parent 0542026 commit 68dc965
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/hello_imgui/app_window_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ enum class WindowPositionMode
};


enum class EmscriptenKeyboardElement
{
Window,
Document,
Screen,
Canvas,
Default
};


enum class WindowSizeMeasureMode
{
// ScreenCoords: measure window size in screen coords.
Expand Down Expand Up @@ -215,6 +225,18 @@ struct AppWindowParams
// If true, HelloImGui will handle the edgeInsets on iOS.
bool handleEdgeInsets = true;


// --------------- Emscripten ------------------
// `emscriptenKeyboardElement`: _EmscriptenKeyboardElement, default=Default_. HTML element in which SDL will capture the keyboard events.
// (For Emscripten only)
// Choose between: Window, Document, Screen, Canvas, Default.
// If Default:
// - the default SDL behavior is used, which is to capture the keyboard events for the window,
// if no previous hint was set in the javascript code.
// - under Pyodide, the default behavior is to capture the keyboard events for the canvas.
EmscriptenKeyboardElement emscriptenKeyboardElement = EmscriptenKeyboardElement::Default;


// ----------------- repaint the window during resize -----------------
// Very advanced and reserved for advanced C++ users.
// If you set this to true, the window will be repainted during resize.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ namespace HelloImGui { namespace BackendApi
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif

#ifdef __EMSCRIPTEN__
if (appWindowParams.emscriptenKeyboardElement == EmscriptenKeyboardElement::Window)
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#window");
else if (appWindowParams.emscriptenKeyboardElement == EmscriptenKeyboardElement::Document)
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#document");
else if (appWindowParams.emscriptenKeyboardElement == EmscriptenKeyboardElement::Screen)
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#screen");
else if (appWindowParams.emscriptenKeyboardElement == EmscriptenKeyboardElement::Canvas)
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#canvas");
else if (appWindowParams.emscriptenKeyboardElement == EmscriptenKeyboardElement::Default)
{
// If Default:
// - the default SDL behavior is used, which is to capture the keyboard events for the window,
// if no previous hint was set in the javascript code.

// - under Pyodide, the default behavior is to capture the keyboard events for the canvas.
#ifdef IMGUI_BUNDLE_BUILD_PYODIDE
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#canvas");
#endif
}
#endif // __EMSCRIPTEN__

// If the window is created with the SDL_WINDOW_ALLOW_HIGHDPI flag,
// its size in pixels may differ from its size in screen coordinates on platforms with high-DPI support
// (e.g. iOS and macOS).
Expand Down

0 comments on commit 68dc965

Please sign in to comment.