Skip to content

armgit / WacOS

Public Imported from GitHub
0

A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.

Rust
1.6 KB
Raw Blame
//! Porte de src\comum\pressao.h: a pressão de memória como a do macOS.

use windows::Win32::System::ProcessStatus::{GetPerformanceInfo, PERFORMANCE_INFORMATION};
use windows::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};

#[derive(Default, Debug, Clone, Copy)]
pub struct PressaoMemoria {
    pub valor: f32,
    pub nivel: i32,
    pub livre: f64,
    pub commit: f64,
}

pub fn medir() -> PressaoMemoria {
    let mut p = PressaoMemoria { valor: 0.0, nivel: 0, livre: 1.0, commit: 0.0 };
    unsafe {
        let mut ms = MEMORYSTATUSEX { dwLength: std::mem::size_of::<MEMORYSTATUSEX>() as u32, ..Default::default() };
        if GlobalMemoryStatusEx(&mut ms).is_err() || ms.ullTotalPhys == 0 {
            return p;
        }
        let mut pi = PERFORMANCE_INFORMATION { cb: std::mem::size_of::<PERFORMANCE_INFORMATION>() as u32, ..Default::default() };
        if GetPerformanceInfo(&mut pi, pi.cb).is_ok() && pi.CommitLimit != 0 {
            p.commit = pi.CommitTotal as f64 / pi.CommitLimit as f64;
        }
        p.livre = ms.ullAvailPhys as f64 / ms.ullTotalPhys as f64;
    }
    let critico = p.commit > 0.985 || (p.commit > 0.95 && p.livre < 0.25);
    p.nivel = if p.livre < 0.05 || critico {
        2
    } else if p.livre < 0.12 || p.commit > 0.88 {
        1
    } else {
        0
    };
    let mut v = (((0.40 - p.livre) / 0.40).max((p.commit - 0.55) / 0.45) as f32).clamp(0.06, 1.0);
    if p.nivel == 1 {
        v = v.max(0.62);
    }
    if p.nivel == 2 {
        v = v.max(0.86);
    }
    if p.nivel == 0 {
        v = v.min(0.58);
    }
    p.valor = v;
    p
}