Skip to content

Commit 3820c91

Browse files
committed
Pause screen capture when window is not in focus
1 parent 2abaf3d commit 3820c91

1 file changed

Lines changed: 43 additions & 25 deletions

File tree

injector/src/gui.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
use crate::injector::{self, WindowInfo};
22
use eframe::{
33
Renderer,
4-
egui::{self, ColorImage, IconData},
4+
egui::{self, ColorImage, Direction, IconData, Layout},
55
};
66
use image::{GenericImageView, ImageFormat, ImageReader};
77
use std::sync::{Arc, Mutex};
88
use std::thread;
99
use std::{io::Cursor, mem};
10-
use windows_capture::capture::{CaptureControl, Context, GraphicsCaptureApiHandler};
11-
use windows_capture::frame::Frame;
12-
use windows_capture::monitor::Monitor;
13-
use windows_capture::settings::{
14-
ColorFormat, CursorCaptureSettings, DirtyRegionSettings, DrawBorderSettings,
15-
MinimumUpdateIntervalSettings, SecondaryWindowSettings, Settings,
10+
use windows_capture::{
11+
capture::{CaptureControl, Context, GraphicsCaptureApiHandler},
12+
frame::Frame,
13+
monitor::Monitor,
14+
settings::{
15+
ColorFormat, CursorCaptureSettings, DirtyRegionSettings, DrawBorderSettings,
16+
MinimumUpdateIntervalSettings, SecondaryWindowSettings, Settings,
17+
},
1618
};
1719

18-
enum CaptureWorkerEvents {
20+
enum CaptureWorkerEvent {
1921
CAPTURE(Monitor),
2022
NONE,
2123
}
@@ -60,7 +62,7 @@ impl GraphicsCaptureApiHandler for ScreenCapture {
6062
}
6163

6264
#[derive(Debug)]
63-
enum WorkerEvents {
65+
enum InjectorWorkerEvent {
6466
UPDATE,
6567
HIDE(u32, u32, bool),
6668
SHOW(u32, u32, bool),
@@ -69,8 +71,8 @@ enum WorkerEvents {
6971
struct Gui {
7072
monitors: Vec<Monitor>,
7173
windows: Arc<Mutex<Vec<WindowInfo>>>,
72-
event_sender: crossbeam_channel::Sender<WorkerEvents>,
73-
capture_event_send: crossbeam_channel::Sender<CaptureWorkerEvents>,
74+
event_sender: crossbeam_channel::Sender<InjectorWorkerEvent>,
75+
capture_event_send: crossbeam_channel::Sender<CaptureWorkerEvent>,
7476
capture_recv: crossbeam_channel::Receiver<ColorImage>,
7577
capture_tex: Option<egui::TextureHandle>,
7678
hide_from_taskbar: bool,
@@ -88,17 +90,17 @@ impl Gui {
8890
thread::spawn(move || {
8991
for event in receiver {
9092
match event {
91-
WorkerEvents::UPDATE => {
93+
InjectorWorkerEvent::UPDATE => {
9294
println!("populating");
9395
let mut w = injector::get_top_level_windows();
9496
*windows_copy.lock().unwrap() = mem::take(&mut w);
9597
println!("populating done");
9698
}
97-
WorkerEvents::HIDE(pid, hwnd, show_on_taskbar) => {
99+
InjectorWorkerEvent::HIDE(pid, hwnd, show_on_taskbar) => {
98100
println!("wanna hide {:?}", hwnd);
99101
injector::set_window_props_with_pid(pid, hwnd, true, show_on_taskbar);
100102
}
101-
WorkerEvents::SHOW(pid, hwnd, show_on_taskbar) => {
103+
InjectorWorkerEvent::SHOW(pid, hwnd, show_on_taskbar) => {
102104
println!("wanna show {:?}", hwnd);
103105
injector::set_window_props_with_pid(pid, hwnd, false, show_on_taskbar);
104106
}
@@ -118,7 +120,7 @@ impl Gui {
118120
}
119121

120122
match event {
121-
CaptureWorkerEvents::CAPTURE(monitor) => {
123+
CaptureWorkerEvent::CAPTURE(monitor) => {
122124
let settings = Settings::new(
123125
monitor,
124126
CursorCaptureSettings::Default,
@@ -134,15 +136,15 @@ impl Gui {
134136
active_capture_control = Some(capture_control);
135137
}
136138
}
137-
CaptureWorkerEvents::NONE => (),
139+
CaptureWorkerEvent::NONE => (),
138140
}
139141
}
140142
});
141143

142144
let monitors = Monitor::enumerate().unwrap_or_default();
143145

144146
if monitors.len() > 0 {
145-
let _ = capture_event_send.send(CaptureWorkerEvents::CAPTURE(monitors[0]));
147+
let _ = capture_event_send.send(CaptureWorkerEvent::CAPTURE(monitors[0]));
146148
}
147149

148150
Gui {
@@ -161,19 +163,35 @@ impl Gui {
161163

162164
impl eframe::App for Gui {
163165
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
164-
let events = ctx.input(|i| i.events.clone());
166+
let (events, focused) = ctx.input(|i| (i.events.clone(), i.focused));
165167

166168
for event in events {
167169
if let egui::Event::WindowFocused(focused) = event {
168170
if focused {
169171
println!("focused");
170-
self.event_sender.send(WorkerEvents::UPDATE).unwrap();
172+
self.event_sender.send(InjectorWorkerEvent::UPDATE).unwrap();
173+
if self.show_desktop_preview {
174+
let _ = self.capture_event_send.send(CaptureWorkerEvent::CAPTURE(
175+
self.monitors[self.active_monitor],
176+
));
177+
}
171178
} else {
172-
println!("unfocused");
179+
let _ = self.capture_event_send.send(CaptureWorkerEvent::NONE);
173180
}
174181
}
175182
}
183+
176184
egui::CentralPanel::default().show(ctx, |ui| {
185+
if !focused {
186+
ui.with_layout(
187+
Layout::centered_and_justified(Direction::LeftToRight),
188+
|ui| {
189+
ui.label(":)");
190+
},
191+
);
192+
return;
193+
}
194+
177195
egui::ScrollArea::vertical().show(ui, |ui| {
178196
if self.show_desktop_preview {
179197
ui.heading("Desktop Preview");
@@ -190,7 +208,7 @@ impl eframe::App for Gui {
190208
self.active_monitor = i;
191209
let _ = self
192210
.capture_event_send
193-
.send(CaptureWorkerEvents::CAPTURE(*monitor));
211+
.send(CaptureWorkerEvent::CAPTURE(*monitor));
194212
}
195213
}
196214
});
@@ -224,13 +242,13 @@ impl eframe::App for Gui {
224242
ui.checkbox(&mut window_info.hidden, &window_info.title);
225243
if checkbox_response.changed() {
226244
let event = if window_info.hidden {
227-
WorkerEvents::HIDE(
245+
InjectorWorkerEvent::HIDE(
228246
window_info.pid,
229247
window_info.hwnd,
230248
self.hide_from_taskbar,
231249
)
232250
} else {
233-
WorkerEvents::SHOW(
251+
InjectorWorkerEvent::SHOW(
234252
window_info.pid,
235253
window_info.hwnd,
236254
self.hide_from_taskbar,
@@ -246,10 +264,10 @@ impl eframe::App for Gui {
246264
ui.checkbox(&mut self.show_desktop_preview, "Show desktop preview");
247265
if preview_checkbox_response.changed() {
248266
let event = if self.show_desktop_preview {
249-
CaptureWorkerEvents::CAPTURE(self.monitors[self.active_monitor])
267+
CaptureWorkerEvent::CAPTURE(self.monitors[self.active_monitor])
250268
} else {
251269
self.capture_tex = None;
252-
CaptureWorkerEvents::NONE
270+
CaptureWorkerEvent::NONE
253271
};
254272
self.capture_event_send.send(event).unwrap();
255273
}

0 commit comments

Comments
 (0)