wallet: fork editbox into chatedit for multiline editing

This commit is contained in:
darkfi
2024-12-21 09:52:19 +01:00
parent affb11184d
commit 5a59617586
6 changed files with 1673 additions and 99 deletions

View File

@@ -471,6 +471,7 @@ pub enum Pimpl {
VectorArt(ui::VectorArtPtr),
Text(ui::TextPtr),
EditBox(ui::EditBoxPtr),
ChatEdit(ui::ChatEditPtr),
ChatView(ui::ChatViewPtr),
Image(ui::ImagePtr),
Button(ui::ButtonPtr),

File diff suppressed because it is too large Load Diff

View File

@@ -25,8 +25,8 @@ use crate::{
util::{enumerate_ref, is_whitespace},
};
pub(super) type TextPos = usize;
pub(super) type TextIdx = usize;
pub type TextPos = usize;
pub type TextIdx = usize;
/// Android composing text from autosuggest.
/// We need this because IMEs can arbitrary set a composing region after
@@ -88,7 +88,7 @@ impl ComposingText {
}
}
pub(super) struct RenderedEditable {
pub struct RenderedEditable {
pub glyphs: Vec<Glyph>,
pub under_start: TextPos,
pub under_end: TextPos,
@@ -187,7 +187,7 @@ impl RenderedEditable {
/// to first render and move in terms of glyphs due to kerning. For example the chars "ae"
/// may be rendered as a single glyph in some fonts. Same for emojis represented by multiple
/// chars which are often not even a single byte.
pub(super) struct Editable {
pub struct Editable {
text_shaper: TextShaperPtr,
composer: ComposingText,
@@ -370,7 +370,7 @@ fn glyphs_to_string(glyphs: &Vec<Glyph>) -> String {
}
#[derive(Debug, Clone)]
pub(super) struct Selection {
pub struct Selection {
pub start: TextPos,
pub end: TextPos,
}

View File

@@ -51,15 +51,18 @@ use crate::{
use super::{DrawUpdate, OnModify, UIObject};
mod editable;
pub mod editable;
use editable::{Editable, Selection, TextIdx, TextPos};
pub mod repeat;
use repeat::{PressedKey, PressedKeysSmoothRepeat};
// EOL whitespace is given a nudge since it has a width of 0 after text shaping
const CURSOR_EOL_WS_NUDGE: f32 = 0.8;
// EOL chars are more aesthetic when given a smallish nudge
const CURSOR_EOL_NUDGE: f32 = 0.2;
fn eol_nudge(font_size: f32, glyphs: &Vec<Glyph>) -> f32 {
pub fn eol_nudge(font_size: f32, glyphs: &Vec<Glyph>) -> f32 {
if is_whitespace(&glyphs.last().unwrap().substr) {
(font_size * CURSOR_EOL_WS_NUDGE).round()
} else {
@@ -67,82 +70,6 @@ fn eol_nudge(font_size: f32, glyphs: &Vec<Glyph>) -> f32 {
}
}
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
enum PressedKey {
Char(char),
Key(KeyCode),
}
/// On key press (repeat=false), we immediately process the event.
/// Then there's a delay (repeat=true) and then for every step time
/// while key press events are being sent, we allow an event.
/// This ensures smooth typing in the editbox.
struct PressedKeysSmoothRepeat {
/// When holding keys, we track from start and last sent time.
/// This is useful for initial delay and smooth scrolling.
pressed_keys: HashMap<PressedKey, RepeatingKeyTimer>,
/// Initial delay before allowing keys
start_delay: u32,
/// Minimum time between repeated keys
step_time: u32,
}
impl PressedKeysSmoothRepeat {
fn new(start_delay: u32, step_time: u32) -> Self {
Self { pressed_keys: HashMap::new(), start_delay, step_time }
}
fn key_down(&mut self, key: PressedKey, repeat: bool) -> u32 {
//debug!(target: "PressedKeysSmoothRepeat", "key_down({:?}, {})", key, repeat);
if !repeat {
self.pressed_keys.remove(&key);
return 1;
}
// Insert key if not exists
if !self.pressed_keys.contains_key(&key) {
//debug!(target: "PressedKeysSmoothRepeat", "insert key {:?}", key);
self.pressed_keys.insert(key.clone(), RepeatingKeyTimer::new());
}
let repeater = self.pressed_keys.get_mut(&key).expect("repeat map");
repeater.update(self.start_delay, self.step_time)
}
/*
fn key_up(&mut self, key: &PressedKey) {
//debug!(target: "PressedKeysSmoothRepeat", "key_up({:?})", key);
assert!(self.pressed_keys.contains_key(key));
self.pressed_keys.remove(key).expect("key was pressed");
}
*/
}
struct RepeatingKeyTimer {
start: Instant,
actions: u32,
}
impl RepeatingKeyTimer {
fn new() -> Self {
Self { start: Instant::now(), actions: 0 }
}
fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
let elapsed = self.start.elapsed().as_millis();
//debug!(target: "RepeatingKeyTimer", "update() elapsed={}, actions={}",
// elapsed, self.actions);
if elapsed < start_delay as u128 {
return 0
}
let total_actions = ((elapsed - start_delay as u128) / step_time as u128) as u32;
let remaining_actions = total_actions - self.actions;
self.actions = total_actions;
remaining_actions
}
}
#[derive(Clone)]
struct TouchStartInfo {
pos: Point,
@@ -1645,11 +1572,11 @@ impl UIObject for EditBox {
/// Filter these char events from being handled since we handle them
/// using the key_up/key_down events.
/// Avoids duplicate processing of keyboard input events.
static DISALLOWED_CHARS: &'static [char] = &['\r', '\u{8}', '\u{7f}', '\t', '\n'];
pub static DISALLOWED_CHARS: &'static [char] = &['\r', '\u{8}', '\u{7f}', '\t', '\n'];
/// These keycodes are handled via normal key_up/key_down events.
/// Anything in this list must be disallowed char events.
static ALLOWED_KEYCODES: &'static [KeyCode] = &[
pub static ALLOWED_KEYCODES: &'static [KeyCode] = &[
KeyCode::Left,
KeyCode::Right,
KeyCode::Up,
@@ -1672,16 +1599,3 @@ static ALLOWED_KEYCODES: &'static [KeyCode] = &[
KeyCode::Home,
KeyCode::End,
];
/*
impl Stoppable for EditBox {
async fn stop(&self) {
// TODO: Delete own draw call
// Free buffers
// Should this be in drop?
//self.render_api.delete_buffer(self.vertex_buffer);
//self.render_api.delete_buffer(self.index_buffer);
}
}
*/

View File

@@ -0,0 +1,96 @@
/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2024 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use miniquad::KeyCode;
use std::{collections::HashMap, time::Instant};
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum PressedKey {
Char(char),
Key(KeyCode),
}
/// On key press (repeat=false), we immediately process the event.
/// Then there's a delay (repeat=true) and then for every step time
/// while key press events are being sent, we allow an event.
/// This ensures smooth typing in the editbox.
pub struct PressedKeysSmoothRepeat {
/// When holding keys, we track from start and last sent time.
/// This is useful for initial delay and smooth scrolling.
pressed_keys: HashMap<PressedKey, RepeatingKeyTimer>,
/// Initial delay before allowing keys
start_delay: u32,
/// Minimum time between repeated keys
step_time: u32,
}
impl PressedKeysSmoothRepeat {
pub fn new(start_delay: u32, step_time: u32) -> Self {
Self { pressed_keys: HashMap::new(), start_delay, step_time }
}
pub fn key_down(&mut self, key: PressedKey, repeat: bool) -> u32 {
//debug!(target: "PressedKeysSmoothRepeat", "key_down({:?}, {})", key, repeat);
if !repeat {
self.pressed_keys.remove(&key);
return 1;
}
// Insert key if not exists
if !self.pressed_keys.contains_key(&key) {
//debug!(target: "PressedKeysSmoothRepeat", "insert key {:?}", key);
self.pressed_keys.insert(key.clone(), RepeatingKeyTimer::new());
}
let repeater = self.pressed_keys.get_mut(&key).expect("repeat map");
repeater.update(self.start_delay, self.step_time)
}
/*
fn key_up(&mut self, key: &PressedKey) {
//debug!(target: "PressedKeysSmoothRepeat", "key_up({:?})", key);
assert!(self.pressed_keys.contains_key(key));
self.pressed_keys.remove(key).expect("key was pressed");
}
*/
}
struct RepeatingKeyTimer {
start: Instant,
actions: u32,
}
impl RepeatingKeyTimer {
fn new() -> Self {
Self { start: Instant::now(), actions: 0 }
}
fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
let elapsed = self.start.elapsed().as_millis();
//debug!(target: "RepeatingKeyTimer", "update() elapsed={}, actions={}",
// elapsed, self.actions);
if elapsed < start_delay as u128 {
return 0
}
let total_actions = ((elapsed - start_delay as u128) / step_time as u128) as u32;
let remaining_actions = total_actions - self.actions;
self.actions = total_actions;
remaining_actions
}
}

View File

@@ -34,11 +34,13 @@ mod button;
pub use button::{Button, ButtonPtr};
pub mod chatview;
pub use chatview::{ChatView, ChatViewPtr};
mod chatedit;
pub use chatedit::{ChatEdit, ChatEditPtr};
mod editbox;
pub use editbox::{EditBox, EditBoxPtr};
mod image;
pub use image::{Image, ImagePtr};
pub mod vector_art;
mod vector_art;
pub use vector_art::{
shape::{ShapeVertex, VectorShape},
VectorArt, VectorArtPtr,
@@ -167,6 +169,7 @@ pub fn get_ui_object_ptr(node: &SceneNode3) -> Arc<dyn UIObject + Send> {
Pimpl::VectorArt(obj) => obj.clone(),
Pimpl::Text(obj) => obj.clone(),
Pimpl::EditBox(obj) => obj.clone(),
Pimpl::ChatEdit(obj) => obj.clone(),
Pimpl::ChatView(obj) => obj.clone(),
Pimpl::Image(obj) => obj.clone(),
Pimpl::Button(obj) => obj.clone(),
@@ -179,6 +182,7 @@ pub fn get_ui_object3<'a>(node: &'a SceneNode3) -> &'a dyn UIObject {
Pimpl::VectorArt(obj) => obj.as_ref(),
Pimpl::Text(obj) => obj.as_ref(),
Pimpl::EditBox(obj) => obj.as_ref(),
Pimpl::ChatEdit(obj) => obj.as_ref(),
Pimpl::ChatView(obj) => obj.as_ref(),
Pimpl::Image(obj) => obj.as_ref(),
Pimpl::Button(obj) => obj.as_ref(),