feat(wayland): SetTextInput takes text-input-v3's purposes and its version 1 hints, and enables anew for a field of another kind
Window::SetTextInput knew two purposes, Normal and Phone, and
Device::ApplyTextInput chose the content hints from them: none for
Phone, and completion, spellcheck and auto_capitalization for Normal.
Nothing could send another purpose, or hidden_text or sensitive_data:
a password field was Normal, with those three hints, or Phone.
Types: TextInputPurpose has the fourteen content purposes of the
bundled text-input-v3 protocol, in its order, so that the enumerators
have the protocol's values; Phone was 1 and is 4. kTextInputHintNone
and the ten kTextInputHint* bits are the content hints of version 1
of the protocol, the version Device binds the text input's manager
at, with the protocol's values, and kTextInputHintMask is the ten
together. TextInputDefaultHints(purpose) is what a field gets when
the caller names no hints: the three Normal had, hidden_text and
sensitive_data for Password and Pin, none for the other eleven. For
Normal and Phone without hints that is the content type they had
before.
Window: SetTextInput takes the hints as a last, defaulted
std::optional<std::uint32_t>, which tells kTextInputHintNone (no
hints) from no argument (the purpose's defaults). It keeps only the
bits of kTextInputHintMask: version 2's hints are not in version 1's
enum. On Win32 and in the browser the call still does nothing.
Device, ApplyTextInput: it sends the window's purpose and hints as
they are. For a window whose field is enabled it compares them with
the ones the enable in force went out with, which the window now
keeps, and when either differs it sends an enable before the content
type, where it sent the content type without one. The protocol, as
bundled in lib/text-input-unstable-v3-client-protocol.h, says of
set_content_type's values that "Subsequent attempts to update them
may have no effect" and that they "remain valid until the next
committed enable or disable request", and of enable that it "must be
issued every time the focused text input changes to a new one,
including within the current surface". The same text has an enable
reset "the state associated with preedit_string, commit_string, and
delete_surrounding_text events", so such an enable clears the pending
flags and the record of the composing text the window was last told,
as the disable path does (6f471ca), and fires no onPreedit. A call
with the purpose and hints of the enable in force sends no enable, as
before, also when it is for another field: the window does not know
one field from another of the same purpose and hints, and a caller
marks that move with SetTextInput(false) first.
Device, text_input_done: a listener of onTextInputDelete, onTextInput
or onPreedit may call SetTextInput, and the disable path's clearing
then ran in the middle of a done that was still reading the pending
flags and strings it clears: a disable from a listener of onPreedit
cleared the string the listeners' string_view points into, and one
from a listener of onTextInputDelete reset the flag the commit of the
same done waited on, so that commit never reached onTextInput. The
enable for a field of another kind clears the same values. A listener
may also dispatch the display, as Clipboard::GetText and Window's
constructor do, and the next done then ran inside the first, on
pending values the first had not yet reset. The handler now moves a
done out of the pending values as it arrives, onto a queue, and
delivers from there: the deletion, then the commit, then the
composing text. A done that arrives while another is being delivered
waits on the queue for it, so the window hears the dones in the order
they came. Only the dones wait: a key, pointer or touch event that
the same dispatch brings still fires where it arrives, ahead of a
done that waits, which the consumer notes now say. The commit goes
out whatever a listener of the deletion called SetTextInput with. The
composing text is reported only when nothing has overtaken it since
its done arrived: textInputEpoch, a counter in Device.cpp, moves at a
disable, at an enable for a field of another kind, at a leave and at
every done, and the handler compares the value a done arrived with
against the one after its commit's listeners. A listener may also
destroy the window: ~Window calls Device::ForgetTextInputDones, which
takes the window out of the queue's entries, the one being delivered
among them, and the handler fires nothing more of those. A look-up of
the address in Device::windows would take a window opened at that
address in the meantime for the one destroyed.
Tests: TextInputContent (new) makes the text input a proxy on a
wl_display over a socketpair, as TitleWayland makes its proxies,
reads the requests off the other end and calls the text input's
listener callbacks itself. It checks the purposes and the hint
constants against the bundled protocol header's values, the mask,
and the default hints of the fourteen purposes; then, on the wire:
nothing from a window the text input has not entered; an enable, the
content type, the caret and a commit at the enter; no enable for a
caret that moves; an enable with its own content type for the other
thirteen purposes in turn, after which a composing text equal to the
one before them reaches onPreedit again; named hints in place of the
defaults; an enable for other hints under the same purpose; a
version 2 hint (no_emoji) neither sent nor taken for a change;
kTextInputHintNone sent as 0; the caret in surface coordinates at a
scale of 2; one disable for a field losing the focus; no enable for
a call for another field of the same purpose and hints, and one when
a disable comes before it; and after a leave, nothing until the
enter, which enables the field the window has by then. For the done,
with two listeners on onTextInput and on onPreedit: a first listener
of onPreedit that changes the purpose, or disables the field, and the
listener after it are given the composing text as the input method
sent it; after a listener of onTextInputDelete that changes the
hints, or disables the field, the commit of the same done reaches
both listeners of onTextInput and that done fires no onPreedit; a
done fed from inside a listener of
onTextInputDelete is delivered after the one that listener was called
for, whose commit reaches the listeners of onTextInput before the fed
one's; a done fed from inside a listener of onTextInput is delivered
after that listener's done, whose own composing text is not reported;
a leave fed from inside a listener of onTextInput is followed by no
onPreedit for that done's composing text; and when a listener of
onTextInputDelete feeds a done, destroys the window and puts another
at its address, listed in Device::windows, neither onTextInput nor
onPreedit fires on the new one.
Docs: the consumer notes list the purposes and the hints, the default
hints, what a call that changes either does to a composition under
way, the SetTextInput(false) that marks a move to another field of
the same purpose and hints, and the order the input method's dones
are delivered in. AGENTS.template.md names TextInputContent among
the tests that need no GPU.
This commit is contained in:
parent
89111e0d65
commit
a9ce1a02e8
9 changed files with 707 additions and 24 deletions
|
|
@ -387,6 +387,35 @@ namespace {
|
|||
static TextInputStrings strings;
|
||||
return strings;
|
||||
}
|
||||
|
||||
// Moves whenever the composing text of a done that is yet to be
|
||||
// delivered stops being the current one: at a leave, a disable, an
|
||||
// enable for a field of another kind, and at every done.
|
||||
std::uint32_t textInputEpoch = 0;
|
||||
|
||||
// One done of the input method, taken out of the pending values when it
|
||||
// arrived.
|
||||
struct TextInputDone {
|
||||
Window* window; // the one the text input followed then; null once it is destroyed
|
||||
bool deletes;
|
||||
TextDelete deletion;
|
||||
std::string commit;
|
||||
std::string preedit;
|
||||
std::uint32_t epoch; // textInputEpoch as this done left it
|
||||
};
|
||||
|
||||
// The dones yet to be delivered, in the order they arrived: the one
|
||||
// whose listeners are running, then those a listener that dispatched the
|
||||
// display brought meanwhile. Never destroyed: ~Window reads it
|
||||
// (Device::ForgetTextInputDones), and a Window that is destroyed at exit
|
||||
// may be so after this deque's own destructor would have run.
|
||||
std::deque<TextInputDone>& TextDones() {
|
||||
static std::deque<TextInputDone>& dones = *new std::deque<TextInputDone>();
|
||||
return dones;
|
||||
}
|
||||
|
||||
// True while text_input_done is delivering.
|
||||
bool textInputDelivering = false;
|
||||
}
|
||||
|
||||
void Device::text_input_enter(void*, zwp_text_input_v3*, wl_surface* surface) {
|
||||
|
|
@ -404,6 +433,7 @@ void Device::text_input_leave(void*, zwp_text_input_v3*, wl_surface*) {
|
|||
textInputWindow = nullptr;
|
||||
textInputHasPreedit = textInputHasCommit = textInputHasDelete = false;
|
||||
TextStrings().preedit.clear();
|
||||
++textInputEpoch;
|
||||
if (window != nullptr) {
|
||||
window->textInputEnabled_ = false;
|
||||
if (!TextStrings().shown.empty()) window->onPreedit.Invoke(std::string_view());
|
||||
|
|
@ -431,33 +461,95 @@ void Device::text_input_delete(void*, zwp_text_input_v3*, std::uint32_t before,
|
|||
// text at the cursor, then the composing text after it. The pending values
|
||||
// reset with every done, so a round that set no preedit means an empty one;
|
||||
// the window hears about it only when that differs from what it shows.
|
||||
//
|
||||
// A listener may call SetTextInput, whose disable, or enable for a field of
|
||||
// another kind, clears the pending values (ApplyTextInput), and it may
|
||||
// dispatch the display, which can bring a leave or the next done. So a done
|
||||
// is taken out of the pending values as it arrives and delivered from that
|
||||
// copy, and one that arrives while another is being delivered waits its turn:
|
||||
// the window hears the dones whole and in the order the input method sent
|
||||
// them. Only the dones wait: a key, a pointer or touch event or a leave that
|
||||
// the same dispatch brings is handled where it arrives, ahead of a done that
|
||||
// waits, and putting those in order too would take a queue of every window
|
||||
// event. The deletion and the commit are one edit: both go out whatever a
|
||||
// listener does in between, short of destroying the window
|
||||
// (ForgetTextInputDones). The composing text is reported only while it is the
|
||||
// current one (textInputEpoch): after a leave, a disable or such an enable
|
||||
// its composition is over, and after a later done the window gets that one's.
|
||||
void Device::text_input_done(void*, zwp_text_input_v3*, std::uint32_t) {
|
||||
Window* window = textInputWindow;
|
||||
TextInputStrings& strings = TextStrings();
|
||||
if (!textInputHasPreedit) strings.preedit.clear();
|
||||
if (window != nullptr) {
|
||||
if (textInputHasDelete && (textInputDeleteBefore > 0 || textInputDeleteAfter > 0)) window->onTextInputDelete.Invoke({ textInputDeleteBefore, textInputDeleteAfter });
|
||||
if (textInputHasCommit && !strings.commit.empty()) window->onTextInput.Invoke(strings.commit);
|
||||
if (strings.preedit != strings.shown || (textInputHasCommit && !strings.commit.empty())) {
|
||||
strings.shown = strings.preedit;
|
||||
window->onPreedit.Invoke(strings.preedit);
|
||||
std::deque<TextInputDone>& dones = TextDones();
|
||||
dones.push_back({
|
||||
.window = textInputWindow,
|
||||
.deletes = textInputHasDelete && (textInputDeleteBefore > 0 || textInputDeleteAfter > 0),
|
||||
.deletion = { textInputDeleteBefore, textInputDeleteAfter },
|
||||
.commit = textInputHasCommit ? std::move(strings.commit) : std::string(),
|
||||
.preedit = textInputHasPreedit ? std::move(strings.preedit) : std::string(),
|
||||
.epoch = ++textInputEpoch,
|
||||
});
|
||||
textInputHasPreedit = textInputHasCommit = textInputHasDelete = false;
|
||||
strings.preedit.clear();
|
||||
strings.commit.clear();
|
||||
if (textInputDelivering) return;
|
||||
textInputDelivering = true;
|
||||
struct Delivering {
|
||||
~Delivering() { textInputDelivering = false; }
|
||||
} delivering;
|
||||
while (!dones.empty()) {
|
||||
// The done stays at the front while its listeners run, where
|
||||
// ForgetTextInputDones finds it, and goes when they are through,
|
||||
// also when one of them throws.
|
||||
struct Pop {
|
||||
~Pop() { TextDones().pop_front(); }
|
||||
} pop;
|
||||
const TextInputDone& done = dones.front();
|
||||
if (done.window != nullptr && done.deletes) done.window->onTextInputDelete.Invoke(done.deletion);
|
||||
if (done.window != nullptr && !done.commit.empty()) done.window->onTextInput.Invoke(done.commit);
|
||||
if (done.window != nullptr && done.epoch == textInputEpoch && (done.preedit != strings.shown || !done.commit.empty())) {
|
||||
strings.shown = done.preedit;
|
||||
done.window->onPreedit.Invoke(done.preedit);
|
||||
}
|
||||
}
|
||||
textInputHasPreedit = textInputHasCommit = textInputHasDelete = false;
|
||||
strings.commit.clear();
|
||||
}
|
||||
|
||||
// enable resets the input method's state, so it goes once per focus; a
|
||||
// caret that moved sends its rectangle alone.
|
||||
void Device::ForgetTextInputDones(Window* window) {
|
||||
for (TextInputDone& done : TextDones()) {
|
||||
if (done.window == window) done.window = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// enable resets the input method's state, so it goes only when the window's
|
||||
// field goes from none to one (at the enter, or after a disable or a leave)
|
||||
// and when the field that has the focus is of another purpose or other hints
|
||||
// than the one enabled. For the second the content type alone would not do:
|
||||
// it holds from the enable it was committed with, and "subsequent attempts
|
||||
// to update [it] may have no effect". Every other call sends the content
|
||||
// type and the caret's rectangle alone. The protocol asks for an enable
|
||||
// "every time the focused text input changes to a new one, including within
|
||||
// the current surface", and the window does not know one field from another
|
||||
// of the same purpose and hints: a move between two such looks like a caret
|
||||
// that moved, so the caller marks it with SetTextInput(false) first, whose
|
||||
// disable ends the composition (below).
|
||||
void Device::ApplyTextInput(Window* window) {
|
||||
if (textInput == nullptr || window == nullptr || textInputWindow != window) return;
|
||||
if (window->textInputActive_) {
|
||||
if (!window->textInputEnabled_) zwp_text_input_v3_enable(textInput);
|
||||
const bool another = window->textInputEnabled_
|
||||
&& (window->textInputEnabledPurpose_ != window->textInputPurpose_ || window->textInputEnabledHints_ != window->textInputHints_);
|
||||
if (!window->textInputEnabled_ || another) zwp_text_input_v3_enable(textInput);
|
||||
if (another) {
|
||||
// This enable ends the composition as a disable does (below):
|
||||
// what the window was last told goes with it.
|
||||
textInputHasPreedit = textInputHasCommit = textInputHasDelete = false;
|
||||
TextStrings().preedit.clear();
|
||||
TextStrings().shown.clear();
|
||||
++textInputEpoch;
|
||||
}
|
||||
window->textInputEnabled_ = true;
|
||||
const bool phone = window->textInputPurpose_ == TextInputPurpose::Phone;
|
||||
zwp_text_input_v3_set_content_type(textInput,
|
||||
phone ? ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE : (ZWP_TEXT_INPUT_V3_CONTENT_HINT_COMPLETION | ZWP_TEXT_INPUT_V3_CONTENT_HINT_SPELLCHECK | ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION),
|
||||
phone ? ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PHONE : ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL);
|
||||
window->textInputEnabledPurpose_ = window->textInputPurpose_;
|
||||
window->textInputEnabledHints_ = window->textInputHints_;
|
||||
// TextInputPurpose and the kTextInputHint* constants have the
|
||||
// protocol's values (TextInputContent holds them to it).
|
||||
zwp_text_input_v3_set_content_type(textInput, window->textInputHints_, static_cast<std::uint32_t>(window->textInputPurpose_));
|
||||
const float s = window->scale > 0.0f ? window->scale : 1.0f;
|
||||
zwp_text_input_v3_set_cursor_rectangle(textInput,
|
||||
static_cast<std::int32_t>(window->textInputRect_[0] / s), static_cast<std::int32_t>(window->textInputRect_[1] / s),
|
||||
|
|
@ -472,6 +564,7 @@ void Device::ApplyTextInput(Window* window) {
|
|||
textInputHasPreedit = textInputHasCommit = textInputHasDelete = false;
|
||||
TextStrings().preedit.clear();
|
||||
TextStrings().shown.clear();
|
||||
++textInputEpoch;
|
||||
}
|
||||
zwp_text_input_v3_commit(textInput);
|
||||
wl_display_flush(display);
|
||||
|
|
|
|||
|
|
@ -703,6 +703,7 @@ Window::~Window() {
|
|||
Device::ForgetTouches();
|
||||
}
|
||||
if (Device::textInputWindow == this) Device::textInputWindow = nullptr;
|
||||
Device::ForgetTextInputDones(this);
|
||||
#endif
|
||||
|
||||
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
|
||||
|
|
@ -932,7 +933,7 @@ void Window::SetAppId(const std::string_view id) {
|
|||
#endif
|
||||
}
|
||||
|
||||
void Window::SetTextInput(bool active, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height, TextInputPurpose purpose) {
|
||||
void Window::SetTextInput(bool active, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height, TextInputPurpose purpose, std::optional<std::uint32_t> hints) {
|
||||
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||
textInputActive_ = active;
|
||||
textInputRect_[0] = x;
|
||||
|
|
@ -940,6 +941,9 @@ void Window::SetTextInput(bool active, std::int32_t x, std::int32_t y, std::int3
|
|||
textInputRect_[2] = width;
|
||||
textInputRect_[3] = height;
|
||||
textInputPurpose_ = purpose;
|
||||
// Only version 1's hints: a later one's is not a value of the enum the
|
||||
// bound object knows.
|
||||
textInputHints_ = hints.value_or(TextInputDefaultHints(purpose)) & kTextInputHintMask;
|
||||
Device::ApplyTextInput(this);
|
||||
#endif
|
||||
}
|
||||
|
|
@ -2244,7 +2248,7 @@ void Window::SetTitle(const std::string_view title) {
|
|||
|
||||
void Window::SetAppId(const std::string_view) {}
|
||||
|
||||
void Window::SetTextInput(bool, std::int32_t, std::int32_t, std::int32_t, std::int32_t, TextInputPurpose) {}
|
||||
void Window::SetTextInput(bool, std::int32_t, std::int32_t, std::int32_t, std::int32_t, TextInputPurpose, std::optional<std::uint32_t>) {}
|
||||
|
||||
void Window::Resize(std::uint32_t newWidth, std::uint32_t newHeight) {
|
||||
if (newWidth == 0 || newHeight == 0) return;
|
||||
|
|
|
|||
Loading…
Reference in a new issue