1+ //! Main logic for the app
12use crate :: calculator:: Expression ;
23use crate :: clipboard:: ClipBoardContentType ;
34use crate :: commands:: Function ;
@@ -31,11 +32,20 @@ use std::cmp::min;
3132use std:: time:: Duration ;
3233use std:: { fs, thread} ;
3334
35+ /// The default window width
3436pub const WINDOW_WIDTH : f32 = 500. ;
37+
38+ /// The default window height
3539pub const DEFAULT_WINDOW_HEIGHT : f32 = 65. ;
3640
41+ /// The rustcast descriptor name to be put for all rustcast commands
3742pub const RUSTCAST_DESC_NAME : & str = "RustCast" ;
3843
44+ /// The main app struct, that represents an "App"
45+ ///
46+ /// This struct represents a command that rustcast can perform, providing the rustcast
47+ /// the data needed to search for the app, to display the app in search results, and to actually
48+ /// "run" the app.
3949#[ derive( Debug , Clone ) ]
4050pub struct App {
4151 pub open_command : Function ,
@@ -46,6 +56,7 @@ pub struct App {
4656}
4757
4858impl App {
59+ /// This returns the basic apps that rustcast has, such as quiting rustcast and opening preferences
4960 pub fn basic_apps ( ) -> Vec < App > {
5061 vec ! [
5162 App {
@@ -65,6 +76,7 @@ impl App {
6576 ]
6677 }
6778
79+ /// This renders the app into an iced element, allowing it to be displayed in the search results
6880 pub fn render ( & self , theme : & crate :: config:: Theme ) -> impl Into < iced:: Element < ' _ , Message > > {
6981 let mut tile = Row :: new ( ) . width ( Fill ) . height ( 55 ) ;
7082
@@ -119,12 +131,14 @@ impl App {
119131 }
120132}
121133
134+ /// The different pages that rustcast can have / has
122135#[ derive( Debug , Clone , PartialEq ) ]
123136pub enum Page {
124137 Main ,
125138 ClipboardHistory ,
126139}
127140
141+ /// The message type that iced uses for actions that can do something
128142#[ derive( Debug , Clone ) ]
129143pub enum Message {
130144 OpenWindow ,
@@ -140,6 +154,7 @@ pub enum Message {
140154 _Nothing,
141155}
142156
157+ /// The window settings for rustcast
143158pub fn default_settings ( ) -> Settings {
144159 Settings {
145160 resizable : false ,
@@ -156,6 +171,21 @@ pub fn default_settings() -> Settings {
156171 }
157172}
158173
174+ /// This is the base window, and its a "Tile"
175+ /// Its fields are:
176+ /// - Theme ([`iced::Theme`])
177+ /// - Query (String)
178+ /// - Query Lowercase (String, but lowercase)
179+ /// - Previous Query Lowercase (String)
180+ /// - Results (Vec<[`App`]>) the results of the search
181+ /// - Options (Vec<[`App`]>) the options to search through
182+ /// - Visible (bool) whether the window is visible or not
183+ /// - Focused (bool) whether the window is focused or not
184+ /// - Frontmost ([`Option<Retained<NSRunningApplication>>`]) the frontmost application before the window was opened
185+ /// - Config ([`Config`]) the app's config
186+ /// - Open Hotkey ID (`u32`) the id of the hotkey that opens the window
187+ /// - Clipboard Content (`Vec<`[`ClipBoardContentType`]`>`) all of the cliboard contents
188+ /// - Page ([`Page`]) the current page of the window (main or clipboard history)
159189#[ derive( Debug , Clone ) ]
160190pub struct Tile {
161191 theme : iced:: Theme ,
@@ -174,7 +204,7 @@ pub struct Tile {
174204}
175205
176206impl Tile {
177- /// A base window
207+ /// Initialise the base window
178208 pub fn new ( keybind_id : u32 , config : & Config ) -> ( Self , Task < Message > ) {
179209 let ( id, open) = window:: open ( default_settings ( ) ) ;
180210
@@ -227,6 +257,7 @@ impl Tile {
227257 )
228258 }
229259
260+ /// This handles the iced's updates, which have all the variants of [Message]
230261 pub fn update ( & mut self , message : Message ) -> Task < Message > {
231262 match message {
232263 Message :: OpenWindow => {
@@ -412,6 +443,10 @@ impl Tile {
412443 }
413444 }
414445
446+ /// This is the view of the window. It handles the rendering of the window
447+ ///
448+ /// The rendering of the window size (the resizing of the window) is handled by the
449+ /// [`Tile::update`] function.
415450 pub fn view ( & self , wid : window:: Id ) -> Element < ' _ , Message > {
416451 if self . visible {
417452 let title_input = text_input ( self . config . placeholder . as_str ( ) , & self . query )
@@ -456,10 +491,20 @@ impl Tile {
456491 }
457492 }
458493
494+ /// This returns the theme of the window
459495 pub fn theme ( & self , _: window:: Id ) -> Option < Theme > {
460496 Some ( self . theme . clone ( ) )
461497 }
462498
499+ /// This handles the subscriptions of the window
500+ ///
501+ /// The subscriptions are:
502+ /// - Hotkeys
503+ /// - Hot reloading
504+ /// - Clipboard history
505+ /// - Window close events
506+ /// - Keypresses (escape to close the window)
507+ /// - Window focus changes
463508 pub fn subscription ( & self ) -> Subscription < Message > {
464509 Subscription :: batch ( [
465510 Subscription :: run ( handle_hotkeys) ,
@@ -492,6 +537,11 @@ impl Tile {
492537 ] )
493538 }
494539
540+ /// Handles the search query changed event.
541+ ///
542+ /// This is separate from the `update` function because it has a decent amount of logic, and
543+ /// should be separated out to make it easier to test. This function is called by the `update`
544+ /// function to handle the search query changed event.
495545 pub fn handle_search_query_changed ( & mut self ) {
496546 let filter_vec: & Vec < App > = if self . query_lc . starts_with ( & self . prev_query_lc ) {
497547 self . prev_query_lc = self . query_lc . to_owned ( ) ;
@@ -526,13 +576,15 @@ impl Tile {
526576 self . results = exact;
527577 }
528578
579+ /// Gets the frontmost application to focus later.
529580 pub fn capture_frontmost ( & mut self ) {
530581 use objc2_app_kit:: NSWorkspace ;
531582
532583 let ws = NSWorkspace :: sharedWorkspace ( ) ;
533584 self . frontmost = ws. frontmostApplication ( ) ;
534585 }
535586
587+ /// Restores the frontmost application.
536588 #[ allow( deprecated) ]
537589 pub fn restore_frontmost ( & mut self ) {
538590 use objc2_app_kit:: NSApplicationActivationOptions ;
@@ -543,6 +595,7 @@ impl Tile {
543595 }
544596}
545597
598+ /// This is the subscription function that handles hot reloading of the config
546599fn handle_hot_reloading ( ) -> impl futures:: Stream < Item = Message > {
547600 stream:: channel ( 100 , async |mut output| {
548601 let content = fs:: read_to_string (
@@ -563,6 +616,7 @@ fn handle_hot_reloading() -> impl futures::Stream<Item = Message> {
563616 } )
564617}
565618
619+ /// This is the subscription function that handles hotkeys for hiding / showing the window
566620fn handle_hotkeys ( ) -> impl futures:: Stream < Item = Message > {
567621 stream:: channel ( 100 , async |mut output| {
568622 let receiver = GlobalHotKeyEvent :: receiver ( ) ;
@@ -577,6 +631,7 @@ fn handle_hotkeys() -> impl futures::Stream<Item = Message> {
577631 } )
578632}
579633
634+ /// This is the subscription function that handles the change in clipboard history
580635fn handle_clipboard_history ( ) -> impl futures:: Stream < Item = Message > {
581636 stream:: channel ( 100 , async |mut output| {
582637 let mut clipboard = Clipboard :: new ( ) . unwrap ( ) ;
0 commit comments