Skip to content

Commit 1a5ec51

Browse files
authored
Merge pull request #109 from unsecretised/emoji-searching
Add emoji searching to rustcast
2 parents aa4d0b4 + 875845b commit 1a5ec51

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
lines changed

Cargo.lock

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2024"
66
[dependencies]
77
anyhow = "1.0.100"
88
arboard = "3.6.1"
9+
emojis = "0.8.0"
910
global-hotkey = "0.7.0"
1011
iced = { version = "0.14.0", features = ["image", "tokio"] }
1112
icns = "0.3.1"

src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub const RUSTCAST_DESC_NAME: &str = "RustCast";
2121
pub enum Page {
2222
Main,
2323
ClipboardHistory,
24+
EmojiSearch,
2425
}
2526

2627
/// The types of arrow keys

src/app/apps.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use iced::{
1111

1212
use crate::{
1313
app::{Message, Page, RUSTCAST_DESC_NAME},
14+
clipboard::ClipBoardContentType,
1415
commands::Function,
1516
styles::{result_button_style, result_row_container_style},
1617
utils::handle_from_icns,
@@ -49,6 +50,21 @@ impl PartialEq for App {
4950
}
5051

5152
impl App {
53+
/// A vec of all the emojis as App structs
54+
pub fn emoji_apps() -> Vec<App> {
55+
emojis::iter()
56+
.filter(|x| x.unicode_version() < emojis::UnicodeVersion::new(13, 0))
57+
.map(|x| App {
58+
icons: None,
59+
name: x.to_string(),
60+
name_lc: x.name().to_string(),
61+
open_command: AppCommand::Function(Function::CopyToClipboard(
62+
ClipBoardContentType::Text(x.to_string()),
63+
)),
64+
desc: x.name().to_string(),
65+
})
66+
.collect()
67+
}
5268
/// This returns the basic apps that rustcast has, such as quiting rustcast and opening preferences
5369
pub fn basic_apps() -> Vec<App> {
5470
let app_version = option_env!("APP_VERSION").unwrap_or("Unknown Version");
@@ -72,6 +88,15 @@ impl App {
7288
name: "Open RustCast Preferences".to_string(),
7389
name_lc: "settings".to_string(),
7490
},
91+
App {
92+
open_command: AppCommand::Message(Message::SwitchToPage(Page::EmojiSearch)),
93+
desc: RUSTCAST_DESC_NAME.to_string(),
94+
icons: handle_from_icns(Path::new(
95+
"/Applications/Rustcast.app/Contents/Resources/icon.icns",
96+
)),
97+
name: "Search for an Emoji".to_string(),
98+
name_lc: "emoji".to_string(),
99+
},
75100
App {
76101
open_command: AppCommand::Message(Message::SwitchToPage(Page::ClipboardHistory)),
77102
desc: RUSTCAST_DESC_NAME.to_string(),

src/app/tile.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use tray_icon::TrayIcon;
2929

3030
use std::collections::BTreeMap;
3131
use std::fs;
32+
use std::ops::Bound;
3233
use std::path::PathBuf;
3334
use std::time::Duration;
3435

@@ -51,7 +52,7 @@ impl AppIndex {
5152
/// Search for an element in the index that starts with the provided prefix
5253
fn search_prefix<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item = &'a App> + 'a {
5354
self.by_name
54-
.range(prefix.to_string()..) // start at prefix
55+
.range::<str, _>((Bound::Included(prefix), Bound::Unbounded))
5556
.take_while(move |(k, _)| k.starts_with(prefix))
5657
.map(|(_, v)| v)
5758
}
@@ -90,6 +91,7 @@ pub struct Tile {
9091
query_lc: String,
9192
results: Vec<App>,
9293
options: AppIndex,
94+
emoji_apps: AppIndex,
9395
visible: bool,
9496
focused: bool,
9597
frontmost: Option<Retained<NSRunningApplication>>,
@@ -207,8 +209,14 @@ impl Tile {
207209
/// function to handle the search query changed event.
208210
pub fn handle_search_query_changed(&mut self) {
209211
let query = self.query_lc.clone();
210-
let results: Vec<App> = self
211-
.options
212+
let options = if self.page == Page::Main {
213+
&self.options
214+
} else if self.page == Page::EmojiSearch {
215+
&self.emoji_apps
216+
} else {
217+
&AppIndex::from_apps(vec![])
218+
};
219+
let results: Vec<App> = options
212220
.search_prefix(&query)
213221
.map(|x| x.to_owned())
214222
.collect();

src/app/tile/elm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
6868
focus_id: 0,
6969
results: vec![],
7070
options,
71+
emoji_apps: AppIndex::from_apps(App::emoji_apps()),
7172
hotkey,
7273
visible: true,
7374
frontmost: None,

src/app/tile/update.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::cmp::min;
33
use std::fs;
44
use std::path::Path;
55
use std::thread;
6-
use std::time::Duration;
76

87
use iced::Task;
98
use iced::widget::image::Handle;
@@ -98,7 +97,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
9897
tile.query_lc = input.trim().to_lowercase();
9998
tile.query = input;
10099
let prev_size = tile.results.len();
101-
if tile.query_lc.is_empty() && tile.page == Page::Main {
100+
if tile.query_lc.is_empty() && tile.page != Page::ClipboardHistory {
102101
tile.results = vec![];
103102
return window::resize(
104103
id,
@@ -240,9 +239,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
240239
tile.page = Page::ClipboardHistory
241240
}
242241

243-
if prev_size != new_length && tile.page == Page::Main {
244-
std::thread::sleep(Duration::from_millis(30));
245-
242+
if prev_size != new_length && tile.page != Page::ClipboardHistory {
246243
Task::batch([
247244
window::resize(
248245
id,
@@ -367,7 +364,10 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
367364

368365
Message::SwitchToPage(page) => {
369366
tile.page = page;
370-
Task::none()
367+
Task::batch([
368+
Task::done(Message::ClearSearchQuery),
369+
Task::done(Message::ClearSearchResults),
370+
])
371371
}
372372

373373
Message::RunFunction(command) => {

0 commit comments

Comments
 (0)