Skip to content

Commit

Permalink
Add support for shortcodes search
Browse files Browse the repository at this point in the history
  • Loading branch information
Maeeen committed Sep 2, 2024
1 parent f25b378 commit edcd559
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ Multi-platform features:
## TO-DO

* [ ] “Recently used” group
* [ ] Better emoji look-ups
* [x] Better emoji look-ups
* Uses shortcodes
* [x] Emojis should be displayed in a grid
* [ ] Polish design
* [ ] Add a *tooltip* to see the name of the emojis
Expand Down
4 changes: 4 additions & 0 deletions src/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ impl EmojiWrapper {
self.0.skin_tones().map(|x| x.map(EmojiWrapper))
}

pub fn shortcodes(&self) -> impl Iterator<Item = &str> {
self.0.shortcodes()
}

pub fn skin_tone(&self) -> Option<u16> {
self.0.skin_tone().and_then(EmojiWrapper::skin_tone_idx)
}
Expand Down
13 changes: 10 additions & 3 deletions src/emoji_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
emoji::{EmojiGroupWrapper, EmojiWrapper},
EmojiGroupModel, EmojiModel, EmojiSkinToneModel,
};
use slint::{Model, ModelNotify, ModelRc, VecModel};
use slint::{Model, ModelNotify, ModelRc, SharedString, VecModel};

impl From<EmojiWrapper> for EmojiModel {
fn from(e: EmojiWrapper) -> EmojiModel {
Expand All @@ -17,6 +17,7 @@ impl From<EmojiWrapper> for EmojiModel {
name: e.name().into(),
code: e.code().into(),
image: image.unwrap_or_default(),
shortcodes: ModelRc::new(VecModel::from(e.shortcodes().map(|x| SharedString::from(x)).collect::<Vec<_>>())),
skin_tones: ModelRc::new(VecModel::from(match e.skin_tones() {
Some(iterator) => iterator
.map(EmojiSkinToneModel::try_from)
Expand Down Expand Up @@ -71,19 +72,25 @@ impl VecEmojiListModel {
fn filter_down(&self, filter: String) {
let filter = filter.to_lowercase();
let mut emojis = self.vec.borrow_mut();
emojis.retain(|x| x.name.contains(&filter));
VecEmojiListModel::retain_search(&mut emojis, &filter);
self.filter.replace(filter);
self.notify.reset()
}

pub fn filter_up(&self, filter: String) {
let filter = filter.to_lowercase();
let mut emojis = self.initial.borrow().clone();
emojis.retain(|x| x.name.to_lowercase().contains(&filter));
VecEmojiListModel::retain_search(&mut emojis, &filter);
self.filter.replace(filter);
self.vec.replace(emojis);
self.notify.reset()
}

fn retain_search(v: &mut Vec<EmojiModel>, filter: &str) {
v.retain(|x| {
x.name.contains(&filter) || x.shortcodes.iter().any(|s| s.contains(&filter))
})
}
}

impl Model for VecEmojiListModel {
Expand Down
1 change: 1 addition & 0 deletions ui/emoji-model.slint
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export struct EmojiModel {
name: string,
code: string,
image: image,
shortcodes: [string],
skin-tones: [EmojiSkinToneModel]
}

Expand Down

0 comments on commit edcd559

Please sign in to comment.