terminal: Draw directly onto the window canvas

This commit is contained in:
apio 2023-09-16 13:29:22 +02:00
parent 945dc6c732
commit 7f23931028
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 8 additions and 12 deletions

View File

@ -46,10 +46,8 @@ Result<void> TerminalWidget::init(char* const* args)
m_font = ui::Font::default_font(); m_font = ui::Font::default_font();
m_bold_font = ui::Font::default_bold_font(); m_bold_font = ui::Font::default_bold_font();
m_terminal_canvas = ui::Canvas { .width = m_rect.width, m_terminal_canvas = ui::App::the().main_window()->canvas();
.height = m_rect.height, m_terminal_canvas.fill(ui::BLACK);
.stride = m_rect.width,
.ptr = (u8*)TRY(calloc_impl(m_rect.width, m_rect.height * sizeof(u32), false)) };
signal(SIGCHLD, sigchld_handler); signal(SIGCHLD, sigchld_handler);
@ -213,13 +211,12 @@ Result<ui::EventResult> TerminalWidget::handle_key_event(const ui::KeyEventReque
return ui::EventResult::DidHandle; return ui::EventResult::DidHandle;
} }
Result<void> TerminalWidget::draw(ui::Canvas& canvas) Result<void> TerminalWidget::draw(ui::Canvas&)
{ {
canvas.fill((u32*)m_terminal_canvas.ptr, m_terminal_canvas.stride);
return {}; return {};
} }
Result<void> TerminalWidget::process() Result<bool> TerminalWidget::process()
{ {
char buffer[BUFSIZ]; char buffer[BUFSIZ];
ssize_t nread = read(m_pty, buffer, BUFSIZ); ssize_t nread = read(m_pty, buffer, BUFSIZ);
@ -238,7 +235,7 @@ Result<void> TerminalWidget::process()
if (should_update_cursor || nread > 0) ui::App::the().main_window()->draw(); if (should_update_cursor || nread > 0) ui::App::the().main_window()->draw();
return {}; return nread == 0;
} }
bool TerminalWidget::tick_cursor() bool TerminalWidget::tick_cursor()

View File

@ -16,7 +16,7 @@ class TerminalWidget : public ui::Widget
Result<void> draw(ui::Canvas& canvas) override; Result<void> draw(ui::Canvas& canvas) override;
Result<void> process(); Result<bool> process();
void quit(); void quit();

View File

@ -11,7 +11,6 @@ Result<int> luna_main(int argc, char** argv)
auto* window = TRY(ui::Window::create(ui::Rect { 150, 150, 640, 400 })); auto* window = TRY(ui::Window::create(ui::Rect { 150, 150, 640, 400 }));
app.set_main_window(window); app.set_main_window(window);
window->set_background(ui::BLACK);
window->set_title("Terminal"); window->set_title("Terminal");
TerminalWidget terminal; TerminalWidget terminal;
@ -24,8 +23,8 @@ Result<int> luna_main(int argc, char** argv)
while (app.process_events()) while (app.process_events())
{ {
TRY(terminal.process()); bool should_sleep = TRY(terminal.process());
usleep(10000); if (should_sleep) usleep(10000);
} }
terminal.quit(); terminal.quit();