bevy_full_throttle/src/lib.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

use bevy::{prelude::*, window::WindowFocused};
2022-01-19 20:52:52 +00:00
#[cfg(windows)]
use windows::{
core::GUID,
Win32::System::{Power, SystemServices::GUID_MIN_POWER_SAVINGS},
};
2022-01-19 20:52:52 +00:00
#[cfg(windows)]
#[derive(Resource, Deref, DerefMut)]
struct DefaultScheme(GUID);
#[cfg(not(windows))]
#[derive(Resource)]
struct DefaultScheme();
#[allow(unused_mut, unused_variables)]
fn setup(mut commands: Commands) {
2022-01-19 20:52:52 +00:00
#[cfg(windows)]
unsafe {
let mut active: *mut GUID = std::ptr::null_mut();
Power::PowerGetActiveScheme(None, &mut active);
if let Some(active) = active.as_ref() {
let scheme = DefaultScheme(*active);
commands.insert_resource(scheme);
}
}
#[cfg(not(windows))]
commands.insert_resource(DefaultScheme());
}
#[allow(unused_variables)]
fn focus_change(
config: Res<FullThrottlePlugin>,
mut focus: EventReader<WindowFocused>,
scheme: Res<DefaultScheme>,
) {
for event in focus.iter() {
if event.focused {
#[cfg(windows)]
unsafe {
2022-09-21 23:25:52 +00:00
Power::PowerSetActiveScheme(None, Some(&GUID_MIN_POWER_SAVINGS));
}
} else {
#[cfg(windows)]
if config.restore_original_scheme_on_unfocus {
unsafe {
2022-09-21 23:25:52 +00:00
Power::PowerSetActiveScheme(None, Some(&**scheme));
}
}
}
2022-01-19 20:52:52 +00:00
}
}
#[derive(Resource, Clone, Copy, Default)]
pub struct FullThrottlePlugin {
pub restore_original_scheme_on_unfocus: bool,
}
2022-01-19 20:52:52 +00:00
impl Plugin for FullThrottlePlugin {
fn build(&self, app: &mut App) {
app.insert_resource(*self)
.add_startup_system(setup)
.add_system(focus_change);
2022-01-19 20:52:52 +00:00
}
}