|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2026 - kunitoki@gmail.com |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +namespace yup |
| 23 | +{ |
| 24 | + |
| 25 | +//============================================================================== |
| 26 | +/** |
| 27 | + An interface for components that need to accept text input. |
| 28 | +
|
| 29 | + Components that need to accept text input (such as TextEditor) should inherit |
| 30 | + from this class and implement the required methods. This enables features like |
| 31 | + on-screen keyboards on mobile devices and IME (Input Method Editor) support. |
| 32 | +
|
| 33 | + The text input system is automatically managed based on focus changes. When a |
| 34 | + component that implements TextInputTarget gains focus, text input will be started. |
| 35 | + When it loses focus, text input will be stopped (unless the newly focused component |
| 36 | + also implements TextInputTarget). |
| 37 | +
|
| 38 | + Example usage: |
| 39 | + @code |
| 40 | + class MyTextComponent : public Component, |
| 41 | + public TextInputTarget |
| 42 | + { |
| 43 | + public: |
| 44 | + MyTextComponent() |
| 45 | + { |
| 46 | + setWantsKeyboardFocus (true); |
| 47 | + } |
| 48 | +
|
| 49 | + void focusGained() override |
| 50 | + { |
| 51 | + Component::focusGained(); |
| 52 | + requestTextInput(); |
| 53 | + } |
| 54 | +
|
| 55 | + void focusLost() override |
| 56 | + { |
| 57 | + Component::focusLost(); |
| 58 | + relinquishTextInput(); |
| 59 | + } |
| 60 | +
|
| 61 | + Rectangle<float> getTextInputRect() const override |
| 62 | + { |
| 63 | + return getLocalBounds(); |
| 64 | + } |
| 65 | + }; |
| 66 | + @endcode |
| 67 | +
|
| 68 | + @see Component, TextEditor |
| 69 | +*/ |
| 70 | +class TextInputTarget |
| 71 | +{ |
| 72 | +public: |
| 73 | + /** Destructor. */ |
| 74 | + virtual ~TextInputTarget() = default; |
| 75 | + |
| 76 | + //============================================================================== |
| 77 | + /** |
| 78 | + Called to get the screen rectangle where text input is being edited. |
| 79 | +
|
| 80 | + This rectangle is used to position on-screen keyboards and IME windows |
| 81 | + to avoid obscuring the text being edited. |
| 82 | +
|
| 83 | + @return The rectangle in screen coordinates where text is being edited |
| 84 | + */ |
| 85 | + virtual Rectangle<float> getTextInputRect() const = 0; |
| 86 | + |
| 87 | + //============================================================================== |
| 88 | + /** |
| 89 | + Requests that the system starts accepting text input for this target. |
| 90 | +
|
| 91 | + Call this method when your component wants to receive text input events, |
| 92 | + typically in focusGained(). The system will show on-screen keyboards on |
| 93 | + mobile devices and enable IME where appropriate. |
| 94 | + */ |
| 95 | + void requestTextInput(); |
| 96 | + |
| 97 | + /** |
| 98 | + Relinquishes text input, telling the system this target no longer needs text input. |
| 99 | +
|
| 100 | + Call this method when your component no longer needs text input, |
| 101 | + typically in focusLost(). This will hide on-screen keyboards and |
| 102 | + disable IME. |
| 103 | + */ |
| 104 | + void relinquishTextInput(); |
| 105 | + |
| 106 | + /** |
| 107 | + Returns true if this target currently has active text input. |
| 108 | +
|
| 109 | + @return True if text input is currently active for this target |
| 110 | + */ |
| 111 | + bool isTextInputActive() const noexcept; |
| 112 | + |
| 113 | +private: |
| 114 | + bool textInputActive = false; |
| 115 | +}; |
| 116 | + |
| 117 | +} // namespace yup |
0 commit comments