Skip to content

Commit

Permalink
Refactor get_char() -> get_event() adding new Event and Key types
Browse files Browse the repository at this point in the history
Instead of using raw ncurses codes now we have our own scancodes.
  • Loading branch information
dacap committed Mar 24, 2024
1 parent 2ed3f5d commit 84f3743
Show file tree
Hide file tree
Showing 18 changed files with 674 additions and 193 deletions.
13 changes: 4 additions & 9 deletions app/alert_view.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// handy text editor
// Copyright (c) 2016-2018 David Capello
// Copyright (c) 2016-2024 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand All @@ -8,11 +8,6 @@

AlertView::AlertView(const char* msg) {
m_msg = msg;
m_result = 0;
}

int AlertView::result() const {
return m_result;
}

std::string AlertView::get_status_text() const {
Expand All @@ -25,9 +20,9 @@ void AlertView::show(Ctx* ctx) {
ctx->status()->update();
}

bool AlertView::on_key(Ctx* ctx, int ch) {
View::on_key(ctx, ch);
m_result = ch;
bool AlertView::on_key(Ctx* ctx, const Key& key) {
View::on_key(ctx, key);
m_result = key;
ctx->back_view();
return true;
}
8 changes: 4 additions & 4 deletions app/alert_view.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// handy text editor
// Copyright (c) 2016-2018 David Capello
// Copyright (c) 2016-2024 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand All @@ -13,13 +13,13 @@
class AlertView : public View {
public:
AlertView(const char* msg);
int result() const;
const Key& result() const { return m_result; }

std::string get_status_text() const override;
void show(Ctx* ctx) override;
bool on_key(Ctx* ctx, int ch) override;
bool on_key(Ctx* ctx, const Key& key) override;

private:
std::string m_msg;
int m_result;
Key m_result;
};
8 changes: 5 additions & 3 deletions app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/fs.h"
#include "ctx.h"
#include "doc_view.h"
#include "event.h"
#include "lua.h"
#include "view.h"

Expand Down Expand Up @@ -87,7 +88,7 @@ void App::back_view() {
view()->set_panel(panel);
}

int App::alert(const char* msg) {
Key App::alert(const char* msg) {
auto alert_view = std::make_shared<AlertView>(msg);
set_view(alert_view);
while (is_running() && view() == alert_view)
Expand All @@ -103,9 +104,10 @@ void App::loop() {
ViewPtr view = this->view();
view->show(this);

int ch = view->panel()->get_char();
Event ev = view->panel()->get_event();

view->on_key(this, ch);
if (ev.type() == Event::Type::Key)
view->on_key(this, ev.key());
}

void App::open_file(const char* fn) {
Expand Down
4 changes: 2 additions & 2 deletions app/app.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// handy text editor
// Copyright (c) 2016-2021 David Capello
// Copyright (c) 2016-2024 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -30,7 +30,7 @@ class App : public Ctx {
ViewPtr view() override;
void set_view(const ViewPtr& v) override;
void back_view() override;
int alert(const char* msg) override;
Key alert(const char* msg) override;
bool is_running() const;
void loop();
void open_file(const char* fn) override;
Expand Down
5 changes: 3 additions & 2 deletions app/ctx.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// handy text editor
// Copyright (c) 2016-2017 David Capello
// Copyright (c) 2016-2024 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.

#pragma once

#include "base.h"
#include "keys.h"

class Ctx {
public:
Expand All @@ -16,6 +17,6 @@ class Ctx {
virtual ViewPtr view() = 0;
virtual void set_view(const ViewPtr& view) = 0;
virtual void back_view() = 0;
virtual int alert(const char* msg) = 0;
virtual Key alert(const char* msg) = 0;
virtual void open_file(const char* fn) = 0;
};
Loading

0 comments on commit 84f3743

Please sign in to comment.