launcher: Handle grapheme clusters correctly

This commit is contained in:
Hilmar Gústafsson
2020-07-22 01:44:06 +02:00
committed by Kenny Levinsen
parent 20e08f74ee
commit 3b9aed089c
3 changed files with 24 additions and 5 deletions
Generated
+7
View File
@@ -653,6 +653,11 @@ name = "ucd-trie"
version = "0.1.2" version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-segmentation"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.1.0" version = "0.1.0"
@@ -782,6 +787,7 @@ dependencies = [
"shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"smithay-client-toolkit 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "smithay-client-toolkit 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"timerfd 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "timerfd 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-client 0.23.6 (registry+https://github.com/rust-lang/crates.io-index)", "wayland-client 0.23.6 (registry+https://github.com/rust-lang/crates.io-index)",
"wayland-protocols 0.23.6 (registry+https://github.com/rust-lang/crates.io-index)", "wayland-protocols 0.23.6 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -896,6 +902,7 @@ dependencies = [
"checksum ttf-parser 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "52fbe7769f5af5d7d25aea74b9443b64e544a5ffb4d2b2968295ddea934f1a06" "checksum ttf-parser 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "52fbe7769f5af5d7d25aea74b9443b64e544a5ffb4d2b2968295ddea934f1a06"
"checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9"
"checksum ucd-trie 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8f00ed7be0c1ff1e24f46c3d2af4859f7e863672ba3a6e92e7cff702bf9f06c2" "checksum ucd-trie 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8f00ed7be0c1ff1e24f46c3d2af4859f7e863672ba3a6e92e7cff702bf9f06c2"
"checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
+1
View File
@@ -44,3 +44,4 @@ serde_yaml = { version = "0.8", optional = true }
libpulse-binding = { version = "2.15", optional = true } libpulse-binding = { version = "2.15", optional = true }
alsa = { version = "0.4.0", optional = true } alsa = { version = "0.4.0", optional = true }
fontconfig = "0.2.0" fontconfig = "0.2.0"
unicode-segmentation = "1.6.0"
+16 -5
View File
@@ -16,6 +16,7 @@ use std::sync::mpsc::Sender;
use fuzzy_matcher::skim::{fuzzy_indices, fuzzy_match}; use fuzzy_matcher::skim::{fuzzy_indices, fuzzy_match};
use smithay_client_toolkit::keyboard::keysyms; use smithay_client_toolkit::keyboard::keysyms;
use unicode_segmentation::UnicodeSegmentation;
pub struct Launcher<'a> { pub struct Launcher<'a> {
cursor: usize, cursor: usize,
@@ -349,17 +350,21 @@ impl<'a> Widget for Launcher<'a> {
match key { match key {
keysyms::XKB_KEY_u if modifiers.ctrl => self.leave(), keysyms::XKB_KEY_u if modifiers.ctrl => self.leave(),
keysyms::XKB_KEY_BackSpace => { keysyms::XKB_KEY_BackSpace => {
if !self.input.is_empty() && self.cursor > 0 { let mut indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect();
if !indices.is_empty() && self.cursor > 0 {
self.cursor -= 1; self.cursor -= 1;
self.input.remove(self.cursor); indices.remove(self.cursor);
self.input = indices.iter().fold("".into(), |acc, el| acc + el.1);
self.offset = 0; self.offset = 0;
self.result = None; self.result = None;
self.dirty = true self.dirty = true
} }
} }
keysyms::XKB_KEY_Delete => { keysyms::XKB_KEY_Delete => {
if !self.input.is_empty() && self.cursor < self.input.len() { let mut indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect();
self.input.remove(self.cursor); if !indices.is_empty() && self.cursor < indices.len() {
indices.remove(self.cursor);
self.input = indices.iter().fold("".into(), |acc, el| acc + el.1);
self.dirty = true; self.dirty = true;
} }
} }
@@ -450,7 +455,13 @@ impl<'a> Widget for Launcher<'a> {
_ => { _ => {
println!("{}", key); println!("{}", key);
if let Some(v) = interpreted { if let Some(v) = interpreted {
self.input.insert_str(self.cursor, &v); let indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect();
if self.cursor == indices.len() {
self.input += &v;
} else {
let index_at = indices[self.cursor].0;
self.input.insert_str(index_at, &v);
}
self.cursor += 1; self.cursor += 1;
self.offset = 0; self.offset = 0;
self.result = None; self.result = None;