Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Dropdown options with callback #826

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/component/dropdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ int main() {

auto layout = Container::Vertical({
Container::Horizontal({
Dropdown(&entries, &selected_1),
Dropdown(&entries, &selected_1, { .on_change = [&] { selected_3 = selected_1; } } ), // Keep in sync with selected_3.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
Could you revert this file change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - done 👍

Dropdown(&entries, &selected_2),
}),
Container::Horizontal({
Dropdown(&entries, &selected_3),
Dropdown(&entries, &selected_4),
Dropdown(&entries, &selected_3, { .border = false }),
Dropdown(&entries, &selected_4, { .border = false }),
}),
});

Expand Down
2 changes: 1 addition & 1 deletion include/ftxui/component/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Component Radiobox(ConstStringListRef entries,
int* selected_,
RadioboxOption options = {});

Component Dropdown(ConstStringListRef entries, int* selected);
Component Dropdown(ConstStringListRef entries, int* selected, DropdownOption options = {});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you format?

clang-format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Component Toggle(ConstStringListRef entries, int* selected);

// General slider constructor:
Expand Down
11 changes: 11 additions & 0 deletions include/ftxui/component/component_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ struct CheckboxOption {
std::function<void()> on_change = [] {};
};

/// @brief Option for the Dropdown component.
/// @ingroup component
struct DropdownOption {

bool border = true;

// Observer:
/// Called when the user change the state.
std::function<void()> on_change = [] {};
};

/// @brief Used to define style for the Input component.
struct InputState {
Element element;
Expand Down
36 changes: 28 additions & 8 deletions src/ftxui/component/dropdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace ftxui {
/// @ingroup component
/// @param entries The list of entries to display.
/// @param selected The index of the selected entry.
Component Dropdown(ConstStringListRef entries, int* selected) {
Component Dropdown(ConstStringListRef entries, int* selected, DropdownOption option) {
class Impl : public ComponentBase {
public:
Impl(ConstStringListRef entries, int* selected)
Impl(ConstStringListRef entries, int* selected, DropdownOption dropDownOption)
: entries_(entries), selected_(selected) {
CheckboxOption option;
option.transform = [](const EntryState& s) {
Expand All @@ -37,30 +37,49 @@ Component Dropdown(ConstStringListRef entries, int* selected) {
return hbox({prefix, t});
};
checkbox_ = Checkbox(&title_, &show_, option);
radiobox_ = Radiobox(entries_, selected_);

RadioboxOption radioboxOption;
radioboxOption.on_change = dropDownOption.on_change;

radiobox_ = Radiobox(entries_, selected_, radioboxOption);

Add(Container::Vertical({
checkbox_,
Maybe(radiobox_, &show_),
}));

border_ = dropDownOption.border;
}

Element Render() override {
*selected_ = util::clamp(*selected_, 0, int(entries_.size()) - 1);
title_ = entries_[static_cast<size_t>(*selected_)];
if (show_) {
const int max_height = 12;
return vbox({
auto element = vbox({
checkbox_->Render(),
separator(),
radiobox_->Render() | vscroll_indicator | frame |
size(HEIGHT, LESS_THAN, max_height),
}) |
border;
});

if (border_)
{
element |= border;
}

return element;
}

auto element = checkbox_->Render();

if (border_)
{
element |= border;
}

return vbox({
checkbox_->Render() | border,
element,
filler(),
});
}
Expand All @@ -86,13 +105,14 @@ Component Dropdown(ConstStringListRef entries, int* selected) {
private:
ConstStringListRef entries_;
bool show_ = false;
bool border_ = true;
int* selected_;
std::string title_;
Component checkbox_;
Component radiobox_;
};

return Make<Impl>(entries, selected);
return Make<Impl>(entries, selected, option);
}

} // namespace ftxui