use eframe::egui;
mod agent_tools;
pub mod app;
mod agents_tab;
pub mod headless;
mod inference_tab;
mod profile_tab;
mod soc;
mod system_summary;
mod theme;
pub mod widgets;
pub use app::IronMonitorApp;
#[derive(Clone, Copy, Debug, PartialEq)]
struct ScreenRoom {
work_area: egui::Rect,
frame: egui::Vec2,
}
impl ScreenRoom {
fn max_inner_size(&self) -> egui::Vec2 {
(self.work_area.size() - self.frame).max(egui::Vec2::ZERO)
}
}
pub(crate) static MAX_INNER_SIZE: std::sync::OnceLock<egui::Vec2> = std::sync::OnceLock::new();
pub(crate) static DISPLAY_PPI: std::sync::OnceLock<f32> = std::sync::OnceLock::new();
#[cfg(any(windows, target_os = "macos"))]
const PLAUSIBLE_PPI: std::ops::RangeInclusive<f32> = 50.0..=600.0;
#[cfg(windows)]
fn display_ppi() -> Option<f32> {
const HORZSIZE: i32 = 4;
const DESKTOPHORZRES: i32 = 118;
unsafe extern "system" {
fn GetDC(hwnd: isize) -> isize;
fn ReleaseDC(hwnd: isize, hdc: isize) -> i32;
}
#[link(name = "gdi32")]
unsafe extern "system" {
fn GetDeviceCaps(hdc: isize, index: i32) -> i32;
}
let (width_mm, width_px) = unsafe {
let dc = GetDC(0);
if dc == 0 {
return None;
}
let read = (
GetDeviceCaps(dc, HORZSIZE),
GetDeviceCaps(dc, DESKTOPHORZRES),
);
ReleaseDC(0, dc);
read
};
if width_mm <= 0 || width_px <= 0 {
return None;
}
Some(width_px as f32 / (width_mm as f32 / 25.4)).filter(|ppi| PLAUSIBLE_PPI.contains(ppi))
}
#[cfg(target_os = "macos")]
fn display_ppi() -> Option<f32> {
#[repr(C)]
struct CgSize {
width: f64,
height: f64,
}
#[link(name = "CoreGraphics", kind = "framework")]
unsafe extern "C" {
fn CGMainDisplayID() -> u32;
fn CGDisplayScreenSize(display: u32) -> CgSize;
}
let mtm = objc2::MainThreadMarker::new()?;
let screen = objc2_app_kit::NSScreen::mainScreen(mtm)?;
let width_px = screen.frame().size.width * screen.backingScaleFactor();
let size = unsafe { CGDisplayScreenSize(CGMainDisplayID()) };
if size.width <= 0.0 || width_px <= 0.0 {
return None;
}
Some((width_px / (size.width / 25.4)) as f32).filter(|ppi| PLAUSIBLE_PPI.contains(ppi))
}
#[cfg(not(any(windows, target_os = "macos")))]
fn display_ppi() -> Option<f32> {
None
}
#[cfg(windows)]
fn screen_room() -> Option<ScreenRoom> {
#[repr(C)]
#[derive(Default)]
struct Rect {
left: i32,
top: i32,
right: i32,
bottom: i32,
}
const SPI_GETWORKAREA: u32 = 0x0030;
const SM_CYCAPTION: i32 = 4;
const SM_CXSIZEFRAME: i32 = 32;
const SM_CYSIZEFRAME: i32 = 33;
const SM_CXPADDEDBORDER: i32 = 92;
unsafe extern "system" {
fn GetSystemMetrics(index: i32) -> i32;
fn SystemParametersInfoW(action: u32, param: u32, value: *mut Rect, ini: u32) -> i32;
}
let mut area = Rect::default();
let (ok, caption, frame_x, frame_y, padding) = unsafe {
(
SystemParametersInfoW(SPI_GETWORKAREA, 0, &mut area, 0),
GetSystemMetrics(SM_CYCAPTION),
GetSystemMetrics(SM_CXSIZEFRAME),
GetSystemMetrics(SM_CYSIZEFRAME),
GetSystemMetrics(SM_CXPADDEDBORDER),
)
};
if ok == 0 || area.right <= area.left || area.bottom <= area.top {
return None;
}
Some(ScreenRoom {
work_area: egui::Rect::from_min_max(
egui::pos2(area.left as f32, area.top as f32),
egui::pos2(area.right as f32, area.bottom as f32),
),
frame: egui::vec2(
(2 * (frame_x + padding)) as f32,
(caption + 2 * (frame_y + padding)) as f32,
),
})
}
#[cfg(target_os = "macos")]
fn screen_room() -> Option<ScreenRoom> {
use objc2_app_kit::{NSScreen, NSWindow, NSWindowStyleMask};
let mtm = objc2::MainThreadMarker::new()?;
let screen = NSScreen::mainScreen(mtm)?;
let full = screen.frame();
let visible = screen.visibleFrame();
if visible.size.width <= 0.0 || visible.size.height <= 0.0 {
return None;
}
let style = NSWindowStyleMask::Titled
| NSWindowStyleMask::Closable
| NSWindowStyleMask::Miniaturizable
| NSWindowStyleMask::Resizable;
let outer = NSWindow::frameRectForContentRect_styleMask(full, style, mtm);
let top = full.size.height - (visible.origin.y + visible.size.height);
Some(ScreenRoom {
work_area: egui::Rect::from_min_size(
egui::pos2(visible.origin.x as f32, top as f32),
egui::vec2(visible.size.width as f32, visible.size.height as f32),
),
frame: egui::vec2(
(outer.size.width - full.size.width).max(0.0) as f32,
(outer.size.height - full.size.height).max(0.0) as f32,
),
})
}
#[cfg(not(any(windows, target_os = "macos")))]
fn screen_room() -> Option<ScreenRoom> {
None
}
fn opening_geometry(
preferred: egui::Vec2,
room: Option<ScreenRoom>,
) -> (egui::Vec2, Option<egui::Pos2>) {
let Some(room) = room else {
return (preferred, None);
};
let size = preferred.min(room.max_inner_size());
if size == preferred {
return (size, None);
}
let outer = size + room.frame;
let position =
room.work_area.min + ((room.work_area.size() - outer) * 0.5).max(egui::Vec2::ZERO);
(size, Some(position))
}
pub fn run() -> Result<(), eframe::Error> {
const PREFERRED_WIDTH: f32 = 1400.0;
const PREFERRED_HEIGHT: f32 = 900.0;
let preferred = egui::vec2(PREFERRED_WIDTH, PREFERRED_HEIGHT);
let room = screen_room();
if let Some(room) = room {
let _ = MAX_INNER_SIZE.set(room.max_inner_size());
}
if let Some(ppi) = display_ppi() {
let _ = DISPLAY_PPI.set(ppi);
}
let (size, position) = opening_geometry(preferred, room);
let mut viewport = egui::ViewportBuilder::default()
.with_inner_size([size.x, size.y])
.with_min_inner_size([800.0_f32.min(size.x), 600.0_f32.min(size.y)])
.with_title("IronMonitor")
.with_icon(load_icon());
if let Some(position) = position {
viewport = viewport.with_position(position);
}
let options = eframe::NativeOptions {
viewport,
hardware_acceleration: eframe::HardwareAcceleration::Preferred,
vsync: true,
..Default::default()
};
eframe::run_native(
"IronMonitor",
options,
Box::new(|cc| Ok(Box::new(IronMonitorApp::new(cc)))),
)
}
fn load_icon() -> egui::IconData {
let size = 32;
let mut rgba = vec![0u8; size * size * 4];
for y in 0..size {
for x in 0..size {
let idx = (y * size + x) * 4;
let in_border = x < 2 || x >= size - 2 || y < 2 || y >= size - 2;
let in_s = (y < size / 3 && (x > size / 4 || y < 4))
|| (y >= size / 3 && y < 2 * size / 3 && x > size / 4 && x < 3 * size / 4)
|| (y >= 2 * size / 3 && (x < 3 * size / 4 || y >= size - 4));
if in_border || in_s {
rgba[idx] = 0; rgba[idx + 1] = 255; rgba[idx + 2] = 255; rgba[idx + 3] = 255; }
}
}
egui::IconData {
rgba,
width: size as u32,
height: size as u32,
}
}
#[cfg(test)]
mod opening_geometry_tests {
use super::*;
fn laptop() -> ScreenRoom {
ScreenRoom {
work_area: egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1280.0, 672.0)),
frame: egui::vec2(16.0, 39.0),
}
}
#[test]
fn the_window_fits_inside_the_work_area_on_a_13_inch_laptop() {
let (size, position) = opening_geometry(egui::vec2(1400.0, 900.0), Some(laptop()));
let position = position.expect("a clamped window is placed, not cascaded");
let outer = egui::Rect::from_min_size(position, size + laptop().frame);
assert!(
laptop().work_area.contains_rect(outer),
"window {outer:?} leaves the work area {:?}",
laptop().work_area
);
assert_eq!(size, egui::vec2(1264.0, 633.0));
}
#[test]
fn a_window_that_already_fits_is_left_where_the_system_puts_it() {
let desktop = ScreenRoom {
work_area: egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(2560.0, 1392.0)),
frame: egui::vec2(16.0, 39.0),
};
let preferred = egui::vec2(1400.0, 900.0);
assert_eq!(
opening_geometry(preferred, Some(desktop)),
(preferred, None)
);
}
#[test]
fn an_unreadable_screen_opens_at_the_preferred_size() {
let preferred = egui::vec2(1400.0, 900.0);
assert_eq!(opening_geometry(preferred, None), (preferred, None));
}
}