rsgt 0.2.1

Rust simple GUI Toolkit
//! Widget Definitions

use crate::event::EventListener;

/// Trait common to all widgets
pub trait Widget {
    /// Specify the widget's EventListener
    fn add_event_listener(&mut self, listener: (impl EventListener + 'static));

    /// Specify the ID of the widget
    fn set_id(&mut self, id: impl Into<String>);

    /// Get Widget ID
    fn get_id(&self) -> &str;
}

/// Base trait Buttons
/// Widget implemented as a sub trait
pub trait Button: Widget {
    fn set_text(&mut self, text: impl Into<String>);
    /// Get the text of the button
    fn get_text(&self) -> &str;
}

/// # RButton
/// plain button.
pub struct RButton {
    /// Text displayed on the button
    text: String,

    // Implementations
    /// ID
    id: String,
    /// Event Listener
    event_listener: Box<dyn EventListener>,
}

// Widget implementation for RButton
impl Widget for RButton {
    fn add_event_listener(&mut self, listener: (impl EventListener + 'static)) {
        self.event_listener = Box::new(listener);
    }

    fn set_id(&mut self, id: impl Into<String>) {
        self.id = id.into();
    }

    fn get_id(&self) -> &str {
        self.id.as_str()
    }
}

// Button implementation for RButton
impl Button for RButton {
    /// Set the text of the button
    fn set_text(&mut self, text: impl Into<String>) {
        self.text = text.into();
    }

    /// Get the text of the button
    fn get_text(&self) -> &str {
        self.text.as_str()
    }
}