Skip to content

Commit

Permalink
Handle state status explicitly in TextInputV1
Browse files Browse the repository at this point in the history
  • Loading branch information
tarek-y-ismail committed Dec 16, 2024
1 parent 721404a commit fd58ab6
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/server/frontend_wayland/text_input_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include "wl_surface.h"
#include "mir/executor.h"
#include "mir/scene/text_input_hub.h"
#include "mir/wayland/client.h"

#define MIR_LOG_COMPONENT "TextInputV1"
#include "mir/log.h"

#include <memory>
#include <boost/throw_exception.hpp>
Expand Down Expand Up @@ -171,8 +173,13 @@ class TextInputV1
mw::Weak<mf::WlSurface> current_surface;
/// Set to true if and only if the text input has been enabled since the last commit
bool on_new_input_field{false};
/// nullopt if the state is inactive, otherwise holds the pending and/or committed state
std::optional<ms::TextInputState> pending_state;

enum class State
{
active,
inactive,
} state;
ms::TextInputState pending_state;

struct SerialPair
{
Expand Down Expand Up @@ -247,7 +254,8 @@ TextInputV1::TextInputV1(
std::shared_ptr<TextInputV1Ctx> const& ctx)
: mw::TextInputV1{resource, Version<1>()},
ctx{ctx},
handler{std::make_shared<Handler>(this, ctx->wayland_executor)}
handler{std::make_shared<Handler>(this, ctx->wayland_executor)},
state{State::inactive}
{
}

Expand All @@ -258,15 +266,15 @@ TextInputV1::~TextInputV1()
seat.value()->remove_focus_listener(client, this);
}

state = State::inactive;
on_new_input_field = false;
pending_state.reset();
ctx->text_input_hub->deactivate_handler(handler);
}

void TextInputV1::send_text_change(ms::TextInputChange const& change)
{
auto const client_serial = find_client_serial(change.serial);
if (!pending_state || !current_surface || !client_serial)
if (state == State::inactive || !current_surface || !client_serial)
{
// We are no longer enabled, or we don't have a valid serial
return;
Expand Down Expand Up @@ -360,15 +368,15 @@ void TextInputV1::activate(wl_resource *seat_resource, wl_resource *surface)
if (current_surface)
{
on_new_input_field = true;
pending_state.emplace();
state = State::active;
}
}

void TextInputV1::deactivate(wl_resource *seat)
{
(void)seat;
on_new_input_field = false;
pending_state.reset();
state = State::inactive;
}

/// Electron appears to call show_input_panel() when commit_state() should be called.
Expand All @@ -386,25 +394,30 @@ void TextInputV1::hide_input_panel()

void TextInputV1::reset()
{
pending_state.emplace();
if(state == State::inactive)
{
mir::log_warning("Attempted to reset state while inactive");
return;
}
pending_state = {};
}

void TextInputV1::set_surrounding_text(const std::string &text, uint32_t cursor, uint32_t anchor)
{
if (pending_state)
if (state == State::active)
{
pending_state->surrounding_text = text;
pending_state->cursor = cursor;
pending_state->anchor = anchor;
pending_state.surrounding_text = text;
pending_state.cursor = cursor;
pending_state.anchor = anchor;
}
}

void TextInputV1::set_content_type(uint32_t hint, uint32_t purpose)
{
if (pending_state)
if (state == State::active)
{
pending_state->content_hint.emplace(wayland_to_mir_content_hint(hint));
pending_state->content_purpose = wayland_to_mir_content_purpose(purpose);
pending_state.content_hint.emplace(wayland_to_mir_content_hint(hint));
pending_state.content_purpose = wayland_to_mir_content_purpose(purpose);
}
}

Expand All @@ -424,9 +437,9 @@ void TextInputV1::set_preferred_language(const std::string &language)

void TextInputV1::commit_state(uint32_t client_serial)
{
if (pending_state && current_surface)
if (state == State::active && current_surface)
{
auto const hub_serial = ctx->text_input_hub->set_handler_state(handler, on_new_input_field, *pending_state);
auto const hub_serial = ctx->text_input_hub->set_handler_state(handler, on_new_input_field, pending_state);
state_serials.push_back({client_serial, hub_serial});
while (state_serials.size() > max_remembered_serials)
{
Expand Down

0 comments on commit fd58ab6

Please sign in to comment.