#![forbid(unsafe_code)]
use std::cell::Cell;
use std::time::Instant;
use mgui_core::{KeyCode, KeyModifiers, MouseButton, Point, Rect, Size};
pub use mgui_core::{ClipboardProvider, NoopClipboard};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ShellWindowId(pub u64);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ShellMetrics {
pub logical_size: Size,
pub scale_factor: f32,
}
impl ShellMetrics {
pub fn new(logical_size: Size, scale_factor: f32) -> Self {
Self {
logical_size: Size::new(logical_size.width.max(1.0), logical_size.height.max(1.0)),
scale_factor: scale_factor.max(0.01),
}
}
pub fn from_physical(physical_width: u32, physical_height: u32, scale_factor: f32) -> Self {
let scale_factor = scale_factor.max(0.01);
Self::new(Size::new(physical_width as f32 / scale_factor, physical_height as f32 / scale_factor), scale_factor)
}
}
impl Default for ShellMetrics {
fn default() -> Self {
Self::new(Size::new(800.0, 600.0), 1.0)
}
}
pub trait WindowShell {
fn metrics(&self) -> ShellMetrics;
fn request_redraw(&self);
fn set_title(&self, title: &str);
fn set_visible(&self, visible: bool);
fn set_ime_allowed(&self, allowed: bool);
fn set_ime_cursor_area(&self, rect: Rect);
}
#[derive(Clone, Debug, PartialEq)]
pub enum ShellEvent {
CursorMoved {
position: Point,
},
MouseInput {
position: Point,
button: MouseButton,
pressed: bool,
modifiers: KeyModifiers,
},
Wheel {
position: Point,
delta: Point,
modifiers: KeyModifiers,
},
ModifiersChanged(KeyModifiers),
Keyboard {
key: KeyCode,
pressed: bool,
repeat: bool,
modifiers: KeyModifiers,
},
Focused(bool),
Ime(ImeEvent),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ImeEvent {
Enabled,
Disabled,
Preedit(String, Option<(usize, usize)>),
Commit(String),
}
pub trait Clock {
fn now_ms(&self) -> u64;
}
#[derive(Clone, Debug)]
pub struct ManualClock {
now_ms: Cell<u64>,
}
impl ManualClock {
pub fn new(start_ms: u64) -> Self {
Self { now_ms: Cell::new(start_ms) }
}
pub fn set(&self, now_ms: u64) {
self.now_ms.set(now_ms);
}
pub fn advance(&self, delta_ms: u64) -> u64 {
let next = self.now_ms.get().saturating_add(delta_ms);
self.now_ms.set(next);
next
}
}
impl Default for ManualClock {
fn default() -> Self {
Self::new(0)
}
}
impl Clock for ManualClock {
fn now_ms(&self) -> u64 {
self.now_ms.get()
}
}
#[derive(Debug)]
pub struct SystemClock {
start: Instant,
}
impl SystemClock {
pub fn new() -> Self {
Self { start: Instant::now() }
}
}
impl Default for SystemClock {
fn default() -> Self {
Self::new()
}
}
impl Clock for SystemClock {
fn now_ms(&self) -> u64 {
self.start.elapsed().as_millis() as u64
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn manual_clock_is_deterministic() {
let clock = ManualClock::new(100);
assert_eq!(clock.now_ms(), 100);
assert_eq!(clock.advance(16), 116);
clock.set(500);
assert_eq!(clock.now_ms(), 500);
}
#[test]
fn metrics_clamp_invalid_values() {
let m = ShellMetrics::new(Size::new(0.0, -5.0), 0.0);
assert_eq!(m.logical_size, Size::new(1.0, 1.0));
assert_eq!(m.scale_factor, 0.01);
}
#[test]
fn metrics_from_physical_divides_by_dpr() {
let m = ShellMetrics::from_physical(1920, 1080, 2.0);
assert_eq!(m.logical_size, Size::new(960.0, 540.0));
assert_eq!(m.scale_factor, 2.0);
}
}