pub mod components;
pub mod forms;
pub mod events;
pub mod themes;
pub use components::*;
pub use forms::*;
pub use events::*;
pub use themes::*;
pub struct VisualApplication {
name: String,
width: u32,
height: u32,
forms: Vec<Box<dyn Form>>,
main_form_id: Option<String>,
current_theme: Option<Box<dyn Theme>>,
}
impl VisualApplication {
pub fn new(name: &str, width: u32, height: u32) -> Self {
Self {
name: name.to_string(),
width,
height,
forms: Vec::new(),
main_form_id: None,
current_theme: None,
}
}
pub fn add_form(&mut self, form: Box<dyn Form>) {
self.forms.push(form);
}
pub fn get_form(&self, form_id: &str) -> Option<&dyn Form> {
self.forms.iter()
.find(|f| f.id() == form_id)
.map(|f| &**f as &dyn Form)
}
pub fn set_main_form(&mut self, form_id: &str) {
self.main_form_id = Some(form_id.to_string());
}
pub fn apply_theme(&mut self, theme: Box<dyn Theme>) {
self.current_theme = Some(theme);
}
pub fn form_count(&self) -> usize {
self.forms.len()
}
pub fn run(&self) {
println!("Running application: {}", self.name);
println!("Window size: {}x{}", self.width, self.height);
println!("Number of forms: {}", self.form_count());
if let Some(ref form_id) = self.main_form_id {
if let Some(form) = self.get_form(form_id) {
println!("Main form: {}", form.title());
}
}
if let Some(ref theme) = self.current_theme {
println!("Theme applied: {}", theme.name());
}
}
}
pub trait Component {
fn id(&self) -> &str;
fn name(&self) -> &str;
fn bounds(&self) -> (i32, i32, u32, u32);
fn set_bounds(&mut self, x: i32, y: i32, width: u32, height: u32);
fn parent_form(&self) -> Option<String>;
fn set_parent_form(&mut self, form_id: String);
fn handle_event(&mut self, event: &Event) -> EventResult;
fn render(&self) -> String {
format!("Component: {}", self.name())
}
}
pub trait Form {
fn id(&self) -> &str;
fn title(&self) -> &str;
fn set_title(&mut self, title: &str);
fn bounds(&self) -> (i32, i32, u32, u32);
fn set_bounds(&mut self, x: i32, y: i32, width: u32, height: u32);
fn add_component(&mut self, component: Box<dyn Component>);
fn remove_component(&mut self, component_id: &str) -> bool;
fn components(&self) -> &Vec<Box<dyn Component>>;
fn handle_event(&mut self, event: &Event) -> EventResult;
}
#[derive(Debug, Clone, PartialEq)]
pub enum EventResult {
Handled,
NotHandled,
Propagate,
}
impl Default for EventResult {
fn default() -> Self {
EventResult::NotHandled
}
}