Skip to content

Commit 9199ff4

Browse files
committed
nit
1 parent 7105f90 commit 9199ff4

File tree

3 files changed

+64
-65
lines changed

3 files changed

+64
-65
lines changed

src/monitor.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ struct WindowHandleState {
1010
}
1111

1212
impl WindowHandleState {
13-
pub fn new(handle: isize, transparency: u8, enabled: bool) -> Self {
13+
pub fn new(handle: isize) -> Self {
1414
Self {
1515
handle,
16-
transparency,
17-
enabled,
16+
transparency: 1,
17+
enabled: false,
1818
}
1919
}
2020

@@ -61,7 +61,7 @@ impl WindowHandleState {
6161
*/
6262
#[inline(always)]
6363
pub async fn monitor_windows(app_state: Arc<AppState>) {
64-
let refresh_interval = Duration::from_millis(120);
64+
let refresh_interval = Duration::from_millis(160);
6565

6666
let mut config = app_state.get_config().await;
6767
let mut is_enabled = app_state.is_enabled().await;
@@ -107,7 +107,8 @@ fn refresh_window_cache(config: &Config, cache: &mut HashMap<String, Vec<WindowH
107107

108108
for &handle in &handles {
109109
if !states.iter().any(|state| state.handle == handle) {
110-
states.push(WindowHandleState::new(handle, 1, false));
110+
// Add missing handles
111+
states.push(WindowHandleState::new(handle));
111112
}
112113
}
113114
states.retain(|state| handles.contains(&state.handle));

src/util.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{window_config::WindowConfig, ConfigWindow};
22
use anyhow::{anyhow, Error};
33
use directories::ProjectDirs;
44
use serde::{Deserialize, Serialize};
5+
use serde_json::from_str;
56
use slint::ComponentHandle;
67
use std::{
78
collections::HashMap,
@@ -93,7 +94,6 @@ pub fn create_config_error_window(config_path: PathBuf) -> Result<(), Error> {
9394
}
9495
Err(e) => {
9596
_ = anyhow!("AHHHHH {}", e);
96-
println!("yo");
9797
}
9898
}
9999

@@ -109,19 +109,18 @@ pub fn create_config_error_window(config_path: PathBuf) -> Result<(), Error> {
109109

110110
window.on_cancel(move || {
111111
if !*action_taken_clone.lock().unwrap() {
112-
println!("false");
113112
if let Ok(config_clone) = config_clone_cancel.lock()
114113
&& let Ok(config_json) = serde_json::to_string_pretty(&[serde_json::json!({})])
115114
{
116115
fs::write(config_clone.as_str(), config_json).expect("better not.");
117116
}
118117
}
119118

120-
if let Some(window) = window_handle.upgrade() {
121-
let _ = window.hide();
119+
if let Some(handle) = window_handle.upgrade() {
120+
_ = handle.hide();
122121
}
123122
});
124-
println!("false");
123+
125124
window.run()?;
126125
Ok(())
127126
}
@@ -139,13 +138,11 @@ pub fn load_config() -> (Config, PathBuf) {
139138
if config_path.exists()
140139
&& let Ok(config_data) = fs::read_to_string(&config_path)
141140
{
142-
if let Ok(existing) = serde_json::from_str::<Config>(&config_data) {
141+
if let Ok(existing) = from_str::<Config>(&config_data) {
143142
(existing, config_path)
144143
} else {
145-
match create_config_error_window(config_path) {
146-
Ok(_) => (),
147-
Err(e) => eprintln!("Error: {}", e),
148-
}
144+
_ = create_config_error_window(config_path);
145+
149146
load_config()
150147
}
151148
} else {

src/window_config.rs

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
win_utils::{convert_to_full, convert_to_human, make_window_transparent, WindowInfo},
33
TransparencyRule,
44
};
5-
use core::ffi::c_void;
5+
use core::{ffi::c_void, iter::once, mem::transmute};
66
use serde::{Deserialize, Serialize};
77
use std::path::Path;
88
use windows::{
@@ -36,49 +36,6 @@ pub struct WindowConfig {
3636
old_class: Option<String>,
3737
}
3838

39-
impl Default for WindowConfig {
40-
fn default() -> Self {
41-
Self {
42-
process_name: String::new(),
43-
window_class: String::new(),
44-
transparency: 255,
45-
enabled: false,
46-
force: false,
47-
old_class: None,
48-
}
49-
}
50-
}
51-
52-
impl From<&WindowConfig> for TransparencyRule {
53-
fn from(config: &WindowConfig) -> Self {
54-
TransparencyRule {
55-
process_name: config.process_name.to_owned().into(),
56-
window_class: config.window_class.to_owned().into(),
57-
transparency: convert_to_human(config.transparency).into(),
58-
enabled: config.enabled,
59-
force: config.force,
60-
old_class: config.old_class.to_owned().unwrap_or_default().into(),
61-
}
62-
}
63-
}
64-
65-
impl From<TransparencyRule> for WindowConfig {
66-
fn from(config: TransparencyRule) -> Self {
67-
WindowConfig {
68-
process_name: config.process_name.to_owned().into(),
69-
window_class: config.window_class.to_owned().into(),
70-
transparency: convert_to_full(config.transparency.try_into().unwrap_or(100)),
71-
enabled: config.enabled,
72-
force: config.force,
73-
old_class: if config.old_class.is_empty() {
74-
None
75-
} else {
76-
Some(config.old_class.into())
77-
},
78-
}
79-
}
80-
}
81-
8239
impl WindowConfig {
8340
pub fn new(info: &WindowInfo, transparency: u8) -> Self {
8441
Self {
@@ -163,7 +120,7 @@ impl WindowConfig {
163120
let wide_class: Vec<u16> = self
164121
.get_window_class()
165122
.encode_utf16()
166-
.chain(std::iter::once(0))
123+
.chain(once(0))
167124
.collect();
168125

169126
let class_ptr = PCWSTR::from_raw(wide_class.as_ptr());
@@ -180,18 +137,19 @@ impl WindowConfig {
180137
{
181138
let mut buffer = [0u8; 260];
182139
let len = GetProcessImageFileNameA(process_handle, &mut buffer);
183-
let _ = CloseHandle(process_handle);
140+
_ = CloseHandle(process_handle);
184141

185142
if len > 0 {
186143
let path_str =
187144
String::from_utf8_lossy(&buffer[..len as usize]).to_string();
188-
if let Some(name) = Path::new(&path_str)
145+
let name = Path::new(&path_str)
189146
.file_name()
190147
.and_then(|n| n.to_str())
191-
.map(|s| s.split('.').next().unwrap_or(s))
192-
{
148+
.map(|s| s.split('.').next().unwrap_or(s));
149+
150+
if let Some(name) = name {
193151
if name == self.process_name {
194-
handles.push(std::mem::transmute(hwnd));
152+
handles.push(transmute(hwnd));
195153
}
196154
}
197155
}
@@ -292,3 +250,46 @@ fn find_window_by_class(target_class: &str) -> windows::core::Result<Option<HWND
292250
Ok(None)
293251
}
294252
}
253+
254+
impl Default for WindowConfig {
255+
fn default() -> Self {
256+
Self {
257+
process_name: String::new(),
258+
window_class: String::new(),
259+
transparency: 255,
260+
enabled: false,
261+
force: false,
262+
old_class: None,
263+
}
264+
}
265+
}
266+
267+
impl From<&WindowConfig> for TransparencyRule {
268+
fn from(config: &WindowConfig) -> Self {
269+
TransparencyRule {
270+
process_name: config.process_name.to_owned().into(),
271+
window_class: config.window_class.to_owned().into(),
272+
transparency: convert_to_human(config.transparency).into(),
273+
enabled: config.enabled,
274+
force: config.force,
275+
old_class: config.old_class.to_owned().unwrap_or_default().into(),
276+
}
277+
}
278+
}
279+
280+
impl From<TransparencyRule> for WindowConfig {
281+
fn from(config: TransparencyRule) -> Self {
282+
WindowConfig {
283+
process_name: config.process_name.to_owned().into(),
284+
window_class: config.window_class.to_owned().into(),
285+
transparency: convert_to_full(config.transparency.try_into().unwrap_or(100)),
286+
enabled: config.enabled,
287+
force: config.force,
288+
old_class: if config.old_class.is_empty() {
289+
None
290+
} else {
291+
Some(config.old_class.into())
292+
},
293+
}
294+
}
295+
}

0 commit comments

Comments
 (0)