-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathelm.rs
More file actions
227 lines (206 loc) · 7.28 KB
/
elm.rs
File metadata and controls
227 lines (206 loc) · 7.28 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
//! This module handles the logic for the new and view functions according to the elm
//! architecture. If the subscription function becomes too large, it should be moved to this file
use global_hotkey::hotkey::HotKey;
use iced::border::Radius;
use iced::widget::scrollable::{Anchor, Direction, Scrollbar};
use iced::widget::text::LineHeight;
use iced::widget::{Column, Row, Scrollable, Text, container, space};
use iced::{Alignment, Color, Length, Vector, window};
use iced::{Element, Task};
use iced::{Length::Fill, widget::text_input};
use log::info;
use rayon::slice::ParallelSliceMut;
use crate::app::DEFAULT_WINDOW_HEIGHT;
use crate::app::pages::emoji::emoji_page;
use crate::app::tile::{AppIndex, Hotkeys};
use crate::config::Theme;
use crate::styles::{contents_style, rustcast_text_input_style, tint, with_alpha};
use crate::{app::WINDOW_WIDTH, platform};
use crate::{app::pages::clipboard::clipboard_view, platform::get_installed_apps};
use crate::{
app::{Message, Page, apps::App, default_settings, tile::Tile},
config::Config,
platform::transform_process_to_ui_element,
};
/// Initialise the base window
pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
let (id, open) = window::open(default_settings());
info!("Opening window");
let open = open.discard().chain(window::run(id, |handle| {
platform::window_config(&handle.window_handle().expect("Unable to get window handle"));
transform_process_to_ui_element();
}));
info!("MacOS platform config applied");
let store_icons = config.theme.show_icons;
let mut options = get_installed_apps(store_icons);
options.extend(config.shells.iter().map(|x| x.to_app()));
info!("Loaded shell commands");
options.extend(App::basic_apps());
info!("Loaded basic apps / default apps");
options.par_sort_by_key(|x| x.display_name.len());
let options = AppIndex::from_apps(options);
let hotkeys = Hotkeys {
toggle: hotkey,
clipboard_hotkey: config
.clipboard_hotkey
.parse()
.unwrap_or("SUPER+SHIFT+C".parse().unwrap()),
};
(
Tile {
query: String::new(),
query_lc: String::new(),
focus_id: 0,
results: vec![],
options,
emoji_apps: AppIndex::from_apps(App::emoji_apps()),
hotkeys,
visible: true,
frontmost: None,
focused: false,
config: config.clone(),
theme: config.theme.to_owned().into(),
clipboard_content: vec![],
tray_icon: None,
sender: None,
page: Page::Main,
height: DEFAULT_WINDOW_HEIGHT,
},
Task::batch([open.map(|_| Message::OpenWindow)]),
)
}
pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {
if tile.visible {
let round_bottom_edges = match &tile.page {
Page::Main | Page::EmojiSearch => tile.results.is_empty(),
Page::ClipboardHistory => tile.clipboard_content.is_empty(),
};
let title_input = text_input(tile.config.placeholder.as_str(), &tile.query)
.on_input(move |a| Message::SearchQueryChanged(a, wid))
.on_paste(move |a| Message::SearchQueryChanged(a, wid))
.font(tile.config.theme.font())
.on_submit(Message::OpenFocused)
.id("query")
.width(Fill)
.line_height(LineHeight::Relative(1.75))
.style(move |_, _| rustcast_text_input_style(&tile.config.theme, round_bottom_edges))
.padding(20);
let scrollbar_direction = if tile.config.theme.show_scroll_bar {
Direction::Vertical(
Scrollbar::new()
.width(10)
.scroller_width(10)
.anchor(Anchor::Start),
)
} else {
Direction::Vertical(Scrollbar::hidden())
};
let results = if tile.page == Page::ClipboardHistory {
clipboard_view(
tile.clipboard_content.clone(),
tile.focus_id,
tile.config.theme.clone(),
tile.focus_id,
)
} else if tile.results.is_empty() {
space().into()
} else if tile.page == Page::EmojiSearch {
emoji_page(
tile.config.theme.clone(),
tile.emoji_apps
.search_prefix(&tile.query_lc)
.map(|x| x.to_owned())
.collect(),
tile.focus_id,
)
} else {
container(Column::from_iter(tile.results.iter().enumerate().map(
|(i, app)| {
app.clone()
.render(tile.config.theme.clone(), i as u32, tile.focus_id)
},
)))
.into()
};
let results_count = match &tile.page {
Page::Main => tile.results.len(),
Page::ClipboardHistory => tile.clipboard_content.len(),
Page::EmojiSearch => tile.results.len(),
};
let height = if tile.page == Page::ClipboardHistory {
385
} else {
std::cmp::min(tile.results.len() * 60, 290)
};
let scrollable = Scrollable::with_direction(results, scrollbar_direction)
.id("results")
.height(height as u32);
let contents = container(
Column::new()
.push(title_input)
.push(scrollable)
.push(footer(tile.config.theme.clone(), results_count))
.spacing(0),
)
.style(|_| container::Style {
text_color: None,
background: None,
border: iced::Border {
color: Color::TRANSPARENT,
width: 0.,
radius: Radius::new(15),
},
..Default::default()
});
container(contents.clip(false))
.style(|_| contents_style(&tile.config.theme))
.into()
} else {
space().into()
}
}
fn footer(theme: Theme, results_count: usize) -> Element<'static, Message> {
let text = if results_count == 0 {
return space().into();
} else if results_count == 1 {
"1 result found"
} else {
&format!("{} results found", results_count)
};
container(
Row::new()
.push(
Text::new(text.to_string())
.size(12)
.height(30)
.color(theme.text_color(0.7))
.font(theme.font())
.align_x(Alignment::Center),
)
.padding(4)
.width(Fill)
.height(30),
)
.center(Length::Fill)
.width(WINDOW_WIDTH)
.padding(5)
.style(move |_| container::Style {
text_color: None,
background: Some(iced::Background::Color(with_alpha(
tint(theme.bg_color(), 0.04),
1.0,
))),
border: iced::Border {
color: Color::WHITE,
width: 0.,
radius: Radius::new(15).top(0),
},
shadow: iced::Shadow {
color: Color::TRANSPARENT,
offset: Vector::ZERO,
blur_radius: 0.,
},
snap: false,
})
.into()
}