Skip to content

Commit 0509331

Browse files
committed
Remove manual input handling
1 parent 1ed5fa3 commit 0509331

File tree

11 files changed

+336
-146
lines changed

11 files changed

+336
-146
lines changed

Cargo.lock

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ repository = "https://github.com/RustCastLabs/rustcast"
1111
arboard = "3.6.1"
1212
block2 = "0.6.2"
1313
emojis = "0.8.0"
14-
global-hotkey = "0.7.0"
1514
iced = { version = "0.14.0", features = ["image", "tokio"] }
1615
icns = "0.3.1"
1716
image = { version = "0.25.9", features = ["tiff"] }
@@ -20,10 +19,7 @@ log = "0.4.29"
2019
minreq = { version = "2.14.1", features = ["https"] }
2120
objc2 = "0.6.3"
2221
objc2-app-kit = { version = "0.3.2", features = ["NSImage"] }
23-
objc2-application-services = { version = "0.3.2", default-features = false, features = [
24-
"HIServices",
25-
"Processes",
26-
] }
22+
objc2-application-services = { version = "0.3.2", default-features = false, features = ["HIServices", "Processes"] }
2723
objc2-core-foundation = "0.3.2"
2824
objc2-foundation = { version = "0.3.2", features = ["NSString"] }
2925
objc2-service-management = "0.3.2"

src/app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::app::apps::{App, AppCommand, ICNS_ICON};
55
use crate::commands::Function;
66
use crate::config::{Config, MainPage, Shelly};
77
use crate::debounce::DebouncePolicy;
8+
use crate::platform::macos::launching::Shortcut;
89
use crate::utils::icns_data_to_handle;
910
use crate::{app::tile::ExtSender, clipboard::ClipBoardContentType};
1011
use iced::time::Duration;
@@ -90,7 +91,7 @@ pub enum Message {
9091
OpenResult(u32),
9192
OpenToSettings,
9293
SearchQueryChanged(String, Id),
93-
KeyPressed(u32),
94+
KeyPressed(Shortcut),
9495
FocusTextInput(Move),
9596
HideWindow(Id),
9697
RunFunction(Function),

src/app/menubar.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
33
use std::{collections::HashMap, io::Cursor};
44

5-
use global_hotkey::hotkey::{Code, HotKey, Modifiers};
65
use image::{DynamicImage, ImageReader};
76
use log::info;
87
use tray_icon::{
98
Icon, TrayIcon, TrayIconBuilder,
109
menu::{
1110
AboutMetadataBuilder, Icon as Ico, IsMenuItem, Menu, MenuEvent, MenuItem,
12-
PredefinedMenuItem, Submenu, accelerator::Accelerator,
11+
PredefinedMenuItem, Submenu,
1312
},
1413
};
1514

1615
use crate::{
1716
app::{Message, tile::ExtSender},
1817
config::Config,
18+
platform::macos::launching::Shortcut,
1919
utils::open_url,
2020
};
2121

@@ -39,14 +39,15 @@ pub fn menu_icon(config: Config, sender: ExtSender) -> TrayIcon {
3939
}
4040

4141
pub fn menu_builder(config: Config, sender: ExtSender, update_item: bool) -> Menu {
42-
let hotkey = config.toggle_hotkey.parse::<HotKey>().ok();
42+
let shortcut =
43+
Shortcut::parse(&config.toggle_hotkey).unwrap_or(Shortcut::parse("opt+space").unwrap());
4344

4445
let mut modes = config.modes;
4546
if !modes.contains_key("default") {
4647
modes.insert("Default".to_string(), "default".to_string());
4748
}
4849

49-
init_event_handler(sender, hotkey.map(|x| x.id));
50+
init_event_handler(sender, shortcut);
5051

5152
Menu::with_items(&[
5253
&MenuItem::with_id(
@@ -64,7 +65,7 @@ pub fn menu_builder(config: Config, sender: ExtSender, update_item: bool) -> Men
6465
&open_github_item(),
6566
&PredefinedMenuItem::separator(),
6667
&refresh_item(),
67-
&open_item(hotkey),
68+
&open_item(),
6869
&mode_item(modes),
6970
&PredefinedMenuItem::separator(),
7071
&open_issue_item(),
@@ -86,10 +87,12 @@ fn get_image() -> DynamicImage {
8687
.unwrap()
8788
}
8889

89-
fn init_event_handler(sender: ExtSender, hotkey_id: Option<u32>) {
90+
fn init_event_handler(sender: ExtSender, shortcut: Shortcut) {
9091
let runtime = Runtime::new().unwrap();
92+
let shortcut = shortcut.clone();
9193

9294
MenuEvent::set_event_handler(Some(move |x: MenuEvent| {
95+
let shortcut = shortcut.clone();
9396
let sender = sender.clone();
9497
let sender = sender.0.clone();
9598
info!("Menubar event called: {}", x.id.0);
@@ -107,11 +110,12 @@ fn init_event_handler(sender: ExtSender, hotkey_id: Option<u32>) {
107110
open_url("https://github.com/RustCastLabs/rustcast/issues/new");
108111
}
109112
"show_rustcast" => {
110-
if let Some(hk) = hotkey_id {
111-
runtime.spawn(async move {
112-
sender.clone().try_send(Message::KeyPressed(hk)).unwrap();
113-
});
114-
}
113+
runtime.spawn(async move {
114+
sender
115+
.clone()
116+
.try_send(Message::KeyPressed(shortcut.clone()))
117+
.unwrap();
118+
});
115119
}
116120
"update" => {
117121
open_url("https://github.com/RustCastLabs/rustcast/releases/latest");
@@ -178,13 +182,8 @@ fn mode_item(modes: HashMap<String, String>) -> Submenu {
178182
Submenu::with_items("Modes", true, &items).unwrap()
179183
}
180184

181-
fn open_item(hotkey: Option<HotKey>) -> MenuItem {
182-
MenuItem::with_id(
183-
"show_rustcast",
184-
"Toggle View",
185-
true,
186-
hotkey.map(|hk| Accelerator::new(Some(hk.mods), hk.key)),
187-
)
185+
fn open_item() -> MenuItem {
186+
MenuItem::with_id("show_rustcast", "Toggle View", true, None)
188187
}
189188

190189
fn open_github_item() -> MenuItem {
@@ -196,24 +195,11 @@ fn open_issue_item() -> MenuItem {
196195
}
197196

198197
fn refresh_item() -> MenuItem {
199-
MenuItem::with_id(
200-
"refresh_rustcast",
201-
"Refresh",
202-
true,
203-
Some(Accelerator::new(
204-
Some(Modifiers::SUPER),
205-
global_hotkey::hotkey::Code::KeyR,
206-
)),
207-
)
198+
MenuItem::with_id("refresh_rustcast", "Refresh", true, None)
208199
}
209200

210201
fn open_settings_item() -> MenuItem {
211-
MenuItem::with_id(
212-
"open_preferences",
213-
"Open Preferences",
214-
true,
215-
Some(Accelerator::new(Some(Modifiers::SUPER), Code::Comma)),
216-
)
202+
MenuItem::with_id("open_preferences", "Open Preferences", true, None)
217203
}
218204

219205
fn get_help_item() -> MenuItem {

src/app/pages/settings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn settings_page(config: Config) -> Element<'static, Message> {
4444
.width(Length::Fill)
4545
.style(move |_, _| settings_text_input_item_style(&hotkey_theme))
4646
.into(),
47-
notice_item(theme.clone(), "Requires a restart"),
47+
notice_item(theme.clone(), "Use \"+\" as a seperator"),
4848
]);
4949

5050
let cb_theme = theme.clone();
@@ -56,7 +56,7 @@ pub fn settings_page(config: Config) -> Element<'static, Message> {
5656
.width(Length::Fill)
5757
.style(move |_, _| settings_text_input_item_style(&cb_theme))
5858
.into(),
59-
notice_item(theme.clone(), "Requires a restart"),
59+
notice_item(theme.clone(), "Use \"+\" as a seperator"),
6060
]);
6161

6262
let placeholder_theme = theme.clone();

src/app/tile.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ pub mod update;
55
use crate::app::apps::App;
66
use crate::app::{ArrowKey, Message, Move, Page};
77
use crate::clipboard::ClipBoardContentType;
8-
use crate::config::Config;
8+
use crate::config::{Config, Shelly};
99
use crate::debounce::Debouncer;
1010
use crate::platform::default_app_paths;
11+
use crate::platform::macos::launching::Shortcut;
1112

1213
use arboard::Clipboard;
13-
use global_hotkey::hotkey::HotKey;
14-
use global_hotkey::{GlobalHotKeyEvent, HotKeyState};
1514

1615
use iced::futures::SinkExt;
1716
use iced::futures::channel::mpsc::{Sender, channel};
@@ -190,9 +189,9 @@ pub struct Tile {
190189
/// Stores the toggle [`HotKey`] and the Clipboard [`HotKey`]
191190
#[derive(Clone, Debug)]
192191
pub struct Hotkeys {
193-
pub toggle: HotKey,
194-
pub clipboard_hotkey: HotKey,
195-
pub shells: HashMap<u32, String>,
192+
pub toggle: Shortcut,
193+
pub clipboard_hotkey: Shortcut,
194+
pub shells: HashMap<Shortcut, Shelly>,
196195
}
197196

198197
impl Tile {
@@ -229,7 +228,6 @@ impl Tile {
229228
_ => None,
230229
});
231230
Subscription::batch([
232-
Subscription::run(handle_hotkeys),
233231
Subscription::run(handle_hot_reloading),
234232
keyboard,
235233
Subscription::run(handle_recipient),
@@ -241,37 +239,34 @@ impl Tile {
241239
if let keyboard::Event::KeyPressed { key, modifiers, .. } = event {
242240
match key {
243241
keyboard::Key::Named(Named::ArrowUp) => {
244-
return Some(Message::ChangeFocus(ArrowKey::Up, 1));
242+
Some(Message::ChangeFocus(ArrowKey::Up, 1))
245243
}
246244
keyboard::Key::Named(Named::ArrowLeft) => {
247-
return Some(Message::ChangeFocus(ArrowKey::Left, 1));
245+
Some(Message::ChangeFocus(ArrowKey::Left, 1))
248246
}
249247
keyboard::Key::Named(Named::ArrowRight) => {
250-
return Some(Message::ChangeFocus(ArrowKey::Right, 1));
248+
Some(Message::ChangeFocus(ArrowKey::Right, 1))
251249
}
252250
keyboard::Key::Named(Named::ArrowDown) => {
253-
return Some(Message::ChangeFocus(ArrowKey::Down, 1));
251+
Some(Message::ChangeFocus(ArrowKey::Down, 1))
254252
}
255253
keyboard::Key::Character(chr) => {
256254
if modifiers.command() && chr.to_string() == "r" {
257-
return Some(Message::ReloadConfig);
255+
Some(Message::ReloadConfig)
258256
} else if chr.to_string() == "p" && modifiers.control() {
259-
return Some(Message::ChangeFocus(ArrowKey::Up, 1));
257+
Some(Message::ChangeFocus(ArrowKey::Up, 1))
260258
} else if chr.to_string() == "n" && modifiers.control() {
261-
return Some(Message::ChangeFocus(ArrowKey::Down, 1));
259+
Some(Message::ChangeFocus(ArrowKey::Down, 1))
262260
} else {
263-
return Some(Message::FocusTextInput(Move::Forwards(
264-
chr.to_string(),
265-
)));
261+
Some(Message::FocusTextInput(Move::Forwards(chr.to_string())))
266262
}
267263
}
268-
keyboard::Key::Named(Named::Enter) => return Some(Message::OpenFocused),
264+
keyboard::Key::Named(Named::Enter) => Some(Message::OpenFocused),
269265
keyboard::Key::Named(Named::Backspace) => {
270-
return Some(Message::FocusTextInput(Move::Back));
266+
Some(Message::FocusTextInput(Move::Back))
271267
}
272-
_ => {}
268+
_ => None,
273269
}
274-
None
275270
} else {
276271
None
277272
}
@@ -337,22 +332,6 @@ impl Tile {
337332
}
338333
}
339334

340-
/// This is the subscription function that handles hotkeys, e.g. for hiding / showing the window
341-
fn handle_hotkeys() -> impl futures::Stream<Item = Message> {
342-
stream::channel(100, async |mut output| {
343-
let receiver = GlobalHotKeyEvent::receiver();
344-
loop {
345-
info!("Hotkey received");
346-
if let Ok(event) = receiver.recv()
347-
&& event.state == HotKeyState::Pressed
348-
{
349-
output.try_send(Message::KeyPressed(event.id)).unwrap();
350-
}
351-
tokio::time::sleep(Duration::from_millis(100)).await;
352-
}
353-
})
354-
}
355-
356335
/// This is the subscription function that handles the change in clipboard history
357336
fn handle_clipboard_history() -> impl futures::Stream<Item = Message> {
358337
stream::channel(100, async |mut output| {
@@ -602,7 +581,6 @@ fn handle_recipient() -> impl futures::Stream<Item = Message> {
602581
let abcd = recipient
603582
.try_recv()
604583
.map(async |msg| {
605-
info!("Sending a message");
606584
output.send(msg).await.unwrap();
607585
})
608586
.ok();

src/app/tile/elm.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::collections::HashMap;
55
use std::fs;
66

7-
use global_hotkey::hotkey::HotKey;
87
use iced::border::Radius;
98
use iced::widget::scrollable::{Anchor, Direction, Scrollbar};
109
use iced::widget::text::LineHeight;
@@ -35,7 +34,7 @@ use crate::{
3534
};
3635

3736
/// Initialise the base window
38-
pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
37+
pub fn new(hotkeys: Hotkeys, config: &Config) -> (Tile, Task<Message>) {
3938
let (id, open) = window::open(default_settings());
4039
info!("Opening window");
4140

@@ -60,24 +59,6 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
6059
options.par_sort_by_key(|x| x.display_name.len());
6160
let options = AppIndex::from_apps(options);
6261

63-
let mut shells_map = HashMap::new();
64-
for shell in &config.shells {
65-
if let Some(hk_str) = &shell.hotkey
66-
&& let Ok(hk) = hk_str.parse::<HotKey>()
67-
{
68-
shells_map.insert(hk.id, shell.command.clone());
69-
}
70-
}
71-
72-
let hotkeys = Hotkeys {
73-
toggle: hotkey,
74-
clipboard_hotkey: config
75-
.clipboard_hotkey
76-
.parse()
77-
.unwrap_or("SUPER+SHIFT+C".parse().unwrap()),
78-
shells: shells_map,
79-
};
80-
8162
let home = std::env::var("HOME").unwrap_or("/".to_string());
8263

8364
let ranking = toml::from_str(
@@ -94,8 +75,8 @@ pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
9475
focus_id: 0,
9576
results: vec![],
9677
options,
97-
emoji_apps: AppIndex::from_apps(App::emoji_apps()),
9878
hotkeys,
79+
emoji_apps: AppIndex::from_apps(App::emoji_apps()),
9980
visible: true,
10081
frontmost: None,
10182
focused: false,

0 commit comments

Comments
 (0)