Skip to content

Commit 5fa72ee

Browse files
committed
Add search ranking
1 parent 232c44a commit 5fa72ee

7 files changed

Lines changed: 58 additions & 15 deletions

File tree

src/app/apps.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub enum AppCommand {
3434
/// "run" the app.
3535
#[derive(Debug, Clone)]
3636
pub struct App {
37+
pub ranking: i32,
3738
pub open_command: AppCommand,
3839
pub desc: String,
3940
pub icons: Option<iced::widget::image::Handle>,
@@ -56,6 +57,7 @@ impl App {
5657
emojis::iter()
5758
.filter(|x| x.unicode_version() < emojis::UnicodeVersion::new(17, 13))
5859
.map(|x| App {
60+
ranking: 0,
5961
icons: None,
6062
display_name: x.to_string(),
6163
search_name: x.name().to_string(),
@@ -74,41 +76,47 @@ impl App {
7476

7577
vec![
7678
App {
79+
ranking: 0,
7780
open_command: AppCommand::Function(Function::Quit),
7881
desc: RUSTCAST_DESC_NAME.to_string(),
7982
icons: icons.clone(),
8083
display_name: "Quit RustCast".to_string(),
8184
search_name: "quit".to_string(),
8285
},
8386
App {
87+
ranking: 0,
8488
open_command: AppCommand::Function(Function::OpenPrefPane),
8589
desc: RUSTCAST_DESC_NAME.to_string(),
8690
icons: icons.clone(),
8791
display_name: "Open RustCast Preferences".to_string(),
8892
search_name: "settings".to_string(),
8993
},
9094
App {
95+
ranking: 0,
9196
open_command: AppCommand::Message(Message::SwitchToPage(Page::EmojiSearch)),
9297
desc: RUSTCAST_DESC_NAME.to_string(),
9398
icons: icons.clone(),
9499
display_name: "Search for an Emoji".to_string(),
95100
search_name: "emoji".to_string(),
96101
},
97102
App {
103+
ranking: 0,
98104
open_command: AppCommand::Message(Message::SwitchToPage(Page::ClipboardHistory)),
99105
desc: RUSTCAST_DESC_NAME.to_string(),
100106
icons: icons.clone(),
101107
display_name: "Clipboard History".to_string(),
102108
search_name: "clipboard".to_string(),
103109
},
104110
App {
111+
ranking: 0,
105112
open_command: AppCommand::Message(Message::ReloadConfig),
106113
desc: RUSTCAST_DESC_NAME.to_string(),
107114
icons: icons.clone(),
108115
display_name: "Reload RustCast".to_string(),
109116
search_name: "refresh".to_string(),
110117
},
111118
App {
119+
ranking: 0,
112120
open_command: AppCommand::Display,
113121
desc: RUSTCAST_DESC_NAME.to_string(),
114122
icons: icons.clone(),

src/app/tile.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ impl AppIndex {
5757
.take_while(move |(k, _)| k.starts_with(prefix))
5858
.map(|(_, v)| v)
5959
}
60+
fn update_ranking(&mut self, name: &str) {
61+
let app = match self.by_name.get_mut(name) {
62+
Some(a) => a,
63+
None => return,
64+
};
65+
66+
app.ranking += 5;
67+
}
6068

6169
/// Factory function for creating
6270
pub fn from_apps(options: Vec<App>) -> Self {

src/app/tile/update.rs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,34 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
172172
)
173173
}
174174

175-
Message::OpenFocused => match tile.results.get(tile.focus_id as usize) {
176-
Some(App {
177-
open_command: AppCommand::Function(func),
178-
..
179-
}) => Task::done(Message::RunFunction(func.to_owned())),
180-
Some(App {
181-
open_command: AppCommand::Message(msg),
182-
..
183-
}) => Task::done(msg.to_owned()),
184-
Some(App {
185-
open_command: AppCommand::Display,
186-
..
187-
}) => Task::done(Message::ReturnFocus),
188-
None => Task::none(),
189-
},
175+
Message::OpenFocused => {
176+
// TODO: update ranking here
177+
match tile.results.get(tile.focus_id as usize) {
178+
Some(App {
179+
search_name: name,
180+
open_command: AppCommand::Function(func),
181+
..
182+
}) => {
183+
info!("Updating ranking for: {name}");
184+
tile.options.update_ranking(name);
185+
Task::done(Message::RunFunction(func.to_owned()))
186+
}
187+
Some(App {
188+
search_name: name,
189+
open_command: AppCommand::Message(msg),
190+
..
191+
}) => {
192+
info!("Updating ranking for: {name}");
193+
tile.options.update_ranking(name);
194+
Task::done(msg.to_owned())
195+
}
196+
Some(App {
197+
open_command: AppCommand::Display,
198+
..
199+
}) => Task::done(Message::ReturnFocus),
200+
None => Task::none(),
201+
}
202+
}
190203

191204
Message::ReloadConfig => {
192205
info!("Reloading config");
@@ -351,6 +364,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
351364
} else if tile.query_lc == "randomvar" {
352365
let rand_num = rand::random_range(0..100);
353366
tile.results = vec![App {
367+
ranking: 0,
354368
open_command: AppCommand::Function(Function::RandomVar(rand_num)),
355369
desc: "Easter egg".to_string(),
356370
icons: None,
@@ -360,6 +374,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
360374
return single_item_resize_task(id);
361375
} else if tile.query_lc == "67" {
362376
tile.results = vec![App {
377+
ranking: 0,
363378
open_command: AppCommand::Function(Function::RandomVar(67)),
364379
desc: "Easter egg".to_string(),
365380
icons: None,
@@ -369,6 +384,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
369384
return single_item_resize_task(id);
370385
} else if tile.query_lc.ends_with("?") {
371386
tile.results = vec![App {
387+
ranking: 0,
372388
open_command: AppCommand::Function(Function::GoogleSearch(tile.query.clone())),
373389
icons: None,
374390
desc: "Web Search".to_string(),
@@ -388,6 +404,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
388404
&& let Some(res) = Expr::from_str(&tile.query).ok()
389405
{
390406
tile.results.push(App {
407+
ranking: 0,
391408
open_command: AppCommand::Function(Function::Calculate(res.clone())),
392409
desc: RUSTCAST_DESC_NAME.to_string(),
393410
icons: None,
@@ -411,6 +428,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
411428
conversion.target_unit.name
412429
);
413430
App {
431+
ranking: 0,
414432
open_command: AppCommand::Function(Function::CopyToClipboard(
415433
ClipBoardContentType::Text(target.clone()),
416434
)),
@@ -423,6 +441,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
423441
.collect();
424442
} else if tile.results.is_empty() && is_valid_url(&tile.query) {
425443
tile.results.push(App {
444+
ranking: 0,
426445
open_command: AppCommand::Function(Function::OpenWebsite(tile.query.clone())),
427446
desc: "Web Browsing".to_string(),
428447
icons: None,
@@ -431,6 +450,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
431450
});
432451
} else if tile.query_lc.split(' ').count() > 1 {
433452
tile.results.push(App {
453+
ranking: 0,
434454
open_command: AppCommand::Function(Function::GoogleSearch(tile.query.clone())),
435455
icons: None,
436456
desc: "Web Search".to_string(),
@@ -439,6 +459,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
439459
});
440460
} else if tile.results.is_empty() && tile.query_lc == "lemon" {
441461
tile.results.push(App {
462+
ranking: 0,
442463
open_command: AppCommand::Display,
443464
desc: "Easter Egg".to_string(),
444465
icons: lemon_icon_handle(),
@@ -454,6 +475,8 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
454475
.collect();
455476
}
456477

478+
tile.results.sort_by_key(|x| -x.ranking);
479+
457480
let new_length = tile.results.len();
458481
let max_elem = min(5, new_length);
459482

src/clipboard.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl ClipBoardContentType {
2626
display_name = display_name.lines().next().unwrap_or("").to_string();
2727

2828
App {
29+
ranking: 0,
2930
open_command: crate::app::apps::AppCommand::Function(Function::CopyToClipboard(
3031
self_clone.to_owned(),
3132
)),

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl Shelly {
186186
}
187187
});
188188
App {
189+
ranking: 0,
189190
open_command: AppCommand::Function(Function::RunShellCommand(
190191
self_clone.command,
191192
self_clone.alias_lc.clone(),

src/platform/cross.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ fn discover_apps(
152152

153153
let name = file_name.strip_suffix(".app").unwrap().to_string();
154154
Some(App {
155+
ranking: 0,
155156
open_command: AppCommand::Function(Function::OpenApp(path_str)),
156157
desc: "Application".to_string(),
157158
icons,

src/platform/macos/discovery.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ fn query_app(url: impl AsRef<NSURL>, store_icons: bool) -> Option<App> {
252252
.flatten();
253253

254254
Some(App {
255+
ranking: 0,
255256
display_name: name.clone(),
256257
search_name: name.to_lowercase(),
257258
desc: "Application".to_string(),

0 commit comments

Comments
 (0)