2021-12-13 18:38:31 +08:00
|
|
|
#include <memory> // for make_shared
|
|
|
|
#include <utility> // for move
|
|
|
|
|
2022-01-07 18:03:54 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for Decorator, Element, focusPosition, focusPositionRelative
|
2021-12-13 18:38:31 +08:00
|
|
|
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
|
2022-01-07 18:03:54 +08:00
|
|
|
#include "ftxui/dom/requirement.hpp" // for Requirement, Requirement::NORMAL, Requirement::Selection
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
2021-12-13 18:38:31 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
|
|
|
/// @brief Used inside a `frame`, this force the view to be scrolled toward a
|
|
|
|
/// a given position. The position is expressed in proportion of the requested
|
|
|
|
/// size.
|
2022-01-07 18:03:54 +08:00
|
|
|
///
|
2021-12-13 18:38:31 +08:00
|
|
|
/// For instance:
|
|
|
|
/// - (0, 0) means that the view is scrolled toward the upper left.
|
|
|
|
/// - (1, 0) means that the view is scrolled toward the upper right.
|
|
|
|
/// - (0, 1) means that the view is scrolled toward the bottom left.
|
|
|
|
/// @ingroup dom
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = huge_document()
|
|
|
|
/// | focusPositionRelative(0.f, 1.f)
|
|
|
|
/// | frame;
|
|
|
|
/// ```
|
|
|
|
Decorator focusPositionRelative(float x, float y) {
|
|
|
|
class Impl : public NodeDecorator {
|
|
|
|
public:
|
|
|
|
Impl(Element child, float x, float y)
|
2022-03-31 08:17:43 +08:00
|
|
|
: NodeDecorator(std::move(child)), x_(x), y_(y) {}
|
2021-12-13 18:38:31 +08:00
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
|
|
|
NodeDecorator::ComputeRequirement();
|
|
|
|
requirement_.selection = Requirement::Selection::NORMAL;
|
|
|
|
|
|
|
|
Box& box = requirement_.selected_box;
|
2022-03-31 08:17:43 +08:00
|
|
|
box.x_min = (int)((float)requirement_.min_x * x_);
|
|
|
|
box.y_min = (int)((float)requirement_.min_y * y_);
|
|
|
|
box.x_max = (int)((float)requirement_.min_x * x_);
|
|
|
|
box.y_max = (int)((float)requirement_.min_y * y_);
|
2021-12-13 18:38:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const float x_;
|
|
|
|
const float y_;
|
|
|
|
};
|
|
|
|
|
|
|
|
return [x, y](Element child) {
|
|
|
|
return std::make_shared<Impl>(std::move(child), x, y);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Used inside a `frame`, this force the view to be scrolled toward a
|
|
|
|
/// a given position. The position is expressed in the numbers of cells.
|
2022-01-07 18:03:54 +08:00
|
|
|
///
|
2021-12-13 18:38:31 +08:00
|
|
|
/// @ingroup dom
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// Element document = huge_document()
|
|
|
|
/// | focusPosition(10, 10)
|
|
|
|
/// | frame;
|
|
|
|
/// ```
|
|
|
|
Decorator focusPosition(int x, int y) {
|
|
|
|
class Impl : public NodeDecorator {
|
|
|
|
public:
|
2022-03-31 08:17:43 +08:00
|
|
|
Impl(Element child, int x, int y)
|
|
|
|
: NodeDecorator(std::move(child)), x_(x), y_(y) {}
|
2021-12-13 18:38:31 +08:00
|
|
|
|
|
|
|
void ComputeRequirement() override {
|
|
|
|
NodeDecorator::ComputeRequirement();
|
|
|
|
requirement_.selection = Requirement::Selection::NORMAL;
|
|
|
|
|
|
|
|
Box& box = requirement_.selected_box;
|
|
|
|
box.x_min = x_;
|
|
|
|
box.y_min = y_;
|
|
|
|
box.x_max = x_;
|
|
|
|
box.y_max = y_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int x_;
|
|
|
|
const int y_;
|
|
|
|
};
|
|
|
|
|
|
|
|
return [x, y](Element child) {
|
|
|
|
return std::make_shared<Impl>(std::move(child), x, y);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ftxui
|
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|