ArthurSonzogni / FTXUI

:computer: C++ Functional Terminal User Interface. :heart:
MIT License
6.73k stars 401 forks source link

hscroll_indicator #752

Closed ibrahimnasson closed 10 months ago

ibrahimnasson commented 11 months ago

could we get this element Decorator as i notice we only have the vscroll_indicator

ArthurSonzogni commented 11 months ago

Sound like a good idea, would you like to give it a try?

ibrahimnasson commented 11 months ago

I have tried copying from the vscroll_indicator but the concept seems to be slipping away

ibrahimnasson commented 11 months ago

Element hscroll_indicator(Element child) { class Impl : public NodeDecorator { using NodeDecorator::NodeDecorator;

void ComputeRequirement() override {
  NodeDecorator::ComputeRequirement();
  requirement_ = children_[0]->requirement();
  requirement_.min_y++;
}

void SetBox(Box box) override {
  box_ = box;
  box.y_max--;
  children_[0]->SetBox(box);
}

void Render(Screen& screen) final {
  NodeDecorator::Render(screen);

  const Box& stencil = screen.stencil;

  const int size_inner = box_.x_max - box_.x_min;
  if (size_inner <= 0) {
    return;
  }
  const int size_outter = stencil.x_max - stencil.x_min + 1;
  if (size_outter >= size_inner) {
    return;
  }

  int size = 2 * size_outter * size_outter / size_inner;
  size = std::max(size, 1);

  const int start_x =
      2 * stencil.x_min +  //
      2 * (stencil.x_min - box_.x_min) * size_outter / size_inner;

  const int y = stencil.y_max;
  for (int x = stencil.x_min; x <= stencil.y_max; ++x) {
    const int x_up = 2 * x + 0;
    const int x_down = 2 * x + 1;
    const bool up = (start_x <= x_up) && (x_up <= start_x + size);
    const bool down = (start_x <= x_down) && (x_down <= start_x + size);

    const char* c = up ? (down ? "__" : "_ ") : (down ? " _" : " ");  // NOLINT
    screen.PixelAt(x, y) = Pixel();
    screen.PixelAt(x, y).character = c;
  }
}

}; return std::make_shared(std::move(child)); }

ArthurSonzogni commented 11 months ago

Thanks! I will add the feature from this PR: https://github.com/ArthurSonzogni/FTXUI/pull/753