YggdrasilWM  0.1.1
A tiny window manager coded in C++
TreeLayoutManager Class Reference

TreeLayoutManager class this class is a subclass of LayoutManager it handles the tree style layout When a client is added, it looks recursively for the biggest space and splits it in two placing the new client in the left space and the old client in the right space. More...

#include <TreeLayoutManager.hpp>

Inheritance diagram for TreeLayoutManager:
Inheritance graph
Collaboration diagram for TreeLayoutManager:
Collaboration graph

Public Member Functions

 TreeLayoutManager (Display *display, Window root, int sizeX, int sizeY, int posX, int posY, int borderSize, int gap, int barHeight)
 Construct a new Tree Layout Manager object the size and position of the root space should take account of the bar size and position and the border size. More...
 
 ~TreeLayoutManager () override
 Destroy the Tree Layout Manager object. More...
 
void updateGeometry (unsigned int sizeX, unsigned int sizeY, unsigned int posX, unsigned int posY) override
 update the geometry of the layout (resize) More...
 
void reSize (const Point &size, const Point &pos) override
 
void recursiveResize (const Point &size, const Point &pos, BinarySpace *space)
 
BinarySpacefindSpace (Client *client)
 find the space containing the client it looks recursively for the space containing the client More...
 
BinarySpacefindSpaceRecursive (Client *client, BinarySpace *space)
 recursive method to find the space containing the client More...
 
BinarySpacefindSpace (int index)
 
void addClient (std::shared_ptr< Client > client) override
 add a client to the layout it looks for the biggest space and splits it in two More...
 
void addClientRecursive (std::shared_ptr< Client > client, BinarySpace *space)
 
void placeClientInSpace (const std::shared_ptr< Client > &client, BinarySpace *space)
 place a client in a space moves and resizes the client window to fit the space restack the client window More...
 
void splitSpace (const std::shared_ptr< Client > &client, BinarySpace *space, bool splitAlongX)
 split a space in two Move the client to the left space and the old client to the right space increase the subspaces counter of all the parent spaces More...
 
void removeClient (Client *client) override
 remove a client from the layout use the recursive method to remove the client More...
 
void removeClientRecursive (Client *client, BinarySpace *space)
 recursive method to add a client to the layout More...
 
void growSpace (Client *client, int inc)
 grow the space of the client in the x axis More...
 
void recursiveShrinkSiblingSpace (BinarySpace *space, int inc, bool vertical)
 

Protected Attributes

int screen_width_
 
int screen_height_
 
int gap_
 
int border_size_
 
int space_count_
 
Display * display_
 
Window rootWindow_
 

Private Member Functions

void deleteSpace (BinarySpace *space)
 

Private Attributes

std::unique_ptr< BinarySpacerootSpace_
 

Detailed Description

TreeLayoutManager class this class is a subclass of LayoutManager it handles the tree style layout When a client is added, it looks recursively for the biggest space and splits it in two placing the new client in the left space and the old client in the right space.

Constructor & Destructor Documentation

◆ TreeLayoutManager()

TreeLayoutManager::TreeLayoutManager ( Display *  display,
Window  root,
int  sizeX,
int  sizeY,
int  posX,
int  posY,
int  borderSize,
int  gap,
int  barHeight 
)

Construct a new Tree Layout Manager object the size and position of the root space should take account of the bar size and position and the border size.

Parameters
display
root
sizeX
sizeY
posX
posY
borderSize
gap
barHeight
37  :
38  LayoutManager(display, root, sizeX, sizeY, posX, posY, gap, barHeight) {
39  Point pos(posX, posY);
40  Point size(sizeX - borderSize, sizeY - borderSize);
41  this->rootSpace_ = std::make_unique<BinarySpace>(pos, size, 0);
42 }
LayoutManager(Display *display, Window root, int size_x, int size_y, int pos_x, int pos_y, int gap, int border_size)
Construct a new LayoutManager object.
Definition: LayoutManager.cpp:29
std::unique_ptr< BinarySpace > rootSpace_
Definition: TreeLayoutManager.hpp:156
Point struct This struct represents a 2D point.
Definition: Point.hpp:8

References rootSpace_.

◆ ~TreeLayoutManager()

TreeLayoutManager::~TreeLayoutManager ( )
override

Destroy the Tree Layout Manager object.

52 {}

Member Function Documentation

◆ addClient()

void TreeLayoutManager::addClient ( std::shared_ptr< Client client)
overridevirtual

add a client to the layout it looks for the biggest space and splits it in two

Parameters
client
See also
addClientRecursive
placeClientInSpace

Implements LayoutManager.

110  {
111  addClientRecursive(client, rootSpace_.get());
112 }
void addClientRecursive(std::shared_ptr< Client > client, BinarySpace *space)
Definition: TreeLayoutManager.cpp:113

References addClientRecursive(), and rootSpace_.

Here is the call graph for this function:

◆ addClientRecursive()

void TreeLayoutManager::addClientRecursive ( std::shared_ptr< Client client,
BinarySpace space 
)
113  {
114  if (space->getClient() == nullptr && space->getLeft() == nullptr && space->getRight() == nullptr ) {
115  placeClientInSpace(client, space);
116  return;
117  }
118  if (space->getLeft() != nullptr && space->getRight() != nullptr) {
119  if (space->getLeft()->getSubspaceCount() > space->getRight()->getSubspaceCount()) {
120  addClientRecursive(client, space->getRight().get());
121  } else {
122  addClientRecursive(client, space->getLeft().get());
123  }
124  return;
125  }
126  if (space->getSize().x > space->getSize().y) {
127  splitSpace(client, space, true);
128  } else {
129  splitSpace(client, space, false);
130  }
131 }
std::shared_ptr< Client > getClient() const
Definition: BinarySpace.cpp:28
const std::unique_ptr< BinarySpace > & getRight() const
Definition: BinarySpace.cpp:24
const Point & getSize() const
Definition: BinarySpace.cpp:18
const std::unique_ptr< BinarySpace > & getLeft() const
Definition: BinarySpace.cpp:26
void splitSpace(const std::shared_ptr< Client > &client, BinarySpace *space, bool splitAlongX)
split a space in two Move the client to the left space and the old client to the right space increase...
Definition: TreeLayoutManager.cpp:139
void placeClientInSpace(const std::shared_ptr< Client > &client, BinarySpace *space)
place a client in a space moves and resizes the client window to fit the space restack the client win...
Definition: TreeLayoutManager.cpp:132
unsigned int x
Definition: Point.hpp:9
unsigned int y
Definition: Point.hpp:10

References BinarySpace::getClient(), BinarySpace::getLeft(), BinarySpace::getRight(), BinarySpace::getSize(), placeClientInSpace(), splitSpace(), Point::x, and Point::y.

Referenced by addClient().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deleteSpace()

void TreeLayoutManager::deleteSpace ( BinarySpace space)
private
43  {
44  if (space->getLeft() != nullptr) {
45  deleteSpace(space->getLeft().get());
46  }
47  if (space->getRight() != nullptr) {
48  deleteSpace(space->getRight().get());
49  }
50  delete space;
51 }
void deleteSpace(BinarySpace *space)
Definition: TreeLayoutManager.cpp:43

References BinarySpace::getLeft(), and BinarySpace::getRight().

Here is the call graph for this function:

◆ findSpace() [1/2]

BinarySpace * TreeLayoutManager::findSpace ( Client client)

find the space containing the client it looks recursively for the space containing the client

Parameters
client
Returns
75  {
76  BinarySpace* space = rootSpace_.get();
77  return findSpaceRecursive(client, space);
78 }
BinarySpace class This class represents a space in the layout. it's designed like a binary tree,...
Definition: BinarySpace.hpp:41
BinarySpace * findSpaceRecursive(Client *client, BinarySpace *space)
recursive method to find the space containing the client
Definition: TreeLayoutManager.cpp:56

References findSpaceRecursive(), and rootSpace_.

Referenced by growSpace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findSpace() [2/2]

BinarySpace * TreeLayoutManager::findSpace ( int  index)
79  {
80  return nullptr;
81 }

◆ findSpaceRecursive()

BinarySpace * TreeLayoutManager::findSpaceRecursive ( Client client,
BinarySpace space 
)

recursive method to find the space containing the client

Parameters
client
space
Returns
57  {
58  if (space->getClient().get() == client) {
59  return space;
60  }
61  if (space->getLeft() != nullptr) {
62  BinarySpace * leftSpace = findSpaceRecursive(client, space->getLeft().get());
63  if (leftSpace != nullptr) {
64  return leftSpace;
65  }
66  }
67  if (space->getRight() != nullptr) {
68  BinarySpace * rightSpace = findSpaceRecursive(client, space->getRight().get());
69  if (rightSpace != nullptr) {
70  return rightSpace;
71  }
72  }
73  return nullptr;
74 }

References BinarySpace::getClient(), BinarySpace::getLeft(), and BinarySpace::getRight().

Referenced by findSpace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ growSpace()

void TreeLayoutManager::growSpace ( Client client,
int  inc 
)

grow the space of the client in the x axis

Todo:
this method is not implemented yet
Parameters
client
165  {
166  BinarySpace *space = findSpace(client);
167  if (space == nullptr || space == rootSpace_.get()) {
168  return;
169  }
170  bool isLeftChild = (space->getParent()->getLeft().get() == space);
171  int dx = std::abs((int)space->getSize().x - (int)space->getParent()->getSize().x);
172  int dy = std::abs((int)space->getSize().y - (int)space->getParent()->getSize().y);
173  bool vertical;
174  if (std::max(dx, dy) == dx) {
175  vertical = false;
176  } else {
177  vertical = true;
178  }
179  BinarySpace *siblingSpace = isLeftChild ? space->getParent()->getRight().get()
180  : space->getParent()->getLeft().get();
181  space->setSize(vertical ? Point(space->getSize().x, space->getSize().y + inc) : Point(space->getSize().x + inc,
182  space->getSize().y));
183  if (!isLeftChild) {
184  space->setPos(vertical ? Point(space->getPos().x, space->getPos().y - inc) : Point(space->getPos().x - inc,
185  space->getPos().y));
186  } else {
187  siblingSpace->setPos(vertical ? Point(siblingSpace->getPos().x, siblingSpace->getPos().y + inc) : Point(
188  siblingSpace->getPos().x + inc, siblingSpace->getPos().y));
189  }
190  if (space->getClient() != nullptr) {
191  placeClientInSpace(space->getClient(), space);
192  }
193  siblingSpace->setSize(vertical ? Point(siblingSpace->getSize().x, siblingSpace->getSize().y - inc) : Point(
194  siblingSpace->getSize().x - inc, siblingSpace->getSize().y));
195  if (siblingSpace->getClient() != nullptr) {
196  placeClientInSpace(siblingSpace->getClient(), siblingSpace);
197  } else {
198  recursiveShrinkSiblingSpace(siblingSpace, inc, vertical);
199  }
200 }
BinarySpace * getParent() const
Definition: BinarySpace.cpp:22
void setPos(const Point &pos)
Definition: BinarySpace.cpp:17
const Point & getPos() const
Definition: BinarySpace.cpp:15
void setSize(const Point &size)
Definition: BinarySpace.cpp:19
BinarySpace * findSpace(Client *client)
find the space containing the client it looks recursively for the space containing the client
Definition: TreeLayoutManager.cpp:75
void recursiveShrinkSiblingSpace(BinarySpace *space, int inc, bool vertical)
Definition: TreeLayoutManager.cpp:201

References findSpace(), BinarySpace::getClient(), BinarySpace::getLeft(), BinarySpace::getParent(), BinarySpace::getPos(), BinarySpace::getRight(), BinarySpace::getSize(), placeClientInSpace(), recursiveShrinkSiblingSpace(), rootSpace_, BinarySpace::setPos(), BinarySpace::setSize(), Point::x, and Point::y.

Referenced by Grow::execute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ placeClientInSpace()

void TreeLayoutManager::placeClientInSpace ( const std::shared_ptr< Client > &  client,
BinarySpace space 
)

place a client in a space moves and resizes the client window to fit the space restack the client window

Parameters
client
space
132  {
133  client->move(static_cast<int>(space->getPos().x) + border_size_ + gap_ / 2,static_cast<int>(space->getPos().y) + border_size_ + gap_ / 2);
134  client->resize(space->getSize().x - (border_size_ * 2)- gap_, space->getSize().y - (border_size_ * 2) - gap_);
135  client->restack();
136  if (space->getClient().get() != client.get())
137  space->setClient(client);
138 }
void setClient(std::shared_ptr< Client > client)
Definition: BinarySpace.cpp:35
int gap_
Definition: LayoutManager.hpp:87
int border_size_
Definition: LayoutManager.hpp:88

References LayoutManager::border_size_, LayoutManager::gap_, BinarySpace::getClient(), BinarySpace::getPos(), BinarySpace::getSize(), BinarySpace::setClient(), Point::x, and Point::y.

Referenced by addClientRecursive(), growSpace(), recursiveResize(), recursiveShrinkSiblingSpace(), removeClientRecursive(), and splitSpace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ recursiveResize()

void TreeLayoutManager::recursiveResize ( const Point size,
const Point pos,
BinarySpace space 
)
224  {
225  Point oldSize = space->getSize();
226  space->setSize(size);
227  space->setPos(pos);
228  if (space->getClient() != nullptr) {
229  placeClientInSpace(space->getClient(), space);
230  }
231  if (space->getLeft() != nullptr) {
232  Point leftPos = pos;
233  Point leftSize = size;
234  Point rightPos = pos;
235  Point rightSize = size;
236  if (space->getLeft()->getSize().x == oldSize.x) {
237  leftSize.y = size.y / 2;
238  rightSize.y = size.y - leftSize.y;
239  rightPos.y = pos.y + leftSize.y;
240  } else {
241  leftSize.x = size.x / 2;
242  rightSize.x = size.x - leftSize.x;
243  rightPos.x = pos.x + leftSize.x;
244  }
245  recursiveResize(leftSize, leftPos, space->getLeft().get());
246  recursiveResize(rightSize, rightPos, space->getRight().get());
247  }
248 }
void recursiveResize(const Point &size, const Point &pos, BinarySpace *space)
Definition: TreeLayoutManager.cpp:222

References BinarySpace::getClient(), BinarySpace::getLeft(), BinarySpace::getRight(), BinarySpace::getSize(), placeClientInSpace(), BinarySpace::setPos(), BinarySpace::setSize(), Point::x, and Point::y.

Referenced by reSize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ recursiveShrinkSiblingSpace()

void TreeLayoutManager::recursiveShrinkSiblingSpace ( BinarySpace space,
int  inc,
bool  vertical 
)
201  {
202  if (space->getLeft() != nullptr) {
203  recursiveShrinkSiblingSpace(space->getLeft().get(), inc, vertical);
204  }
205  if (space->getRight() != nullptr) {
206  recursiveShrinkSiblingSpace(space->getRight().get(), inc, vertical);
207  }
208  space->setSize(vertical ? Point(space->getSize().x, space->getSize().y - inc) : Point(space->getSize().x - inc,
209  space->getSize().y));
210  if (space->getClient() != nullptr) {
211  placeClientInSpace(space->getClient(), space);
212  }
213 }

References BinarySpace::getClient(), BinarySpace::getLeft(), BinarySpace::getRight(), BinarySpace::getSize(), placeClientInSpace(), BinarySpace::setSize(), Point::x, and Point::y.

Referenced by growSpace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeClient()

void TreeLayoutManager::removeClient ( Client client)
overridevirtual

remove a client from the layout use the recursive method to remove the client

Parameters
client

Implements LayoutManager.

82  {
83  removeClientRecursive(client, rootSpace_.get());
84 }
void removeClientRecursive(Client *client, BinarySpace *space)
recursive method to add a client to the layout
Definition: TreeLayoutManager.cpp:85

References removeClientRecursive(), and rootSpace_.

Here is the call graph for this function:

◆ removeClientRecursive()

void TreeLayoutManager::removeClientRecursive ( Client client,
BinarySpace space 
)

recursive method to add a client to the layout

recursive method to remove a client from the layout

Parameters
client
space
85  {
86  if (space->getClient().get() == client) {
87  space->setClient(nullptr);
88  if (space != rootSpace_.get()) {
89  bool isLeftChild = (space->getParent()->getLeft().get() == space);
90  BinarySpace* siblingSpace = isLeftChild ? space->getParent()->getRight().get() : space->getParent()->getLeft().get();
91  if (siblingSpace->getClient() != nullptr) {
92  placeClientInSpace(siblingSpace->getClient(), space->getParent());
93  siblingSpace->setClient(nullptr);
94  }
95  if (isLeftChild) {
96  space->getParent()->setLeft(nullptr);
97  } else {
98  space->getParent()->setRight(nullptr);
99  }
100  }
101  return;
102  }
103  if (space->getLeft() != nullptr) {
104  removeClientRecursive(client, space->getLeft().get());
105  }
106  if (space->getRight() != nullptr) {
107  removeClientRecursive(client, space->getRight().get());
108  }
109 }
void setLeft(std::unique_ptr< BinarySpace > left)
Definition: BinarySpace.cpp:27
void setRight(std::unique_ptr< BinarySpace > right)
Definition: BinarySpace.cpp:25

References BinarySpace::getClient(), BinarySpace::getLeft(), BinarySpace::getParent(), BinarySpace::getRight(), placeClientInSpace(), rootSpace_, BinarySpace::setClient(), BinarySpace::setLeft(), and BinarySpace::setRight().

Referenced by removeClient().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reSize()

void TreeLayoutManager::reSize ( const Point size,
const Point pos 
)
overridevirtual

Implements LayoutManager.

215  {
216  if (rootSpace_->getSize().x == size.x && rootSpace_->getSize().y == size.y) {
217  return;
218  }
219  recursiveResize(size, pos,rootSpace_.get());
220 }

References recursiveResize(), rootSpace_, Point::x, and Point::y.

Referenced by updateGeometry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ splitSpace()

void TreeLayoutManager::splitSpace ( const std::shared_ptr< Client > &  client,
BinarySpace space,
bool  splitAlongX 
)

split a space in two Move the client to the left space and the old client to the right space increase the subspaces counter of all the parent spaces

Parameters
client
space
splitAlongX
139  {
140  Point sizeLeft, sizeRight;
141  Point posRight;
142  if (splitAlongX) {
143  sizeLeft = Point(space->getSize().x / 2, space->getSize().y);
144  sizeRight = Point(space->getSize().x - sizeLeft.x, space->getSize().y);
145  posRight = Point(space->getPos().x + sizeLeft.x, space->getPos().y);
146  } else {
147  sizeLeft = Point(space->getSize().x, space->getSize().y / 2);
148  sizeRight = Point(space->getSize().x, space->getSize().y - sizeLeft.y);
149  posRight = Point(space->getPos().x, space->getPos().y + sizeLeft.y);
150  }
151  auto leftSpace = std::make_unique<BinarySpace>(space->getPos(), sizeLeft, space_count_++, space);
152  auto rightSpace = std::make_unique<BinarySpace>(posRight, sizeRight, space_count_++, space);
153  placeClientInSpace(space->getClient(), leftSpace.get());
154  placeClientInSpace(client, rightSpace.get());
155  space->setLeft(std::move(leftSpace));
156  space->setRight(std::move(rightSpace));
157  space->setClient(nullptr);
158  space->incSubSpaceCount();
159  while(space->getParent() != nullptr) {
160  space->getParent()->incSubSpaceCount();
161  space = space->getParent();
162  }
163 }
void incSubSpaceCount()
Definition: BinarySpace.cpp:16
int space_count_
Definition: LayoutManager.hpp:89

References BinarySpace::getClient(), BinarySpace::getParent(), BinarySpace::getPos(), BinarySpace::getSize(), BinarySpace::incSubSpaceCount(), placeClientInSpace(), BinarySpace::setClient(), BinarySpace::setLeft(), BinarySpace::setRight(), LayoutManager::space_count_, Point::x, and Point::y.

Referenced by addClientRecursive().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updateGeometry()

void TreeLayoutManager::updateGeometry ( unsigned int  sizeX,
unsigned int  sizeY,
unsigned int  posX,
unsigned int  posY 
)
overridevirtual

update the geometry of the layout (resize)

Implements LayoutManager.

53  {
54  reSize(Point(sizeX,sizeY),Point(posX,posY));
55 }
void reSize(const Point &size, const Point &pos) override
Definition: TreeLayoutManager.cpp:214

References reSize().

Here is the call graph for this function:

Member Data Documentation

◆ border_size_

int LayoutManager::border_size_
protectedinherited

Referenced by placeClientInSpace().

◆ display_

Display* LayoutManager::display_
protectedinherited

◆ gap_

int LayoutManager::gap_
protectedinherited

Referenced by placeClientInSpace().

◆ rootSpace_

std::unique_ptr<BinarySpace> TreeLayoutManager::rootSpace_
private

◆ rootWindow_

Window LayoutManager::rootWindow_
protectedinherited

◆ screen_height_

int LayoutManager::screen_height_
protectedinherited

◆ screen_width_

int LayoutManager::screen_width_
protectedinherited

◆ space_count_

int LayoutManager::space_count_
protectedinherited

Referenced by splitSpace().


The documentation for this class was generated from the following files: