docs, password config, help, about
render -> draw, file explorer, writer is RefCell
This commit is contained in:
@@ -11,7 +11,7 @@ pub struct HighlightButton<T> {
|
||||
name_: String,
|
||||
top_left: Point,
|
||||
size: Dimensions,
|
||||
text: &'static str,
|
||||
text: String,
|
||||
pub highlighted: bool,
|
||||
click_return: T,
|
||||
toggle_highlight_return: T, //also unhighlight return
|
||||
@@ -38,12 +38,12 @@ impl<T: Clone> Component<T> for HighlightButton<T> {
|
||||
vec![
|
||||
//highlight background
|
||||
DrawInstructions::Rect(self.top_left, self.size, theme_info.top),
|
||||
DrawInstructions::Text([self.top_left[0] + 4, self.top_left[1] + (self.size[1] - font_height) / 2], vec!["times-new-roman".to_string()], self.text.to_string(), theme_info.top_text, theme_info.top, None, None),
|
||||
DrawInstructions::Text([self.top_left[0] + 4, self.top_left[1] + (self.size[1] - font_height) / 2], vec!["times-new-roman".to_string()], self.text.clone(), theme_info.top_text, theme_info.top, None, None),
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
DrawInstructions::Rect(self.top_left, self.size, theme_info.background),
|
||||
DrawInstructions::Text([self.top_left[0] + 4, self.top_left[1] + (self.size[1] - font_height) / 2], vec!["times-new-roman".to_string()], self.text.to_string(), theme_info.text, theme_info.background, None, None),
|
||||
DrawInstructions::Text([self.top_left[0] + 4, self.top_left[1] + (self.size[1] - font_height) / 2], vec!["times-new-roman".to_string()], self.text.clone(), theme_info.text, theme_info.background, None, None),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,7 @@ impl<T: Clone> Component<T> for HighlightButton<T> {
|
||||
}
|
||||
|
||||
impl<T> HighlightButton<T> {
|
||||
pub fn new(name_: String, top_left: Point, size: Dimensions, text: &'static str, click_return: T, toggle_highlight_return: T, highlighted: bool) -> Self {
|
||||
pub fn new(name_: String, top_left: Point, size: Dimensions, text: String, click_return: T, toggle_highlight_return: T, highlighted: bool) -> Self {
|
||||
Self {
|
||||
name_,
|
||||
top_left,
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::window_manager::DrawInstructions;
|
||||
|
||||
pub mod toggle_button;
|
||||
pub mod highlight_button;
|
||||
pub mod paragraph;
|
||||
|
||||
pub trait Component<T> {
|
||||
fn handle_message(&mut self, message: WindowMessage) -> Option<T>;
|
||||
|
||||
91
src/components/paragraph.rs
Normal file
91
src/components/paragraph.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use std::vec;
|
||||
use std::vec::Vec;
|
||||
|
||||
use crate::components::Component;
|
||||
use crate::framebuffer::{ Dimensions, Point };
|
||||
use crate::themes::ThemeInfo;
|
||||
use crate::messages::WindowMessage;
|
||||
use crate::window_manager::DrawInstructions;
|
||||
use crate::utils::calc_actual_lines;
|
||||
|
||||
const MONO_WIDTH: u8 = 10;
|
||||
const LINE_HEIGHT: usize = 18;
|
||||
|
||||
pub struct Paragraph<T> {
|
||||
name_: String,
|
||||
actual_lines: Vec<(bool, usize, String)>, //first, line #, actual line
|
||||
top_left: Point,
|
||||
size: Dimensions,
|
||||
line_pos: usize,
|
||||
key_return: T,
|
||||
}
|
||||
|
||||
impl<T: Copy> Component<T> for Paragraph<T> {
|
||||
fn handle_message(&mut self, message: WindowMessage) -> Option<T> {
|
||||
if let WindowMessage::KeyPress(key_press) = message {
|
||||
if key_press.key == 'j' {
|
||||
//down
|
||||
if self.line_pos != self.actual_lines.len() - 1 {
|
||||
self.line_pos += 1;
|
||||
}
|
||||
Some(self.key_return)
|
||||
} else if key_press.key == 'k' {
|
||||
//up
|
||||
if self.line_pos != 0 {
|
||||
self.line_pos -= 1;
|
||||
}
|
||||
Some(self.key_return)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn draw(&self, theme_info: &ThemeInfo) -> Vec<DrawInstructions> {
|
||||
let mut instructions = Vec::new();
|
||||
let max_lines = self.size[1] / LINE_HEIGHT;
|
||||
let mut start_y = self.top_left[1];
|
||||
for line in self.actual_lines.iter().skip(self.line_pos).take(max_lines) {
|
||||
instructions.push(DrawInstructions::Text([self.top_left[0], start_y], vec!["times-new-romono".to_string()], line.2.clone(), theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
|
||||
start_y += LINE_HEIGHT;
|
||||
}
|
||||
instructions
|
||||
}
|
||||
|
||||
//properties
|
||||
fn focusable(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn clickable(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn name(&self) -> &String {
|
||||
&self.name_
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Paragraph<T> {
|
||||
pub fn new(name_: String, top_left: Point, size: Dimensions, text: String, key_return: T) -> Self {
|
||||
let mut s = Self {
|
||||
name_,
|
||||
actual_lines: Vec::new(),
|
||||
top_left,
|
||||
size,
|
||||
line_pos: 0,
|
||||
key_return,
|
||||
};
|
||||
s.new_text(text);
|
||||
s
|
||||
}
|
||||
|
||||
pub fn new_text(&mut self, text: String) {
|
||||
let max_chars_per_line = self.size[0] / MONO_WIDTH as usize;
|
||||
let lines: Vec<String> = text.split("\n").map(|s| s.to_string()).collect();
|
||||
self.actual_lines = calc_actual_lines(lines.iter(), max_chars_per_line, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,6 @@ use crate::themes::ThemeInfo;
|
||||
use crate::messages::WindowMessage;
|
||||
use crate::window_manager::DrawInstructions;
|
||||
|
||||
//we need a text width and height measure function first
|
||||
pub enum ToggleButtonAlignment {
|
||||
Centre,
|
||||
Left,
|
||||
}
|
||||
|
||||
pub struct ToggleButton<T> {
|
||||
name_: String,
|
||||
top_left: Point,
|
||||
@@ -20,7 +14,6 @@ pub struct ToggleButton<T> {
|
||||
text: String,
|
||||
draw_bg: bool,
|
||||
pub inverted: bool, //whether is it clicked or not
|
||||
alignment: ToggleButtonAlignment,
|
||||
click_return: T,
|
||||
unclick_return: T,
|
||||
}
|
||||
@@ -73,7 +66,7 @@ impl<T: Clone> Component<T> for ToggleButton<T> {
|
||||
}
|
||||
|
||||
impl<T> ToggleButton<T> {
|
||||
pub fn new(name_: String, top_left: Point, size: Dimensions, text: String, click_return: T, unclick_return: T, draw_bg: bool, alignment: Option<ToggleButtonAlignment>) -> Self {
|
||||
pub fn new(name_: String, top_left: Point, size: Dimensions, text: String, click_return: T, unclick_return: T, draw_bg: bool) -> Self {
|
||||
Self {
|
||||
name_,
|
||||
top_left,
|
||||
@@ -83,7 +76,6 @@ impl<T> ToggleButton<T> {
|
||||
unclick_return,
|
||||
draw_bg,
|
||||
inverted: false,
|
||||
alignment: alignment.unwrap_or(ToggleButtonAlignment::Centre),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user