refactor: no longer calculating the area, but rather storing it

This commit is contained in:
Matthew Kosarek 2023-12-08 07:21:10 -05:00
parent 1909dffc40
commit 5b9be0bdf6
3 changed files with 19 additions and 61 deletions

View File

@ -18,56 +18,27 @@
using namespace miracle;
Node::Node() : state{NodeState::lane}
Node::Node(geom::Rectangle area)
: state{NodeState::lane},
area{area}
{}
Node::Node(std::shared_ptr<Node> parent, miral::Window &window)
Node::Node(geom::Rectangle area, std::shared_ptr<Node> parent, miral::Window &window)
: parent{parent},
window{window},
state{NodeState::window}
state{NodeState::window},
area{area}
{
}
geom::Rectangle Node::get_rectangle()
{
if (is_window())
{
return geom::Rectangle{window.top_left(), window.size()};
}
else
{
geom::Point top_left{INT_MAX, INT_MAX};
geom::Point bottom_right{0, 0};
for (auto item : sub_nodes)
{
auto item_rectangle = item->get_rectangle();
top_left.x = std::min(top_left.x, item_rectangle.top_left.x);
top_left.y = std::min(top_left.y, item_rectangle.top_left.y);
bottom_right.x = geom::X{
std::max(
bottom_right.x.as_int(),
item_rectangle.top_left.x.as_int() + item_rectangle.size.width.as_int())
};
bottom_right.y = geom::Y{
std::max(
bottom_right.y.as_int(),
item_rectangle.top_left.y.as_int() + item_rectangle.size.height.as_int())
};
}
return geom::Rectangle(
top_left,
geom::Size {
geom::Width{bottom_right.x.as_int() - top_left.x.as_value()},
geom::Height{bottom_right.y.as_int() - top_left.y.as_value()}
});
}
return area;
}
void Node::set_rectangle(geom::Rectangle rect)
{
area = rect;
if (is_window())
{
window.move_to(rect.top_left);
@ -129,7 +100,8 @@ void Node::to_lane()
if (parent != nullptr && parent->sub_nodes.size() == 1)
return;
sub_nodes.push_back(std::make_shared<Node>(shared_from_this(), window));
sub_nodes.push_back(std::make_shared<Node>(
geom::Rectangle{window.top_left(), window.size()}, shared_from_this(), window));
}
std::shared_ptr<miracle::Node> Node::find_node_for_window(miral::Window &window)

View File

@ -43,8 +43,8 @@ enum class NodeDirection
class Node : public std::enable_shared_from_this<Node>
{
public:
Node();
Node(std::shared_ptr<Node> parent, miral::Window& window);
Node(geom::Rectangle);
Node(geom::Rectangle, std::shared_ptr<Node> parent, miral::Window& window);
/// The rectangle defined by the node can be retrieved dynamically
/// by calculating the dimensions of the content in this node
@ -76,6 +76,7 @@ private:
std::vector<std::shared_ptr<Node>> sub_nodes;
NodeState state;
NodeDirection direction = NodeDirection::horizontal;
geom::Rectangle area;
};
}

View File

@ -22,7 +22,7 @@
using namespace miracle;
WindowTree::WindowTree(geom::Size default_size)
: root_lane{std::make_shared<Node>()},
: root_lane{std::make_shared<Node>(geom::Rectangle{geom::Point{}, default_size})},
active_lane{root_lane},
size{default_size}
{
@ -36,13 +36,6 @@ void WindowTree::resize_node_to(std::shared_ptr<Node> node)
miral::WindowSpecification WindowTree::allocate_position(const miral::WindowSpecification &requested_specification)
{
miral::WindowSpecification new_spec = requested_specification;
if (active_lane->is_root() && active_lane->get_sub_nodes().size() == 0)
{
// Special case: take up the full size of the root node.
new_spec.top_left() = geom::Point{0, 0};
new_spec.size() = size;
return new_spec;
}
// Everyone get out the damn way. Slide the other items to the left for now.
// TODO: Handle inserting in the middle of the group
@ -76,19 +69,11 @@ miral::WindowSpecification WindowTree::allocate_position(const miral::WindowSpec
void WindowTree::confirm(miral::Window &window)
{
geom::Rectangle rectangle;
if (active_lane->is_root() && active_lane->get_sub_nodes().size() == 0)
{
// Special case: take up the full size of the root node.
rectangle.top_left = geom::Point{0, 0};
rectangle.size = size;
}
else
{
rectangle = active_lane->get_rectangle();
}
active_lane->get_sub_nodes().push_back(std::make_shared<Node>(active_lane, window));
geom::Rectangle rectangle = active_lane->get_rectangle();
active_lane->get_sub_nodes().push_back(std::make_shared<Node>(
geom::Rectangle{window.top_left(), window.size()},
active_lane,
window));
active_lane->set_rectangle(rectangle);
advise_focus_gained(window);
}