mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-11-04 13:38:14 +08:00 
			
		
		
		
	Fix slider focus. (#549)
This resolves: https://github.com/ArthurSonzogni/FTXUI/issues/547 From discussion: https://github.com/ArthurSonzogni/FTXUI/discussions/546
This commit is contained in:
		@@ -227,6 +227,7 @@ class SliderWithLabel : public ComponentBase {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Element Render() override {
 | 
			
		||||
    auto focus_management = Focused() ? focus : Active() ? select : nothing;
 | 
			
		||||
    auto gauge_color = Focused() ? color(Color::White) : color(Color::GrayDark);
 | 
			
		||||
    return hbox({
 | 
			
		||||
               text(label_()) | dim | vcenter,
 | 
			
		||||
@@ -236,7 +237,7 @@ class SliderWithLabel : public ComponentBase {
 | 
			
		||||
                   text("]"),
 | 
			
		||||
               }) | xflex,
 | 
			
		||||
           }) |
 | 
			
		||||
           gauge_color | xflex | reflect(box_);
 | 
			
		||||
           gauge_color | xflex | reflect(box_) | focus_management;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ConstStringRef label_;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
#include <gtest/gtest.h>  // for AssertionResult, Message, TestPartResult, Test, EXPECT_TRUE, EXPECT_EQ, SuiteApiResolver, TestInfo (ptr only), EXPECT_FALSE, TEST, TestFactoryImpl
 | 
			
		||||
#include <array>          // for array
 | 
			
		||||
#include <ftxui/component/mouse.hpp>  // for Mouse, Mouse::Left, Mouse::Pressed, Mouse::Released
 | 
			
		||||
#include <ftxui/dom/elements.hpp>  // for GaugeDirection, GaugeDirection::Down, GaugeDirection::Left, GaugeDirection::Right, GaugeDirection::Up
 | 
			
		||||
#include <memory>  // for __shared_ptr_access, shared_ptr, allocator
 | 
			
		||||
@@ -129,6 +130,59 @@ TEST(SliderTest, Up) {
 | 
			
		||||
  EXPECT_FALSE(slider->OnEvent(MousePressed(2, 5)));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(SliderTest, Focus) {
 | 
			
		||||
  static std::array<int, 10> values;
 | 
			
		||||
  auto container = Container::Vertical({});
 | 
			
		||||
  for(size_t i = 0; i<values.size(); ++i) {
 | 
			
		||||
    container->Add(Slider(std::to_string(i), &values[i]));
 | 
			
		||||
  }
 | 
			
		||||
  container |= frame;
 | 
			
		||||
 | 
			
		||||
  Screen screen(10, 3);
 | 
			
		||||
 | 
			
		||||
  Render(screen, container->Render());
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 0), "0"); // Select 0
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 1), "1");
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 2), "2");
 | 
			
		||||
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown));
 | 
			
		||||
  Render(screen, container->Render());
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 0), "0");
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 1), "1"); // Select 1
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 2), "2");
 | 
			
		||||
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown));
 | 
			
		||||
  Render(screen, container->Render());
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 0), "1");
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 1), "2"); // Select 2
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 2), "3");
 | 
			
		||||
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown)); // Select 3
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown)); // Select 4
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown)); // Select 5
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown)); // Select 6
 | 
			
		||||
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown));
 | 
			
		||||
  Render(screen, container->Render());
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 0), "6");
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 1), "7"); // Select 7
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 2), "8");
 | 
			
		||||
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown));
 | 
			
		||||
  Render(screen, container->Render());
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 0), "7");
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 1), "8"); // Select 8
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 2), "9");
 | 
			
		||||
 | 
			
		||||
  EXPECT_TRUE(container->OnEvent(Event::ArrowDown));
 | 
			
		||||
  Render(screen, container->Render());
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 0), "7");
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 1), "8");
 | 
			
		||||
  EXPECT_EQ(screen.at(0, 2), "9"); // Select 9
 | 
			
		||||
  
 | 
			
		||||
  EXPECT_FALSE(container->OnEvent(Event::ArrowDown));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ftxui
 | 
			
		||||
 | 
			
		||||
// Copyright 2022 Arthur Sonzogni. All rights reserved.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user