Skip to content

Commit

Permalink
trying to move things in display objects to better manage rendering a…
Browse files Browse the repository at this point in the history
…nd event handling
  • Loading branch information
jaytaph committed Dec 28, 2023
1 parent c80f6e7 commit e967f9d
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 158 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2021"
anyhow = "1.0.77"
crossterm = "0.27.0"
ratatui = "0.25.0"
tui-input = "0.8.0"
236 changes: 145 additions & 91 deletions src/dive/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,119 +4,173 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::event::Event::Key;
use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout};
use crate::dive::help::{help_process_keys, help_render};
use crate::dive::menu::menu_render;
use crate::dive::status::status_render;
use crate::dive::tab::{Tab, tab_switch, tabs_render};
use crate::dive::display_object::DisplayObject;

pub struct App {
pub tabs: Vec<Tab>,
pub current_tab: usize,

pub should_quit: bool,
pub status: String,

pub display_objects: Vec<DisplayObject>,
pub active_display_object_index: usize,

pub menu_active: bool,
pub menu_item_active: usize,
pub current_tab: usize,
pub show_help: bool,
pub help_scroll: u16,
pub status: String,

// pub show_help: bool,
// pub popup: bool,
}

pub fn app_ui(app: &mut App, f: &mut Frame) {
let size = f.size();
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints(
[
Constraint::Length(1), // menu bar
Constraint::Min(0), // content
Constraint::Length(1), // status bar
]
.as_ref(),
)
.split(size);

let menu = menu_render(app);
f.render_widget(menu, chunks[0]);

let tabs = tabs_render(app);
f.render_widget(tabs, chunks[1]);

let status = status_render(app);
f.render_widget(status, chunks[2]);

if app.show_help {
help_render(app, f);
impl App {
/// Find the display object with the given id or None when not found
pub(crate) fn find_display_object(&self, id: &str) -> Option<&DisplayObject> {
for display_object in self.display_objects.iter() {
if display_object.id == id {
return Some(display_object);
}
}

None
}
}

fn main_process_keys(app: &mut App, key: KeyEvent) -> anyhow::Result<()> {
match key.code {
Char('0') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 0),
Char('1') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 1),
Char('2') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 2),
Char('3') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 3),
Char('4') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 4),
Char('5') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 5),
Char('6') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 6),
Char('7') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 7),
Char('8') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 8),
Char('9') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(app, 9),

KeyCode::F(1) => {
app.show_help = !app.show_help;
app.help_scroll = 0;
if app.show_help {
app.status = "Opened help screen".into();
} else {
app.status = "Closed help screen".into();
pub(crate) fn find_display_object_mut(&mut self, id: &str) -> Option<&mut DisplayObject> {
for display_object in self.display_objects.iter_mut() {
if display_object.id == id {
return Some(display_object);
}
}
KeyCode::F(9) => app.menu_active = !app.menu_active,
KeyCode::Tab => {
app.current_tab = (app.current_tab + 1) % app.tabs.len();
app.status = format!("Switched to tab {}", app.current_tab);
},

Char('w') if key.modifiers.contains(KeyModifiers::CONTROL) => {
if app.tabs.len() > 1 {
app.tabs.remove(app.current_tab);
app.status = format!("Closed tab {}", app.current_tab);
if app.current_tab > 0 {
app.current_tab -= 1;
}
} else {
app.status = "Can't close last tab".into();

None
}

/// Sets the given display object to be the active one (ie: handling events)
pub(crate) fn set_active_display_object(&mut self, id: &str) {
for (idx, display_object) in self.display_objects.iter_mut().enumerate() {
if display_object.id == id {
self.active_display_object_index = idx;
}
},
Char('n') if key.modifiers.contains(KeyModifiers::CONTROL) => {
app.tabs.push(Tab {
name: "New Tab".to_string(),
url: "gosub://blank".to_string(),
content: String::new(),
});
app.status = format!("Opened new tab {}", app.tabs.len() - 1);
app.current_tab = app.tabs.len() - 1;
},
Char('q') if key.modifiers.contains(KeyModifiers::CONTROL) => app.should_quit = true,
_ => {},
}
}
Ok(())
}

pub fn app_update(app: &mut App) -> anyhow::Result<()> {
if ! event::poll(std::time::Duration::from_millis(250))? {
return Ok(());
pub(crate) fn handle_events(&mut self) -> anyhow::Result<()> {
if ! event::poll(std::time::Duration::from_millis(250))? {
return Ok(());
}

if let Key(key) = event::read()? {
if key.kind == event::KeyEventKind::Press {
let display_obj = &mut self.display_objects[self.active_display_object_index];
display_obj.object.event_handler(self, key);

// also let main app handle the key
self.process_key(key);
}
}

Ok(())
}

if let Key(key) = event::read()? {
if key.kind == event::KeyEventKind::Press {
if app.show_help {
help_process_keys(app, key)?;
} else {
main_process_keys(app, key)?;
// Renders the screen, and all display objects
pub(crate) fn render(&mut self, f: &mut Frame) {
let size = f.size();
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints(
[
Constraint::Length(1), // menu bar
Constraint::Min(0), // content
Constraint::Length(1), // status bar
]
.as_ref(),
)
.split(size);

let menu = menu_render(self);
f.render_widget(menu, chunks[0]);

let tabs = tabs_render(self);
f.render_widget(tabs, chunks[1]);

let status = status_render(self);
f.render_widget(status, chunks[2]);

// Iterate all display objects, and sort them by priority (0 == first)
self.display_objects.sort_by(|a, b| a.priority.cmp(&b.priority));

// Render all showable display objects
for display_object in self.display_objects.iter_mut() {
if !display_object.show {
continue;
}
display_object.object.render(self, f);
}
}

Ok(())
/// Main key handling
fn process_key(&mut self, key: KeyEvent) -> anyhow::Result<()> {
match key.code {
Char('0') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 0),
Char('1') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 1),
Char('2') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 2),
Char('3') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 3),
Char('4') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 4),
Char('5') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 5),
Char('6') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 6),
Char('7') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 7),
Char('8') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 8),
Char('9') if key.modifiers.contains(KeyModifiers::ALT) => tab_switch(self, 9),

KeyCode::F(1) => {
let obj = self.find_display_object_mut("help").unwrap();
obj.show = !obj.show;

self.set_active_display_object("help");
}
KeyCode::F(2) => {
let obj = self.find_display_object_mut("test").unwrap();
obj.show = !obj.show;

self.set_active_display_object("test");
}
KeyCode::F(9) => self.menu_active = !self.menu_active,
KeyCode::Tab => {
self.current_tab = (self.current_tab + 1) % self.tabs.len();
self.status = format!("Switched to tab {}", self.current_tab);
},

// Char('i') if key.modifiers.contains(KeyModifiers::CONTROL) => {
// // change the name of the current tab
// self.popup = true;
// }
Char('w') if key.modifiers.contains(KeyModifiers::CONTROL) => {
if self.tabs.len() > 1 {
self.tabs.remove(self.current_tab);
self.status = format!("Closed tab {}", self.current_tab);
if self.current_tab > 0 {
self.current_tab -= 1;
}
} else {
self.status = "Can't close last tab".into();
}
},
Char('n') if key.modifiers.contains(KeyModifiers::CONTROL) => {
self.tabs.push(Tab {
name: "New Tab".to_string(),
url: "gosub://blank".to_string(),
content: String::new(),
});
self.status = format!("Opened new tab {}", self.tabs.len() - 1);
self.current_tab = self.tabs.len() - 1;
},
Char('q') if key.modifiers.contains(KeyModifiers::CONTROL) => self.should_quit = true,
_ => {},
}
Ok(())
}
}
38 changes: 38 additions & 0 deletions src/dive/display_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crossterm::event::KeyEvent;
use ratatui::Frame;
use crate::dive::app::App;

pub trait Displayable {
fn render(&mut self, app: &mut App, f: &mut Frame);
fn event_handler(&mut self, app: &mut App, key: KeyEvent) -> anyhow::Result<()>;
fn on_show(&mut self, app: &mut App);
fn on_hide(&mut self, app: &mut App);
}

pub struct DisplayObject {
pub id: String, // Unique identifier for this object
pub priority: u8, // 0 is the highest, 255 is the lowest
pub show: bool, // Does this object need to be rendered
pub object: Box<dyn Displayable>, // Actual object with rendering and event handling
}

impl DisplayObject {
pub fn new(id: &str, priority: u8, object: Box<dyn Displayable>) -> Self {
Self {
id: id.into(),
priority,
show: false,
object,
}
}

pub fn show(&mut self, app: &mut App) {
self.show = true;
self.object.on_show(app);
}

pub fn hide(&mut self, app: &mut App) {
self.show = false;
self.object.on_hide(app);
}
}
Loading

0 comments on commit e967f9d

Please sign in to comment.