223 lines
6.5 KiB
C++
223 lines
6.5 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
module;
|
||
|
|
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
module Nostro.Ui:Edit_impl;
|
||
|
|
import std;
|
||
|
|
import :Edit;
|
||
|
|
import :Text;
|
||
|
|
|
||
|
|
using namespace Nostro::Ui;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
// Overwrites a string's whole buffer, the part past its length too, where what was deleted from it may still be,
|
||
|
|
// and empties it. explicit_bzero is the call a compiler may not take out as a write nobody reads.
|
||
|
|
void Wipe(std::string& text) {
|
||
|
|
text.resize(text.capacity());
|
||
|
|
explicit_bzero(text.data(), text.size());
|
||
|
|
text.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
// What of `utf8` a field of `kind` takes.
|
||
|
|
std::string Taken(std::string_view utf8, FieldKind kind) {
|
||
|
|
std::string taken;
|
||
|
|
for(std::size_t i = 0; i < utf8.size();) {
|
||
|
|
const std::size_t at = i;
|
||
|
|
const std::uint32_t codepoint = DecodeUtf8(utf8, i);
|
||
|
|
const std::string_view bytes = utf8.substr(at, i - at);
|
||
|
|
const bool digit = codepoint >= '0' && codepoint <= '9';
|
||
|
|
if(kind == FieldKind::Decimal || kind == FieldKind::Digits) {
|
||
|
|
if(digit || (kind == FieldKind::Decimal && (codepoint == '.' || codepoint == ','))) {
|
||
|
|
taken += bytes;
|
||
|
|
}
|
||
|
|
} else if(codepoint == '\n' || codepoint == '\t') {
|
||
|
|
// A field is one line: a break in prose that was pasted is a space, and one around a key or an address
|
||
|
|
// (the newline a terminal's selection ends with) is nothing.
|
||
|
|
if(kind == FieldKind::Text) {
|
||
|
|
taken += ' ';
|
||
|
|
}
|
||
|
|
} else if(codepoint == ' ') {
|
||
|
|
// A key has no space in it; an IBAN typed in groups keeps its own.
|
||
|
|
if(kind != FieldKind::Secret) {
|
||
|
|
taken += ' ';
|
||
|
|
}
|
||
|
|
} else if(codepoint >= 0x20 && codepoint != 0x7F && !(codepoint == 0xFFFD && bytes != "\xEF\xBF\xBD")) {
|
||
|
|
// No control characters, and no malformed sequence, which decodes as U+FFFD without being its bytes: the
|
||
|
|
// bus carries valid UTF-8 alone.
|
||
|
|
taken += bytes;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return taken;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
EditState::EditState(FieldKind kind) : kind(kind) {
|
||
|
|
if(kind == FieldKind::Secret) {
|
||
|
|
value.reserve(secretLimit);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
EditState::~EditState() {
|
||
|
|
Clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool EditState::Empty() const noexcept {
|
||
|
|
return value.empty() && preedit.empty();
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string EditState::Shown() const {
|
||
|
|
std::string shown = value;
|
||
|
|
shown.insert(cursor, preedit);
|
||
|
|
return shown;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::size_t EditState::ShownCursor() const noexcept {
|
||
|
|
return cursor + preedit.size();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool EditState::HasSelection() const noexcept {
|
||
|
|
return cursor != anchor;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::size_t EditState::SelectionBegin() const noexcept {
|
||
|
|
return std::min(cursor, anchor);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::size_t EditState::SelectionEnd() const noexcept {
|
||
|
|
return std::max(cursor, anchor);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string_view EditState::Selected() const noexcept {
|
||
|
|
return std::string_view(value).substr(SelectionBegin(), SelectionEnd() - SelectionBegin());
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::Set(std::string_view text) {
|
||
|
|
Clear();
|
||
|
|
Insert(text);
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::Clear() {
|
||
|
|
if(kind == FieldKind::Secret) {
|
||
|
|
Wipe(value);
|
||
|
|
Wipe(preedit);
|
||
|
|
}
|
||
|
|
value.clear();
|
||
|
|
preedit.clear();
|
||
|
|
cursor = 0;
|
||
|
|
anchor = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::Commit(std::string_view utf8) {
|
||
|
|
preedit.clear();
|
||
|
|
Insert(utf8);
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::Paste(std::string_view utf8) {
|
||
|
|
CommitPreedit();
|
||
|
|
Insert(utf8);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool EditState::Apply(EditKey key, bool extend) {
|
||
|
|
if(key == EditKey::Backspace || key == EditKey::Delete) {
|
||
|
|
if(DeleteSelection()) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
const std::size_t from = key == EditKey::Backspace ? PreviousCodepoint(value, cursor) : cursor;
|
||
|
|
const std::size_t to = key == EditKey::Backspace ? cursor : NextCodepoint(value, cursor);
|
||
|
|
if(from == to) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
value.erase(from, to - from);
|
||
|
|
cursor = from;
|
||
|
|
anchor = from;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
// A move that does not stretch a selection ends it: Left and Right at the side they point to.
|
||
|
|
const bool collapse = HasSelection() && !extend;
|
||
|
|
std::size_t to = value.size();
|
||
|
|
if(key == EditKey::Left) {
|
||
|
|
to = collapse ? SelectionBegin() : PreviousCodepoint(value, cursor);
|
||
|
|
} else if(key == EditKey::Right) {
|
||
|
|
to = collapse ? SelectionEnd() : NextCodepoint(value, cursor);
|
||
|
|
} else if(key == EditKey::Home) {
|
||
|
|
to = 0;
|
||
|
|
}
|
||
|
|
cursor = to;
|
||
|
|
if(!extend) {
|
||
|
|
anchor = to;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::DeleteAround(std::size_t beforeBytes, std::size_t afterBytes) {
|
||
|
|
std::size_t from = cursor;
|
||
|
|
while(from > 0 && cursor - from < beforeBytes) {
|
||
|
|
from = PreviousCodepoint(value, from);
|
||
|
|
}
|
||
|
|
std::size_t to = cursor;
|
||
|
|
while(to < value.size() && to - cursor < afterBytes) {
|
||
|
|
to = NextCodepoint(value, to);
|
||
|
|
}
|
||
|
|
value.erase(from, to - from);
|
||
|
|
cursor = from;
|
||
|
|
anchor = from;
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::CommitPreedit() {
|
||
|
|
if(preedit.empty()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
std::string composing = std::exchange(preedit, std::string());
|
||
|
|
Insert(composing);
|
||
|
|
if(kind == FieldKind::Secret) {
|
||
|
|
Wipe(composing);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::MoveTo(std::size_t byte) {
|
||
|
|
std::size_t at = std::min(byte, value.size());
|
||
|
|
while(at > 0 && at < value.size() && (static_cast<std::uint8_t>(value[at]) & 0xC0) == 0x80) {
|
||
|
|
at--;
|
||
|
|
}
|
||
|
|
cursor = at;
|
||
|
|
anchor = at;
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::SelectAll() {
|
||
|
|
anchor = 0;
|
||
|
|
cursor = value.size();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool EditState::DeleteSelection() {
|
||
|
|
if(!HasSelection()) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const std::size_t from = SelectionBegin();
|
||
|
|
value.erase(from, SelectionEnd() - from);
|
||
|
|
cursor = from;
|
||
|
|
anchor = from;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void EditState::Insert(std::string_view utf8) {
|
||
|
|
DeleteSelection();
|
||
|
|
std::string taken = Taken(utf8, kind);
|
||
|
|
// No more than the field holds, cut between code points.
|
||
|
|
const std::size_t room = (kind == FieldKind::Secret ? secretLimit : limit) - std::min(value.size(), kind == FieldKind::Secret ? secretLimit : limit);
|
||
|
|
if(taken.size() > room) {
|
||
|
|
std::size_t cut = room;
|
||
|
|
while(cut > 0 && (static_cast<std::uint8_t>(taken[cut]) & 0xC0) == 0x80) {
|
||
|
|
cut--;
|
||
|
|
}
|
||
|
|
taken.resize(cut);
|
||
|
|
}
|
||
|
|
value.insert(cursor, taken);
|
||
|
|
cursor += taken.size();
|
||
|
|
anchor = cursor;
|
||
|
|
if(kind == FieldKind::Secret) {
|
||
|
|
Wipe(taken);
|
||
|
|
}
|
||
|
|
}
|