-use super::tui::{ActivityState, HandleEventResult, OurKey, OurKey::*};
+use super::coloured_string::ColouredString;
+use super::text::*;
+use super::tui::{
+ ActivityState, CursorPosition, HandleEventResult,
+ OurKey, OurKey::*
+};
struct Menu {
+ title: Box<dyn TextFragment>,
}
pub fn main_menu() -> Box<dyn ActivityState> {
Box::new(Menu{
+ title: FileHeader::new(
+ ColouredString::uniform("Mastodonochrome Main Menu", 'H')),
})
}
impl ActivityState for Menu {
+ fn draw(&self, h: usize, w: usize)
+ -> (Vec<ColouredString>, CursorPosition) {
+ let mut lines = Vec::new();
+ lines.extend_from_slice(&self.title.render(w));
+ (lines, CursorPosition::End)
+ }
+
fn handle_keypress(&mut self, key: OurKey) -> HandleEventResult {
match key {
Pr('b') => HandleEventResult::Beep,
self.terminal.draw(|frame| {
let area = frame.size();
let buf = frame.buffer_mut();
- self.state.draw_frame(area, buf);
+ if let Some((y, x)) = self.state.draw_frame(area, buf) {
+ if let (Ok(y), Ok(x)) = (y.try_into(), x.try_into()) {
+ frame.set_cursor(x, y);
+ }
+ }
})?;
match self.subthread_receiver.recv() {
}
}
+pub enum CursorPosition {
+ None, // cursor is hidden
+ End, // cursor at the end of the last drawn line (quite common in this UI)
+ At(usize, usize), // (y,x)
+}
+
pub trait ActivityState {
+ fn draw(&self, h: usize, w: usize) -> (Vec<ColouredString>, CursorPosition);
fn handle_keypress(&mut self, key: OurKey) -> HandleEventResult;
}
}
}
- fn draw_frame(&self, _area: Rect, buf: &mut Buffer) {
+ fn draw_frame(&self, area: Rect, buf: &mut Buffer)
+ -> Option<(usize, usize)> {
+ let (lines, cursorpos) = self.activity_state.draw(
+ area.height as usize, area.width as usize);
buf.reset();
- ratatui_set_string(buf, 2, 4, &ColouredString::general(
- "#HelloWorld from Mastodonochrome",
- "########### ",
- ).slice());
+ let mut last_x = 0;
+ let mut last_y = 0;
+ for (y, line) in lines.iter().enumerate() {
+ ratatui_set_string(buf, y, 0, &line.slice());
+ last_y = y;
+ last_x = line.width();
+ }
+ match cursorpos {
+ CursorPosition::None => None,
+ CursorPosition::At(y, x) => Some((y, x)),
+ CursorPosition::End => Some((last_y, last_x)),
+ }
}
fn handle_keypress(&mut self, key: OurKey) -> HandleEventResult {