Skip to content

Commit

Permalink
all: Widgets should own the text
Browse files Browse the repository at this point in the history
I created a by - in which all widgets have a string_view as the text.
Said text might be out of sope when drawing (for example, when the
widget was created in a function, and now the string view points to a
non-existing stack variable).

Now the main demo shows many tabs with a generated text (and color!).
  • Loading branch information
diegoiast committed Jun 14, 2024
1 parent 23181c0 commit 7670595
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Button : Widget {
bool has_frame = true;
std::shared_ptr<Bitmap> icon;

std::string_view text;
std::string text;
std::function<void(Button &)> on_button_click;
AbstractButtonState state;

Expand Down Expand Up @@ -83,7 +83,7 @@ struct Button : Widget {
return std::dynamic_pointer_cast<Button>(this->shared_from_this());
}

auto set_icon(std::shared_ptr<Bitmap> icon) {
auto set_icon(std::shared_ptr<Bitmap> icon) {
this->icon = icon;
return std::dynamic_pointer_cast<Button>(this->shared_from_this());
}
Expand Down
6 changes: 3 additions & 3 deletions src/combobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include "listview.h"
#include "theme.h"

Combobox::Combobox(const std::vector<std::string_view> &strings) : Combobox({}, 0, strings) {}
Combobox::Combobox(const std::vector<std::string> &strings) : Combobox({}, 0, strings) {}

Combobox::Combobox(Position position, int width, const std::vector<std::string_view> &strings)
Combobox::Combobox(Position position, int width, const std::vector<std::string> &strings)
: Widget(position, {0, 0}, 0) {
this->strings = strings;
this->can_focus = true;
Expand Down Expand Up @@ -189,7 +189,7 @@ auto Combobox::set_active_index(int index) -> void {
this->invalidate();
}

auto Combobox::set_items(const std::vector<std::string_view> &new_strings) -> void {
auto Combobox::set_items(const std::vector<std::string> &new_strings) -> void {
this->strings = new_strings;
if (this->selected_item >= this->strings.size()) {
this->selected_item = this->strings.size();
Expand Down
8 changes: 4 additions & 4 deletions src/combobox.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ struct ComboboxList;

struct Combobox : Widget {
std::shared_ptr<ComboboxList> popup_list = {};
std::vector<std::string_view> strings = {};
std::vector<std::string> strings = {};
std::function<void(Combobox &, int)> on_item_selected = {};

int selected_item = 0;

Combobox(Position position, int width, const std::vector<std::string_view> &strings);
Combobox(const std::vector<std::string_view> &strings);
Combobox(Position position, int width, const std::vector<std::string> &strings);
Combobox(const std::vector<std::string> &strings);
auto get_value() const -> std::string_view;
auto get_item() const -> int { return selected_item; }
virtual auto draw() -> void override;
Expand All @@ -38,7 +38,7 @@ struct Combobox : Widget {
auto show_popup() -> void;
auto set_active_index(int index) -> void;
auto get_active_index() const -> int { return selected_item; }
auto set_items(const std::vector<std::string_view> &new_strings) -> void;
auto set_items(const std::vector<std::string> &new_strings) -> void;

private:
std::shared_ptr<Button> popup_button = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <widget.h>

struct Label : public Widget {
std::string_view text;
std::string text;

Label(std::string_view text) : Widget({}, {}, 0) {
this->text = text;
Expand Down
4 changes: 2 additions & 2 deletions src/listview.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ struct ItemAdapter {
};

struct ListItemAdapter : ItemAdapter {
std::vector<std::string_view> strings;
std::vector<std::string> strings;

explicit ListItemAdapter(const std::vector<std::string_view> &s) { this->strings = s; };
explicit ListItemAdapter(const std::vector<std::string> &s) { this->strings = s; };
virtual auto get_count() const -> size_t override { return strings.size(); }
virtual auto get_widget(size_t position, std::shared_ptr<Theme> theme) -> PWidget override;
virtual auto set_content(PWidget widget, size_t position, ItemStatus status) -> void override;
Expand Down
14 changes: 9 additions & 5 deletions src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main() {
auto w2 = platform.open_window(300, 300, 640, 480, "test 2");
w2->main_widget.layout->padding.set_vertical(2);

auto list = w2->add_new<Combobox>(std::vector<std::string_view>());
auto list = w2->add_new<Combobox>(std::vector<std::string>());
auto tabs = w2->add_new<TabWidget>();
auto bitmap = std::make_shared<Bitmap>();
auto icon = std::make_shared<Bitmap>();
Expand Down Expand Up @@ -158,8 +158,12 @@ int main() {

auto b1 = std::make_shared<Button>("+")
->set_on_click([&tabs](auto &button) {
tabs->add_new_tab<Label>("Tab", "This is a dynamic tab");
MakeColor(0x33, 0xaa, 0x22);
auto count = tabs->page_names().size() + 1;
auto tab_title = fmt::format("Tab {} ", count);
auto tab_content = fmt::format("This is dynamic tab #{} ", count);

tabs->add_new_tab<Label>(tab_title, tab_content)->content.background_color =
MakeColor(0x11 * count, 0xaa, 0x22 + (7 * count));
})
->set_auto_shrink(true)
->set_has_frame(false);
Expand Down Expand Up @@ -191,7 +195,7 @@ int main() {

// l_right->margin.set(5);
w1->add_new_to_layout<ListView>(l_left)->adapter =
std::make_shared<ListItemAdapter>(std::vector<std::string_view>{
std::make_shared<ListItemAdapter>(std::vector<std::string>{
"Option 1 (default)",
"Option 2",
"Option 3",
Expand All @@ -215,7 +219,7 @@ int main() {
spdlog::info("Selected item {} with text {}", index, button.text);
};
rb->radio_buttons[1]->is_enabled = false;
w1->add_new_to_layout<Combobox>(l_right, std::vector<std::string_view>{
w1->add_new_to_layout<Combobox>(l_right, std::vector<std::string>{
"Spring",
"Summer",
"Autumn/Fall",
Expand Down
2 changes: 1 addition & 1 deletion src/tabheader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TabHeader::TabHeader() : Widget() { this->padding_style = PaddingStyle::TabHeade

auto TabHeader::add_tab(const std::string_view name) -> int {
this->needs_redraw = true;
this->names.push_back(name);
this->names.push_back(std::string(name));
return this->names.size() - 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tabheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <widget.h>

struct TabHeader : Widget {
std::vector<std::string_view> names;
std::vector<std::string> names;
std::function<void(TabHeader &, int)> on_item_selected = {};

TabHeader();
Expand Down
2 changes: 1 addition & 1 deletion src/tabwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct TabWidget : Widget {

auto get_active_tab() const -> size_t { return headers->get_active_tab(); }

auto page_names() const -> std::vector<std::string_view> { return headers->names; }
auto page_names() const -> std::vector<std::string> & { return headers->names; }

private:
std::shared_ptr<HorizontalLayout> top_layout;
Expand Down
3 changes: 2 additions & 1 deletion src/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "theme.h"
#include <string>

auto Theme::draw_frame(Bitmap &content, Position position, Size size, FrameStyles style,
FrameSize frame_size) -> void {
Expand Down Expand Up @@ -87,7 +88,7 @@ auto Theme::draw_frame(Bitmap &content, Position position, Size size, FrameStyle
}

auto Theme::draw_tabs(Bitmap &content, bool has_focus, int selected_index, int hover_index,
const LayoutParams &padding, const std::vector<std::string_view> &names)
const LayoutParams &padding, const std::vector<std::string> &names)
-> std::vector<TabHeaderOffsets> {
auto tab_offset = std::vector<TabHeaderOffsets>();

Expand Down
2 changes: 1 addition & 1 deletion src/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Theme {
auto draw_frame(Bitmap &content, Position position, Size size, FrameStyles style,
FrameSize frame_size) -> void;
auto draw_tabs(Bitmap &content, bool has_focus, int selected_index, int hover_index,
const LayoutParams &padding, const std::vector<std::string_view> &names)
const LayoutParams &padding, const std::vector<std::string> &names)
-> std::vector<TabHeaderOffsets>;

virtual auto init() -> void = 0;
Expand Down

0 comments on commit 7670595

Please sign in to comment.