diff --git a/docs/config.toml b/docs/config.toml index 0cd92ad..a59ea80 100644 --- a/docs/config.toml +++ b/docs/config.toml @@ -1,13 +1,8 @@ # Things you can configure -# Full list of modifiers: https://docs.rs/global-hotkey/0.7.0/global_hotkey/hotkey/struct.Modifiers.html#impl-Modifiers -# Do note, CMD is Super on MacOS -# If you are not sure, use google or GenAI to find out. -toggle_mod = "SHIFT" - -# Full list of keys: https://docs.rs/global-hotkey/0.7.0/global_hotkey/hotkey/enum.Code.html#variants -# Same things as Modifiers, google if unsure. If something should work, but isn't working, open an issue and I'll help -toggle_key = "1" +# Your hotkey that will be used to toggle rustcast +toggle_hotkey = "ALT+SHIFT+1" + placeholder = "Oopsie Dasies" # Buffer (all fields are optional bools) diff --git a/docs/default.toml b/docs/default.toml index 34c15e0..e81e61e 100644 --- a/docs/default.toml +++ b/docs/default.toml @@ -1,7 +1,6 @@ # Default Config -toggle_mod = "ALT" -toggle_key = "Space" +toggle_hotkey = "ALT+SPACE" placeholder = "Time to be productive!" search_url = "https://google.com/search?q=%s" haptic_feedback = false diff --git a/src/app/menubar.rs b/src/app/menubar.rs index ff7f067..1aff612 100644 --- a/src/app/menubar.rs +++ b/src/app/menubar.rs @@ -1,6 +1,6 @@ //! This has the menubar icon logic for the app -use global_hotkey::{hotkey::Code, hotkey::Modifiers}; +use global_hotkey::hotkey::{Code, HotKey, Modifiers}; use image::{DynamicImage, ImageReader}; use tray_icon::{ Icon, TrayIcon, TrayIconBuilder, @@ -18,13 +18,13 @@ use crate::{ use tokio::runtime::Runtime; /// This create a new menubar icon for the app -pub fn menu_icon(hotkey: (Option, Code), hotkey_id: u32, sender: ExtSender) -> TrayIcon { +pub fn menu_icon(hotkey: HotKey, sender: ExtSender) -> TrayIcon { let builder = TrayIconBuilder::new(); let image = get_image(); let icon = Icon::from_rgba(image.as_bytes().to_vec(), image.width(), image.height()).unwrap(); - init_event_handler(sender, hotkey_id); + init_event_handler(sender, hotkey.id()); let menu = Menu::with_items(&[ &version_item(), @@ -110,12 +110,12 @@ fn hide_tray_icon() -> MenuItem { MenuItem::with_id("hide_tray_icon", "Hide Tray Icon", true, None) } -fn open_item(hotkey: (Option, Code)) -> MenuItem { +fn open_item(hotkey: HotKey) -> MenuItem { MenuItem::with_id( "show_rustcast", "Toggle View", true, - Some(Accelerator::new(hotkey.0, hotkey.1)), + Some(Accelerator::new(Some(hotkey.mods), hotkey.key)), ) } diff --git a/src/app/tile.rs b/src/app/tile.rs index de5e4de..5562593 100644 --- a/src/app/tile.rs +++ b/src/app/tile.rs @@ -11,7 +11,7 @@ use crate::config::Config; use crate::utils::open_settings; use arboard::Clipboard; -use global_hotkey::hotkey::{Code, Modifiers}; +use global_hotkey::hotkey::HotKey; use global_hotkey::{GlobalHotKeyEvent, HotKeyState}; use iced::futures::SinkExt; @@ -69,8 +69,8 @@ pub struct Tile { focused: bool, frontmost: Option>, config: Config, - open_hotkey_id: u32, - hotkey: (Option, Code), + /// The opening hotkey + hotkey: HotKey, clipboard_content: Vec, tray_icon: Option, sender: Option, @@ -79,12 +79,8 @@ pub struct Tile { impl Tile { /// Initialise the base window - pub fn new( - hotkey: (Option, Code), - keybind_id: u32, - config: &Config, - ) -> (Self, Task) { - elm::new(hotkey, keybind_id, config) + pub fn new(hotkey: HotKey, config: &Config) -> (Self, Task) { + elm::new(hotkey, config) } /// This handles the iced's updates, which have all the variants of [Message] diff --git a/src/app/tile/elm.rs b/src/app/tile/elm.rs index 14d6674..0c5366e 100644 --- a/src/app/tile/elm.rs +++ b/src/app/tile/elm.rs @@ -1,7 +1,7 @@ //! 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::{Code, Modifiers}; +use global_hotkey::hotkey::HotKey; use iced::border::Radius; use iced::widget::scrollable::{Anchor, Direction, Scrollbar}; use iced::widget::text::LineHeight; @@ -38,11 +38,7 @@ pub fn default_app_paths() -> Vec { } /// Initialise the base window -pub fn new( - hotkey: (Option, Code), - keybind_id: u32, - config: &Config, -) -> (Tile, Task) { +pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task) { let (id, open) = window::open(default_settings()); let open = open.discard().chain(window::run(id, |handle| { @@ -78,7 +74,6 @@ pub fn new( focused: false, config: config.clone(), theme: config.theme.to_owned().into(), - open_hotkey_id: keybind_id, clipboard_content: vec![], tray_icon: None, sender: None, diff --git a/src/app/tile/update.rs b/src/app/tile/update.rs index 9f3fa61..2770007 100644 --- a/src/app/tile/update.rs +++ b/src/app/tile/update.rs @@ -5,7 +5,6 @@ use std::path::Path; use std::thread; use std::time::Duration; -use global_hotkey::hotkey::HotKey; use iced::Task; use iced::widget::image::Handle; use iced::widget::operation; @@ -57,9 +56,8 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task { Message::SetSender(sender) => { tile.sender = Some(sender.clone()); - let hotkey_id = HotKey::new(tile.hotkey.0, tile.hotkey.1).id(); if tile.config.show_trayicon { - tile.tray_icon = Some(menu_icon(tile.hotkey, hotkey_id, sender)); + tile.tray_icon = Some(menu_icon(tile.hotkey, sender)); } Task::none() } @@ -271,7 +269,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task { } Message::KeyPressed(hk_id) => { - if hk_id == tile.open_hotkey_id { + if hk_id == tile.hotkey.id { tile.visible = !tile.visible; if tile.visible { Task::chain( diff --git a/src/config.rs b/src/config.rs index a6f4b43..7fa8dc7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,6 @@ //! This is the config file type definitions for rustcast use std::{path::Path, sync::Arc}; -use global_hotkey::hotkey::Code; - use iced::{Font, font::Family, theme::Custom, widget::image::Handle}; use serde::{Deserialize, Serialize}; @@ -16,8 +14,7 @@ use crate::{ #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(default)] pub struct Config { - pub toggle_mod: String, - pub toggle_key: Code, + pub toggle_hotkey: String, pub buffer_rules: Buffer, pub theme: Theme, pub placeholder: String, @@ -31,8 +28,7 @@ impl Default for Config { /// The default config fn default() -> Self { Self { - toggle_mod: "ALT".to_string(), - toggle_key: Code::Space, + toggle_hotkey: "ALT+SPACE".to_string(), buffer_rules: Buffer::default(), theme: Theme::default(), placeholder: String::from("Time to be productive!"), diff --git a/src/main.rs b/src/main.rs index d4284fe..633dd5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,10 +11,7 @@ use std::path::Path; use crate::{app::tile::Tile, config::Config}; -use global_hotkey::{ - GlobalHotKeyManager, - hotkey::{HotKey, Modifiers}, -}; +use global_hotkey::GlobalHotKeyManager; fn main() -> iced::Result { #[cfg(target_os = "macos")] @@ -40,11 +37,7 @@ fn main() -> iced::Result { let manager = GlobalHotKeyManager::new().unwrap(); - let modifier = Modifiers::from_name(&config.toggle_mod); - - let key = config.toggle_key; - - let show_hide = HotKey::new(modifier, key); + let show_hide = config.toggle_hotkey.parse().unwrap(); // Hotkeys are stored as a vec so that hyperkey support can be added later let hotkeys = vec![show_hide]; @@ -54,7 +47,7 @@ fn main() -> iced::Result { .expect("Unable to register hotkey"); iced::daemon( - move || Tile::new((modifier, key), show_hide.id(), &config), + move || Tile::new(show_hide, &config), Tile::update, Tile::view, )