fix: Restore original scheme on exit.

This commit is contained in:
Nolan Darilek 2023-02-12 10:44:26 -06:00 committed by Nolan Darilek
parent 1e4559f181
commit 363268e61e
2 changed files with 19 additions and 2 deletions

View File

@ -10,6 +10,7 @@ edition = "2021"
[dependencies] [dependencies]
bevy = { version = "0.9", default-features = false } bevy = { version = "0.9", default-features = false }
ctrlc = { version = "3", features = ["termination"] }
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
windows = { version = "0.44", features = ["Win32_Foundation", "Win32_System_Power", "Win32_System_Registry", "Win32_System_SystemServices"] } windows = { version = "0.44", features = ["Win32_Foundation", "Win32_System_Power", "Win32_System_Registry", "Win32_System_SystemServices"] }

View File

@ -1,4 +1,4 @@
use bevy::{prelude::*, window::WindowFocused}; use bevy::{app::AppExit, prelude::*, window::WindowFocused};
#[cfg(windows)] #[cfg(windows)]
use windows::{ use windows::{
core::GUID, core::GUID,
@ -22,6 +22,11 @@ fn setup(mut commands: Commands) {
if let Some(active) = active.as_ref() { if let Some(active) = active.as_ref() {
let scheme = DefaultScheme(*active); let scheme = DefaultScheme(*active);
commands.insert_resource(scheme); commands.insert_resource(scheme);
ctrlc::set_handler(move || {
Power::PowerSetActiveScheme(None, Some(active));
std::process::exit(1);
})
.expect("Failed to set exit handler");
} }
} }
#[cfg(not(windows))] #[cfg(not(windows))]
@ -51,6 +56,16 @@ fn focus_change(
} }
} }
#[allow(unused_variables)]
fn exit(mut exit: EventReader<AppExit>, scheme: Res<DefaultScheme>) {
for event in exit.iter() {
#[cfg(windows)]
unsafe {
Power::PowerSetActiveScheme(None, Some(&**scheme));
}
}
}
#[derive(Resource, Clone, Copy, Default)] #[derive(Resource, Clone, Copy, Default)]
pub struct FullThrottlePlugin { pub struct FullThrottlePlugin {
pub restore_original_scheme_on_unfocus: bool, pub restore_original_scheme_on_unfocus: bool,
@ -60,6 +75,7 @@ impl Plugin for FullThrottlePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(*self) app.insert_resource(*self)
.add_startup_system(setup) .add_startup_system(setup)
.add_system(focus_change); .add_system(focus_change)
.add_system_to_stage(CoreStage::PostUpdate, exit);
} }
} }