feat: Implement application launcher with pinned apps functionality
- Added `AppItem` struct to represent application details. - Implemented `collect_lnk_one_level` function to gather .lnk files from specified directories. - Created `get_apps` command to retrieve applications from the Taskbar and Start Menu. - Developed `launch_app` command to open applications. - Introduced `get_app_icon` command to fetch application icons in base64 format. - Added functionality to manage pinned applications with `get_pinned_apps` and `set_pinned_apps` commands. - Implemented system tray with toggle window functionality. - Created UI components for displaying application icons and tiles. - Developed hooks for managing application state, drag-and-drop reordering, and search functionality. - Styled components for a cohesive user interface.
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
use serde::Serialize;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
pub struct AppItem {
|
||||
pub name: String,
|
||||
pub path: String,
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
pub fn collect_lnk_one_level(dir: &Path, source: &str, apps: &mut Vec<AppItem>) {
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return;
|
||||
};
|
||||
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
|
||||
// 1) .lnk прямо в цій папці
|
||||
if path.is_file()
|
||||
&& path
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.map(|e| e.eq_ignore_ascii_case("lnk"))
|
||||
== Some(true)
|
||||
{
|
||||
let name = path
|
||||
.file_stem()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
apps.push(AppItem {
|
||||
name,
|
||||
path: path.to_string_lossy().to_string(),
|
||||
source: source.to_string(),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
// 2) один рівень глибше: Programs\SomeFolder\*.lnk
|
||||
if path.is_dir() {
|
||||
let Ok(sub_entries) = std::fs::read_dir(&path) else {
|
||||
continue;
|
||||
};
|
||||
for sub in sub_entries.flatten() {
|
||||
let sub_path = sub.path();
|
||||
if sub_path.is_file()
|
||||
&& sub_path
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.map(|e| e.eq_ignore_ascii_case("lnk"))
|
||||
== Some(true)
|
||||
{
|
||||
let name = sub_path
|
||||
.file_stem()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
apps.push(AppItem {
|
||||
name,
|
||||
path: sub_path.to_string_lossy().to_string(),
|
||||
source: source.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_apps() -> Vec<AppItem> {
|
||||
let mut apps = Vec::new();
|
||||
|
||||
if let Some(mut path) = dirs::data_dir() {
|
||||
path.push(r"Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar");
|
||||
if let Ok(entries) = std::fs::read_dir(&path) {
|
||||
for entry in entries.flatten() {
|
||||
let file_path = entry.path();
|
||||
if file_path.extension().unwrap_or_default() == "lnk" {
|
||||
let name = file_path
|
||||
.file_stem()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
apps.push(AppItem {
|
||||
name,
|
||||
path: file_path.to_string_lossy().to_string(),
|
||||
source: "Taskbar".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(mut path) = dirs::data_dir() {
|
||||
path.push(r"Microsoft\Windows\Start Menu\Programs");
|
||||
collect_lnk_one_level(&path, "Start Menu", &mut apps);
|
||||
}
|
||||
|
||||
if let Ok(program_data) = std::env::var("ProgramData") {
|
||||
let path =
|
||||
std::path::PathBuf::from(program_data).join(r"Microsoft\Windows\Start Menu\Programs");
|
||||
collect_lnk_one_level(&path, "Start Menu (All)", &mut apps);
|
||||
}
|
||||
|
||||
apps
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn launch_app(path: String) -> Result<(), String> {
|
||||
open::that(path).map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#[cfg(target_os = "windows")]
|
||||
use windows_icons::{get_icon_base64_by_path_with_size, IconSize};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_app_icon(path: String) -> Result<String, String> {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let result = get_icon_base64_by_path_with_size(&path, IconSize::Large)
|
||||
.or_else(|_| windows_icons::get_icon_base64_by_path(&path));
|
||||
|
||||
match result {
|
||||
Ok(b64) => {
|
||||
if b64.starts_with("data:") {
|
||||
Ok(b64)
|
||||
} else {
|
||||
Ok(format!("data:image/png;base64,{}", b64))
|
||||
}
|
||||
}
|
||||
Err(_) => Ok(String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
let _ = path;
|
||||
Ok(String::new())
|
||||
}
|
||||
}
|
||||
+15
-174
@@ -1,129 +1,20 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tauri::AppHandle;
|
||||
use tauri::{
|
||||
menu::{Menu, MenuItem},
|
||||
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
|
||||
Manager,
|
||||
};
|
||||
mod apps;
|
||||
mod icons;
|
||||
mod pinned;
|
||||
mod tray;
|
||||
|
||||
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState};
|
||||
use windows_icons::{get_icon_base64_by_path_with_size, IconSize};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct AppItem {
|
||||
name: String,
|
||||
path: String,
|
||||
source: String,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_apps() -> Vec<AppItem> {
|
||||
let mut apps = Vec::new();
|
||||
|
||||
if let Some(mut path) = dirs::data_dir() {
|
||||
path.push("Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\TaskBar");
|
||||
|
||||
if let Ok(entries) = std::fs::read_dir(&path) {
|
||||
for entry in entries.flatten() {
|
||||
let file_path = entry.path();
|
||||
if file_path.extension().unwrap_or_default() == "lnk" {
|
||||
let name = file_path
|
||||
.file_stem()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
apps.push(AppItem {
|
||||
name,
|
||||
path: file_path.to_string_lossy().to_string(),
|
||||
source: "Taskbar".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(mut path) = dirs::data_dir() {
|
||||
path.push("Microsoft\\Windows\\Start Menu\\Programs");
|
||||
if let Ok(entries) = std::fs::read_dir(&path) {
|
||||
for entry in entries.flatten() {
|
||||
let file_path = entry.path();
|
||||
if file_path.extension().unwrap_or_default() == "lnk" {
|
||||
let name = file_path
|
||||
.file_stem()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
apps.push(AppItem {
|
||||
name,
|
||||
path: file_path.to_string_lossy().to_string(),
|
||||
source: "Start Menu".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
apps
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn launch_app(path: String) -> Result<(), String> {
|
||||
open::that(path).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn pinned_file(app: &AppHandle) -> Result<PathBuf, String> {
|
||||
let dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
|
||||
fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
|
||||
Ok(dir.join("pinned.json"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_pinned_apps(app: AppHandle) -> Result<Vec<String>, String> {
|
||||
let path = pinned_file(&app)?;
|
||||
if !path.exists() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let data = fs::read_to_string(&path).map_err(|e| e.to_string())?;
|
||||
serde_json::from_str(&data).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_pinned_apps(app: AppHandle, paths: Vec<String>) -> Result<(), String> {
|
||||
let path = pinned_file(&app)?;
|
||||
let data = serde_json::to_string_pretty(&paths).map_err(|e| e.to_string())?;
|
||||
fs::write(&path, data).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_app_icon(path: String) -> Result<String, String> {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let result = get_icon_base64_by_path_with_size(&path, IconSize::Large)
|
||||
.or_else(|_| windows_icons::get_icon_base64_by_path(&path));
|
||||
|
||||
match result {
|
||||
Ok(b64) => {
|
||||
if b64.starts_with("data:") {
|
||||
Ok(b64)
|
||||
} else {
|
||||
Ok(format!("data:image/png;base64,{}", b64))
|
||||
}
|
||||
}
|
||||
Err(_) => Ok(String::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
get_apps,
|
||||
launch_app,
|
||||
get_pinned_apps,
|
||||
set_pinned_apps,
|
||||
get_app_icon
|
||||
apps::get_apps,
|
||||
apps::launch_app,
|
||||
pinned::get_pinned_apps,
|
||||
pinned::set_pinned_apps,
|
||||
icons::get_app_icon
|
||||
])
|
||||
.plugin(
|
||||
tauri_plugin_autostart::Builder::new()
|
||||
@@ -134,18 +25,10 @@ fn main() {
|
||||
.plugin(
|
||||
tauri_plugin_global_shortcut::Builder::new()
|
||||
.with_handler(|app, shortcut, event| {
|
||||
if event.state == ShortcutState::Pressed {
|
||||
if shortcut.matches(Modifiers::ALT, Code::Space) {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
let is_visible = window.is_visible().unwrap_or(false);
|
||||
if is_visible {
|
||||
window.hide().unwrap();
|
||||
} else {
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
if event.state == ShortcutState::Pressed
|
||||
&& shortcut.matches(Modifiers::ALT, Code::Space)
|
||||
{
|
||||
tray::toggle_window(app);
|
||||
}
|
||||
})
|
||||
.build(),
|
||||
@@ -154,49 +37,7 @@ fn main() {
|
||||
let alt_space = Shortcut::new(Some(Modifiers::ALT), Code::Space);
|
||||
app.global_shortcut().register(alt_space)?;
|
||||
|
||||
let show_i = MenuItem::with_id(app, "show", "Показати/Сховати", true, None::<&str>)?;
|
||||
let quit_i = MenuItem::with_id(app, "quit", "Вийти", true, None::<&str>)?;
|
||||
let menu = Menu::with_items(app, &[&show_i, &quit_i])?;
|
||||
|
||||
TrayIconBuilder::new()
|
||||
.icon(app.default_window_icon().unwrap().clone())
|
||||
.menu(&menu)
|
||||
.show_menu_on_left_click(false)
|
||||
.on_tray_icon_event(|tray, event| match event {
|
||||
TrayIconEvent::Click {
|
||||
button: MouseButton::Left,
|
||||
button_state: MouseButtonState::Up,
|
||||
..
|
||||
} => {
|
||||
let app_handle = tray.app_handle();
|
||||
if let Some(window) = app_handle.get_webview_window("main") {
|
||||
let is_visible = window.is_visible().unwrap_or(false);
|
||||
if is_visible {
|
||||
window.hide().unwrap();
|
||||
} else {
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.on_menu_event(|app_handle, event| match event.id.as_ref() {
|
||||
"quit" => std::process::exit(0),
|
||||
"show" => {
|
||||
if let Some(window) = app_handle.get_webview_window("main") {
|
||||
let is_visible = window.is_visible().unwrap_or(false);
|
||||
if is_visible {
|
||||
window.hide().unwrap();
|
||||
} else {
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.build(app)?;
|
||||
tray::setup_tray(app)?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
pub fn pinned_file(app: &AppHandle) -> Result<PathBuf, String> {
|
||||
let dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
|
||||
fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
|
||||
Ok(dir.join("pinned.json"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_pinned_apps(app: AppHandle) -> Result<Vec<String>, String> {
|
||||
let path = pinned_file(&app)?;
|
||||
if !path.exists() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let data = fs::read_to_string(&path).map_err(|e| e.to_string())?;
|
||||
serde_json::from_str(&data).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn set_pinned_apps(app: AppHandle, paths: Vec<String>) -> Result<(), String> {
|
||||
let path = pinned_file(&app)?;
|
||||
let data = serde_json::to_string_pretty(&paths).map_err(|e| e.to_string())?;
|
||||
fs::write(&path, data).map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use tauri::{
|
||||
menu::{Menu, MenuItem},
|
||||
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
|
||||
AppHandle, Manager,
|
||||
};
|
||||
|
||||
pub fn toggle_window(app_handle: &AppHandle) {
|
||||
if let Some(window) = app_handle.get_webview_window("main") {
|
||||
let is_visible = window.is_visible().unwrap_or(false);
|
||||
if is_visible {
|
||||
let _ = window.hide();
|
||||
} else {
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup_tray(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let show_i = MenuItem::with_id(app, "show", "Показати/Сховати", true, None::<&str>)?;
|
||||
let quit_i = MenuItem::with_id(app, "quit", "Вийти", true, None::<&str>)?;
|
||||
let menu = Menu::with_items(app, &[&show_i, &quit_i])?;
|
||||
|
||||
let default_icon = app
|
||||
.default_window_icon()
|
||||
.ok_or("default window icon missing")?
|
||||
.clone();
|
||||
|
||||
TrayIconBuilder::new()
|
||||
.icon(default_icon)
|
||||
.menu(&menu)
|
||||
.show_menu_on_left_click(false)
|
||||
.on_tray_icon_event(|tray, event| match event {
|
||||
TrayIconEvent::Click {
|
||||
button: MouseButton::Left,
|
||||
button_state: MouseButtonState::Up,
|
||||
..
|
||||
} => {
|
||||
let app_handle = tray.app_handle();
|
||||
toggle_window(app_handle);
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.on_menu_event(|app_handle, event| match event.id.as_ref() {
|
||||
"quit" => std::process::exit(0),
|
||||
"show" => {
|
||||
toggle_window(app_handle);
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
.build(app)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -19,7 +19,8 @@
|
||||
"decorations": false,
|
||||
"transparent": true,
|
||||
"center": true,
|
||||
"skipTaskbar": true
|
||||
"skipTaskbar": true,
|
||||
"dragDropEnabled": false
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
|
||||
Reference in New Issue
Block a user