Просмотр исходного кода

wallet: editbox, add a little extra spacing when lines end in whitespace. This is because the glyph for space has nearly 0 width at the end of lines.

darkfi 1 год назад
Родитель
Сommit
1f313d2380

+ 1 - 5
bin/darkwallet/src/ui/chatview/mod.rs

@@ -48,7 +48,7 @@ use crate::{
     pubsub::Subscription,
     scene::{Pimpl, SceneGraph, SceneGraphPtr2, SceneNodeId},
     text::{self, Glyph, GlyphPositionIter, TextShaperPtr},
-    util::enumerate,
+    util::{enumerate, is_whitespace},
     ExecutorPtr,
 };
 
@@ -57,10 +57,6 @@ use super::{eval_rect, get_parent_rect, read_rect, DrawUpdate, OnModify};
 const EPSILON: f32 = 0.001;
 const BIG_EPSILON: f32 = 0.05;
 
-fn is_whitespace(s: &str) -> bool {
-    s.chars().all(char::is_whitespace)
-}
-
 fn is_zero(x: f32) -> bool {
     x.abs() < EPSILON
 }

+ 11 - 1
bin/darkwallet/src/ui/editbox.rs

@@ -41,6 +41,7 @@ use crate::{
     pubsub::Subscription,
     scene::{Pimpl, SceneGraph, SceneGraphPtr2, SceneNodeId},
     text::{self, Glyph, GlyphPositionIter, TextShaperPtr},
+    util::is_whitespace,
     ExecutorPtr,
 };
 
@@ -392,6 +393,10 @@ impl EditBox {
             let cursor_rect = Rectangle { x: 0., y: 0., w: CURSOR_WIDTH, h: clip.h };
             mesh.draw_box(&cursor_rect, cursor_color, &Rectangle::zero());
         } else if is_focused && cursor_pos == glyphs.len() {
+            if is_whitespace(&glyphs.last().unwrap().substr) {
+                rhs += font_size / 2.;
+            }
+
             let cursor_rect =
                 Rectangle { x: rhs - CURSOR_WIDTH, y: 0., w: CURSOR_WIDTH, h: clip.h };
             mesh.draw_box(&cursor_rect, cursor_color, &Rectangle::zero());
@@ -1151,7 +1156,12 @@ impl EditBox {
                 0.
             } else if cursor_pos == glyphs.len() {
                 let glyph_pos = glyph_pos_iter.last().unwrap();
-                glyph_pos.rhs()
+
+                let mut rhs = glyph_pos.rhs();
+                if is_whitespace(&glyphs.last().unwrap().substr) {
+                    rhs += font_size / 2.;
+                }
+                rhs
             } else {
                 assert!(cursor_pos < glyphs.len());
                 let glyph_pos = glyph_pos_iter.nth(cursor_pos).expect("glyph pos mismatch glyphs");

+ 4 - 0
bin/darkwallet/src/util.rs

@@ -18,6 +18,10 @@
 
 use colored::Colorize;
 
+pub fn is_whitespace(s: &str) -> bool {
+    s.chars().all(char::is_whitespace)
+}
+
 #[allow(dead_code)]
 pub fn ansi_texture(width: usize, height: usize, data: &Vec<u8>) -> String {
     let mut out = String::new();