updated grapher to use new widtgets system (not working yet but close)

made a basic app using the widgets system

implemented CgTextEdit for CgLineEdit (now fully working, but there may be bugs so i'll address them tomorrow)
This commit is contained in:
FantasyPvP
2023-11-24 00:40:27 +00:00
parent 467a42a5fa
commit 140ac0ab32
13 changed files with 288 additions and 419 deletions
+5 -5
View File
@@ -18,12 +18,12 @@ pub trait CgComponent {
fn render(&self) -> Result<Frame, RenderError>;
}
/// trait for components that can have editable text, such as search boxes, command palletes, terminals, text inputs etc.
/// trait for components that can have editable text, such as search boxes, command palettes, terminals, text inputs etc.
pub trait CgTextEdit: CgComponent {
fn write_char(&self) -> Result<Frame, RenderError>; // this can also be implemented in a way that inserts characters
fn delete_char(&self) -> Result<Frame, RenderError>;
fn move_cursor(&self, direction: bool) -> Result<(), RenderError>; // true = right, false = left
fn clear(&self) -> Result<Frame, RenderError>;
fn write_char(&mut self, c: char); // this can also be implemented in a way that inserts characters
fn backspace(&mut self);
fn move_cursor(&mut self, direction: bool); // true = right, false = left
fn clear(&mut self);
}
+41 -12
View File
@@ -3,6 +3,7 @@ use alloc::vec::Vec;
use crate::std::frame::{ColouredChar, Dimensions, Frame, Position, RenderError};
use crate::user::lib::libgui::cg_core::{CgComponent, CgTextEdit};
#[derive(Debug, Clone)]
pub struct CgLineEdit {
pub position: Position,
pub dimensions: Dimensions,
@@ -26,28 +27,56 @@ impl CgLineEdit {
impl CgComponent for CgLineEdit {
fn render(&self) -> Result<Frame, RenderError> {
let mut frame = Frame::new(self.position, self.dimensions)?;
let mut ptr = 0;
let mut idx = 0;
for c in self.prompt.chars() {
if ptr >= self.dimensions.x {
if idx >= self.dimensions.x {
break;
}
frame.write(Position::new(ptr, 0), ColouredChar::new(c));
ptr += 1
frame.write(Position::new(idx, 0), ColouredChar::new(c));
idx += 1
}
ptr += 1; // create a space between the prompt and the text
idx += 1; // create a space between the prompt and the text
for c in self.text.iter() {
if ptr >= self.dimensions.x {
break;
}
frame.write(Position::new(ptr, 0), ColouredChar::new(*c));
ptr += 1
if idx + self.text.len() > self.dimensions.x {
frame.write(Position::new(idx, 0), ColouredChar::new('['));
frame.write(Position::new(idx + 1, 0), ColouredChar::new('.'));
frame.write(Position::new(idx + 2, 0), ColouredChar::new('.'));
frame.write(Position::new(idx + 3, 0), ColouredChar::new('.'));
frame.write(Position::new(idx + 4, 0), ColouredChar::new(']'));
idx += 5
}
self.text.iter().rev().take(self.dimensions.x - idx).rev().for_each(|c| {
frame.write(Position::new(idx, 0), ColouredChar::new(*c));
idx += 1
});
Ok(frame)
}
}
impl CgTextEdit for CgLineEdit {
fn write_char(&mut self, c: char) {
self.text.insert(self.ptr, c);
self.ptr += 1;
}
fn backspace(&mut self) {
if self.ptr >= 0 {
self.ptr -= 1;
self.text.remove(self.ptr);
}
}
fn move_cursor(&mut self, direction: bool) {
match direction {
true => if self.ptr < self.text.len() { self.ptr += 1; },
false => if self.ptr > 0 { self.ptr -= 1; },
}
}
fn clear(&mut self) {
self.text.clear();
self.ptr = 0
}
}
+15 -9
View File
@@ -10,7 +10,6 @@ use super::cg_core::{
use crate::std::frame::{ColouredChar, Dimensions, Position, Frame};
use crate::std::io::Color;
pub struct CgContainer<'a> {
pub elements: Vec<Box<&'a dyn CgComponent>>,
pub position: Position,
@@ -27,6 +26,9 @@ impl<'a> CgContainer<'a> {
outlined,
}
}
pub fn insert(&mut self, element: Box<&'a dyn CgComponent>) {
self.elements.push(element);
}
}
impl CgOutline for CgContainer<'_> {
@@ -58,7 +60,7 @@ impl CgComponent for CgContainer<'_> {
for widget in &self.elements {
let frame = widget.render()?;
match result.render_bounds_check(&frame, true) { // TODO: this needs to be set to false for production
Ok(()) => result.render_element(&frame),
Ok(()) => result.place_child_element(&frame),
Err(e) => return Err(e),
}
}
@@ -71,7 +73,7 @@ impl CgComponent for CgContainer<'_> {
}
}
#[derive(Debug, Clone)]
pub struct CgTextBox {
title: String,
content: String,
@@ -178,7 +180,7 @@ impl CgOutline for CgTextBox {
#[derive(Debug, Clone)]
pub struct CgLabel {
content: String,
position: Position,
@@ -193,6 +195,9 @@ impl CgLabel {
dimensions: Dimensions::new(width, 1),
}
}
pub fn set_text(&mut self, text: String) {
self.content = text;
}
}
impl CgComponent for CgLabel {
@@ -208,6 +213,7 @@ impl CgComponent for CgLabel {
}
}
#[derive(Debug, Clone)]
pub struct CgIndicatorWidget {
content: String,
colour: ColorCode,
@@ -256,6 +262,7 @@ impl CgComponent for CgIndicatorWidget {
}
}
#[derive(Debug, Clone)]
pub struct CgIndicatorBar {
pub fields: Vec<CgIndicatorWidget>,
position: Position,
@@ -288,7 +295,7 @@ impl CgComponent for CgIndicatorBar {
width_idx += widget.len();
match result.render_bounds_check(&frame, true) {
Ok(()) => result.render_element(&frame),
Ok(()) => result.place_child_element(&frame),
Err(e) => return Err(e),
}
}
@@ -297,6 +304,7 @@ impl CgComponent for CgIndicatorBar {
}
}
#[derive(Debug, Clone)]
pub struct CgStatusBar {
position: Position,
dimensions: Dimensions,
@@ -321,10 +329,8 @@ impl CgComponent for CgStatusBar {
let width = screen_mode.dimensions().x;
screen_mode.set_position(Position::new(self.dimensions.x - width, 0));
frame.render_element(&window_title);
frame.render_element(&screen_mode);
serial_println!("{:?}", frame);
frame.place_child_element(&window_title);
frame.place_child_element(&screen_mode);
Ok(frame)
}