Skip to content

Commit

Permalink
perf(start): reduce loading time caching start-menu items
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Dec 27, 2024
1 parent 35e147f commit 2ef2858
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 28 deletions.
51 changes: 45 additions & 6 deletions src/background/modules/start/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,44 @@ use arc_swap::ArcSwap;
use lazy_static::lazy_static;
use tauri::{path::BaseDirectory, Manager};

use crate::{error_handler::Result, seelen::get_app_handle, windows_api::WindowsApi};
use crate::{
error_handler::Result, seelen::get_app_handle, utils::constants::SEELEN_COMMON,
windows_api::WindowsApi,
};

use super::domain::StartMenuItem;

lazy_static! {
pub static ref START_MENU_ITEMS: ArcSwap<Vec<StartMenuItem>> =
ArcSwap::from_pointee(StartMenuManager::get_items().unwrap());
pub static ref START_MENU_ITEMS: ArcSwap<Vec<StartMenuItem>> = ArcSwap::from_pointee({
let mut manager = StartMenuManager::new();
manager.init().unwrap();
manager.list
});
}

pub struct StartMenuManager {}
pub struct StartMenuManager {
list: Vec<StartMenuItem>,
cache_path: PathBuf,
}

impl StartMenuManager {
pub fn new() -> StartMenuManager {
StartMenuManager {
list: Vec::new(),
cache_path: SEELEN_COMMON.app_cache_dir().join("start_menu.json"),
}
}

fn init(&mut self) -> Result<()> {
if self.cache_path.exists() {
self.load_cache()?;
} else {
self.read_start_menu_folders()?;
self.store_cache()?;
}
Ok(())
}

pub fn common_items_path() -> PathBuf {
PathBuf::from(r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs")
}
Expand All @@ -30,6 +56,18 @@ impl StartMenuManager {
.expect("Failed to resolve user start menu path")
}

pub fn store_cache(&self) -> Result<()> {
let writer = std::fs::File::create(&self.cache_path)?;
serde_json::to_writer(writer, &self.list)?;
Ok(())
}

pub fn load_cache(&mut self) -> Result<()> {
let reader = std::fs::File::open(&self.cache_path)?;
self.list = serde_json::from_reader(reader)?;
Ok(())
}

fn _get_items(dir: &Path) -> Result<Vec<StartMenuItem>> {
let mut items = Vec::new();
for entry in std::fs::read_dir(dir)?.flatten() {
Expand All @@ -49,10 +87,11 @@ impl StartMenuManager {
Ok(items)
}

pub fn get_items() -> Result<Vec<StartMenuItem>> {
pub fn read_start_menu_folders(&mut self) -> Result<()> {
let mut items = vec![];
items.extend(Self::_get_items(&Self::common_items_path())?);
items.extend(Self::_get_items(&Self::user_items_path())?);
Ok(items)
self.list = items;
Ok(())
}
}
4 changes: 3 additions & 1 deletion src/background/modules/start/domain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::path::PathBuf;

#[derive(Debug, Clone)]
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StartMenuItem {
pub path: PathBuf,
pub umid: Option<String>,
Expand Down
4 changes: 3 additions & 1 deletion src/background/restoration_and_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ impl RestorationAndMigration {
std::fs::remove_dir_all(&old_path)?;
}

// temporal folder to group artifacts
std::fs::create_dir_all(SEELEN_COMMON.app_temp_dir())?;

let create_if_needed = move |folder: &str| -> Result<()> {
let path = data_path.join(folder);
if !path.exists() {
std::fs::create_dir_all(path)?;
}
Ok(())
};

create_if_needed("themes")?;
create_if_needed("layouts")?;
create_if_needed("placeholders")?;
Expand Down
21 changes: 10 additions & 11 deletions src/background/seelen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
state::application::{FullState, FULL_STATE},
system::{declare_system_events_handlers, release_system_events_handlers},
trace_lock,
utils::{ahk::AutoHotKey, is_running_as_appx_package, PERFORMANCE_HELPER},
utils::{ahk::AutoHotKey, is_running_as_appx_package},
windows_api::WindowsApi,
APP_HANDLE,
};
Expand Down Expand Up @@ -175,14 +175,6 @@ impl Seelen {
}

async fn start_async() -> Result<()> {
if FULL_STATE.load().is_weg_enabled() {
SeelenWeg::enumerate_all_windows()?;
}

if FULL_STATE.load().is_window_manager_enabled() {
WindowManagerV2::enumerate_all_windows()?;
}

Self::start_ahk_shortcuts()?;
Self::refresh_path_environment()?;
Self::refresh_auto_start_path().await?;
Expand Down Expand Up @@ -214,12 +206,19 @@ impl Seelen {
MonitorManager::subscribe(Self::on_monitor_event);

tauri::async_runtime::spawn(async {
trace_lock!(PERFORMANCE_HELPER).start("lazy setup");
log_error!(Self::start_async().await);
trace_lock!(PERFORMANCE_HELPER).end("lazy setup");
});

self.refresh_windows_positions()?;

if FULL_STATE.load().is_weg_enabled() {
SeelenWeg::enumerate_all_windows()?;
}

if FULL_STATE.load().is_window_manager_enabled() {
WindowManagerV2::enumerate_all_windows()?;
}

register_win_hook()?;
Ok(())
}
Expand Down
44 changes: 35 additions & 9 deletions src/background/utils/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ impl Icons {
}

pub struct SeelenCommon {
// general
resource_dir: PathBuf,
data_dir: PathBuf,
cache_dir: PathBuf,
temp_dir: PathBuf,
// specifits
history: PathBuf,
settings: PathBuf,
weg_items: PathBuf,
Expand All @@ -82,15 +88,14 @@ pub struct SeelenCommon {

impl SeelenCommon {
pub fn new() -> Self {
let handle = get_app_handle();
let data_dir = handle
.path()
.app_data_dir()
.expect("Failed to get app data dir");
let resource_dir = handle
.path()
.resource_dir()
.expect("Failed to get resource dir");
let resolver = get_app_handle().path();
let data_dir = resolver.app_data_dir().expect("Failed to get app data dir");
let resource_dir = resolver.resource_dir().expect("Failed to get resource dir");
let cache_dir = resolver.app_cache_dir().expect("Failed to get cache dir");
let temp_dir = resolver
.temp_dir()
.expect("Failed to get temp dir")
.join("com.seelen.seelen-ui");

Self {
history: data_dir.join("history"),
Expand All @@ -112,9 +117,30 @@ impl SeelenCommon {
wallpapers: data_dir.join("wallpapers"),
profiles: data_dir.join("profiles"),
bundled_profiles: resource_dir.join("static/profiles"),
// general
data_dir,
resource_dir,
cache_dir,
temp_dir,
}
}

pub fn app_resource_dir(&self) -> &Path {
&self.resource_dir
}

pub fn app_data_dir(&self) -> &Path {
&self.data_dir
}

pub fn app_cache_dir(&self) -> &Path {
&self.cache_dir
}

pub fn app_temp_dir(&self) -> &Path {
&self.temp_dir
}

pub fn settings_path(&self) -> &Path {
&self.settings
}
Expand Down

0 comments on commit 2ef2858

Please sign in to comment.