Skip to content

Commit a0cb0c5

Browse files
authored
Merge pull request #166 from unsecretised/logging
Add logging + bit of documentation
2 parents abc23f7 + fc3f04a commit a0cb0c5

File tree

11 files changed

+188
-40
lines changed

11 files changed

+188
-40
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ iced = { version = "0.14.0", features = ["image", "tokio"] }
1212
icns = "0.3.1"
1313
image = "0.25.9"
1414
libc = "0.2.180"
15+
log = "0.4.29"
1516
objc2 = "0.6.3"
1617
objc2-app-kit = { version = "0.3.2", features = ["NSImage"] }
1718
objc2-application-services = { version = "0.3.2", default-features = false, features = [
@@ -26,5 +27,6 @@ rayon = "1.11.0"
2627
serde = { version = "1.0.228", features = ["derive"] }
2728
tokio = { version = "1.48.0", features = ["full"] }
2829
toml = "0.9.8"
30+
tracing-subscriber = { version = "0.3.22", features = ["env-filter"] }
2931
tray-icon = "0.21.3"
3032
url = { version = "2.5.8", default-features = false }

src/app/menubar.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io::Cursor;
44

55
use global_hotkey::hotkey::{Code, HotKey, Modifiers};
66
use image::{DynamicImage, ImageReader};
7+
use log::info;
78
use tray_icon::{
89
Icon, TrayIcon, TrayIconBuilder,
910
menu::{
@@ -69,6 +70,7 @@ fn init_event_handler(sender: ExtSender, hotkey_id: u32) {
6970
MenuEvent::set_event_handler(Some(move |x: MenuEvent| {
7071
let sender = sender.clone();
7172
let sender = sender.0.clone();
73+
info!("Menubar event called: {}", x.id.0);
7274
match x.id().0.as_str() {
7375
"refresh_rustcast" => {
7476
runtime.spawn(async move {

src/app/tile.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ use iced::{
2222
};
2323
use iced::{event, window};
2424

25+
use log::info;
2526
use objc2::rc::Retained;
2627
use objc2_app_kit::NSRunningApplication;
2728
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
2829
use tray_icon::TrayIcon;
2930

31+
use std::fmt::Debug;
3032
use std::fs;
3133
use std::ops::Bound;
3234
use std::time::Duration;
@@ -70,18 +72,23 @@ impl AppIndex {
7072
/// This is the base window, and its a "Tile"
7173
/// Its fields are:
7274
/// - Theme ([`iced::Theme`])
75+
/// - Focus "ID" (which element in the choices is currently selected)
7376
/// - Query (String)
7477
/// - Query Lowercase (String, but lowercase)
7578
/// - Previous Query Lowercase (String)
7679
/// - Results (Vec<[`App`]>) the results of the search
77-
/// - Options (Vec<[`App`]>) the options to search through
80+
/// - Options ([`AppIndex`]) the options to search through (is a BTreeMap wrapper)
81+
/// - Emoji Apps ([`AppIndex`]) emojis that are considered as "apps"
7882
/// - Visible (bool) whether the window is visible or not
7983
/// - Focused (bool) whether the window is focused or not
8084
/// - Frontmost ([`Option<Retained<NSRunningApplication>>`]) the frontmost application before the window was opened
8185
/// - Config ([`Config`]) the app's config
82-
/// - Open Hotkey ID (`u32`) the id of the hotkey that opens the window
86+
/// - Hotkeys, storing the hotkey used for directly opening to the clipboard history page, and
87+
/// opening the app
88+
/// - Sender (The [`ExtSender`] that sends messages, used by the tray icon currently)
8389
/// - Clipboard Content (`Vec<`[`ClipBoardContentType`]`>`) all of the cliboard contents
8490
/// - Page ([`Page`]) the current page of the window (main or clipboard history)
91+
/// - RustCast's height: to figure out which height to resize to
8592
#[derive(Clone)]
8693
pub struct Tile {
8794
pub theme: iced::Theme,
@@ -95,7 +102,6 @@ pub struct Tile {
95102
focused: bool,
96103
frontmost: Option<Retained<NSRunningApplication>>,
97104
pub config: Config,
98-
/// The opening hotkey
99105
hotkeys: Hotkeys,
100106
clipboard_content: Vec<ClipBoardContentType>,
101107
tray_icon: Option<TrayIcon>,
@@ -104,7 +110,7 @@ pub struct Tile {
104110
pub height: f32,
105111
}
106112

107-
#[derive(Clone)]
113+
#[derive(Clone, Debug)]
108114
pub struct Hotkeys {
109115
pub toggle: HotKey,
110116
pub clipboard_hotkey: HotKey,
@@ -285,6 +291,7 @@ fn handle_hot_reloading() -> impl futures::Stream<Item = Message> {
285291
})
286292
}
287293

294+
/// Helper fn for counting directories (since macos `.app`'s are directories) inside a directory
288295
fn count_dirs_in_dir(dir: impl AsRef<Path>) -> usize {
289296
// Read the directory; if it fails, treat as empty
290297
let entries = match fs::read_dir(dir) {
@@ -298,11 +305,12 @@ fn count_dirs_in_dir(dir: impl AsRef<Path>) -> usize {
298305
.count()
299306
}
300307

301-
/// This is the subscription function that handles hotkeys for hiding / showing the window
308+
/// This is the subscription function that handles hotkeys, e.g. for hiding / showing the window
302309
fn handle_hotkeys() -> impl futures::Stream<Item = Message> {
303310
stream::channel(100, async |mut output| {
304311
let receiver = GlobalHotKeyEvent::receiver();
305312
loop {
313+
info!("Hotkey received");
306314
if let Ok(event) = receiver.recv()
307315
&& event.state == HotKeyState::Pressed
308316
{
@@ -331,6 +339,7 @@ fn handle_clipboard_history() -> impl futures::Stream<Item = Message> {
331339
if byte_rep != prev_byte_rep
332340
&& let Some(content) = &byte_rep
333341
{
342+
info!("Adding item to cbhist");
334343
output
335344
.send(Message::ClipboardHistory(content.to_owned()))
336345
.await
@@ -342,6 +351,7 @@ fn handle_clipboard_history() -> impl futures::Stream<Item = Message> {
342351
})
343352
}
344353

354+
/// Handles the rx / receiver for sending and receiving messages
345355
fn handle_recipient() -> impl futures::Stream<Item = Message> {
346356
stream::channel(100, async |mut output| {
347357
let (sender, mut recipient) = channel(100);
@@ -353,6 +363,7 @@ fn handle_recipient() -> impl futures::Stream<Item = Message> {
353363
let abcd = recipient
354364
.try_recv()
355365
.map(async |msg| {
366+
info!("Sending a message");
356367
output.send(msg).await.unwrap();
357368
})
358369
.ok();

src/app/tile/elm.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use iced::{Alignment, Color, Length, Vector, window};
1010
use iced::{Element, Task};
1111
use iced::{Length::Fill, widget::text_input};
1212

13+
use log::info;
1314
use rayon::slice::ParallelSliceMut;
1415

1516
use crate::app::DEFAULT_WINDOW_HEIGHT;
@@ -28,18 +29,23 @@ use crate::{
2829
/// Initialise the base window
2930
pub fn new(hotkey: HotKey, config: &Config) -> (Tile, Task<Message>) {
3031
let (id, open) = window::open(default_settings());
32+
info!("Opening window");
3133

3234
let open = open.discard().chain(window::run(id, |handle| {
3335
platform::window_config(&handle.window_handle().expect("Unable to get window handle"));
3436
transform_process_to_ui_element();
3537
}));
38+
info!("MacOS platform config applied");
3639

3740
let store_icons = config.theme.show_icons;
3841

3942
let mut options = get_installed_apps(store_icons);
4043

4144
options.extend(config.shells.iter().map(|x| x.to_app()));
45+
info!("Loaded shell commands");
46+
4247
options.extend(App::basic_apps());
48+
info!("Loaded basic apps / default apps");
4349
options.par_sort_by_key(|x| x.name.len());
4450
let options = AppIndex::from_apps(options);
4551

src/app/tile/update.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use iced::widget::operation;
1010
use iced::widget::operation::AbsoluteOffset;
1111
use iced::window;
1212
use iced::window::Id;
13+
use log::info;
1314
use rayon::slice::ParallelSliceMut;
1415

1516
use crate::app::WINDOW_WIDTH;
@@ -160,6 +161,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
160161
}
161162

162163
Message::ResizeWindow(id, height) => {
164+
info!("Resizing rustcast window");
163165
tile.height = height;
164166
window::resize(
165167
id,
@@ -187,6 +189,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
187189
},
188190

189191
Message::ReloadConfig => {
192+
info!("Reloading config");
190193
let new_config: Config = match toml::from_str(
191194
&fs::read_to_string(
192195
std::env::var("HOME").unwrap_or("".to_owned())
@@ -280,13 +283,15 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
280283
}
281284

282285
Message::HideWindow(a) => {
286+
info!("Hiding RustCast window");
283287
tile.visible = false;
284288
tile.focused = false;
285289
tile.page = Page::Main;
286290
Task::batch([window::close(a), Task::done(Message::ClearSearchResults)])
287291
}
288292

289293
Message::ReturnFocus => {
294+
info!("Restoring frontmost app");
290295
tile.restore_frontmost();
291296
Task::none()
292297
}

0 commit comments

Comments
 (0)