From 5b9be0bdf61b4005cb3764eb0838ade034a51cf0 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Fri, 8 Dec 2023 07:21:10 -0500 Subject: [PATCH] refactor: no longer calculating the area, but rather storing it --- src/node.cpp | 48 ++++++++++----------------------------------- src/node.h | 5 +++-- src/window_tree.cpp | 27 ++++++------------------- 3 files changed, 19 insertions(+), 61 deletions(-) diff --git a/src/node.cpp b/src/node.cpp index 05f15ef..565f588 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -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 parent, miral::Window &window) +Node::Node(geom::Rectangle area, std::shared_ptr 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(shared_from_this(), window)); + sub_nodes.push_back(std::make_shared( + geom::Rectangle{window.top_left(), window.size()}, shared_from_this(), window)); } std::shared_ptr Node::find_node_for_window(miral::Window &window) diff --git a/src/node.h b/src/node.h index 0f6d413..0896c74 100644 --- a/src/node.h +++ b/src/node.h @@ -43,8 +43,8 @@ enum class NodeDirection class Node : public std::enable_shared_from_this { public: - Node(); - Node(std::shared_ptr parent, miral::Window& window); + Node(geom::Rectangle); + Node(geom::Rectangle, std::shared_ptr 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> sub_nodes; NodeState state; NodeDirection direction = NodeDirection::horizontal; + geom::Rectangle area; }; } diff --git a/src/window_tree.cpp b/src/window_tree.cpp index 66c0648..083d24a 100644 --- a/src/window_tree.cpp +++ b/src/window_tree.cpp @@ -22,7 +22,7 @@ using namespace miracle; WindowTree::WindowTree(geom::Size default_size) - : root_lane{std::make_shared()}, + : root_lane{std::make_shared(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) 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(active_lane, window)); + geom::Rectangle rectangle = active_lane->get_rectangle(); + active_lane->get_sub_nodes().push_back(std::make_shared( + geom::Rectangle{window.top_left(), window.size()}, + active_lane, + window)); active_lane->set_rectangle(rectangle); advise_focus_gained(window); }