1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_V3_H_
#define UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_V3_H_
#include <text-input-unstable-v3-client-protocol.h>
#include <cstdint>
#include <optional>
#include <string>
#include "base/memory/raw_ptr.h"
#include "ui/base/ime/text_input_mode.h"
#include "ui/base/ime/text_input_type.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/range/range.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
namespace ui {
struct SpanStyle;
class WaylandConnection;
class ZwpTextInputV3Client {
public:
virtual ~ZwpTextInputV3Client() = default;
// Called when a new composing text (pre-edit) should be set around the
// current cursor position. Any previously set composing text should
// be removed.
// Note that the preedit_cursor is byte-offset. It is the pre-edit cursor
// position if the range is empty and selection otherwise.
virtual void OnPreeditString(std::string_view text,
const std::vector<SpanStyle>& spans,
const gfx::Range& preedit_cursor) = 0;
// Called when a complete input sequence has been entered. The text to
// commit could be either just a single character after a key press or the
// result of some composing (pre-edit).
virtual void OnCommitString(std::string_view text) = 0;
};
class ZwpTextInputV3 {
public:
virtual ~ZwpTextInputV3() = default;
virtual void SetClient(ZwpTextInputV3Client* context) = 0;
virtual void OnClientDestroyed(ZwpTextInputV3Client* context) = 0;
virtual void Enable() = 0;
virtual void Disable() = 0;
virtual void Reset() = 0;
virtual void SetCursorRect(const gfx::Rect& rect) = 0;
virtual void SetSurroundingText(const std::string& text,
const gfx::Range& preedit_range,
const gfx::Range& selection_range) = 0;
virtual void SetContentType(TextInputType type,
uint32_t flags,
bool should_do_learning) = 0;
};
// Represents a zwp_text_input_v3 object.
class ZwpTextInputV3Impl : public ZwpTextInputV3 {
public:
ZwpTextInputV3Impl(WaylandConnection* connection,
zwp_text_input_manager_v3* text_input_manager);
ZwpTextInputV3Impl(const ZwpTextInputV3Impl&) = delete;
ZwpTextInputV3Impl& operator=(const ZwpTextInputV3Impl&) = delete;
~ZwpTextInputV3Impl() override;
void SetClient(ZwpTextInputV3Client* context) override;
void OnClientDestroyed(ZwpTextInputV3Client* context) override;
void Enable() override;
void Disable() override;
void Reset() override;
void SetCursorRect(const gfx::Rect& rect) override;
void SetSurroundingText(const std::string& text,
const gfx::Range& preedit_range,
const gfx::Range& selection_range) override;
void SetContentType(TextInputType type,
uint32_t flags,
bool should_do_learning) override;
private:
struct ContentType {
constexpr ContentType() = default;
constexpr ContentType(uint32_t content_hint, uint32_t content_purpose)
: content_hint(content_hint), content_purpose(content_purpose) {}
bool operator==(const ContentType& other) const = default;
uint32_t content_hint = ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE;
uint32_t content_purpose = ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
};
struct SetSurroundingTextData {
constexpr SetSurroundingTextData() = default;
constexpr SetSurroundingTextData(std::string text,
int32_t cursor,
int32_t anchor)
: text(std::move(text)), cursor(cursor), anchor(anchor) {}
bool operator==(const SetSurroundingTextData&) const = default;
std::string text;
int32_t cursor = 0;
int32_t anchor = 0;
};
struct PreeditData {
constexpr PreeditData() = default;
constexpr PreeditData(std::string text,
int32_t cursor_begin,
int32_t cursor_end)
: text(std::move(text)),
cursor_begin(cursor_begin),
cursor_end(cursor_end) {}
std::string text;
int32_t cursor_begin = 0;
int32_t cursor_end = 0;
};
// Text input state received from IME, to be applied on done event.
struct InputEvents {
InputEvents();
~InputEvents();
std::optional<PreeditData> preedit;
std::optional<std::string> commit;
uint32_t last_done_serial = 0;
};
// Data sent to IME.
struct ImeData {
ImeData();
~ImeData();
void Reset();
std::unique_ptr<gfx::Rect> cursor_rect;
std::unique_ptr<ContentType> content_type;
std::unique_ptr<SetSurroundingTextData> surrounding_text;
// Only used when committed.
uint32_t commit_count = 0;
};
bool DoneSerialEqualsCommitCount();
bool SendCursorRect();
bool SendContentType();
bool SendSurroundingText();
void SendPendingImeData();
void ResetPendingImeData();
void ResetCommittedImeData();
void ResetInputEventsState();
void Commit();
// zwp_text_input_v3_listener
static void OnEnter(void* data,
struct zwp_text_input_v3* text_input,
struct wl_surface* surface);
static void OnLeave(void* data,
struct zwp_text_input_v3* text_input,
struct wl_surface* surface);
static void OnPreeditString(void* data,
struct zwp_text_input_v3* text_input,
const char* text,
int32_t cursor_begin,
int32_t cursor_end);
static void OnCommitString(void* data,
struct zwp_text_input_v3* text_input,
const char* text);
static void OnDeleteSurroundingText(void* data,
struct zwp_text_input_v3* text_input,
uint32_t before_length,
uint32_t after_length);
static void OnDone(void* data,
struct zwp_text_input_v3* text_input,
uint32_t serial);
const raw_ptr<WaylandConnection> connection_;
wl::Object<zwp_text_input_v3> obj_;
raw_ptr<ZwpTextInputV3Client> client_;
// Input events state that will be applied in done event.
InputEvents pending_input_events_;
// Pending data to be sent to IME.
ImeData pending_ime_data_;
// Data that was last sent to IME and committed.
ImeData committed_ime_data_;
};
} // namespace ui
#endif // UI_OZONE_PLATFORM_WAYLAND_HOST_ZWP_TEXT_INPUT_V3_H_
|