Add edit support for Notepad (#1093)

* WIP file editing

* WIP file editing

* Add "on_pop" handler to navigation.

* WIP Editing

* WIP for draft

* Fix mock and unit tests,  support +newline at end.

* Clean up Painter API and use string_view

* Fix optional rvalue functions

* Fix Result 'take' to be more standard

* FileWrapper stack buffer reads

* Grasping at straws

* Nit

* Move set_on_pop impl to cpp

* Workaround "Open" when file not dirty.

---------

Co-authored-by: kallanreed <kallanreed@outlook.com>
This commit is contained in:
Kyle Reed
2023-06-01 15:45:55 -07:00
committed by GitHub
parent 69011754c9
commit 8d7fdeb633
11 changed files with 847 additions and 148 deletions

View File

@@ -43,10 +43,8 @@ class Optional {
const T& value() const& { return value_; }
const T& operator*() const& { return value_; }
T&& value() && { return value_; }
T&& operator*() && { return value_; }
const T&& value() const&& { return value_; }
const T&& operator*() const&& { return value_; }
T&& value() && { return std::move(value_); }
T&& operator*() && { return std::move(value_); }
T* operator->() { return &value_; }
const T* operator->() const { return &value_; }

View File

@@ -35,18 +35,27 @@ Style Style::invert() const {
.foreground = background};
}
int Painter::draw_char(const Point p, const Style& style, const char c) {
int Painter::draw_char(Point p, const Style& style, char c) {
const auto glyph = style.font.glyph(c);
display.draw_glyph(p, glyph, style.foreground, style.background);
return glyph.advance().x();
}
int Painter::draw_string(Point p, const Font& font, const Color foreground, const Color background, const std::string& text) {
int Painter::draw_string(Point p, const Style& style, std::string_view text) {
return draw_string(p, style.font, style.foreground, style.background, text);
}
int Painter::draw_string(
Point p,
const Font& font,
Color foreground,
Color background,
std::string_view text) {
bool escape = false;
size_t width = 0;
Color pen = foreground;
for (const auto c : text) {
for (auto c : text) {
if (escape) {
if (c <= 15)
pen = term_colors[c & 15];
@@ -65,48 +74,45 @@ int Painter::draw_string(Point p, const Font& font, const Color foreground, cons
}
}
}
return width;
}
int Painter::draw_string(Point p, const Style& style, const std::string& text) {
return draw_string(p, style.font, style.foreground, style.background, text);
}
void Painter::draw_bitmap(const Point p, const Bitmap& bitmap, const Color foreground, const Color background) {
void Painter::draw_bitmap(Point p, const Bitmap& bitmap, Color foreground, Color background) {
display.draw_bitmap(p, bitmap.size, bitmap.data, foreground, background);
}
void Painter::draw_hline(Point p, int width, const Color c) {
void Painter::draw_hline(Point p, int width, Color c) {
display.fill_rectangle({p, {width, 1}}, c);
}
void Painter::draw_vline(Point p, int height, const Color c) {
void Painter::draw_vline(Point p, int height, Color c) {
display.fill_rectangle({p, {1, height}}, c);
}
void Painter::draw_rectangle(const Rect r, const Color c) {
void Painter::draw_rectangle(Rect r, Color c) {
draw_hline(r.location(), r.width(), c);
draw_vline({r.left(), r.top() + 1}, r.height() - 2, c);
draw_vline({r.left() + r.width() - 1, r.top() + 1}, r.height() - 2, c);
draw_hline({r.left(), r.top() + r.height() - 1}, r.width(), c);
}
void Painter::fill_rectangle(const Rect r, const Color c) {
void Painter::fill_rectangle(Rect r, Color c) {
display.fill_rectangle(r, c);
}
void Painter::fill_rectangle_unrolled8(const Rect r, const Color c) {
void Painter::fill_rectangle_unrolled8(Rect r, Color c) {
display.fill_rectangle_unrolled8(r, c);
}
void Painter::paint_widget_tree(Widget* const w) {
void Painter::paint_widget_tree(Widget* w) {
if (ui::is_dirty()) {
paint_widget(w);
ui::dirty_clear();
}
}
void Painter::paint_widget(Widget* const w) {
void Painter::paint_widget(Widget* w) {
if (w->hidden()) {
// Mark widget (and all children) as invisible.
w->visible(false);

View File

@@ -25,7 +25,7 @@
#include "ui.hpp"
#include "ui_text.hpp"
#include <string>
#include <string_view>
namespace ui {
@@ -46,24 +46,24 @@ class Painter {
Painter(const Painter&) = delete;
Painter(Painter&&) = delete;
int draw_char(const Point p, const Style& style, const char c);
int draw_char(Point p, const Style& style, char c);
int draw_string(Point p, const Font& font, const Color foreground, const Color background, const std::string& text);
int draw_string(Point p, const Style& style, const std::string& text);
int draw_string(Point p, const Style& style, std::string_view text);
int draw_string(Point p, const Font& font, Color foreground, Color background, std::string_view text);
void draw_bitmap(const Point p, const Bitmap& bitmap, const Color background, const Color foreground);
void draw_bitmap(Point p, const Bitmap& bitmap, Color background, Color foreground);
void draw_rectangle(const Rect r, const Color c);
void fill_rectangle(const Rect r, const Color c);
void fill_rectangle_unrolled8(const Rect r, const Color c);
void draw_rectangle(Rect r, Color c);
void fill_rectangle(Rect r, Color c);
void fill_rectangle_unrolled8(Rect r, Color c);
void paint_widget_tree(Widget* const w);
void paint_widget_tree(Widget* w);
void draw_hline(Point p, int width, const Color c);
void draw_vline(Point p, int height, const Color c);
void draw_hline(Point p, int width, Color c);
void draw_vline(Point p, int height, Color c);
private:
void paint_widget(Widget* const w);
void paint_widget(Widget* w);
};
} /* namespace ui */