Skip to content

Commit 8b08518

Browse files
authored
Merge branch 'master' into quitting-apps
2 parents 049fc72 + 83c7289 commit 8b08518

7 files changed

Lines changed: 110 additions & 45 deletions

File tree

docs/config.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,3 @@ command = "echo "
6666
# icon_path is optional
6767
alias = "Variables 1" # the name that will be displayed in the results
6868
alias_lc = "var test" # the name used to search for it
69-

src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub enum Message {
8383
UpdateAvailable,
8484
ResizeWindow(Id, f32),
8585
OpenWindow,
86+
OpenResult(u32),
8687
OpenToSettings,
8788
SearchQueryChanged(String, Id),
8889
KeyPressed(u32),

src/app/apps.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl App {
173173
theme: crate::config::Theme,
174174
id_num: u32,
175175
focussed_id: u32,
176+
on_press: Option<Message>,
176177
) -> iced::Element<'static, Message> {
177178
let focused = focussed_id == id_num;
178179

@@ -210,11 +211,11 @@ impl App {
210211
}
211212
row = row.push(container(text_block).width(Fill));
212213

213-
let msg = match self.open_command.clone() {
214+
let msg = on_press.or(match self.open_command.clone() {
214215
AppCommand::Function(func) => Some(Message::RunFunction(func)),
215216
AppCommand::Message(msg) => Some(msg),
216217
AppCommand::Display => None,
217-
};
218+
});
218219

219220
let theme_clone = theme.clone();
220221

src/app/pages/clipboard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn clipboard_view(
6060
Column::from_iter(clipboard_content.iter().enumerate().map(|(i, content)| {
6161
content
6262
.to_app()
63-
.render(theme.clone(), i as u32, focussed_id)
63+
.render(theme.clone(), i as u32, focussed_id, None)
6464
}))
6565
.width(WINDOW_WIDTH / 3.),
6666
Direction::Vertical(Scrollbar::hidden()),

src/app/tile.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use log::{info, warn};
2727
use objc2::rc::Retained;
2828
use objc2_app_kit::NSRunningApplication;
2929
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
30+
use rayon::slice::ParallelSliceMut;
3031
use tokio::io::AsyncBufReadExt;
3132
use tray_icon::TrayIcon;
3233

@@ -71,6 +72,24 @@ impl AppIndex {
7172
app.ranking += 1;
7273
}
7374

75+
fn top_ranked(&self, limit: usize) -> Vec<App> {
76+
let mut ranked: Vec<App> = self
77+
.by_name
78+
.values()
79+
.filter(|app| app.ranking > 0)
80+
.cloned()
81+
.collect();
82+
83+
ranked.par_sort_by(|left, right| {
84+
right
85+
.ranking
86+
.cmp(&left.ranking)
87+
.then_with(|| left.display_name.cmp(&right.display_name))
88+
});
89+
ranked.truncate(limit);
90+
ranked
91+
}
92+
7493
fn empty() -> AppIndex {
7594
AppIndex {
7695
by_name: HashMap::new(),
@@ -261,6 +280,10 @@ impl Tile {
261280
self.results = results;
262281
}
263282

283+
pub fn frequent_results(&self) -> Vec<App> {
284+
self.options.top_ranked(5)
285+
}
286+
264287
/// Gets the frontmost application to focus later.
265288
pub fn capture_frontmost(&mut self) {
266289
use objc2_app_kit::NSWorkspace;

src/app/tile/elm.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {
136136
Page::Settings => settings_page(tile.config.clone()),
137137
Page::FileSearch | Page::Main => container(Column::from_iter(
138138
tile.results.iter().enumerate().map(|(i, app)| {
139-
app.clone()
140-
.render(tile.config.theme.clone(), i as u32, tile.focus_id)
139+
app.clone().render(
140+
tile.config.theme.clone(),
141+
i as u32,
142+
tile.focus_id,
143+
Some(Message::OpenResult(i as u32)),
144+
)
141145
}),
142146
))
143147
.into(),
@@ -164,7 +168,11 @@ pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {
164168
.height(height as u32);
165169

166170
let text = if tile.query_lc.is_empty() {
167-
tile.page.to_string()
171+
if tile.page == Page::Main {
172+
"Frequently used".to_string()
173+
} else {
174+
tile.page.to_string()
175+
}
168176
} else {
169177
match results_count {
170178
1 => "1 result found".to_string(),

src/app/tile/update.rs

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
4949
focus_this_app();
5050
tile.focused = true;
5151
tile.visible = true;
52-
Task::none()
52+
53+
if tile.page == Page::Main && tile.query_lc.is_empty() {
54+
window::latest()
55+
.map(|x| x.unwrap())
56+
.map(|id| Message::SearchQueryChanged(String::new(), id))
57+
} else {
58+
Task::none()
59+
}
5360
}
5461

5562
Message::UpdateAvailable => {
@@ -222,42 +229,8 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
222229
)
223230
}
224231

225-
Message::OpenFocused => {
226-
info!("Open Focussed called");
227-
let results = if tile.page == Page::ClipboardHistory {
228-
tile.clipboard_content
229-
.iter()
230-
.map(|x| x.to_app().to_owned())
231-
.collect()
232-
} else {
233-
tile.results.clone()
234-
};
235-
match results.get(tile.focus_id as usize) {
236-
Some(App {
237-
search_name: name,
238-
open_command: AppCommand::Function(func),
239-
..
240-
}) => {
241-
info!("Updating ranking for: {name}");
242-
tile.options.update_ranking(name);
243-
Task::done(Message::RunFunction(func.to_owned()))
244-
}
245-
Some(App {
246-
search_name: name,
247-
open_command: AppCommand::Message(msg),
248-
..
249-
}) => {
250-
info!("Updating ranking for: {name}");
251-
tile.options.update_ranking(name);
252-
Task::done(msg.to_owned())
253-
}
254-
Some(App {
255-
open_command: AppCommand::Display,
256-
..
257-
}) => Task::done(Message::ReturnFocus),
258-
None => Task::none(),
259-
}
260-
}
232+
Message::OpenFocused => Task::done(Message::OpenResult(tile.focus_id)),
233+
Message::OpenResult(id) => open_result(tile, id as usize),
261234

262235
Message::ReloadConfig => {
263236
info!("Reloading config");
@@ -356,10 +329,19 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
356329
_ => Task::none(),
357330
};
358331

332+
let refresh_empty_main_query = if tile.page == Page::Main {
333+
window::latest()
334+
.map(|x| x.unwrap())
335+
.map(|id| Message::SearchQueryChanged(String::new(), id))
336+
} else {
337+
Task::none()
338+
};
339+
359340
Task::batch([
360341
Task::done(Message::ClearSearchQuery),
361342
Task::done(Message::ClearSearchResults),
362343
task,
344+
refresh_empty_main_query,
363345
])
364346
}
365347

@@ -575,7 +557,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
575557
let mut final_config = tile.config.clone();
576558
match config {
577559
SetConfigFields::ToggleHotkey(hk) => final_config.toggle_hotkey = hk,
578-
SetConfigFields::ClipboardHotkey(hk) => final_config.toggle_hotkey = hk,
560+
SetConfigFields::ClipboardHotkey(hk) => final_config.clipboard_hotkey = hk,
579561
SetConfigFields::Modes(Editable::Create((key, value))) => {
580562
final_config.modes.insert(key, value);
581563
}
@@ -732,6 +714,52 @@ fn resize_task(id: Id, count: u32) -> Task<Message> {
732714
))
733715
}
734716

717+
fn resize_for_results_count(id: Id, count: usize) -> Task<Message> {
718+
if count == 0 {
719+
return zero_item_resize_task(id);
720+
}
721+
if count == 1 {
722+
return single_item_resize_task(id);
723+
}
724+
725+
let max_elem = min(5, count);
726+
Task::done(Message::ResizeWindow(
727+
id,
728+
((max_elem * 55) + 35 + DEFAULT_WINDOW_HEIGHT as usize) as f32,
729+
))
730+
}
731+
732+
fn open_result(tile: &mut Tile, id: usize) -> Task<Message> {
733+
let results = if tile.page == Page::ClipboardHistory {
734+
tile.clipboard_content
735+
.iter()
736+
.map(|x| x.to_app().to_owned())
737+
.collect()
738+
} else {
739+
tile.results.clone()
740+
};
741+
742+
let Some(app) = results.get(id).cloned() else {
743+
return Task::none();
744+
};
745+
746+
let search_name = app.search_name.clone();
747+
748+
match app.open_command {
749+
AppCommand::Function(func) => {
750+
info!("Updating ranking for: {search_name}");
751+
tile.options.update_ranking(&search_name);
752+
Task::done(Message::RunFunction(func))
753+
}
754+
AppCommand::Message(msg) => {
755+
info!("Updating ranking for: {search_name}");
756+
tile.options.update_ranking(&search_name);
757+
Task::done(msg)
758+
}
759+
AppCommand::Display => Task::done(Message::ReturnFocus),
760+
}
761+
}
762+
735763
/// Handling the lemon easter egg icon
736764
fn lemon_icon_handle() -> Option<Handle> {
737765
image::ImageReader::new(Cursor::new(include_bytes!("../../../docs/lemon.png")))
@@ -755,6 +783,11 @@ fn execute_query(tile: &mut Tile, id: Id) -> Task<Message> {
755783
_ => {}
756784
}
757785

786+
if tile.page == Page::Main && tile.query_lc.is_empty() {
787+
tile.results = tile.frequent_results();
788+
return resize_for_results_count(id, tile.results.len());
789+
}
790+
758791
if tile.query_lc.is_empty()
759792
|| (tile.query_lc.chars().count() < 2 && tile.page == Page::FileSearch)
760793
{

0 commit comments

Comments
 (0)