Skip to content

Commit

Permalink
Code enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Jun 28, 2024
1 parent 701945f commit 150a99c
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 48 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ features = [
# If you use cargo directly instead of tauri's cli you can use this feature flag to switch between tauri's `dev` and `build` modes.
custom-protocol = ["tauri/custom-protocol"]
devtools = ["tauri/devtools"]
trace_lock = []
25 changes: 17 additions & 8 deletions src/background/apps_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize};
use windows::Win32::Foundation::HWND;

use crate::{error_handler::Result, windows_api::WindowsApi};
use crate::{error_handler::Result, trace_lock, windows_api::WindowsApi};

lazy_static! {
pub static ref REGEX_IDENTIFIERS: Arc<Mutex<HashMap<String, Regex>>> =
Expand Down Expand Up @@ -77,7 +77,7 @@ impl AppIdentifier {
if matches!(self.matching_strategy, MatchingStrategy::Regex) {
let result = Regex::new(&self.id);
if let Ok(re) = result {
let mut regex_identifiers = REGEX_IDENTIFIERS.lock();
let mut regex_identifiers = trace_lock!(REGEX_IDENTIFIERS);
regex_identifiers.insert(self.id.clone(), re);
}
}
Expand Down Expand Up @@ -109,7 +109,7 @@ impl AppIdentifier {
AppIdentifierType::Exe => exe.contains(&self.id),
AppIdentifierType::Path => path.contains(&self.id),
},
MatchingStrategy::Regex => match REGEX_IDENTIFIERS.lock().get(&self.id) {
MatchingStrategy::Regex => match trace_lock!(REGEX_IDENTIFIERS).get(&self.id) {
Some(re) => match self.kind {
AppIdentifierType::Title => re.is_match(title),
AppIdentifierType::Class => re.is_match(class),
Expand Down Expand Up @@ -199,7 +199,7 @@ impl AppsConfigurations {

pub fn load(&mut self) -> Result<()> {
log::trace!("Loading apps configurations from {:?}", self.user_path);
REGEX_IDENTIFIERS.lock().clear();
trace_lock!(REGEX_IDENTIFIERS).clear();
self.cache.clear();
self.apps.clear();

Expand Down Expand Up @@ -236,16 +236,25 @@ impl AppsConfigurations {
None
}
} */
for app in self.apps.iter() {
if app.match_window(hwnd) {
return Option::from(app);

if let (title, Ok(path), Ok(exe), Ok(class)) = (
WindowsApi::get_window_text(hwnd),
WindowsApi::exe_path(hwnd),
WindowsApi::exe(hwnd),
WindowsApi::get_class(hwnd),
) {
for app in self.apps.iter() {
if app.identifier.validate(&title, &class, &exe, &path) {
return Option::from(app);
}
}
}

None
}
}

#[tauri::command]
pub fn reload_apps_configurations() {
std::thread::spawn(|| -> Result<()> { SETTINGS_BY_APP.lock().load() });
std::thread::spawn(|| -> Result<()> { trace_lock!(SETTINGS_BY_APP).load() });
}
14 changes: 7 additions & 7 deletions src/background/exposed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::seelen_wm::handler::*;
use crate::system::brightness::*;
use crate::utils::{is_windows_10, is_windows_11};
use crate::windows_api::WindowsApi;
use crate::{apps_config::*, log_error};
use crate::{apps_config::*, log_error, trace_lock};

use crate::modules::network::infrastructure::*;
use crate::modules::power::infrastructure::*;
Expand Down Expand Up @@ -76,21 +76,21 @@ pub fn set_volume_level(level: f32) -> Result<(), String> {
#[command]
fn refresh_state() {
std::thread::spawn(|| {
log_error!(SEELEN.lock().refresh_state());
log_error!(trace_lock!(SEELEN).refresh_state());
});
}

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

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

Expand Down Expand Up @@ -134,7 +134,7 @@ fn is_dev_mode() -> bool {
#[command]
fn ensure_hitboxes_zorder() {
std::thread::spawn(|| -> Result<()> {
let seelen = SEELEN.lock();
let seelen = trace_lock!(SEELEN);
for monitor in seelen.monitors() {
if let Some(toolbar) = monitor.toolbar() {
toolbar.ensure_hitbox_zorder()?;
Expand Down Expand Up @@ -187,14 +187,14 @@ fn get_win_version() -> WinVersion {
#[command]
fn show_app_settings() {
std::thread::spawn(|| {
log_error!(SEELEN.lock().show_settings());
log_error!(trace_lock!(SEELEN).show_settings());
});
}

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

Expand Down
7 changes: 4 additions & 3 deletions src/background/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
log_error,
seelen::{Seelen, SEELEN},
seelen_weg::{SeelenWeg, TASKBAR_CLASS},
trace_lock,
utils::{constants::IGNORE_FOCUS_AND_FULLSCREEN, is_windows_11},
windows_api::WindowsApi,
winevent::WinEvent,
Expand Down Expand Up @@ -118,7 +119,7 @@ impl HookManager {
return;
}

let mut seelen = SEELEN.lock();
let mut seelen = trace_lock!(SEELEN);
log_error!(seelen.process_win_event(event, origin));

for monitor in seelen.monitors_mut() {
Expand All @@ -138,7 +139,7 @@ impl HookManager {
}

pub fn process_vd_event(event: DesktopEvent) -> Result<()> {
let mut seelen = SEELEN.lock();
let mut seelen = trace_lock!(SEELEN);
for monitor in seelen.monitors_mut() {
if let Some(wm) = monitor.wm_mut() {
log_error!(wm.process_vd_event(&event));
Expand Down Expand Up @@ -179,7 +180,7 @@ pub fn process_vd_event(event: DesktopEvent) -> Result<()> {

if let DesktopEvent::WindowChanged(hwnd) = event {
if WindowsApi::is_window(hwnd) {
if let Some(config) = SETTINGS_BY_APP.lock().get_by_window(hwnd) {
if let Some(config) = trace_lock!(SETTINGS_BY_APP).get_by_window(hwnd) {
if config.options_contains(AppExtraFlag::Pinned) && !winvd::is_pinned_window(hwnd)?
{
winvd::pin_window(hwnd)?;
Expand Down
2 changes: 1 addition & 1 deletion src/background/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn app_callback(_: &tauri::AppHandle<tauri::Wry>, event: tauri::RunEvent) {
}
}
tauri::RunEvent::Exit => {
let seelen = SEELEN.lock();
let seelen = trace_lock!(SEELEN);
if seelen.initialized {
log::info!("───────────────────── Exiting Seelen ─────────────────────");
log_error!(seelen.stop());
Expand Down
9 changes: 5 additions & 4 deletions src/background/modules/cli/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::error_handler::Result;
use crate::seelen::SEELEN;
use crate::seelen_bar::FancyToolbar;
use crate::seelen_wm::WindowManager;
use crate::trace_lock;

#[macro_export]
macro_rules! get_subcommands {
Expand Down Expand Up @@ -139,17 +140,17 @@ pub fn handle_cli_events(matches: &clap::ArgMatches) -> Result<()> {
if let Some((subcommand, matches)) = matches.subcommand() {
match subcommand {
"settings" => {
SEELEN.lock().show_settings()?;
trace_lock!(SEELEN).show_settings()?;
}
WindowManager::CLI_IDENTIFIER => {
if let Some(monitor) = SEELEN.lock().focused_monitor_mut() {
if let Some(monitor) = trace_lock!(SEELEN).focused_monitor_mut() {
if let Some(wm) = monitor.wm_mut() {
wm.process(matches)?;
}
}
}
FancyToolbar::CLI_IDENTIFIER => {
let mut seelen = SEELEN.lock();
let mut seelen = trace_lock!(SEELEN);
for monitor in seelen.monitors_mut() {
if let Some(toolbar) = monitor.toolbar_mut() {
toolbar.process(matches)?;
Expand All @@ -162,7 +163,7 @@ pub fn handle_cli_events(matches: &clap::ArgMatches) -> Result<()> {
}

if !tauri::is_dev() {
SEELEN.lock().show_settings()?;
trace_lock!(SEELEN).show_settings()?;
}
Ok(())
}
9 changes: 5 additions & 4 deletions src/background/seelen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
seelen_wm::WindowManager,
state::State,
system::declare_system_events_handlers,
trace_lock,
utils::{ahk::AutoHotKey, sleep_millis},
windows_api::WindowsApi,
};
Expand Down Expand Up @@ -96,7 +97,7 @@ impl Seelen {
.resolve(".config\\seelen\\settings.json", BaseDirectory::Home)?;
self.state = State::new(&path).unwrap_or_default();

let mut settings_by_app = SETTINGS_BY_APP.lock();
let mut settings_by_app = trace_lock!(SETTINGS_BY_APP);
settings_by_app.set_paths(
app.path()
.resolve(".config\\seelen\\applications.yml", BaseDirectory::Home)?,
Expand Down Expand Up @@ -129,7 +130,7 @@ impl Seelen {
let mut all_ready = false;
while !all_ready {
sleep_millis(10);
all_ready = SEELEN.lock().monitors().iter().all(|m| m.is_ready());
all_ready = trace_lock!(SEELEN).monitors().iter().all(|m| m.is_ready());
}

log::trace!("Enumerating windows");
Expand Down Expand Up @@ -404,7 +405,7 @@ impl Seelen {
_rect_clip: *mut RECT,
_lparam: LPARAM,
) -> BOOL {
let mut seelen = SEELEN.lock();
let mut seelen = trace_lock!(SEELEN);
match Monitor::new(hmonitor, &seelen.state) {
Ok(monitor) => seelen.monitors.push(monitor),
Err(err) => log::error!("Failed to create monitor: {:?}", err),
Expand All @@ -413,7 +414,7 @@ impl Seelen {
}

unsafe extern "system" fn enum_windows_proc(hwnd: HWND, _: LPARAM) -> BOOL {
let mut seelen = SEELEN.lock();
let mut seelen = trace_lock!(SEELEN);
for monitor in seelen.monitors_mut() {
if let Some(weg) = monitor.weg_mut() {
if SeelenWeg::is_real_window(hwnd, false) {
Expand Down
8 changes: 3 additions & 5 deletions src/background/seelen_weg/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Deserialize;
use tauri::{command, Manager};
use tauri_plugin_shell::ShellExt;

use crate::{error_handler::Result, seelen::SEELEN, windows_api::WindowsApi};
use crate::{error_handler::Result, seelen::SEELEN, trace_lock, windows_api::WindowsApi};
use windows::Win32::{
Foundation::{HWND, LPARAM, WPARAM},
UI::WindowsAndMessaging::{PostMessageW, SW_MINIMIZE, SW_RESTORE, WM_CLOSE},
Expand Down Expand Up @@ -33,8 +33,7 @@ pub fn weg_request_update_previews(hwnds: Vec<Args>) -> Result<(), String> {
image
.save_with_format(&output_path, ImageFormat::Png)
.expect("could not save image");
SEELEN
.lock()
trace_lock!(SEELEN)
.handle()
.emit(format!("weg-preview-update-{}", hwnd.0).as_str(), ())
.expect("could not emit event");
Expand Down Expand Up @@ -72,8 +71,7 @@ pub fn weg_toggle_window_state(hwnd: isize, exe_path: String) {
WindowsApi::show_window(hwnd, SW_MINIMIZE)?;
}
} else {
SEELEN
.lock()
trace_lock!(SEELEN)
.handle()
.shell()
.command("explorer")
Expand Down
3 changes: 2 additions & 1 deletion src/background/seelen_weg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use windows::Win32::{
use crate::{
error_handler::Result,
seelen::{get_app_handle, SEELEN},
trace_lock,
utils::are_overlaped,
windows_api::{AppBarData, AppBarDataState, WindowsApi},
};
Expand Down Expand Up @@ -341,7 +342,7 @@ impl SeelenWeg {

window.once("complete-setup", move |_event| {
std::thread::spawn(move || {
if let Some(monitor) = SEELEN.lock().monitor_by_id_mut(monitor_id) {
if let Some(monitor) = trace_lock!(SEELEN).monitor_by_id_mut(monitor_id) {
if let Some(weg) = monitor.weg_mut() {
weg.ready = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/background/seelen_wm/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use serde::{Deserialize, Serialize};
use windows::Win32::Foundation::HWND;

use crate::error_handler::Result;
use crate::get_subcommands;
use crate::seelen::SEELEN;
use crate::utils::sleep_millis;
use crate::utils::virtual_desktop::VirtualDesktopManager;
use crate::windows_api::WindowsApi;
use crate::{get_subcommands, trace_lock};

use super::WindowManager;

Expand Down Expand Up @@ -91,7 +91,7 @@ impl WindowManager {
}
SubCommand::Resume => {
self.pause(false, true)?;
SEELEN.lock().start_ahk_shortcuts()?;
trace_lock!(SEELEN).start_ahk_shortcuts()?;
}
SubCommand::SwitchWorkspace(index) => {
let desktops = VirtualDesktopManager::enum_virtual_desktops()?;
Expand Down
4 changes: 2 additions & 2 deletions src/background/seelen_wm/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use windows::Win32::{
},
};

use crate::{seelen::SEELEN, utils::rect::Rect, windows_api::WindowsApi};
use crate::{seelen::SEELEN, trace_lock, utils::rect::Rect, windows_api::WindowsApi};

#[tauri::command]
pub fn set_window_position(hwnd: isize, rect: Rect) -> Result<(), String> {
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn bounce_handle(webview: Webview<Wry>, hwnd: isize) {
let monitor_id = monitor_id.parse::<isize>().expect("Invalid monitor ID");

std::thread::spawn(move || {
if let Some(monitor) = SEELEN.lock().monitor_by_id_mut(monitor_id) {
if let Some(monitor) = trace_lock!(SEELEN).monitor_by_id_mut(monitor_id) {
if let Some(wm) = monitor.wm_mut() {
wm.bounce_handle(HWND(hwnd));
}
Expand Down
5 changes: 3 additions & 2 deletions src/background/seelen_wm/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use winvd::DesktopEvent;
use crate::{
error_handler::Result,
seelen::SEELEN,
trace_lock,
utils::{constants::FORCE_RETILING_AFTER_ADD, sleep_millis},
windows_api::WindowsApi,
winevent::WinEvent,
Expand Down Expand Up @@ -70,7 +71,7 @@ impl WindowManager {
// Todo search a better way to do this
std::thread::spawn(|| -> Result<()> {
sleep_millis(250);
if let Some(monitor) = SEELEN.lock().focused_monitor() {
if let Some(monitor) = trace_lock!(SEELEN).focused_monitor() {
monitor.wm().as_ref().unwrap().force_retiling()?
}
Ok(())
Expand All @@ -86,7 +87,7 @@ impl WindowManager {
// Todo search a better way to do this
std::thread::spawn(|| -> Result<()> {
sleep_millis(250);
if let Some(monitor) = SEELEN.lock().focused_monitor() {
if let Some(monitor) = trace_lock!(SEELEN).focused_monitor() {
monitor.wm().as_ref().unwrap().force_retiling()?
}
Ok(())
Expand Down
Loading

0 comments on commit 150a99c

Please sign in to comment.