Implemented a much more dynamic render/event loop with Antonio

This commit is contained in:
Mikayla Maki 2022-08-03 11:27:36 -07:00
parent bb8263104c
commit ed3d339dec

View file

@ -8,7 +8,7 @@ use alacritty_terminal::{
ansi::{ClearMode, Handler}, ansi::{ClearMode, Handler},
config::{Config, Program, PtyConfig, Scrolling}, config::{Config, Program, PtyConfig, Scrolling},
event::{Event as AlacTermEvent, EventListener, Notify, WindowSize}, event::{Event as AlacTermEvent, EventListener, Notify, WindowSize},
event_loop::{EventLoop, Msg, Notifier, READ_BUFFER_SIZE}, event_loop::{EventLoop, Msg, Notifier},
grid::{Dimensions, Scroll}, grid::{Dimensions, Scroll},
index::{Direction, Point}, index::{Direction, Point},
selection::{Selection, SelectionType}, selection::{Selection, SelectionType},
@ -19,7 +19,10 @@ use alacritty_terminal::{
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender}; use futures::{
channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
FutureExt,
};
use modal::deploy_modal; use modal::deploy_modal;
use settings::{Settings, Shell}; use settings::{Settings, Shell};
@ -50,8 +53,8 @@ const DEBUG_TERMINAL_WIDTH: f32 = 500.;
const DEBUG_TERMINAL_HEIGHT: f32 = 30.; //This needs to be wide enough that the CI & a local dev's prompt can fill the whole space. const DEBUG_TERMINAL_HEIGHT: f32 = 30.; //This needs to be wide enough that the CI & a local dev's prompt can fill the whole space.
const DEBUG_CELL_WIDTH: f32 = 5.; const DEBUG_CELL_WIDTH: f32 = 5.;
const DEBUG_LINE_HEIGHT: f32 = 5.; const DEBUG_LINE_HEIGHT: f32 = 5.;
const MAX_FRAME_RATE: f32 = 60.; // const MAX_FRAME_RATE: f32 = 60.;
const BACK_BUFFER_SIZE: usize = 5000; // const BACK_BUFFER_SIZE: usize = 5000;
///Upward flowing events, for changing the title and such ///Upward flowing events, for changing the title and such
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -273,8 +276,8 @@ impl TerminalBuilder {
//TODO: Properly set the current locale, //TODO: Properly set the current locale,
env.insert("LC_ALL".to_string(), "en_US.UTF-8".to_string()); env.insert("LC_ALL".to_string(), "en_US.UTF-8".to_string());
let mut alac_scrolling = Scrolling::default(); let alac_scrolling = Scrolling::default();
alac_scrolling.set_history((BACK_BUFFER_SIZE * 2) as u32); // alac_scrolling.set_history((BACK_BUFFER_SIZE * 2) as u32);
let config = Config { let config = Config {
pty_config: pty_config.clone(), pty_config: pty_config.clone(),
@ -338,7 +341,7 @@ impl TerminalBuilder {
default_title: shell_txt, default_title: shell_txt,
last_mode: TermMode::NONE, last_mode: TermMode::NONE,
cur_size: initial_size, cur_size: initial_size,
utilization: 0., // utilization: 0.,
}; };
Ok(TerminalBuilder { Ok(TerminalBuilder {
@ -352,19 +355,28 @@ impl TerminalBuilder {
cx.spawn_weak(|this, mut cx| async move { cx.spawn_weak(|this, mut cx| async move {
use futures::StreamExt; use futures::StreamExt;
let mut events = Vec::new();
while let Some(event) = self.events_rx.next().await { while let Some(event) = self.events_rx.next().await {
let mut timer = cx.background().timer(Duration::from_millis(2)).fuse();
let mut events = vec![event];
loop {
futures::select_biased! {
_ = timer => break,
event = self.events_rx.next() => {
if let Some(event) = event {
events.push(event); events.push(event);
while let Ok(Some(event)) = self.events_rx.try_next() { if events.len() > 100 {
events.push(event);
if events.len() > 1000 {
break; break;
} }
} else {
break;
}
},
}
} }
let this = this.upgrade(&cx)?; this.upgrade(&cx)?.update(&mut cx, |this, cx| {
this.update(&mut cx, |this, cx| { for event in events {
for event in events.drain(..) {
this.process_event(&event, cx); this.process_event(&event, cx);
} }
}); });
@ -376,26 +388,26 @@ impl TerminalBuilder {
}) })
.detach(); .detach();
//Render loop // //Render loop
cx.spawn_weak(|this, mut cx| async move { // cx.spawn_weak(|this, mut cx| async move {
loop { // loop {
let utilization = match this.upgrade(&cx) { // let utilization = match this.upgrade(&cx) {
Some(this) => this.update(&mut cx, |this, cx| { // Some(this) => this.update(&mut cx, |this, cx| {
cx.notify(); // cx.notify();
this.utilization() // this.utilization()
}), // }),
None => break, // None => break,
}; // };
let utilization = (1. - utilization).clamp(0.1, 1.); // let utilization = (1. - utilization).clamp(0.1, 1.);
let delay = cx.background().timer(Duration::from_secs_f32( // let delay = cx.background().timer(Duration::from_secs_f32(
1.0 / (Terminal::default_fps() * utilization), // 1.0 / (Terminal::default_fps() * utilization),
)); // ));
delay.await; // delay.await;
} // }
}) // })
.detach(); // .detach();
self.terminal self.terminal
} }
@ -410,17 +422,17 @@ pub struct Terminal {
cur_size: TerminalSize, cur_size: TerminalSize,
last_mode: TermMode, last_mode: TermMode,
//Percentage, between 0 and 1 //Percentage, between 0 and 1
utilization: f32, // utilization: f32,
} }
impl Terminal { impl Terminal {
fn default_fps() -> f32 { // fn default_fps() -> f32 {
MAX_FRAME_RATE // MAX_FRAME_RATE
} // }
fn utilization(&self) -> f32 { // fn utilization(&self) -> f32 {
self.utilization // self.utilization
} // }
fn process_event(&mut self, event: &AlacTermEvent, cx: &mut ModelContext<Self>) { fn process_event(&mut self, event: &AlacTermEvent, cx: &mut ModelContext<Self>) {
match event { match event {
@ -456,6 +468,7 @@ impl Terminal {
} }
AlacTermEvent::Wakeup => { AlacTermEvent::Wakeup => {
cx.emit(Event::Wakeup); cx.emit(Event::Wakeup);
cx.notify();
} }
AlacTermEvent::ColorRequest(_, _) => { AlacTermEvent::ColorRequest(_, _) => {
self.events.push(InternalEvent::TermEvent(event.clone())) self.events.push(InternalEvent::TermEvent(event.clone()))
@ -574,8 +587,7 @@ impl Terminal {
self.process_terminal_event(&e, &mut term, cx) self.process_terminal_event(&e, &mut term, cx)
} }
self.utilization = Self::estimate_utilization(term.take_last_processed_bytes()); // self.utilization = Self::estimate_utilization(term.take_last_processed_bytes());
self.last_mode = term.mode().clone(); self.last_mode = term.mode().clone();
let content = term.renderable_content(); let content = term.renderable_content();
@ -585,12 +597,12 @@ impl Terminal {
f(content, cursor_text) f(content, cursor_text)
} }
fn estimate_utilization(last_processed: usize) -> f32 { // fn estimate_utilization(last_processed: usize) -> f32 {
let buffer_utilization = (last_processed as f32 / (READ_BUFFER_SIZE as f32)).clamp(0., 1.); // let buffer_utilization = (last_processed as f32 / (READ_BUFFER_SIZE as f32)).clamp(0., 1.);
//Scale result to bias low, then high // //Scale result to bias low, then high
buffer_utilization * buffer_utilization // buffer_utilization * buffer_utilization
} // }
///Scroll the terminal ///Scroll the terminal
pub fn scroll(&mut self, scroll: Scroll) { pub fn scroll(&mut self, scroll: Scroll) {