└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Electron to Tauri 2 | 3 | ## Path 4 | 5 | ```js 6 | // Electron 7 | 8 | const { app } = require('electron'); 9 | 10 | app.getPath('home'); // ~/ 11 | 12 | app.getPath('appData'); // ~/Library/Application\ Support 13 | // ~/.config 14 | 15 | app.getPath('userData'); // ~/Library/Application\ Support/app-name 16 | // ~/.config/app-name 17 | 18 | app.getPath('cache'); // ~/Caches 19 | app.getPath('temp'); // 20 | app.getPath('exe'); // 21 | app.getPath('module'); // 22 | app.getPath('desktop'); // ~/Desktop 23 | app.getPath('documents'); // ~/Documents 24 | app.getPath('downloads'); // ~/Downloads 25 | app.getPath('music'); // ~/Music 26 | app.getPath('pictures'); // ~/Pictures 27 | app.getPath('videos'); // ~/Videos 28 | app.getPath('recent'); // 29 | app.getPath('logs'); // 30 | app.getPath('crashDump'); // 31 | ``` 32 | 33 | ```rs 34 | // Tauri 35 | 36 | use tauri::api::path; 37 | 38 | fn main() { 39 | 40 | path::home_dir(); // ~/ 41 | path::desktop_dir(); // ~/Desktop 42 | path::document_dir(); // ~/Documents 43 | path::download_dir(); // ~/Downloads 44 | path::audio_dir(); // ~/Music 45 | path::video_dir(); // ~/Movies 46 | path::picture_dir(); // ~/Pictures 47 | path::public_dir(); // ~/Public 48 | 49 | path::data_dir(); // ~/Library/Application\ Support 50 | path::local_data_dir(); // ~/Library/Application\ Support 51 | path::config_dir(); // ~/Library/Application\ Support 52 | 53 | path::cache_dir(); // ~/Library/Caches 54 | path::font_dir(); // ~/Library/Fonts 55 | 56 | } 57 | ``` 58 | 59 | 60 | ## Desktop Notification 61 | 62 | ```js 63 | // Electron 64 | 65 | const { Notification } = require('electron'); 66 | 67 | new Notification({ 68 | title: 'Greetings', 69 | body: 'Hello World', 70 | }).show(); 71 | ``` 72 | 73 | ```rs 74 | // Tauri 75 | 76 | use tauri::api::notification; 77 | 78 | fn main() { 79 | 80 | let result = notification::Notification::new("id") 81 | .title("Greetings") 82 | .body("Hello world") 83 | .show(); 84 | 85 | match result { 86 | Ok(v) => println!("Success: {:?}", v), 87 | Err(e) => println!("Error: {:?}", e), 88 | } 89 | 90 | } 91 | ``` 92 | 93 | 94 | ## Open a folder/file with default application 95 | 96 | ```js 97 | const { shell } = require('electron') 98 | 99 | await shell.showItemInFolder('/'); 100 | await shell.openPath('/Users/mul14/sample.txt'); 101 | ``` 102 | 103 | ```rs 104 | fn main() { 105 | // Open folder with default application 106 | let _ = shell::open("/".into(), None); 107 | 108 | // Open text file with default application 109 | let result = shell::open("/Users/mul14/sample.txt".into(), None); 110 | 111 | match result { 112 | Ok(v) => println!("Success: {:?}", v), 113 | Err(e) => println!("Error: {:?}", e), 114 | } 115 | } 116 | ``` 117 | 118 | 119 | ## Menu 120 | 121 | Electron https://www.electronjs.org/docs/api/menu 122 | 123 | ### Tauri 124 | 125 | ```rs 126 | // menu.rs 127 | 128 | use tauri::{CustomMenuItem, Menu, MenuItem, Submenu}; 129 | 130 | pub fn mainmenu() -> Menu { 131 | let menu_app = Menu::new() 132 | .add_native_item(MenuItem::About( 133 | "App Name".to_string() 134 | )) 135 | .add_native_item(MenuItem::Services) 136 | .add_native_item(MenuItem::Separator) 137 | .add_native_item(MenuItem::Hide) 138 | .add_native_item(MenuItem::HideOthers) 139 | .add_native_item(MenuItem::ShowAll) 140 | .add_native_item(MenuItem::Separator) 141 | .add_native_item(MenuItem::Quit); 142 | 143 | let new_window = CustomMenuItem::new("new:window", "New Window"); 144 | 145 | let menu_file = Menu::new() 146 | .add_item(new_window) 147 | .add_native_item(MenuItem::Separator) 148 | .add_native_item(MenuItem::CloseWindow); 149 | 150 | let menu_edit = Menu::new() 151 | .add_native_item(MenuItem::Undo) 152 | .add_native_item(MenuItem::Redo) 153 | .add_native_item(MenuItem::Separator) 154 | .add_native_item(MenuItem::Cut) 155 | .add_native_item(MenuItem::Copy) 156 | .add_native_item(MenuItem::Paste) 157 | .add_native_item(MenuItem::SelectAll); 158 | 159 | let menu_view = Menu::new() 160 | .add_native_item(MenuItem::EnterFullScreen); 161 | 162 | let menu_window = Menu::new() 163 | .add_native_item(MenuItem::Minimize) 164 | .add_native_item(MenuItem::Zoom); 165 | 166 | let feedback = CustomMenuItem::new("feedback", "Send Feedback"); 167 | 168 | let menu_help = Menu::new() 169 | .add_item(feedback); 170 | 171 | Menu::new() 172 | .add_submenu(Submenu::new("App Name", menu_app)) 173 | .add_submenu(Submenu::new("File", menu_file)) 174 | .add_submenu(Submenu::new("Edit", menu_edit)) 175 | .add_submenu(Submenu::new("View", menu_view)) 176 | .add_submenu(Submenu::new("Window", menu_window)) 177 | .add_submenu(Submenu::new("Help", menu_help)) 178 | } 179 | 180 | 181 | // main.rs 182 | 183 | mod menu; 184 | 185 | fn main() { 186 | tauri::Builder::default() 187 | .menu(menu::mainmenu()) 188 | .run(tauri::generate_context!()) 189 | .expect("error while running tauri application"); 190 | } 191 | ``` 192 | --------------------------------------------------------------------------------