-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathapps.rs
More file actions
244 lines (224 loc) · 8.37 KB
/
apps.rs
File metadata and controls
244 lines (224 loc) · 8.37 KB
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
//! This modules handles the logic for each "app" that rustcast can load
//!
//! An "app" is effectively, one of the results that rustcast returns when you search for something
use std::io::Cursor;
use iced::{
Alignment,
Length::Fill,
widget::{
Button, Row, Text, container,
image::{Handle, Viewer},
text::Wrapping,
},
};
use crate::{
app::{Message, Page, RUSTCAST_DESC_NAME},
clipboard::ClipBoardContentType,
commands::Function,
styles::{result_button_style, result_row_container_style},
utils::icns_data_to_handle,
};
/// The rustcast icns icons bytes
pub const ICNS_ICON: &[u8] = include_bytes!("../../docs/icon.icns");
/// This tells each "App" what to do when it is clicked, whether it is a function, a message, or a display
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum AppCommand {
Function(Function),
Message(Message),
Display,
}
/// The main app struct, that represents an "App"
///
/// This struct represents a command that rustcast can perform, providing the rustcast
/// the data needed to search for the app, to display the app in search results, and to actually
/// "run" the app.
#[derive(Debug, Clone)]
pub struct App {
pub ranking: i32,
pub open_command: AppCommand,
pub desc: String,
pub icons: Option<iced::widget::image::Handle>,
pub display_name: String,
pub search_name: String,
}
impl PartialEq for App {
fn eq(&self, other: &Self) -> bool {
self.search_name == other.search_name
&& self.icons == other.icons
&& self.desc == other.desc
&& self.display_name == other.display_name
}
}
impl App {
/// A vec of all the emojis as App structs
pub fn emoji_apps() -> Vec<App> {
emojis::iter()
.filter(|x| x.unicode_version() < emojis::UnicodeVersion::new(17, 13))
.map(|x| App {
ranking: 0,
icons: None,
display_name: x.to_string(),
search_name: x.name().to_string(),
open_command: AppCommand::Function(Function::CopyToClipboard(
ClipBoardContentType::Text(x.to_string()),
)),
desc: x.name().to_string(),
})
.collect()
}
/// This returns the basic apps that rustcast has, such as quiting rustcast and opening preferences
pub fn basic_apps() -> Vec<App> {
let app_version = option_env!("APP_VERSION").unwrap_or("Unknown Version");
let icons = icns_data_to_handle(ICNS_ICON.to_vec());
let ferris_handle =
image::ImageReader::new(Cursor::new(include_bytes!("../../docs/ferris_rs.png")))
.with_guessed_format()
.unwrap()
.decode()
.ok()
.map(|img| Handle::from_rgba(img.width(), img.height(), img.into_bytes()));
vec![
App {
ranking: 0,
open_command: AppCommand::Function(Function::OpenWebsite(
"https://ferris.rs".to_string(),
)),
icons: ferris_handle,
desc: "Easter Egg".to_string(),
display_name: "Ferris Plushies".to_string(),
search_name: "ferris.rs".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Function(Function::Quit),
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: "Quit RustCast".to_string(),
search_name: "quit".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Function(Function::QuitAllApps),
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: "Quit All Apps".to_string(),
search_name: "quit all apps".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Message(Message::SwitchToPage(Page::Settings)),
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: "Open RustCast Preferences".to_string(),
search_name: "settings".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Message(Message::SwitchToPage(Page::EmojiSearch)),
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: "Search for an Emoji".to_string(),
search_name: "emoji".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Message(Message::SwitchToPage(Page::ClipboardHistory)),
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: "Clipboard History".to_string(),
search_name: "clipboard".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Message(Message::SwitchToPage(Page::FileSearch)),
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: "Search for a file".to_string(),
search_name: "file search".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Message(Message::ReloadConfig),
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: "Reload RustCast".to_string(),
search_name: "refresh".to_string(),
},
App {
ranking: 0,
open_command: AppCommand::Display,
desc: RUSTCAST_DESC_NAME.to_string(),
icons: icons.clone(),
display_name: format!("Current RustCast Version: {app_version}"),
search_name: "version".to_string(),
},
]
}
/// This renders the app into an iced element, allowing it to be displayed in the search results
pub fn render(
self,
theme: crate::config::Theme,
id_num: u32,
focussed_id: u32,
on_press: Option<Message>,
) -> iced::Element<'static, Message> {
let focused = focussed_id == id_num;
// Title + subtitle (Raycast style)
let text_block = iced::widget::Column::new()
.spacing(2)
.push(
Text::new(self.display_name)
.font(theme.font())
.size(16)
.wrapping(Wrapping::None)
.color(theme.text_color(1.0)),
)
.push(
Text::new(self.desc)
.font(theme.font())
.size(13)
.color(theme.text_color(0.55)),
);
let mut row = Row::new()
.align_y(Alignment::Center)
.width(Fill)
.spacing(10)
.height(50);
if theme.show_icons
&& let Some(icon) = &self.icons
{
row = row.push(
container(Viewer::new(icon).height(40).width(40))
.width(40)
.height(40),
);
}
row = row.push(container(text_block).width(Fill));
let name = self.search_name.clone();
let theme_clone = theme.clone();
row = row.push(
Button::new("♥️")
.on_press_with(move || Message::ToggleFavouriteApp(name.clone()))
.style(move |_, _| result_button_style(&theme_clone)),
);
let msg = on_press.or(match self.open_command.clone() {
AppCommand::Function(func) => Some(Message::RunFunction(func)),
AppCommand::Message(msg) => Some(msg),
AppCommand::Display => None,
});
let theme_clone = theme.clone();
let content = Button::new(row)
.on_press_maybe(msg)
.style(move |_, _| result_button_style(&theme_clone))
.width(Fill)
.padding(0)
.height(50);
container(content)
.id(format!("result-{}", id_num))
.style(move |_| result_row_container_style(&theme, focused))
.padding(8)
.width(Fill)
.into()
}
}