mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: refactored core
This commit is contained in:
parent
461c60d6b3
commit
c033a5618b
9 changed files with 75 additions and 67 deletions
|
|
@ -34,24 +34,22 @@ impl Engine for TuiContext {
|
|||
fn teardown (&mut self) -> Usually<()> {
|
||||
terminal_teardown()
|
||||
}
|
||||
fn handle (&mut self, state: &mut impl Handle<Self, bool>) -> Usually<()> {
|
||||
fn handle (&self, state: &mut impl Handle<Self, bool>) -> Usually<()> {
|
||||
if ::crossterm::event::poll(self.poll).is_ok() {
|
||||
let event = ::crossterm::event::read().unwrap();
|
||||
if let Event::Key(KeyEvent {
|
||||
code: KeyCode::Char('c'), modifiers: KeyModifiers::CONTROL, ..
|
||||
}) = event {
|
||||
self.exited.store(true, Ordering::Relaxed);
|
||||
} else if let Err(e) = state.handle(&mut self) {
|
||||
} else if let Err(e) = state.handle(self) {
|
||||
panic!("{e}")
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn render (&mut self, state: &impl Render<Self, Rect>) -> Usually<()> {
|
||||
if let Ok(state) = state.try_read() {
|
||||
state.render(&mut self).expect("render failed");
|
||||
}
|
||||
std::thread::sleep(self.sleep)
|
||||
state.render(self).expect("render failed");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl TuiContext {
|
||||
|
|
@ -61,7 +59,7 @@ impl TuiContext {
|
|||
) -> Usually<Arc<RwLock<R>>> {
|
||||
let backend = CrosstermBackend::new(stdout());
|
||||
let area = backend.size()?;
|
||||
let engine = Arc::new(Self {
|
||||
let engine = Arc::new(RwLock::new(Self {
|
||||
sleep: Duration::from_millis(20),
|
||||
poll: Duration::from_millis(100),
|
||||
exited: Arc::new(AtomicBool::new(false)),
|
||||
|
|
@ -69,25 +67,30 @@ impl TuiContext {
|
|||
buffers: [Buffer::empty(area), Buffer::empty(area)],
|
||||
backend,
|
||||
area,
|
||||
});
|
||||
}));
|
||||
let _input_thread = {
|
||||
let engine = engine.clone();
|
||||
let state = state.clone();
|
||||
spawn(move || loop {
|
||||
let engine = engine.read().unwrap();
|
||||
if engine.exited() {
|
||||
break
|
||||
}
|
||||
engine.handle(&mut state).expect("handle failed");
|
||||
engine.handle(&mut *state.write().unwrap()).expect("handle failed");
|
||||
})
|
||||
};
|
||||
let main_thread = {
|
||||
let engine = engine.clone();
|
||||
let state = state.clone();
|
||||
spawn(move || loop {
|
||||
let mut engine = engine.write().unwrap();
|
||||
if engine.exited() {
|
||||
break
|
||||
}
|
||||
engine.render(&mut state).expect("render failed");
|
||||
if let Ok(state) = state.try_read() {
|
||||
engine.render(&*state).expect("render failed");
|
||||
}
|
||||
std::thread::sleep(engine.sleep);
|
||||
})
|
||||
};
|
||||
main_thread.join().expect("main thread failed");
|
||||
|
|
@ -248,15 +251,17 @@ pub trait BorderStyle {
|
|||
const W: &'static str = "";
|
||||
|
||||
#[inline]
|
||||
fn draw <'a> (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
|
||||
self.draw_horizontal(to.buffer, to.area, None)?;
|
||||
self.draw_vertical(to.buffer, to.area, None)?;
|
||||
self.draw_corners(to.buffer, to.area, None)?;
|
||||
fn draw <'a> (&self, to: &mut TuiContext) -> Perhaps<Rect> {
|
||||
self.draw_horizontal(to, None)?;
|
||||
self.draw_vertical(to, None)?;
|
||||
self.draw_corners(to, None)?;
|
||||
Ok(Some(to.area))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn draw_horizontal (&self, buf: &mut Buffer, area: Rect, style: Option<Style>) -> Usually<Rect> {
|
||||
fn draw_horizontal (&self, to: &mut TuiContext, style: Option<Style>) -> Usually<Rect> {
|
||||
let area = to.area();
|
||||
let buf = to.buffer();
|
||||
let style = style.or_else(||self.style_horizontal());
|
||||
for x in area.x..(area.x+area.width).saturating_sub(1) {
|
||||
self.draw_north(buf, x, area.y, style)?;
|
||||
|
|
@ -274,7 +279,9 @@ pub trait BorderStyle {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn draw_vertical (&self, buf: &mut Buffer, area: Rect, style: Option<Style>) -> Usually<Rect> {
|
||||
fn draw_vertical (&self, to: &mut TuiContext, style: Option<Style>) -> Usually<Rect> {
|
||||
let area = to.area();
|
||||
let buf = to.buffer();
|
||||
let style = style.or_else(||self.style_vertical());
|
||||
for y in area.y..(area.y+area.height).saturating_sub(1) {
|
||||
Self::W.blit(buf, area.x, y, style)?;
|
||||
|
|
@ -284,7 +291,9 @@ pub trait BorderStyle {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn draw_corners (&self, buf: &mut Buffer, area: Rect, style: Option<Style>) -> Usually<Rect> {
|
||||
fn draw_corners (&self, to: &mut TuiContext, style: Option<Style>) -> Usually<Rect> {
|
||||
let area = to.area();
|
||||
let buf = to.buffer();
|
||||
let style = style.or_else(||self.style_corners());
|
||||
if area.width > 0 && area.height > 0 {
|
||||
Self::NW.blit(buf, area.x, area.y, style)?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue