add window manager system
This commit is contained in:
@@ -1,32 +1,63 @@
|
||||
use crate::hardware::HardwareBridge;
|
||||
use crate::model::Color;
|
||||
use crate::model::{Color, Rect, Window, WindowId};
|
||||
use crate::prometeu_os::PrometeuOS;
|
||||
|
||||
// TODO: Mover para arquivo próprio se crescer.
|
||||
/// Sistema de janelas do PROMETEU.
|
||||
pub struct WindowSystem {
|
||||
pub theme_color: Color,
|
||||
/// Gerenciador de janelas do PROMETEU.
|
||||
pub struct WindowManager {
|
||||
pub windows: Vec<Window>,
|
||||
pub focused: Option<WindowId>,
|
||||
}
|
||||
|
||||
impl WindowSystem {
|
||||
impl WindowManager {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
theme_color: Color::INDIGO,
|
||||
windows: Vec::new(),
|
||||
focused: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_window(&mut self, title: String, viewport: Rect, color: Color) -> WindowId {
|
||||
let id = WindowId(self.windows.len() as u32);
|
||||
let window = Window {
|
||||
id,
|
||||
viewport,
|
||||
has_focus: false,
|
||||
title,
|
||||
color,
|
||||
};
|
||||
self.windows.push(window);
|
||||
id
|
||||
}
|
||||
|
||||
pub fn remove_window(&mut self, id: WindowId) {
|
||||
self.windows.retain(|w| w.id != id);
|
||||
if self.focused == Some(id) {
|
||||
self.focused = None;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_all_windows(&mut self) {
|
||||
self.windows.clear();
|
||||
self.focused = None;
|
||||
}
|
||||
|
||||
pub fn set_focus(&mut self, id: WindowId) {
|
||||
self.focused = Some(id);
|
||||
for window in &mut self.windows {
|
||||
window.has_focus = window.id == id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// PrometeuHub: Launcher e ambiente de UI do sistema.
|
||||
pub struct PrometeuHub {
|
||||
pub window_system: WindowSystem,
|
||||
pub color: Color,
|
||||
pub window_manager: WindowManager,
|
||||
}
|
||||
|
||||
impl PrometeuHub {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
window_system: WindowSystem::new(),
|
||||
color: Color::BLACK,
|
||||
window_manager: WindowManager::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,22 +66,74 @@ impl PrometeuHub {
|
||||
}
|
||||
|
||||
pub fn gui_update(&mut self, _os: &mut PrometeuOS, hw: &mut dyn HardwareBridge) {
|
||||
if hw.pad().a.down {
|
||||
self.color = Color::GREEN;
|
||||
}
|
||||
if hw.pad().b.down {
|
||||
self.color = Color::INDIGO;
|
||||
}
|
||||
if hw.pad().x.down {
|
||||
self.color = Color::YELLOW;
|
||||
}
|
||||
if hw.pad().y.down {
|
||||
self.color = Color::RED;
|
||||
hw.gfx_mut().clear(Color::BLACK);
|
||||
|
||||
let mut next_window = None;
|
||||
|
||||
if hw.pad().a.pressed {
|
||||
next_window = Some(("Green Window".to_string(), Rect { x: 0, y: 0, w: 160, h: 90 }, Color::GREEN));
|
||||
} else if hw.pad().b.pressed {
|
||||
next_window = Some(("Indigo Window".to_string(), Rect { x: 160, y: 0, w: 160, h: 90 }, Color::INDIGO));
|
||||
} else if hw.pad().x.pressed {
|
||||
next_window = Some(("Yellow Window".to_string(), Rect { x: 0, y: 90, w: 160, h: 90 }, Color::YELLOW));
|
||||
} else if hw.pad().y.pressed {
|
||||
next_window = Some(("Red Window".to_string(), Rect { x: 160, y: 90, w: 160, h: 90 }, Color::RED));
|
||||
}
|
||||
|
||||
hw.gfx_mut().clear(self.color);
|
||||
if let Some((title, rect, color)) = next_window {
|
||||
self.window_manager.remove_all_windows();
|
||||
let id = self.window_manager.add_window(title, rect, color);
|
||||
self.window_manager.set_focus(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self, _os: &mut PrometeuOS, _hw: &mut dyn HardwareBridge) {
|
||||
pub fn render(&mut self, _os: &mut PrometeuOS, hw: &mut dyn HardwareBridge) {
|
||||
for window in &self.window_manager.windows {
|
||||
hw.gfx_mut().fill_rect(
|
||||
window.viewport.x,
|
||||
window.viewport.y,
|
||||
window.viewport.w,
|
||||
window.viewport.h,
|
||||
window.color,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::model::Rect;
|
||||
|
||||
#[test]
|
||||
fn test_window_manager_focus() {
|
||||
let mut wm = WindowManager::new();
|
||||
let id1 = wm.add_window("Window 1".to_string(), Rect { x: 0, y: 0, w: 10, h: 10 }, Color::WHITE);
|
||||
let id2 = wm.add_window("Window 2".to_string(), Rect { x: 10, y: 10, w: 10, h: 10 }, Color::WHITE);
|
||||
|
||||
assert_eq!(wm.windows.len(), 2);
|
||||
assert_eq!(wm.focused, None);
|
||||
|
||||
wm.set_focus(id1);
|
||||
assert_eq!(wm.focused, Some(id1));
|
||||
assert!(wm.windows[0].has_focus);
|
||||
assert!(!wm.windows[1].has_focus);
|
||||
|
||||
wm.set_focus(id2);
|
||||
assert_eq!(wm.focused, Some(id2));
|
||||
assert!(!wm.windows[0].has_focus);
|
||||
assert!(wm.windows[1].has_focus);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_window_manager_remove_window() {
|
||||
let mut wm = WindowManager::new();
|
||||
let id = wm.add_window("Window".to_string(), Rect { x: 0, y: 0, w: 10, h: 10 }, Color::WHITE);
|
||||
wm.set_focus(id);
|
||||
assert_eq!(wm.focused, Some(id));
|
||||
|
||||
wm.remove_window(id);
|
||||
assert_eq!(wm.windows.len(), 0);
|
||||
assert_eq!(wm.focused, None);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user