907 lines
33 KiB
C++
907 lines
33 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
module Nostro.Ui:Context_impl;
|
||
|
|
import std;
|
||
|
|
import :Context;
|
||
|
|
import :Geometry;
|
||
|
|
import :Text;
|
||
|
|
import :Edit;
|
||
|
|
import :Gesture;
|
||
|
|
import :Draw;
|
||
|
|
import :Theme;
|
||
|
|
import :Input;
|
||
|
|
|
||
|
|
using namespace Nostro::Ui;
|
||
|
|
|
||
|
|
std::string Nostro::Ui::FieldText(const EditState& edit, bool masked) {
|
||
|
|
const std::string shown = edit.Shown();
|
||
|
|
if(!masked) {
|
||
|
|
return shown;
|
||
|
|
}
|
||
|
|
std::string dots;
|
||
|
|
for(std::size_t i = CountCodepoints(shown); i > 0; i--) {
|
||
|
|
dots += "•";
|
||
|
|
}
|
||
|
|
return dots;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Nostro::Ui::CaretFollowedGrowth(Box told, float toldHeight, Box caret, float height) noexcept {
|
||
|
|
const float grown = height - toldHeight;
|
||
|
|
if(grown <= 0) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const auto near = [](float a, float b) { return std::abs(a - b) <= 1.0f; };
|
||
|
|
return near(caret.x, told.x) && near(caret.w, told.w) && near(caret.h, told.h) && near(caret.y - told.y, grown);
|
||
|
|
}
|
||
|
|
|
||
|
|
Frame::Frame(Context& context, Box bounds, float scale, std::int64_t nowMs) : context(context), draw(context.Measure(), bounds), bounds(bounds), metrics{scale}, sizeClass(SizeClassFor(bounds.w, scale)), nowMs(nowMs) {
|
||
|
|
}
|
||
|
|
|
||
|
|
float Frame::Dp(float dp) const noexcept {
|
||
|
|
return metrics.Dp(dp);
|
||
|
|
}
|
||
|
|
|
||
|
|
const Theme& Frame::Colors() const noexcept {
|
||
|
|
return context.theme;
|
||
|
|
}
|
||
|
|
|
||
|
|
const TextMeasure& Frame::Measure() const noexcept {
|
||
|
|
return context.Measure();
|
||
|
|
}
|
||
|
|
|
||
|
|
WordWrapper& Frame::Wrapper() noexcept {
|
||
|
|
return context.Wrapper();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Frame::Add(Region region) {
|
||
|
|
// What a scrim covers is out of the keyboard's reach as it is out of a finger's: the shortcuts named so far are
|
||
|
|
// the covered screen's.
|
||
|
|
if(region.role == Role::Scrim) {
|
||
|
|
shortcuts.clear();
|
||
|
|
}
|
||
|
|
region.layer = draw.CurrentLayer();
|
||
|
|
region.hit = region.box.Intersect(scrolls.empty() ? bounds : scrolls.back().viewport);
|
||
|
|
if(!scrolls.empty()) {
|
||
|
|
region.scroll = scrolls.back().key;
|
||
|
|
region.scrollTop = scrolls.back().viewport.y;
|
||
|
|
}
|
||
|
|
regions.push_back(std::move(region));
|
||
|
|
}
|
||
|
|
|
||
|
|
void Frame::Shortcut(Key key, Modifiers modifiers, std::function<void()> action) {
|
||
|
|
shortcuts.push_back({key, modifiers, std::move(action)});
|
||
|
|
}
|
||
|
|
|
||
|
|
float Frame::BeginScroll(std::string_view key, Box viewport) {
|
||
|
|
Region area;
|
||
|
|
area.box = viewport;
|
||
|
|
area.key = std::string(key);
|
||
|
|
area.role = Role::Scroll;
|
||
|
|
Add(std::move(area));
|
||
|
|
scrolls.push_back({std::string(key), viewport.Intersect(bounds)});
|
||
|
|
draw.PushClip(viewport);
|
||
|
|
ScrollState& state = context.Scroll(key);
|
||
|
|
state.viewport = viewport.h;
|
||
|
|
// By whole pixels: a list that came to rest between two would draw every row's text and hairline across both.
|
||
|
|
return std::round(state.offset);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Frame::EndScroll(float contentHeight) {
|
||
|
|
ScrollState& state = context.Scroll(scrolls.back().key);
|
||
|
|
state.content = contentHeight;
|
||
|
|
const float before = state.offset;
|
||
|
|
state.Clamp();
|
||
|
|
if(state.offset != before) {
|
||
|
|
// Drawn at an offset the content no longer reaches: the next frame draws it where it belongs.
|
||
|
|
context.Redraw();
|
||
|
|
}
|
||
|
|
draw.PopClip();
|
||
|
|
scrolls.pop_back();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Frame::Refreshable(std::function<void()> refresh) {
|
||
|
|
std::ranges::find(regions, scrolls.back().key, &Region::key)->onRefresh = std::move(refresh);
|
||
|
|
}
|
||
|
|
|
||
|
|
float Frame::Pulled() const noexcept {
|
||
|
|
return context.scrollKey == scrolls.back().key ? context.pull : 0.0f;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Frame::Hovered(std::string_view key) const noexcept {
|
||
|
|
return context.hoverKey == key;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Frame::Pressed(std::string_view key) const noexcept {
|
||
|
|
return !context.pressTaken && context.pressedKey == key;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Frame::Focused(std::string_view key) const noexcept {
|
||
|
|
return context.focusKey == key;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Frame::FocusRing(std::string_view key) const noexcept {
|
||
|
|
return context.focusByKeyboard && context.focusKey == key;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Frame::CaretOn() const noexcept {
|
||
|
|
const std::int64_t since = nowMs - context.caretSince;
|
||
|
|
return since >= Context::caretIdleMs || (since / 500) % 2 == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
Context::Context(TextMeasure measure) : wrapper(std::move(measure)) {
|
||
|
|
}
|
||
|
|
|
||
|
|
const TextMeasure& Context::Measure() const noexcept {
|
||
|
|
return wrapper.Measure();
|
||
|
|
}
|
||
|
|
|
||
|
|
WordWrapper& Context::Wrapper() noexcept {
|
||
|
|
return wrapper;
|
||
|
|
}
|
||
|
|
|
||
|
|
float Context::Dp(float dp) const noexcept {
|
||
|
|
return std::round(dp * scale);
|
||
|
|
}
|
||
|
|
|
||
|
|
Frame& Context::Begin(Box bounds, float newScale, std::int64_t nowMs) {
|
||
|
|
scale = newScale > 0 ? newScale : 1.0f;
|
||
|
|
window = bounds;
|
||
|
|
now = nowMs;
|
||
|
|
frame.emplace(*this, bounds, scale, nowMs);
|
||
|
|
return *frame;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::End() {
|
||
|
|
Frame& ended = *frame;
|
||
|
|
// The notice first: a menu that is open at its place is over it.
|
||
|
|
if(!notice.empty()) {
|
||
|
|
DrawNotice(ended);
|
||
|
|
}
|
||
|
|
if(menu) {
|
||
|
|
DrawMenu(ended);
|
||
|
|
}
|
||
|
|
// From the bottom up: by layer, and within a layer in the order they were added.
|
||
|
|
std::ranges::stable_sort(ended.regions, {}, &Region::layer);
|
||
|
|
regions = std::exchange(ended.regions, {});
|
||
|
|
shortcuts = std::exchange(ended.shortcuts, {});
|
||
|
|
built = true;
|
||
|
|
const Region* const had = focusKey.empty() ? nullptr : Find(focusKey);
|
||
|
|
const Region* focused = FocusedRegion();
|
||
|
|
if(focused == nullptr) {
|
||
|
|
// What had the focus is not on this screen, cannot be used on it, or is under a sheet now: the focus is let go.
|
||
|
|
if(had != nullptr) {
|
||
|
|
// The word being composed in a field goes into it, as the field showed it: the input method is let go with
|
||
|
|
// the focus. And this frame still drew the focus where it was, so it is drawn again without.
|
||
|
|
if(had->edit != nullptr) {
|
||
|
|
had->edit->CommitPreedit();
|
||
|
|
}
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
focusKey.clear();
|
||
|
|
focusByKeyboard = false;
|
||
|
|
} else if(window.h != builtHeight && focused->edit != nullptr) {
|
||
|
|
// The window's height changed under a field, as when the on-screen keyboard comes: the field stays in view.
|
||
|
|
Reveal(*focused);
|
||
|
|
}
|
||
|
|
builtHeight = window.h;
|
||
|
|
UpdateTextInput(window.h);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Context::Built() const noexcept {
|
||
|
|
return built;
|
||
|
|
}
|
||
|
|
|
||
|
|
const Frame& Context::Shown() const {
|
||
|
|
return *frame;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::span<const Region> Context::Regions() const noexcept {
|
||
|
|
return regions;
|
||
|
|
}
|
||
|
|
|
||
|
|
const Region* Context::Find(std::string_view key) const noexcept {
|
||
|
|
const auto found = std::ranges::find(regions, key, &Region::key);
|
||
|
|
return found == regions.end() ? nullptr : &*found;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Stale() {
|
||
|
|
regions.clear();
|
||
|
|
shortcuts.clear();
|
||
|
|
built = false;
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Invalidate() {
|
||
|
|
Stale();
|
||
|
|
menu.reset();
|
||
|
|
// The screen changed under the press that is down: its release is no tap on what the next frame draws there.
|
||
|
|
gestures.Feed(PointerPhase::Cancel, Point(), now);
|
||
|
|
pressedKey.clear();
|
||
|
|
scrollKey.clear();
|
||
|
|
pressTaken = false;
|
||
|
|
pull = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Redraw() noexcept {
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
const Region* Context::HitTest(Point at) const noexcept {
|
||
|
|
for(const Region& region : std::views::reverse(regions)) {
|
||
|
|
if(region.hit.Contains(at)) {
|
||
|
|
return ®ion;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
// The scrolling area a drag or the wheel at a point moves: the topmost one there, unless a sheet or a bar lies over it.
|
||
|
|
const Region* Context::ScrollUnder(Point at) const noexcept {
|
||
|
|
for(const Region& region : std::views::reverse(regions)) {
|
||
|
|
if(!region.hit.Contains(at)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if(region.role == Role::Scroll) {
|
||
|
|
return ®ion;
|
||
|
|
}
|
||
|
|
if(region.role == Role::Scrim || region.role == Role::Panel) {
|
||
|
|
return nullptr;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Whether a sheet covers a region: a scrim above it. A menu's scrim, in the topmost layer, is no sheet's: a menu
|
||
|
|
// belongs to what it was opened on, which keeps the focus under it.
|
||
|
|
bool Context::UnderSheet(const Region& region) const noexcept {
|
||
|
|
const std::span<const Region> above = std::span<const Region>(regions).subspan(static_cast<std::size_t>(®ion - regions.data()) + 1);
|
||
|
|
return std::ranges::any_of(above, [](const Region& other) { return other.role == Role::Scrim && other.layer != Layer::Popup; });
|
||
|
|
}
|
||
|
|
|
||
|
|
// What the keyboard acts on. Not something a sheet covers: that is out of the keyboard's reach as it is out of a
|
||
|
|
// finger's, so a key meant for the sheet presses nothing under it, the button that brought the sheet least of all, and
|
||
|
|
// nothing is typed there.
|
||
|
|
const Region* Context::FocusedRegion() const noexcept {
|
||
|
|
if(focusKey.empty()) {
|
||
|
|
return nullptr;
|
||
|
|
}
|
||
|
|
const Region* region = Find(focusKey);
|
||
|
|
return region != nullptr && region->enabled && !UnderSheet(*region) ? region : nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Pointer(PointerPhase phase, Point at, std::int64_t tMs) {
|
||
|
|
eventTime = tMs;
|
||
|
|
if(phase == PointerPhase::Down) {
|
||
|
|
// The slop and the fling's least speed follow the window's scale, like every other distance.
|
||
|
|
gestures.slopPx = Dp(8);
|
||
|
|
gestures.flingMinPxPerS = Dp(60);
|
||
|
|
pressTaken = false;
|
||
|
|
const Region* target = HitTest(at);
|
||
|
|
pressedKey = target != nullptr ? target->key : std::string();
|
||
|
|
// Only a region with something to open has a long press. A press on anything else stays that control's tap at
|
||
|
|
// its release, however long it was held: a slow finger presses the button all the same.
|
||
|
|
gestures.longPress = target != nullptr && target->enabled && (target->onLongPress != nullptr || target->edit != nullptr);
|
||
|
|
const Region* area = ScrollUnder(at);
|
||
|
|
scrollKey = area != nullptr ? area->key : std::string();
|
||
|
|
if(area != nullptr) {
|
||
|
|
// A finger down stops the fling under it.
|
||
|
|
Scroll(area->key).velocity = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Handle(gestures.Feed(phase, at, tMs));
|
||
|
|
if(phase == PointerPhase::Up || phase == PointerPhase::Cancel) {
|
||
|
|
// A list pulled far enough down and let go is read again, if the frame still has it so.
|
||
|
|
const Region* const area = Find(scrollKey);
|
||
|
|
if(phase == PointerPhase::Up && pull >= Dp(Size::pull) && area != nullptr && area->onRefresh) {
|
||
|
|
const std::function<void()> refresh = area->onRefresh;
|
||
|
|
refresh();
|
||
|
|
}
|
||
|
|
pull = 0;
|
||
|
|
pressedKey.clear();
|
||
|
|
scrollKey.clear();
|
||
|
|
pressTaken = false;
|
||
|
|
}
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Handle(const GestureEvent& event) {
|
||
|
|
if(event.kind == GestureEvent::Kind::Tap || event.kind == GestureEvent::Kind::LongPress) {
|
||
|
|
// A tap or a long press acts on what the press went down on. When a frame built meanwhile moved something else
|
||
|
|
// under the finger, the list having been read again, that names another region, and the release acts on nothing.
|
||
|
|
const Region* target = HitTest(event.at);
|
||
|
|
if(target == nullptr || target->key != pressedKey || !target->enabled) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if(event.kind == GestureEvent::Kind::Tap) {
|
||
|
|
Activate(*target, event.at);
|
||
|
|
} else {
|
||
|
|
pressTaken = true;
|
||
|
|
Held(*target, event.at);
|
||
|
|
}
|
||
|
|
} else if(event.kind == GestureEvent::Kind::DragStart || event.kind == GestureEvent::Kind::Drag) {
|
||
|
|
pressTaken = true;
|
||
|
|
// What moves under a pointer that drags it is not what the pointer rests on.
|
||
|
|
hoverKey.clear();
|
||
|
|
if(!scrollKey.empty()) {
|
||
|
|
ScrollState& state = Scroll(scrollKey);
|
||
|
|
const Region* const area = Find(scrollKey);
|
||
|
|
// A list that can be read again gives way when it is dragged down at its top, by half the finger's way, and
|
||
|
|
// comes back up the same way before it scrolls again.
|
||
|
|
if(area != nullptr && area->onRefresh && state.offset <= 0 && (pull > 0 || event.delta.y > 0)) {
|
||
|
|
pull = std::max(0.0f, pull + event.delta.y / 2);
|
||
|
|
} else {
|
||
|
|
state.ScrollBy(-event.delta.y);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if(event.kind == GestureEvent::Kind::Fling && !scrollKey.empty()) {
|
||
|
|
Scroll(scrollKey).Fling(-event.velocity.y);
|
||
|
|
// The fling moves on from the release's own time.
|
||
|
|
lastTick = eventTime;
|
||
|
|
wants.animating = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Activate(const Region& region, Point at) {
|
||
|
|
if(region.role == Role::Field) {
|
||
|
|
TapField(region, at);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if(region.role == Role::Scroll || region.role == Role::Panel) {
|
||
|
|
// Bare ground: the field that had the focus lets go of it, and the on-screen keyboard goes.
|
||
|
|
MoveFocus({}, false);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// A press with the pointer shows no focus ring; a field keeps the focus it has, and its keyboard.
|
||
|
|
focusByKeyboard = false;
|
||
|
|
if(region.onTap) {
|
||
|
|
// A copy: the action may change the screen, and the regions go with it.
|
||
|
|
const std::function<void()> action = region.onTap;
|
||
|
|
pointed = at;
|
||
|
|
EndComposition();
|
||
|
|
action();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Held(const Region& region, Point at) {
|
||
|
|
if(region.edit != nullptr) {
|
||
|
|
FieldMenu(region, at);
|
||
|
|
} else if(region.onLongPress) {
|
||
|
|
const std::function<void()> action = region.onLongPress;
|
||
|
|
pointed = at;
|
||
|
|
EndComposition();
|
||
|
|
action();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// The word the input method is composing goes into its field, as the field shows it, and the input method starts
|
||
|
|
// afresh. What runs next may read the field, which is then all of what it shows, and may change the screen, where the
|
||
|
|
// word would be left behind: shown, and gone at the next key.
|
||
|
|
void Context::EndComposition() {
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr && !field->edit->preedit.empty()) {
|
||
|
|
field->edit->CommitPreedit();
|
||
|
|
inputSerial++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// A tap places the cursor. Inside the composing text it stays where it is, that span being the input method's;
|
||
|
|
// outside, the word is taken into the field where it was typed and the input method starts afresh, so the half-typed
|
||
|
|
// word does not move to the tap.
|
||
|
|
void Context::TapField(const Region& region, Point at) {
|
||
|
|
EditState& edit = *region.edit;
|
||
|
|
const bool had = focusKey == region.key;
|
||
|
|
// Where in the shown text the tap lands, by the text the field drew.
|
||
|
|
const std::string drawn = FieldText(edit, region.field.masked);
|
||
|
|
const std::size_t first = std::min(region.field.first, drawn.size());
|
||
|
|
const std::size_t drawnByte = first + NearestByte(Measure(), std::string_view(drawn).substr(first), region.field.face, region.field.px, at.x - region.field.textX);
|
||
|
|
std::size_t shownByte = drawnByte;
|
||
|
|
if(region.field.masked) {
|
||
|
|
// A dot for each code point: the dots before the tap, counted off in the text itself.
|
||
|
|
const std::string shown = edit.Shown();
|
||
|
|
shownByte = 0;
|
||
|
|
for(std::size_t dots = CountCodepoints(std::string_view(drawn).substr(0, drawnByte)); dots > 0; dots--) {
|
||
|
|
shownByte = NextCodepoint(shown, shownByte);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(!had) {
|
||
|
|
MoveFocus(region.key, false);
|
||
|
|
}
|
||
|
|
if(!edit.preedit.empty()) {
|
||
|
|
if(shownByte > edit.cursor && shownByte < edit.cursor + edit.preedit.size()) {
|
||
|
|
ShowCaret();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
edit.CommitPreedit();
|
||
|
|
inputSerial++;
|
||
|
|
}
|
||
|
|
edit.MoveTo(shownByte);
|
||
|
|
if(had && keyboardGone) {
|
||
|
|
// The field kept the focus while its keyboard went: a tap on it asks for the keyboard again.
|
||
|
|
inputSerial++;
|
||
|
|
}
|
||
|
|
ShowCaret();
|
||
|
|
}
|
||
|
|
|
||
|
|
// What a held press, or the secondary button, offers on a field: the clipboard, since an on-screen keyboard has no
|
||
|
|
// key for it.
|
||
|
|
void Context::FieldMenu(const Region& region, Point at) {
|
||
|
|
MoveFocus(region.key, false);
|
||
|
|
const EditState& edit = *region.edit;
|
||
|
|
std::vector<MenuItem> items;
|
||
|
|
items.push_back({"Paste", [this] { wants.paste = true; }});
|
||
|
|
if(!edit.Empty() && edit.kind != FieldKind::Secret) {
|
||
|
|
// By the field's name, not its address: it may be gone by the time the choice is made.
|
||
|
|
items.push_back({"Copy", [this, key = region.key] {
|
||
|
|
if(const Region* field = Find(key); field != nullptr && field->edit != nullptr) {
|
||
|
|
Copy(std::string(field->edit->HasSelection() ? field->edit->Selected() : std::string_view(field->edit->value)));
|
||
|
|
}
|
||
|
|
}});
|
||
|
|
}
|
||
|
|
if(!edit.Empty()) {
|
||
|
|
items.push_back({"Select all", [this, key = region.key] {
|
||
|
|
if(const Region* field = Find(key); field != nullptr && field->edit != nullptr) {
|
||
|
|
field->edit->CommitPreedit();
|
||
|
|
field->edit->SelectAll();
|
||
|
|
}
|
||
|
|
}});
|
||
|
|
}
|
||
|
|
OpenMenu(at, std::move(items));
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::MoveFocus(std::string_view key, bool byKeyboard) {
|
||
|
|
if(focusKey != key) {
|
||
|
|
// The word the input method is composing goes into the field that loses the focus, as that field showed it.
|
||
|
|
if(const Region* old = FocusedRegion(); old != nullptr && old->edit != nullptr) {
|
||
|
|
old->edit->CommitPreedit();
|
||
|
|
}
|
||
|
|
focusKey = std::string(key);
|
||
|
|
inputSerial++;
|
||
|
|
}
|
||
|
|
focusByKeyboard = byKeyboard;
|
||
|
|
ShowCaret();
|
||
|
|
if(const Region* region = FocusedRegion()) {
|
||
|
|
Reveal(*region);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::StepFocus(std::int32_t by) {
|
||
|
|
// What Tab reaches: every region that takes the focus, or only a sheet's own while one is open.
|
||
|
|
std::vector<const Region*> order;
|
||
|
|
for(const Region& region : regions) {
|
||
|
|
if(region.role == Role::Scrim) {
|
||
|
|
order.clear();
|
||
|
|
} else if(region.focusable && region.enabled) {
|
||
|
|
order.push_back(®ion);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(order.empty()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const auto at = std::ranges::find(order, focusKey, [](const Region* region) -> const std::string& { return region->key; });
|
||
|
|
std::size_t next = by > 0 ? 0 : order.size() - 1;
|
||
|
|
if(at != order.end()) {
|
||
|
|
const std::size_t index = static_cast<std::size_t>(at - order.begin());
|
||
|
|
next = by > 0 ? (index + 1) % order.size() : (index + order.size() - 1) % order.size();
|
||
|
|
}
|
||
|
|
MoveFocus(order[next]->key, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Reveal(const Region& region) {
|
||
|
|
if(region.scroll.empty()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
ScrollState& state = Scroll(region.scroll);
|
||
|
|
const float top = region.box.y - region.scrollTop + state.offset;
|
||
|
|
if(state.EnsureVisible(top, top + region.box.h, Dp(16))) {
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::ShowCaret() noexcept {
|
||
|
|
caretSince = now;
|
||
|
|
caretPhase = 0;
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Edited(const Region& field) {
|
||
|
|
ShowCaret();
|
||
|
|
Reveal(field);
|
||
|
|
if(field.onChange) {
|
||
|
|
const std::function<void()> changed = field.onChange;
|
||
|
|
changed();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Context::EditKeyDown(const Region& field, Key key, Modifiers modifiers) {
|
||
|
|
EditState& edit = *field.edit;
|
||
|
|
if(modifiers.control) {
|
||
|
|
if(key == Key::A) {
|
||
|
|
edit.CommitPreedit();
|
||
|
|
edit.SelectAll();
|
||
|
|
ShowCaret();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
if(key == Key::V) {
|
||
|
|
wants.paste = true;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
if(key == Key::C || key == Key::X) {
|
||
|
|
// A secret is never copied.
|
||
|
|
if(edit.kind != FieldKind::Secret && edit.HasSelection()) {
|
||
|
|
wants.copy = std::string(edit.Selected());
|
||
|
|
if(key == Key::X) {
|
||
|
|
edit.DeleteSelection();
|
||
|
|
Edited(field);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
static constexpr std::array<std::pair<Key, EditKey>, 6> keys = {{
|
||
|
|
{Key::Backspace, EditKey::Backspace}, {Key::Delete, EditKey::Delete}, {Key::Left, EditKey::Left},
|
||
|
|
{Key::Right, EditKey::Right}, {Key::Home, EditKey::Home}, {Key::End, EditKey::End},
|
||
|
|
}};
|
||
|
|
const auto found = std::ranges::find(keys, key, &std::pair<Key, EditKey>::first);
|
||
|
|
if(found == keys.end()) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const bool deletes = found->second == EditKey::Backspace || found->second == EditKey::Delete;
|
||
|
|
if(edit.Apply(found->second, modifiers.shift) && deletes) {
|
||
|
|
Edited(field);
|
||
|
|
} else {
|
||
|
|
ShowCaret();
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Hover(std::optional<Point> at) {
|
||
|
|
const Region* over = at ? HitTest(*at) : nullptr;
|
||
|
|
// Only what a press acts on shows the pointer over it.
|
||
|
|
const bool acts = over != nullptr && over->enabled && (over->role == Role::Button || over->role == Role::Row || over->role == Role::Field);
|
||
|
|
const std::string key = acts ? over->key : std::string();
|
||
|
|
if(key != hoverKey) {
|
||
|
|
hoverKey = key;
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Secondary(Point at) {
|
||
|
|
const Region* target = HitTest(at);
|
||
|
|
if(target != nullptr && target->enabled) {
|
||
|
|
Held(*target, at);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Wheel(Point at, float detents) {
|
||
|
|
if(const Region* area = ScrollUnder(at)) {
|
||
|
|
Scroll(area->key).ScrollBy(detents * Dp(48));
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::KeyDown(Key key, Modifiers modifiers, bool repeat) {
|
||
|
|
if(menu) {
|
||
|
|
// A menu takes the keyboard: Escape sends it away.
|
||
|
|
if(key == Key::Escape && !repeat) {
|
||
|
|
CloseMenu();
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const Region* focused = FocusedRegion();
|
||
|
|
if(focused != nullptr && focused->onKey) {
|
||
|
|
const std::function<bool(Key, Modifiers)> handler = focused->onKey;
|
||
|
|
if(handler(key, modifiers)) {
|
||
|
|
wants.redraw = true;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
focused = FocusedRegion();
|
||
|
|
}
|
||
|
|
if(focused != nullptr && focused->edit != nullptr && EditKeyDown(*focused, key, modifiers)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if(key == Key::Tab || key == Key::Down || key == Key::Up) {
|
||
|
|
StepFocus(key == Key::Up || (key == Key::Tab && modifiers.shift) ? -1 : 1);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if(key == Key::PageDown || key == Key::PageUp) {
|
||
|
|
// The topmost scrolling area turns a page.
|
||
|
|
const auto area = std::ranges::find(std::views::reverse(regions), Role::Scroll, &Region::role);
|
||
|
|
if(area != std::views::reverse(regions).end()) {
|
||
|
|
Scroll(area->key).ScrollBy((key == Key::PageDown ? 0.9f : -0.9f) * area->box.h);
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// What follows acts once for a press: a key held down is not a row of presses of a button, and its repeats would
|
||
|
|
// land on the screen the first one opened.
|
||
|
|
if(repeat) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if(focused != nullptr && key == Key::Enter && focused->edit != nullptr && focused->onSubmit) {
|
||
|
|
const std::function<void()> submit = focused->onSubmit;
|
||
|
|
EndComposition();
|
||
|
|
submit();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if(focused != nullptr && (key == Key::Enter || key == Key::Space) && focused->edit == nullptr && focused->onTap) {
|
||
|
|
const std::function<void()> action = focused->onTap;
|
||
|
|
pointed = focused->hit.Center();
|
||
|
|
action();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const auto shortcut = std::ranges::find_if(std::views::reverse(shortcuts), [key, modifiers](const Frame::KeyAction& candidate) { return candidate.key == key && candidate.modifiers == modifiers; });
|
||
|
|
if(shortcut != std::views::reverse(shortcuts).end()) {
|
||
|
|
const std::function<void()> action = shortcut->action;
|
||
|
|
EndComposition();
|
||
|
|
action();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Text(std::string_view utf8) {
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr) {
|
||
|
|
field->edit->Commit(utf8);
|
||
|
|
Edited(*field);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Preedit(std::string_view utf8) {
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr) {
|
||
|
|
field->edit->preedit = std::string(utf8);
|
||
|
|
ShowCaret();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::DeleteAround(std::size_t beforeBytes, std::size_t afterBytes) {
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr) {
|
||
|
|
field->edit->DeleteAround(beforeBytes, afterBytes);
|
||
|
|
Edited(*field);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Paste(std::string_view utf8) {
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr) {
|
||
|
|
field->edit->Paste(utf8);
|
||
|
|
Edited(*field);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Tick(std::int64_t nowMs) {
|
||
|
|
// A step of at most a twentieth of a second: after a stall a flung list moves on, it does not jump.
|
||
|
|
const float dt = static_cast<float>(std::clamp<std::int64_t>(nowMs - lastTick, 0, 50)) / 1000.0f;
|
||
|
|
lastTick = nowMs;
|
||
|
|
now = nowMs;
|
||
|
|
if(const std::optional<std::int64_t> due = gestures.LongPressDue(); due && nowMs >= *due) {
|
||
|
|
Handle(gestures.Timeout(nowMs));
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
bool moving = false;
|
||
|
|
for(auto& [key, state] : scrolls) {
|
||
|
|
if(state.velocity != 0) {
|
||
|
|
moving = state.Tick(dt) || moving;
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
wants.animating = moving;
|
||
|
|
if(!notice.empty() && nowMs >= noticeUntil) {
|
||
|
|
notice.clear();
|
||
|
|
Stale();
|
||
|
|
}
|
||
|
|
// What was put off until now, the earliest first. Taken out before it runs: it may put itself off again.
|
||
|
|
std::vector<Later> due;
|
||
|
|
for(auto later = laters.begin(); later != laters.end();) {
|
||
|
|
if(nowMs >= later->second.atMs) {
|
||
|
|
due.push_back(std::move(later->second));
|
||
|
|
later = laters.erase(later);
|
||
|
|
} else {
|
||
|
|
later++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::ranges::stable_sort(due, {}, &Later::atMs);
|
||
|
|
for(const Later& later : due) {
|
||
|
|
later.action();
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr) {
|
||
|
|
const std::int64_t phase = std::min(nowMs - caretSince, caretIdleMs) / 500;
|
||
|
|
if(phase != caretPhase) {
|
||
|
|
caretPhase = phase;
|
||
|
|
wants.redraw = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Context::Tap(std::string_view key) {
|
||
|
|
const Region* region = Find(key);
|
||
|
|
// Only what a finger could reach: not a region that is disabled, scrolled out of sight, or under a sheet.
|
||
|
|
if(region == nullptr || !region->enabled || region->hit.Empty() || HitTest(region->hit.Center()) != region) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
// A field's cursor goes to the end of what it shows.
|
||
|
|
Activate(*region, region->edit != nullptr ? Point{region->hit.Right(), region->hit.Center().y} : region->hit.Center());
|
||
|
|
wants.redraw = true;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Context::LongPress(std::string_view key) {
|
||
|
|
const Region* region = Find(key);
|
||
|
|
if(region == nullptr || !region->enabled || region->hit.Empty() || HitTest(region->hit.Center()) != region) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
Held(*region, region->hit.Center());
|
||
|
|
wants.redraw = true;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Focus(std::string_view key) {
|
||
|
|
MoveFocus(key, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string_view Context::Focused() const noexcept {
|
||
|
|
return focusKey;
|
||
|
|
}
|
||
|
|
|
||
|
|
Point Context::Pointed() const noexcept {
|
||
|
|
return pointed;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::OpenMenu(Point at, std::vector<MenuItem> items) {
|
||
|
|
menu = Menu{at, std::move(items)};
|
||
|
|
// The regions under the menu are not to be reached from here on: a frame with the menu in it comes first.
|
||
|
|
Stale();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::CloseMenu() {
|
||
|
|
if(menu) {
|
||
|
|
menu.reset();
|
||
|
|
Stale();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Context::MenuOpen() const noexcept {
|
||
|
|
return menu.has_value();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Notice(std::string text, std::int64_t forMs) {
|
||
|
|
notice = std::move(text);
|
||
|
|
noticeUntil = now + forMs;
|
||
|
|
Stale();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Copy(std::string text) {
|
||
|
|
wants.copy = std::move(text);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::RestartInput() noexcept {
|
||
|
|
inputSerial++;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::int64_t Context::Now() const noexcept {
|
||
|
|
return now;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::At(std::string_view name, std::int64_t atMs, std::function<void()> action) {
|
||
|
|
laters.insert_or_assign(std::string(name), Later{atMs, std::move(action)});
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::Forget(std::string_view name) {
|
||
|
|
if(const auto found = laters.find(name); found != laters.end()) {
|
||
|
|
laters.erase(found);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
ScrollState& Context::Scroll(std::string_view key) {
|
||
|
|
const auto found = scrolls.find(key);
|
||
|
|
return found != scrolls.end() ? found->second : scrolls.emplace(std::string(key), ScrollState()).first->second;
|
||
|
|
}
|
||
|
|
|
||
|
|
Context::Wants Context::Take() {
|
||
|
|
Wants taken = wants;
|
||
|
|
taken.textInput = told;
|
||
|
|
std::optional<std::int64_t> wake = gestures.LongPressDue();
|
||
|
|
const auto sooner = [&wake](std::int64_t at) { wake = wake ? std::min(*wake, at) : at; };
|
||
|
|
if(!notice.empty()) {
|
||
|
|
sooner(noticeUntil);
|
||
|
|
}
|
||
|
|
for(const auto& [name, later] : laters) {
|
||
|
|
sooner(later.atMs);
|
||
|
|
}
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr && caretPhase * 500 < caretIdleMs) {
|
||
|
|
sooner(caretSince + (caretPhase + 1) * 500);
|
||
|
|
}
|
||
|
|
taken.wakeAtMs = wake;
|
||
|
|
wants.redraw = false;
|
||
|
|
wants.copy.reset();
|
||
|
|
wants.paste = false;
|
||
|
|
return taken;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Tells the window which field has the focus and where its caret is, when that changed. A window taller than at the
|
||
|
|
// last telling has lost its on-screen keyboard (the keyboard's own hide key: the compositor gives the room back), and
|
||
|
|
// a caret that only followed that growth is not told: the telling would bring the keyboard straight back. A tap on
|
||
|
|
// the field asks for it (TapField).
|
||
|
|
void Context::UpdateTextInput(float windowHeight) {
|
||
|
|
TextInputRequest want;
|
||
|
|
if(const Region* field = FocusedRegion(); field != nullptr && field->edit != nullptr) {
|
||
|
|
want = {true, field->field.caret, field->field.purpose, field->field.hints, inputSerial};
|
||
|
|
}
|
||
|
|
if(want.active && told.active && want.field == told.field && want.purpose == told.purpose && want.hints == told.hints) {
|
||
|
|
if(windowHeight < toldHeight) {
|
||
|
|
// The keyboard is up, or up again.
|
||
|
|
toldHeight = windowHeight;
|
||
|
|
}
|
||
|
|
keyboardGone = windowHeight > toldHeight;
|
||
|
|
if(want.caret == told.caret || CaretFollowedGrowth(told.caret, toldHeight, want.caret, windowHeight)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
told = want;
|
||
|
|
toldHeight = windowHeight;
|
||
|
|
keyboardGone = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::DrawMenu(Frame& to) {
|
||
|
|
to.draw.SetLayer(Layer::Popup);
|
||
|
|
// A scrim that shows nothing: a tap beside the menu sends it away, and reaches nothing else.
|
||
|
|
Region scrim;
|
||
|
|
scrim.box = to.bounds;
|
||
|
|
scrim.key = "menu.scrim";
|
||
|
|
scrim.role = Role::Scrim;
|
||
|
|
scrim.onTap = [this] { CloseMenu(); };
|
||
|
|
to.Add(std::move(scrim));
|
||
|
|
|
||
|
|
const float px = to.Dp(TextSize::body);
|
||
|
|
const float rowHeight = to.Dp(Size::touch);
|
||
|
|
const float pad = to.Dp(Size::unit * 2);
|
||
|
|
const float margin = to.Dp(Size::unit * 2);
|
||
|
|
float width = to.Dp(160);
|
||
|
|
for(const MenuItem& item : menu->items) {
|
||
|
|
width = std::max(width, TextWidth(Measure(), item.label, FontFace::Regular, px) + to.Dp(Size::gutter) * 2);
|
||
|
|
}
|
||
|
|
const float height = rowHeight * static_cast<float>(menu->items.size()) + pad * 2;
|
||
|
|
// Beside the point it was asked for at, and inside the window.
|
||
|
|
const Box panel{
|
||
|
|
std::max(to.bounds.x + margin, std::min(menu->at.x, to.bounds.Right() - margin - width)),
|
||
|
|
std::max(to.bounds.y + margin, std::min(menu->at.y, to.bounds.Bottom() - margin - height)),
|
||
|
|
width, height,
|
||
|
|
};
|
||
|
|
to.draw.Rect(RectItem{.box = panel, .color = theme.surface, .corners = Corners::All(to.Dp(Size::radius)), .outline = to.Dp(1), .outlineColor = theme.divider});
|
||
|
|
Region ground;
|
||
|
|
ground.box = panel;
|
||
|
|
ground.key = "menu.panel";
|
||
|
|
ground.role = Role::Panel;
|
||
|
|
to.Add(std::move(ground));
|
||
|
|
for(std::size_t i = 0; i < menu->items.size(); i++) {
|
||
|
|
const MenuItem& item = menu->items[i];
|
||
|
|
const Box row{panel.x, panel.y + pad + rowHeight * static_cast<float>(i), panel.w, rowHeight};
|
||
|
|
const std::string key = std::format("menu.{}", i);
|
||
|
|
if(to.Pressed(key) || to.Hovered(key)) {
|
||
|
|
to.draw.Rect(row, to.Pressed(key) ? theme.Pressed(theme.surface) : theme.Hovered(theme.surface));
|
||
|
|
}
|
||
|
|
to.draw.Text(item.label, row.x + to.Dp(Size::gutter), CenteredBaseline(Measure(), row, FontFace::Regular, px), FontFace::Regular, px, item.danger ? theme.danger : theme.text);
|
||
|
|
Region choice;
|
||
|
|
choice.box = row;
|
||
|
|
choice.key = key;
|
||
|
|
choice.label = item.label;
|
||
|
|
choice.onTap = [this, i] {
|
||
|
|
// The menu goes before its choice acts, since the action may open another; the frame it was drawn in goes
|
||
|
|
// after, since the action may be about something in it.
|
||
|
|
const std::function<void()> action = menu->items[i].action;
|
||
|
|
menu.reset();
|
||
|
|
action();
|
||
|
|
Stale();
|
||
|
|
};
|
||
|
|
to.Add(std::move(choice));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Context::DrawNotice(Frame& to) {
|
||
|
|
to.draw.SetLayer(Layer::Popup);
|
||
|
|
const float px = to.Dp(TextSize::label);
|
||
|
|
const float width = std::min(to.bounds.w - to.Dp(Size::gutter) * 2, TextWidth(Measure(), notice, FontFace::Regular, px) + to.Dp(Size::gutter) * 2);
|
||
|
|
const float height = to.Dp(40);
|
||
|
|
// Clear of a bar along the window's bottom.
|
||
|
|
const Box pill{to.bounds.x + (to.bounds.w - width) / 2, to.bounds.Bottom() - to.Dp(Size::bar) - to.Dp(Size::gutter) - height, width, height};
|
||
|
|
to.draw.Rect(pill, theme.text, height / 2);
|
||
|
|
to.draw.Text(Ellipsize(Measure(), notice, FontFace::Regular, px, width - to.Dp(Size::gutter) * 2), pill.Center().x, CenteredBaseline(Measure(), pill, FontFace::Regular, px), FontFace::Regular, px, theme.background, TextAlign::Center);
|
||
|
|
}
|