Skip to content

Commit

Permalink
perf(load): improve load time speed
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Aug 16, 2024
1 parent 245ae98 commit 844059f
Show file tree
Hide file tree
Showing 29 changed files with 258 additions and 321 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ winreg = "0.52.0"
windows-core = "=0.57.0" # windows-rs already depends and reexports this, but we need it as a direct dependency (implement macro)
win-screenshot = "4.0.8"
base64 = "0.22.1"
arc-swap = "1.7.1"

[dependencies.windows]
version = "=0.57.0"
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## [Unreleased]
### performance
- reduce load time from ~7s to ~4s

### features
- .slu and uri now are loaded correctly on seelen ui.
- allow change wallpaper from seelen settings.
Expand Down
2 changes: 1 addition & 1 deletion src/apps/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import './styles/reset.css';
import './styles/global.css';

(async function main() {
getCurrentWebviewWindow().show();
wrapConsole();
getCurrentWebviewWindow().show();
const container = getRootContainer();

await LoadSettingsToStore();
Expand Down
2 changes: 0 additions & 2 deletions src/apps/settings/modules/shared/store/storeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ export async function saveUserSettings(settings: Pick<UserSettings, 'jsonSetting

await fs.writeTextFile(yaml_route, yaml.dump(settings.yamlSettings));

await invoke('refresh_state');

if (settings.jsonSettings.ahkEnabled) {
await invoke('start_seelen_shortcuts');
} else {
Expand Down
22 changes: 7 additions & 15 deletions src/background/exposed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,17 @@ use crate::modules::notifications::infrastructure::*;
use crate::modules::power::infrastructure::*;
use crate::modules::tray::infrastructure::*;

#[command]
fn refresh_state() {
std::thread::spawn(|| {
log_error!(trace_lock!(SEELEN).refresh_state());
});
}

#[command]
fn start_seelen_shortcuts() {
std::thread::spawn(|| {
log_error!(trace_lock!(SEELEN).start_ahk_shortcuts());
log_error!(Seelen::start_ahk_shortcuts());
});
}

#[command]
fn kill_seelen_shortcuts() {
std::thread::spawn(|| {
trace_lock!(SEELEN).kill_ahk_shortcuts();
Seelen::kill_ahk_shortcuts();
});
}

Expand Down Expand Up @@ -110,17 +103,17 @@ fn get_win_version() -> WinVersion {
}
}

// https://docs.rs/tauri/latest/tauri/window/struct.WindowBuilder.html#known-issues
// https://github.com/tauri-apps/wry/issues/583
#[command]
fn show_app_settings() {
std::thread::spawn(|| {
log_error!(trace_lock!(SEELEN).show_settings());
});
async fn show_app_settings() {
log_error!(Seelen::show_settings());
}

#[command]
fn set_auto_start(enabled: bool) {
std::thread::spawn(move || {
log_error!(trace_lock!(SEELEN).set_auto_start(enabled));
log_error!(Seelen::set_auto_start(enabled));
});
}

Expand Down Expand Up @@ -164,7 +157,6 @@ pub fn register_invoke_handler(app_builder: Builder<Wry>) -> Builder<Wry> {
app_builder.invoke_handler(tauri::generate_handler![
// General
run,
refresh_state,
is_dev_mode,
open_file,
run_as_admin,
Expand Down
4 changes: 2 additions & 2 deletions src/background/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub fn process_vd_event(event: DesktopEvent) -> Result<()> {

if let DesktopEvent::WindowChanged(hwnd) = event {
if WindowsApi::is_window(hwnd) {
if let Some(config) = trace_lock!(FULL_STATE).get_app_config_by_window(hwnd) {
if let Some(config) = FULL_STATE.load().get_app_config_by_window(hwnd) {
if config.options_contains(AppExtraFlag::Pinned) && !winvd::is_pinned_window(hwnd)?
{
winvd::pin_window(hwnd)?;
Expand Down Expand Up @@ -279,7 +279,7 @@ pub extern "system" fn win_event_hook(
return;
}

let mut hook_manager = HOOK_MANAGER.lock();
let mut hook_manager = trace_lock!(HOOK_MANAGER);
hook_manager.event(event, hwnd);

if let Some(synthetic_event) = event.get_synthetic(hwnd) {
Expand Down
13 changes: 7 additions & 6 deletions src/background/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ use modules::{
tray::application::ensure_tray_overflow_creation,
};
use plugins::register_plugins;
use seelen::SEELEN;
use seelen::{Seelen, SEELEN};
use tray::try_register_tray_icon;
use utils::PERFORMANCE_HELPER;

fn register_panic_hook() {
std::panic::set_hook(Box::new(|info| {
Expand Down Expand Up @@ -70,24 +71,23 @@ fn register_panic_hook() {
fn setup(app: &mut tauri::App<tauri::Wry>) -> Result<(), Box<dyn std::error::Error>> {
log::info!("───────────────────── Starting Seelen ─────────────────────");
Client::listen_tcp()?;

// try it at start to avoid made it before
// try it at start it on open the program to avoid do it before
log_error!(ensure_tray_overflow_creation());

let mut seelen = unsafe { SEELEN.make_guard_unchecked() };
seelen.init(app.handle().clone())?;

if !tauri::is_dev() {
log_error!(seelen.show_update_modal());

Seelen::show_update_modal()?;
let command = trace_lock!(SEELEN_COMMAND_LINE).clone();
let matches = command.get_matches();
if !matches.get_flag("silent") {
log_error!(seelen.show_settings());
Seelen::show_settings()?;
}
}

seelen.start()?;

log_error!(try_register_tray_icon(app));
std::mem::forget(seelen);
Ok(())
Expand All @@ -112,6 +112,7 @@ fn app_callback(_: &tauri::AppHandle<tauri::Wry>, event: tauri::RunEvent) {
fn main() -> Result<()> {
color_eyre::install().expect("Failed to install color_eyre");
register_panic_hook();
PERFORMANCE_HELPER.lock().start();

let command = trace_lock!(SEELEN_COMMAND_LINE).clone();
let matches = match command.try_get_matches() {
Expand Down
13 changes: 7 additions & 6 deletions src/background/modules/cli/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use parking_lot::Mutex;
use windows::Win32::System::Console::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS};

use crate::error_handler::Result;
use crate::seelen::SEELEN;
use crate::seelen::{Seelen, SEELEN};
use crate::seelen_bar::FancyToolbar;
use crate::seelen_wm::WindowManager;
use crate::state::application::FULL_STATE;
Expand Down Expand Up @@ -164,9 +164,10 @@ pub fn process_uri(uri: &str) -> Result<()> {
let engine = base64::engine::general_purpose::URL_SAFE_NO_PAD;
let decoded = engine.decode(contents.as_bytes())?;

FULL_STATE
.lock()
.load_resource(serde_yaml::from_slice(&decoded)?)
let mut state = FULL_STATE.load().cloned();
state.load_resource(serde_yaml::from_slice(&decoded)?)?;
state.store();
Ok(())
}

pub fn handle_cli_events(matches: &clap::ArgMatches) -> Result<()> {
Expand All @@ -177,7 +178,7 @@ pub fn handle_cli_events(matches: &clap::ArgMatches) -> Result<()> {
if let Some((subcommand, matches)) = matches.subcommand() {
match subcommand {
"settings" => {
trace_lock!(SEELEN).show_settings()?;
Seelen::show_settings()?;
}
WindowManager::CLI_IDENTIFIER => {
if let Some(monitor) = trace_lock!(SEELEN).focused_monitor_mut() {
Expand All @@ -199,6 +200,6 @@ pub fn handle_cli_events(matches: &clap::ArgMatches) -> Result<()> {
return Ok(());
}

trace_lock!(SEELEN).show_settings()?;
Seelen::show_settings()?;
Ok(())
}
11 changes: 4 additions & 7 deletions src/background/modules/media/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ pub fn register_media_events() {
if !was_registered {
REGISTERED.store(true, Ordering::Release);
}

std::thread::spawn(move || {
let mut manager = trace_lock!(MEDIA_MANAGER);
if !was_registered {
log::info!("Registering media events");
let mut manager = trace_lock!(MEDIA_MANAGER);
log::trace!("Registering media events");
manager.on_change_devices(emit_media_devices);
manager.on_change_players(emit_media_sessions);
}

let media_manager = trace_lock!(MEDIA_MANAGER);
emit_media_devices(media_manager.inputs(), media_manager.outputs());
emit_media_sessions(media_manager.playing());
emit_media_devices(manager.inputs(), manager.outputs());
emit_media_sessions(manager.playing());
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/background/modules/network/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ fn emit_networks(ip: String, adapters: Vec<NetworkAdapter>, has_internet: bool)
static REGISTERED: AtomicBool = AtomicBool::new(false);
pub fn register_network_events() -> Result<()> {
if !REGISTERED.load(Ordering::Acquire) {
log::info!("Registering network events");
log::trace!("Registering network events");
NetworkManager::register_events(move |connectivity| {
log::info!(target: "network", "Connectivity changed: {:?}", connectivity);
log::trace!(target: "network", "Connectivity changed: {:?}", connectivity);
if let (Ok(ip), Ok(adapters)) = (get_local_ip_address(), NetworkManager::get_adapters())
{
let has_internet_ipv4 = connectivity.0 & NLM_CONNECTIVITY_IPV4_INTERNET.0
Expand Down
5 changes: 4 additions & 1 deletion src/background/modules/notifications/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ impl NotificationManager {
}

if let Err(err) = self.listener.NotificationChanged(&self.event_handler) {
log::debug!("Failed to register notification listener using NotificationChanged: {:?}, spawning thread instead", err);
log::debug!(
"Failed to use NotificationChanged: {:?}, spawning thread instead",
err
);
std::thread::spawn(|| -> Result<()> {
RELEASED.store(false, Ordering::SeqCst);
let listener = UserNotificationListener::Current()?;
Expand Down
11 changes: 7 additions & 4 deletions src/background/modules/notifications/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ fn emit_notifications(notifications: &Vec<AppNotification>) {

static REGISTERED: AtomicBool = AtomicBool::new(false);
pub fn register_notification_events() {
std::thread::spawn(|| {
let was_registered = REGISTERED.load(Ordering::Acquire);
if !was_registered {
REGISTERED.store(true, Ordering::Release);
}
std::thread::spawn(move || {
let mut manager = trace_lock!(NOTIFICATION_MANAGER);
if !REGISTERED.load(Ordering::Acquire) {
log::info!("Registering notifications events");
if !was_registered {
log::trace!("Registering notifications events");
manager.on_notifications_change(emit_notifications);
REGISTERED.store(true, Ordering::Release);
}
emit_notifications(manager.notifications());
});
Expand Down
2 changes: 1 addition & 1 deletion src/background/modules/power/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl PowerManager {
if REGISTERED.load(Ordering::Acquire) {
return Ok(());
}
log::info!("Registering system power events");
log::trace!("Registering system power events");

let wide_name: Vec<u16> = "Seelen Power Manager"
.encode_utf16()
Expand Down
15 changes: 10 additions & 5 deletions src/background/modules/system_settings/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ fn emit_colors(colors: &UIColors) {

static REGISTERED: AtomicBool = AtomicBool::new(false);
pub fn register_colors_events() {
let mut manager = trace_lock!(SYSTEM_SETTINGS);
if !REGISTERED.load(Ordering::Acquire) {
log::info!("Registering colors events");
manager.on_colors_change(Box::new(emit_colors));
let was_registered = REGISTERED.load(Ordering::Acquire);
if !was_registered {
REGISTERED.store(true, Ordering::Release);
}
emit_colors(&manager.get_colors().expect("Failed to get colors"));
std::thread::spawn(move || {
let mut manager = trace_lock!(SYSTEM_SETTINGS);
if !was_registered {
log::trace!("Registering colors events");
manager.on_colors_change(Box::new(emit_colors));
}
emit_colors(&manager.get_colors().expect("Failed to get colors"));
});
}

pub fn release_colors_events() {
Expand Down
2 changes: 1 addition & 1 deletion src/background/modules/tray/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn emit_tray_info() -> Result<()> {
static REGISTERED: AtomicBool = AtomicBool::new(false);
pub fn register_tray_events() -> Result<()> {
if !REGISTERED.load(Ordering::Acquire) {
log::info!("Registering tray events");
log::trace!("Registering tray events");
// TODO: add event listener for tray events
REGISTERED.store(true, Ordering::Release);
}
Expand Down
10 changes: 0 additions & 10 deletions src/background/modules/uwp/load_uwp_apps.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
param (
[string]$SavePath = ".\uwp-manifests.json"
)

if (Test-Path $SavePath) {
Remove-Item $SavePath
}

$packages = Get-AppxPackage
$output = @()

Expand Down Expand Up @@ -64,6 +56,4 @@ foreach ($package in $packages) {
$output += $selected
}

$output | ConvertTo-Json -Depth 3 -Compress | Out-File -FilePath $SavePath -Encoding utf8

$output | ConvertTo-Json -Depth 3 -Compress
18 changes: 7 additions & 11 deletions src/background/modules/uwp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ pub static UWP_LIGHTUNPLATED_POSTFIX: &str = "_altform-lightunplated";
pub static UWP_UNPLATED_POSTFIX: &str = "_altform-unplated";

lazy_static! {
pub static ref UWP_MANAGER: Arc<Mutex<WindowsAppsManager>> =
Arc::new(Mutex::new(WindowsAppsManager::default()));
pub static ref UWP_MANAGER: Arc<Mutex<WindowsAppsManager>> = Arc::new(Mutex::new({
let mut manager = WindowsAppsManager::default();
manager.refresh().expect("Failed to refresh UWP manager");
manager
}));
pub static ref UWP_TARGET_SIZE_POSTFIXES: Vec<&'static str> = vec![
".targetsize-256",
".targetsize-96",
Expand Down Expand Up @@ -152,17 +155,10 @@ impl WindowsAppsManager {
}

pub fn refresh(&mut self) -> Result<()> {
let mut script = PwshScript::new(include_str!("load_uwp_apps.ps1"));
let save_path = Self::get_save_path()?;

script.with_args([
"-SavePath",
save_path.to_string_lossy().trim_start_matches("\\\\?\\"),
]);

let script = PwshScript::new(include_str!("load_uwp_apps.ps1"));
let contents = tauri::async_runtime::block_on(script.execute())?;
self.packages = serde_json::from_str(&contents)?;

std::fs::write(Self::get_save_path()?, &contents)?;
Ok(())
}

Expand Down
Loading

0 comments on commit 844059f

Please sign in to comment.