mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-28 16:29:34 +08:00
Clamp selected_ on list resize for Radiobox/Menu/Toggle (#298)
fix: https://github.com/ArthurSonzogni/FTXUI/issues/296#issue-1092343846 When the list in Radiobox/Menu/Toggle is resized, clamp the |selected_| values so that it stays within bounds. Clamping is executed in Render() and in OnEvent() Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include <stddef.h> // for size_t
|
||||
#include <algorithm> // for max, min
|
||||
#include <algorithm> // for clamp, max
|
||||
#include <functional> // for function
|
||||
#include <memory> // for shared_ptr, allocator_traits<>::value_type
|
||||
#include <utility> // for move
|
||||
@@ -13,7 +12,7 @@
|
||||
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
|
||||
#include "ftxui/dom/elements.hpp" // for operator|, Element, Elements, hbox, reflect, separator, text, focus, nothing, select
|
||||
#include "ftxui/screen/box.hpp" // for Box
|
||||
#include "ftxui/util/ref.hpp" // for ConstStringListRef, Ref
|
||||
#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
@@ -30,16 +29,16 @@ class ToggleBase : public ComponentBase {
|
||||
|
||||
private:
|
||||
Element Render() override {
|
||||
Clamp();
|
||||
Elements children;
|
||||
bool is_toggle_focused = Focused();
|
||||
boxes_.resize(entries_.size());
|
||||
for (size_t i = 0; i < entries_.size(); ++i) {
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
// Separator.
|
||||
if (i != 0)
|
||||
children.push_back(separator());
|
||||
|
||||
bool is_focused = (focused_entry() == int(i)) && is_toggle_focused;
|
||||
bool is_selected = (*selected_ == int(i));
|
||||
bool is_focused = (focused_entry() == i) && is_toggle_focused;
|
||||
bool is_selected = (*selected_ == i);
|
||||
|
||||
auto style = is_selected ? (is_focused ? option_->style_selected_focused
|
||||
: option_->style_selected)
|
||||
@@ -55,6 +54,7 @@ class ToggleBase : public ComponentBase {
|
||||
}
|
||||
|
||||
bool OnEvent(Event event) override {
|
||||
Clamp();
|
||||
if (event.is_mouse())
|
||||
return OnMouseEvent(event);
|
||||
|
||||
@@ -63,12 +63,12 @@ class ToggleBase : public ComponentBase {
|
||||
(*selected_)--;
|
||||
if (event == Event::ArrowRight || event == Event::Character('l'))
|
||||
(*selected_)++;
|
||||
if (event == Event::Tab && entries_.size())
|
||||
*selected_ = (*selected_ + 1) % entries_.size();
|
||||
if (event == Event::TabReverse && entries_.size())
|
||||
*selected_ = (*selected_ + entries_.size() - 1) % entries_.size();
|
||||
if (event == Event::Tab && size())
|
||||
*selected_ = (*selected_ + 1) % size();
|
||||
if (event == Event::TabReverse && size())
|
||||
*selected_ = (*selected_ + size() - 1) % size();
|
||||
|
||||
*selected_ = std::max(0, std::min(int(entries_.size()) - 1, *selected_));
|
||||
*selected_ = std::clamp(*selected_, 0, size() - 1);
|
||||
|
||||
if (old_selected != *selected_) {
|
||||
focused_entry() = *selected_;
|
||||
@@ -87,7 +87,7 @@ class ToggleBase : public ComponentBase {
|
||||
bool OnMouseEvent(Event event) {
|
||||
if (!CaptureMouse(event))
|
||||
return false;
|
||||
for (int i = 0; i < int(boxes_.size()); ++i) {
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
|
||||
continue;
|
||||
|
||||
@@ -106,8 +106,15 @@ class ToggleBase : public ComponentBase {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Focusable() const final { return entries_.size(); }
|
||||
void Clamp() {
|
||||
boxes_.resize(size());
|
||||
*selected_ = std::clamp(*selected_, 0, size() - 1);
|
||||
focused_entry() = std::clamp(focused_entry(), 0, size() - 1);
|
||||
}
|
||||
|
||||
bool Focusable() const final { return size(); }
|
||||
int& focused_entry() { return option_->focused_entry(); }
|
||||
int size() const { return entries_.size(); }
|
||||
|
||||
ConstStringListRef entries_;
|
||||
int* selected_ = 0;
|
||||
|
Reference in New Issue
Block a user