mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-11-04 05:28:15 +08:00 
			
		
		
		
	Update tutorial.md
This commit is contained in:
		@@ -20,6 +20,9 @@ class CheckBox : public Component {
 | 
			
		||||
  std::wstring checked = L"☑ ";
 | 
			
		||||
  std::wstring unchecked = L"☐ ";
 | 
			
		||||
 | 
			
		||||
  Decorator focused_style = inverted;
 | 
			
		||||
  Decorator unfocused_style = nothing;
 | 
			
		||||
 | 
			
		||||
  // State update callback.
 | 
			
		||||
  std::function<void()> on_change = [](){};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ class ScreenInteractive : public Screen {
 | 
			
		||||
  public:
 | 
			
		||||
    static ScreenInteractive FixedSize(size_t dimx, size_t dimy);
 | 
			
		||||
    static ScreenInteractive Fullscreen();
 | 
			
		||||
    static ScreenInteractive FitComponent();
 | 
			
		||||
    static ScreenInteractive TerminalOutput();
 | 
			
		||||
 | 
			
		||||
    ~ScreenInteractive();
 | 
			
		||||
@@ -23,9 +24,10 @@ class ScreenInteractive : public Screen {
 | 
			
		||||
   bool quit_ = false;
 | 
			
		||||
 | 
			
		||||
   enum class Dimension {
 | 
			
		||||
     FitComponent,
 | 
			
		||||
     Fixed,
 | 
			
		||||
     TerminalOutput,
 | 
			
		||||
     Fullscreen,
 | 
			
		||||
     TerminalOutput,
 | 
			
		||||
   };
 | 
			
		||||
   Dimension dimension_ = Dimension::Fixed;
 | 
			
		||||
   ScreenInteractive(size_t dimx, size_t dimy, Dimension dimension);
 | 
			
		||||
 
 | 
			
		||||
@@ -50,6 +50,8 @@ class Screen {
 | 
			
		||||
  // Fill with space.
 | 
			
		||||
  void Clear();
 | 
			
		||||
 | 
			
		||||
  void ApplyShader();
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  size_t dimx_;
 | 
			
		||||
  size_t dimy_;
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,8 @@
 | 
			
		||||
namespace ftxui {
 | 
			
		||||
 | 
			
		||||
Element CheckBox::Render() {
 | 
			
		||||
  auto style = Focused() ? bold : nothing;
 | 
			
		||||
  return text((state ? checked : unchecked) + label) | style;
 | 
			
		||||
  auto style = Focused() ? focused_style : unfocused_style;
 | 
			
		||||
  return hbox(text(state ? checked : unchecked), text(label) | style);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool CheckBox::OnEvent(Event event) {
 | 
			
		||||
 
 | 
			
		||||
@@ -65,6 +65,11 @@ ScreenInteractive ScreenInteractive::TerminalOutput() {
 | 
			
		||||
  return ScreenInteractive(0, 0, Dimension::TerminalOutput);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
ScreenInteractive ScreenInteractive::FitComponent() {
 | 
			
		||||
  return ScreenInteractive(0, 0, Dimension::FitComponent);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ScreenInteractive::Loop(Component* component) {
 | 
			
		||||
  //std::cout << "\033[?9h";    [> Send Mouse Row & Column on Button Press <]
 | 
			
		||||
  //std::cout << "\033[?1000h"; [> Send Mouse X & Y on button press and release <]
 | 
			
		||||
@@ -114,10 +119,14 @@ void ScreenInteractive::Draw(Component* component) {
 | 
			
		||||
      dimy = document->requirement().min.y;
 | 
			
		||||
      break;
 | 
			
		||||
    case Dimension::Fullscreen:
 | 
			
		||||
      document->ComputeRequirement();
 | 
			
		||||
      dimx = Terminal::Size().dimx;
 | 
			
		||||
      dimy = Terminal::Size().dimy;
 | 
			
		||||
      break;
 | 
			
		||||
    case Dimension::FitComponent:
 | 
			
		||||
      document->ComputeRequirement();
 | 
			
		||||
      dimx = document->requirement().min.x;
 | 
			
		||||
      dimy = document->requirement().min.y;
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (dimx != dimx_ || dimy != dimy_) {
 | 
			
		||||
 
 | 
			
		||||
@@ -61,20 +61,6 @@ class Frame : public Node {
 | 
			
		||||
      screen.at(box_.right,y) = charset[5];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Try to merge with separator.
 | 
			
		||||
    for(float x = box_.left + 1; x<box_.right; ++x) {
 | 
			
		||||
      if (screen.at(x, box_.top + 1) == charset[5])
 | 
			
		||||
        screen.at(x, box_.top) = charset[6];
 | 
			
		||||
      if (screen.at(x, box_.bottom - 1) == charset[5])
 | 
			
		||||
        screen.at(x, box_.bottom) = charset[7];
 | 
			
		||||
    }
 | 
			
		||||
    for(float y = box_.top + 1; y<box_.bottom; ++y) {
 | 
			
		||||
      if (screen.at(box_.left+1, y) == charset[4])
 | 
			
		||||
        screen.at(box_.left, y) = charset[9];
 | 
			
		||||
      if (screen.at(box_.right-1, y) == charset[4])
 | 
			
		||||
        screen.at(box_.right,y) = charset[8];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Draw title.
 | 
			
		||||
    if (children.size() == 2)
 | 
			
		||||
      children[1]->Render(screen);
 | 
			
		||||
 
 | 
			
		||||
@@ -38,6 +38,9 @@ void Render(Screen& screen, Node* node) {
 | 
			
		||||
 | 
			
		||||
  // Step 3: Draw the element.
 | 
			
		||||
  node->Render(screen);
 | 
			
		||||
 | 
			
		||||
  // Step 4: Apply shaders
 | 
			
		||||
  screen.ApplyShader();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
};  // namespace ftxui
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,10 @@ static const wchar_t* BLINK_RESET = L"\e[25m";
 | 
			
		||||
static const wchar_t* INVERTED_SET = L"\e[7m";
 | 
			
		||||
static const wchar_t* INVERTED_RESET = L"\e[27m";
 | 
			
		||||
 | 
			
		||||
static const char* MOVE_LEFT = "\r";
 | 
			
		||||
static const char* MOVE_UP = "\e[1A";
 | 
			
		||||
static const char* CLEAR_LINE = "\e[2K";
 | 
			
		||||
 | 
			
		||||
Screen::Screen(size_t dimx, size_t dimy)
 | 
			
		||||
    : dimx_(dimx), dimy_(dimy), pixels_(dimy, std::vector<Pixel>(dimx)) {}
 | 
			
		||||
 | 
			
		||||
@@ -94,9 +98,9 @@ Screen Screen::TerminalOutput(std::unique_ptr<Node>& element) {
 | 
			
		||||
 | 
			
		||||
std::string Screen::ResetPosition() {
 | 
			
		||||
  std::stringstream ss;
 | 
			
		||||
  ss << '\r';
 | 
			
		||||
  ss << MOVE_LEFT << CLEAR_LINE;
 | 
			
		||||
  for (size_t y = 1; y < dimy_; ++y) {
 | 
			
		||||
    ss << "\e[2K\r\e[1A";
 | 
			
		||||
    ss << MOVE_UP << CLEAR_LINE;
 | 
			
		||||
  }
 | 
			
		||||
  return ss.str();
 | 
			
		||||
}
 | 
			
		||||
@@ -106,4 +110,37 @@ void Screen::Clear() {
 | 
			
		||||
                                            std::vector<Pixel>(dimx_, Pixel()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Screen::ApplyShader() {
 | 
			
		||||
 | 
			
		||||
  // Merge box characters togethers.
 | 
			
		||||
  for(size_t y = 1; y<dimy_; ++y) {
 | 
			
		||||
    for(size_t x = 1; x<dimx_; ++x) {
 | 
			
		||||
      wchar_t& left = at(x - 1, y);
 | 
			
		||||
      wchar_t& top = at(x, y - 1);
 | 
			
		||||
      wchar_t& cur = at(x, y);
 | 
			
		||||
 | 
			
		||||
      // Left vs current
 | 
			
		||||
      if (cur== U'│' && left == U'─')
 | 
			
		||||
        cur= U'┤';
 | 
			
		||||
      if (cur== U'─' && left == U'│')
 | 
			
		||||
        left = U'├';
 | 
			
		||||
      if (cur== U'├' && left == U'─')
 | 
			
		||||
        cur= U'┼';
 | 
			
		||||
      if (cur== U'─' && left == U'┤')
 | 
			
		||||
        left = U'┼';
 | 
			
		||||
 | 
			
		||||
      // Top vs current
 | 
			
		||||
      if (cur== U'─' && top == U'│')
 | 
			
		||||
        cur= U'┴';
 | 
			
		||||
      if (cur== U'│' && top == U'─')
 | 
			
		||||
        top = U'┬';
 | 
			
		||||
      if (cur== U'┬' && top == U'│')
 | 
			
		||||
        cur= U'┼';
 | 
			
		||||
      if (cur== U'│' && top == U'┴')
 | 
			
		||||
        top = U'┼';
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
};  // namespace ftxui
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user