Skip to content

Commit

Permalink
TabHeader: selecting items works
Browse files Browse the repository at this point in the history
You can modify the active tab by clicing it.
  • Loading branch information
diegoiast committed Jan 27, 2024
1 parent ecd18eb commit 0ba2c2f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int main() {

auto list_names = std::vector<std::string_view>{
"Tab 1",
"Tab 2",
"Tab with very long name",
"Tab 3",
"‎Tab‎ 4",
};
Expand All @@ -145,7 +145,10 @@ int main() {
stack->add_new<Label>(Position{}, Size{}, "Widget 4")->content.background_color =
MakeColor(0x33, 0x22, 0xaa);

list->on_item_selected = [&stack](auto list, auto index /*, auto reason*/) {
list->on_item_selected = [&stack](auto &combo, auto index /*, auto reason*/) {
stack->set_current_page(index);
};
tab_header->on_item_selected = [&stack](auto &tab_header, auto index /*, auto reason*/) {
stack->set_current_page(index);
};

Expand Down
23 changes: 23 additions & 0 deletions src/tabheader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "tabheader.h"
#include "theme.h"
#include <spdlog/spdlog.h>

TabHeader::TabHeader() : Widget() {
//
Expand Down Expand Up @@ -45,6 +46,9 @@ auto TabHeader::draw() -> void {
theme->draw_widget_background(content, has_focus);

this->content.fill(0);
this->tab_offset.clear();
this->tab_offset.resize(names.size());

auto i = 0;
for (auto s : names) {
auto is_active_tab = i == this->active_tab;
Expand All @@ -59,12 +63,31 @@ auto TabHeader::draw() -> void {
tab_size.height + padding_y * 2, bg_color);

font.write(this->content, {offset + padding_x, padding_y}, s, 0x00);
this->tab_offset[i] = {offset, tab_size.width + padding_x};
offset += tab_size.width;
offset += padding_x + padding_x;
i++;
}
}

auto TabHeader::on_mouse_click(const EventMouse &event) -> EventPropagation {
if (event.pressed) {
auto index = 0;
for (const auto &offset : tab_offset) {
if (event.x > offset.offset && event.x < offset.offset + offset.width) {
this->needs_redraw = true;
this->active_tab = index;
if (this->on_item_selected) {
on_item_selected(*this, index);
}
return EventPropagation::handled;
}
index++;
}
}
return Widget::on_mouse_click(event);
}

auto TabHeader::size_hint() const -> Size {
auto hint = this->get_theme()->font.text_size("X");
hint.width = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/tabheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

#pragma once

#include <vector>
#include <widget.h>

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

TabHeader();

Expand All @@ -21,8 +23,15 @@ struct TabHeader : Widget {
auto get_active_tab() const -> int { return active_tab; }

virtual auto draw() -> void override;
virtual auto on_mouse_click(const EventMouse &event) -> EventPropagation override;
virtual auto size_hint() const -> Size override;

private:
struct TabOffsets {
int offset;
int width;
};
std::vector<TabOffsets> tab_offset;

int active_tab = 0;
};

0 comments on commit 0ba2c2f

Please sign in to comment.