-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
mod.rs
611 lines (558 loc) · 21.9 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use crate::app::entries::pop_entry::PopSearchResult;
use crate::app::entries::AsEntry;
use crate::app::mode::ActiveMode;
use crate::app::plugin_matchers::Plugin;
use crate::app::state::{Selection, State};
use crate::app::subscriptions::plugin_configs::PluginMatcherSubscription;
use crate::app::subscriptions::pop_launcher::{PopLauncherSubscription, SubscriptionMessage};
use crate::db::desktop_entry::DesktopEntryEntity;
use crate::db::plugin::PluginCommandEntity;
use crate::db::web::WebEntity;
use crate::font::DEFAULT_FONT;
use crate::freedesktop::desktop::DesktopEntry;
use crate::icons::IconPath;
use crate::{font, THEME};
use iced::alignment::{Horizontal, Vertical};
use iced::futures::channel::mpsc::{Sender, TrySendError};
use iced::keyboard::KeyCode;
use iced::{
window, Alignment, Application, Color, Column, Container, Element, Length, Row, Scrollable,
Settings, Text, TextInput,
};
use iced_native::{Command, Event, Subscription};
use log::{debug, trace};
use pop_launcher_toolkit::launcher::{Request, Response};
use std::path::Path;
use std::process::exit;
pub mod cache;
pub mod entries;
pub mod mode;
pub mod plugin_matchers;
pub mod state;
pub mod style;
pub mod subscriptions;
pub fn run() -> iced::Result {
debug!("Starting Onagre in debug mode");
let default_font = THEME
.font
.as_ref()
.and_then(|font| font::load(font))
.unwrap_or(DEFAULT_FONT);
Onagre::run(Settings {
id: Some("onagre".to_string()),
window: window::Settings {
transparent: true,
size: THEME.size,
decorations: false,
always_on_top: true,
resizable: false,
position: window::Position::Centered,
min_size: None,
max_size: None,
icon: None,
},
default_text_size: THEME.font_size,
text_multithreading: false,
antialiasing: true,
exit_on_close_request: false,
default_font: Some(default_font),
flags: (),
try_opengles_first: false,
})
}
#[derive(Debug)]
pub struct Onagre<'a> {
state: State<'a>,
request_tx: Option<Sender<Request>>,
}
#[derive(Debug, Clone)]
pub enum Message {
InputChanged(String),
KeyboardEvent(KeyCode),
SubscriptionResponse(SubscriptionMessage),
PluginConfig(Plugin),
Unfocused,
}
impl Application for Onagre<'_> {
type Executor = iced::executor::Default;
type Message = Message;
type Flags = ();
fn new(_: Self::Flags) -> (Self, Command<Self::Message>) {
let onagre = Onagre {
state: Default::default(),
request_tx: Default::default(),
};
(onagre, Command::none())
}
fn title(&self) -> String {
"Onagre".to_string()
}
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.state.input.focus();
match message {
Message::InputChanged(input) => self.on_input_changed(input),
Message::KeyboardEvent(event) => self.handle_input(event),
Message::SubscriptionResponse(message) => self.on_pop_launcher_message(message),
Message::Unfocused => {
if THEME.exit_unfocused {
exit(0);
} else {
Command::none()
}
}
Message::PluginConfig(plugin) => {
self.state
.plugin_matchers
.insert(plugin.name.clone(), plugin);
Command::none()
}
}
}
fn subscription(&self) -> Subscription<Message> {
let keyboard_event = Onagre::keyboard_event();
let pop_launcher = PopLauncherSubscription::create().map(Message::SubscriptionResponse);
let matchers = PluginMatcherSubscription::create().map(Message::PluginConfig);
let subs = vec![keyboard_event, pop_launcher, matchers];
Subscription::batch(subs)
}
fn view(&mut self) -> Element<'_, Self::Message> {
// Build rows from current mode search entries
let selected = self.selected();
let rows = match &self.state.get_active_mode() {
ActiveMode::Plugin {
plugin_name,
history,
..
} if *history => {
let icon = self.state.plugin_matchers.get_plugin_icon(plugin_name);
self.state
.cache
.plugin_history(plugin_name)
.iter()
.enumerate()
.map(|(idx, entry)| entry.to_row(selected, idx, icon.as_ref()).into())
.collect()
}
ActiveMode::Web { modifier, .. } => {
let icon = self.state.plugin_matchers.get_plugin_icon("web");
self.state
.cache
.web_history(modifier)
.iter()
.enumerate()
.map(|(idx, entry)| entry.to_row(selected, idx, icon.as_ref()).into())
.collect()
}
ActiveMode::History => {
let icon = self
.state
.plugin_matchers
.get_plugin_icon("desktop_entries");
self.state
.cache
.de_history()
.iter()
.enumerate()
.map(|(idx, entry)| entry.to_row(selected, idx, icon.as_ref()).into())
.collect()
}
_ => self
.state
.pop_search
.iter()
.map(|entry| {
let icon = match &THEME.icon_theme {
Some(theme) => entry
.category_icon
.as_ref()
.and_then(|source| IconPath::from_source(source, theme)),
_ => None,
};
PopSearchResult(entry)
.to_row(selected, entry.id as usize, icon.as_ref())
.into()
})
.collect(),
};
// Scrollable element containing the rows
let scrollable = Scrollable::new(&mut self.state.scroll)
.push(Column::with_children(rows))
.style(THEME.scrollable())
.scrollbar_margin(THEME.scrollable().scrollbar_margin)
.scrollbar_width(THEME.scrollable().scrollbar_width)
.scroller_width(THEME.scrollable().scroller_width)
.width(Length::Fill)
.height(Length::Fill);
let scrollable = Container::new(scrollable)
.style(&THEME.app_container.rows)
.padding(THEME.app_container.rows.padding.to_iced_padding())
.width(THEME.app_container.rows.width)
.height(THEME.app_container.rows.height); // TODO: add this to stylesheet
let search_input = Container::new(
TextInput::new(
&mut self.state.input,
"Search",
&self.state.input_value.input_display,
Message::InputChanged,
)
.padding(THEME.search_input().padding.to_iced_padding())
.width(THEME.search_input().text_width)
.size(THEME.search_input().font_size)
.style(THEME.search_input()),
)
.width(THEME.search_input().width)
.height(THEME.search_input().height)
.align_x(THEME.search_input().align_x)
.align_y(THEME.search_input().align_y);
let search_bar = Row::new().width(Length::Fill).height(Length::Fill);
// Either plugin_hint is enabled and we try to display it
// Or we display the normal search input
let search_bar = match THEME.plugin_hint() {
None => search_bar.push(search_input),
Some(plugin_hint_style) => if !self.state.input_value.modifier_display.is_empty() {
let plugin_hint = Container::new(
Text::new(&self.state.input_value.modifier_display)
.vertical_alignment(Vertical::Center)
.horizontal_alignment(Horizontal::Center)
.size(plugin_hint_style.font_size),
)
.style(plugin_hint_style)
.width(plugin_hint_style.width)
.height(plugin_hint_style.height)
.align_y(plugin_hint_style.align_y)
.align_x(plugin_hint_style.align_x)
.padding(plugin_hint_style.padding.to_iced_padding());
search_bar.push(plugin_hint).push(search_input)
} else {
search_bar.push(search_input)
}
.spacing(THEME.search().spacing),
};
let search_bar = Container::new(search_bar)
.style(THEME.search())
.align_x(THEME.search().align_x)
.align_y(THEME.search().align_y)
.padding(THEME.search().padding.to_iced_padding())
.width(THEME.search().width)
.height(THEME.search().height);
let app_container = Container::new(
Column::new()
.push(search_bar)
.push(scrollable)
.align_items(Alignment::Start),
)
.padding(THEME.app().padding.to_iced_padding())
.style(THEME.app())
.center_y()
.center_x();
let app_wrapper = Container::new(app_container)
.center_y()
.center_x()
.height(Length::Fill)
.width(Length::Fill)
.padding(THEME.padding.to_iced_padding())
.style(&*THEME);
app_wrapper.into()
}
fn background_color(&self) -> Color {
Color::TRANSPARENT
}
}
impl Onagre<'_> {
// Only call this if we are using entries from the database
// in order to re-ask pop-launcher for the exact same entry
fn current_entry(&self) -> Option<String> {
let selected = self.selected();
match &self.state.get_active_mode() {
ActiveMode::History => self
.state
.cache
.de_history()
.get(selected.unwrap())
.map(|entry| entry.path.to_string_lossy().to_string()),
ActiveMode::Plugin { plugin_name, .. } => {
// Get user input as pop-entry
match selected {
None => {
return self.state.pop_search.get(0).map(|entry| entry.name.clone());
}
Some(selected) => self
.state
.cache
.plugin_history(plugin_name)
.get(selected)
.map(|entry| entry.query.to_string()),
}
}
ActiveMode::Web { modifier, .. } => {
// Get user input as pop-entry
match selected {
None => {
return self.state.pop_search.get(0).map(|entry| entry.name.clone());
}
Some(selected) => self
.state
.cache
.web_history(modifier)
.get(selected)
.map(|entry| entry.query()),
}
}
_pop_mode => None,
}
}
fn on_input_changed(&mut self, input: String) -> Command<Message> {
self.state.set_input(&input);
self.state.selected = match self.state.get_active_mode() {
// For those mode first line is unselected on change
// We want to issue a pop-launcher search request to get the query at index 0 in
// the next search response, then activate it
ActiveMode::Web { .. } | ActiveMode::History => Selection::Reset,
ActiveMode::Plugin { history, .. } if *history => Selection::Reset,
_ => Selection::PopLauncher(0),
};
self.state.scroll.snap_to(0.0);
match &self.state.get_active_mode() {
ActiveMode::History => {}
_ => {
let value = self.state.get_input();
self.pop_request(Request::Search(value))
.expect("Unable to send search request to pop-launcher")
}
}
Command::none()
}
fn run_command<P: AsRef<Path>>(&self, desktop_entry_path: P) -> Command<Message> {
let desktop_entry = DesktopEntry::from_path(&desktop_entry_path).unwrap();
DesktopEntryEntity::persist(
&desktop_entry,
desktop_entry_path.as_ref(),
&self.state.cache.db,
);
let argv = shell_words::split(&desktop_entry.exec);
let args = argv.unwrap();
let args = args
.iter()
// Filter out special freedesktop syntax
.filter(|entry| !entry.starts_with('%'))
.collect::<Vec<&String>>();
std::process::Command::new(&args[0])
.args(&args[1..])
.spawn()
.expect("Command failure");
exit(0);
}
fn handle_input(&mut self, key_code: KeyCode) -> Command<Message> {
match key_code {
KeyCode::Up => {
self.dec_selected();
self.snap();
trace!("Selected line : {:?}", self.selected());
}
KeyCode::Down => {
self.inc_selected();
trace!("Selected line : {:?}", self.selected());
}
KeyCode::Enter => return self.on_execute(),
KeyCode::Tab => {
if let Some(selected) = self.selected() {
self.pop_request(Request::Complete(selected as u32))
.expect("Unable to send request to pop-launcher");
}
}
KeyCode::Escape => {
exit(0);
}
_ => {}
};
Command::none()
}
fn snap(&mut self) {
let total_items = self.current_entries_len() as f32;
match self.selected() {
None => self.state.scroll.snap_to(0.0),
Some(selected) => {
let line_offset = if selected == 0 { 0 } else { &selected + 1 } as f32;
let offset = (1.0 / total_items) * (line_offset) as f32;
self.state.scroll.snap_to(offset);
}
}
}
fn on_pop_launcher_message(&mut self, message: SubscriptionMessage) -> Command<Message> {
match message {
SubscriptionMessage::Ready(sender) => {
self.request_tx = Some(sender);
}
SubscriptionMessage::PopMessage(response) => match response {
Response::Close => exit(0),
Response::Context { .. } => todo!("Discrete graphics is not implemented"),
Response::DesktopEntry { path, .. } => {
debug!("Launch DesktopEntry {path:?} via run_command");
self.run_command(path);
}
Response::Update(search_updates) => {
if self.state.exec_on_next_search {
debug!("Launch entry 0 via PopRequest::Activate");
self.pop_request(Request::Activate(0))
.expect("Unable to send Activate request to pop-launcher");
return Command::none();
}
self.state.pop_search = search_updates;
}
Response::Fill(fill) => self.complete(fill),
},
};
Command::none()
}
fn complete(&mut self, fill: String) {
let filled = if THEME.plugin_hint().is_none() {
self.state.input_value.input_display = fill;
self.state.input.move_cursor_to_end();
self.state.input_value.input_display.clone()
} else {
let mode_prefix = &self.state.input_value.modifier_display;
let fill = fill
.strip_prefix(mode_prefix)
.expect("Auto-completion Error");
self.state.input_value.input_display = fill.into();
self.state.input.move_cursor_to_end();
self.state.input_value.input_display.clone()
};
self.on_input_changed(filled);
}
fn on_execute(&mut self) -> Command<Message> {
match &self.state.get_active_mode() {
ActiveMode::Plugin {
plugin_name,
history,
..
} if *history => {
PluginCommandEntity::persist(
plugin_name,
&self.state.get_input(),
&self.state.cache.db,
);
// Running the user input query at index zero
if self.selected().is_none() {
self.pop_request(Request::Activate(0))
.expect("Unable to send pop-launcher request")
} else {
// Re ask pop-launcher for a stored query
self.state.exec_on_next_search = true;
let command = self.current_entry().unwrap();
self.state.set_input(&command);
self.pop_request(Request::Search(command))
.expect("Unable to send pop-launcher request");
}
}
ActiveMode::Web { modifier, .. } => {
let query = self.state.get_input();
let query = query.strip_prefix(modifier).unwrap();
WebEntity::persist(query, modifier, &self.state.cache.db);
// Running the user input query at index zero
if self.selected().is_none() {
self.pop_request(Request::Activate(0))
.expect("Unable to send pop-launcher request")
} else {
// Re ask pop-launcher for a stored query
let command = self.current_entry().unwrap();
self.state.set_input(&command);
self.state.exec_on_next_search = true;
self.pop_request(Request::Search(command))
.expect("Unable to send pop-launcher request")
}
}
ActiveMode::History => {
let path = self.current_entry();
let path = path.unwrap();
self.run_command(path);
}
_ => {
if self.selected().is_none() {
self.pop_request(Request::Activate(0))
.expect("Unable to send pop-launcher request")
} else {
let selected = self.selected().unwrap() as u32;
debug!("Activating pop entry at index {selected}");
self.pop_request(Request::Activate(selected))
.expect("Unable to send pop-launcher request")
}
}
}
Command::none()
}
fn current_entries_len(&self) -> usize {
match &self.state.get_active_mode() {
ActiveMode::Plugin {
plugin_name,
history,
..
} => {
if *history {
self.state.cache.plugin_history_len(plugin_name)
} else {
self.state.pop_search.len()
}
}
ActiveMode::History => self.state.cache.de_len(),
ActiveMode::DesktopEntry => self.state.pop_search.len(),
ActiveMode::Web { modifier, .. } => self.state.cache.web_history_len(modifier),
}
}
fn pop_request(&self, request: Request) -> Result<(), TrySendError<Request>> {
let sender = self.request_tx.as_ref().unwrap();
let mut sender = sender.clone();
debug!("Sending message to pop launcher : {:?}", request);
sender.try_send(request)
}
fn selected(&self) -> Option<usize> {
match self.state.selected {
Selection::Reset => None,
Selection::History(idx) | Selection::PopLauncher(idx) => Some(idx),
}
}
fn dec_selected(&mut self) {
match self.state.selected {
Selection::Reset => self.state.selected = Selection::Reset,
Selection::History(selected) => {
if selected > 0 {
self.state.selected = Selection::History(selected - 1)
}
}
Selection::PopLauncher(selected) => {
if selected > 0 {
self.state.selected = Selection::PopLauncher(selected - 1)
}
}
};
}
fn inc_selected(&mut self) {
match self.state.selected {
Selection::Reset => self.state.selected = Selection::History(0),
Selection::History(selected) => {
let total_items = self.current_entries_len();
if total_items != 0 && selected < total_items - 1 {
self.state.selected = Selection::History(selected + 1);
self.snap();
}
}
Selection::PopLauncher(selected) => {
let total_items = self.current_entries_len();
if total_items != 0 && selected < total_items - 1 {
self.state.selected = Selection::PopLauncher(selected + 1);
self.snap();
}
}
};
}
fn keyboard_event() -> Subscription<Message> {
iced_native::subscription::events_with(|event, _status| match event {
Event::Window(iced_native::window::Event::Unfocused) => Some(Message::Unfocused),
Event::Keyboard(iced::keyboard::Event::KeyPressed {
modifiers: _,
key_code,
}) => Some(Message::KeyboardEvent(key_code)),
_ => None,
})
}
}