New Freqman edit UI (#1272)

* WIP new edit UI

* Fix textfield highlight

* WIP new edit

* Wrap up first pass of freqman edit

* Fix indexing of options

---------

Co-authored-by: kallanreed <kallanreed@noreply.github.com>
This commit is contained in:
Kyle Reed
2023-07-14 18:46:39 -07:00
committed by GitHub
parent 61cb57e48d
commit 25923e82a4
8 changed files with 437 additions and 71 deletions

View File

@@ -349,7 +349,7 @@ Text::Text(
Rect parent_rect,
std::string text)
: Widget{parent_rect},
text{text} {
text{std::move(text)} {
}
Text::Text(
@@ -357,14 +357,14 @@ Text::Text(
: Text{parent_rect, {}} {
}
void Text::set(const std::string value) {
text = value;
void Text::set(std::string_view value) {
text = std::string{value};
set_dirty();
}
void Text::paint(Painter& painter) {
const auto rect = screen_rect();
const auto s = style();
auto s = has_focus() ? style().invert() : style();
painter.fill_rectangle(rect, s.background);
@@ -1608,9 +1608,9 @@ bool OptionsField::on_touch(const TouchEvent event) {
return true;
}
/* TextField ***********************************************************/
/* TextEdit ***********************************************************/
TextField::TextField(
TextEdit::TextEdit(
std::string& str,
size_t max_length,
Point position,
@@ -1624,24 +1624,24 @@ TextField::TextField(
set_focusable(true);
}
const std::string& TextField::value() const {
const std::string& TextEdit::value() const {
return text_;
}
void TextField::set_cursor(uint32_t pos) {
void TextEdit::set_cursor(uint32_t pos) {
cursor_pos_ = std::min<size_t>(pos, text_.length());
set_dirty();
}
void TextField::set_insert_mode() {
void TextEdit::set_insert_mode() {
insert_mode_ = true;
}
void TextField::set_overwrite_mode() {
void TextEdit::set_overwrite_mode() {
insert_mode_ = false;
}
void TextField::char_add(char c) {
void TextEdit::char_add(char c) {
// Don't add if inserting and at max_length and
// don't overwrite if past the end of the text.
if ((text_.length() >= max_length_ && insert_mode_) ||
@@ -1657,7 +1657,7 @@ void TextField::char_add(char c) {
set_dirty();
}
void TextField::char_delete() {
void TextEdit::char_delete() {
if (cursor_pos_ == 0)
return;
@@ -1666,7 +1666,7 @@ void TextField::char_delete() {
set_dirty();
}
void TextField::paint(Painter& painter) {
void TextEdit::paint(Painter& painter) {
constexpr int char_width = 8;
auto rect = screen_rect();
@@ -1702,7 +1702,7 @@ void TextField::paint(Painter& painter) {
painter.draw_rectangle(cursor_box, cursor_style.background);
}
bool TextField::on_key(const KeyEvent key) {
bool TextEdit::on_key(const KeyEvent key) {
if (key == KeyEvent::Left && cursor_pos_ > 0)
cursor_pos_--;
else if (key == KeyEvent::Right && cursor_pos_ < text_.length())
@@ -1716,7 +1716,7 @@ bool TextField::on_key(const KeyEvent key) {
return true;
}
bool TextField::on_encoder(const EncoderEvent delta) {
bool TextEdit::on_encoder(const EncoderEvent delta) {
int32_t new_pos = cursor_pos_ + delta;
// Let the encoder wrap around the ends of the text.
@@ -1729,7 +1729,7 @@ bool TextField::on_encoder(const EncoderEvent delta) {
return true;
}
bool TextField::on_touch(const TouchEvent event) {
bool TextEdit::on_touch(const TouchEvent event) {
if (event.type == TouchEvent::Type::Start)
focus();
@@ -1737,6 +1737,32 @@ bool TextField::on_touch(const TouchEvent event) {
return true;
}
/* TextField *************************************************************/
TextField::TextField(Rect parent_rect, std::string text)
: Text(parent_rect, std::move(text)) {
set_focusable(true);
}
const std::string& TextField::get_text() const {
return text;
}
void TextField::set_text(std::string_view value) {
set(value);
if (on_change)
on_change(*this);
}
bool TextField::on_key(KeyEvent key) {
if (key == KeyEvent::Select && on_select) {
on_select(*this);
return true;
}
return false;
}
/* NumberField ***********************************************************/
NumberField::NumberField(

View File

@@ -33,10 +33,11 @@
#include "portapack.hpp"
#include "utility.hpp"
#include <memory>
#include <vector>
#include <string>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
namespace ui {
@@ -202,11 +203,11 @@ class Text : public Widget {
Text(Rect parent_rect, std::string text);
Text(Rect parent_rect);
void set(const std::string value);
void set(std::string_view value);
void paint(Painter& painter) override;
private:
protected:
std::string text;
};
@@ -619,6 +620,7 @@ class OptionsField : public Widget {
OptionsField(Point parent_pos, size_t length, options_t options);
options_t& options() { return options_; }
const options_t& options() const { return options_; }
void set_options(options_t new_options);
@@ -642,14 +644,14 @@ class OptionsField : public Widget {
size_t selected_index_{0};
};
// A TextField is bound to a string reference and allows the string
// A TextEdit is bound to a string reference and allows the string
// to be manipulated. The field itself does not provide the UI for
// setting the value. It provides the UI of rendering the text,
// a cursor, and an API to edit the string content.
class TextField : public Widget {
class TextEdit : public Widget {
public:
TextField(std::string& str, Point position, uint32_t length = 30)
: TextField{str, 64, position, length} {}
TextEdit(std::string& str, Point position, uint32_t length = 30)
: TextEdit{str, 64, position, length} {}
// Str: the string containing the content to edit.
// Max_length: max length the string is allowed to use.
@@ -658,12 +660,12 @@ class TextField : public Widget {
// - Characters are 8 pixels wide.
// - The screen can show 30 characters max.
// - The control is 16 pixels tall.
TextField(std::string& str, size_t max_length, Point position, uint32_t length = 30);
TextEdit(std::string& str, size_t max_length, Point position, uint32_t length = 30);
TextField(const TextField&) = delete;
TextField(TextField&&) = delete;
TextField& operator=(const TextField&) = delete;
TextField& operator=(TextField&&) = delete;
TextEdit(const TextEdit&) = delete;
TextEdit(TextEdit&&) = delete;
TextEdit& operator=(const TextEdit&) = delete;
TextEdit& operator=(TextEdit&&) = delete;
const std::string& value() const;
@@ -688,6 +690,22 @@ class TextField : public Widget {
bool insert_mode_;
};
class TextField : public Text {
public:
std::function<void(TextField&)> on_select{};
std::function<void(TextField&)> on_change{};
TextField(Rect parent_rect, std::string text);
const std::string& get_text() const;
void set_text(std::string_view value);
bool on_key(KeyEvent key) override;
private:
using Text::set;
};
class NumberField : public Widget {
public:
std::function<void(NumberField&)> on_select{};