Skip to content

Commit

Permalink
Dynamic resizing without bar overleaping
Browse files Browse the repository at this point in the history
  • Loading branch information
corecaps committed Feb 20, 2024
1 parent fbba7f2 commit 7d2130c
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 36 deletions.
32 changes: 30 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
}],
"Bars": [
{
"Height": 30,
"Bar_Size": 30,
"Font": "Arial",
"Font_Size": 12,
"Font_Color": "#000000",
"Background_Color": "#FFFFFF",
"Border_Size": 2,
"Border_Color": "#000000",
"Position": "Top",
"Position": "top",
"Widgets": [
{
"Type": "Group",
Expand All @@ -53,6 +53,34 @@
"Arguments": ""
}
]
},
{
"Bar_Size": 30,
"Font": "Arial",
"Font_Size": 12,
"Font_Color": "#000000",
"Background_Color": "#FFFFFF",
"Border_Size": 2,
"Border_Color": "#000000",
"Position": "bottom",
"Widgets": [
{
"Type": "Group",
"Position": "10",
"Size": "80",
"Color": "#FF0000",
"Background_Color": "#FFFFFF",
"Arguments": ""
},
{
"Type": "Clock",
"Position": "90",
"Size": "10",
"Color": "#000000",
"Background_Color": "#FFFFFF",
"Arguments": ""
}
]
}
],
"Bindings": {
Expand Down
11 changes: 8 additions & 3 deletions inc/Config/ConfigDataBar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class ConfigDataBar : public ConfigDataBase{
*/
Json::Value configSave() override;
/**
* @fn int ConfigDataBar::getBarHeight() const
* @fn int ConfigDataBar::getBarSize() const
* @brief Get the Bar Heightb
* @return
*/
[[nodiscard]] int getBarHeight() const;
[[nodiscard]] int getBarSize() const;
/**
* @fn const std::string &ConfigDataBar::getBarFont() const
* @brief Get the Bar Font
Expand Down Expand Up @@ -95,8 +95,13 @@ class ConfigDataBar : public ConfigDataBase{
*/
[[nodiscard]] unsigned int getBarBorderColor() const;
private:
int barHeight_;
public:
const std::string &getBarPosition() const;

private:
int barSize_;
std::string barFont_;
std::string barPosition_;
unsigned int barFontColor_;
int barFontSize_;
unsigned int barBackgroundColor_;
Expand Down
1 change: 1 addition & 0 deletions inc/Group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class Group {
* @return
*/
unsigned long getActiveColor() const;
void resize (int sizeX, int sizeY, int posX, int posY);

private:
std::string name_;
Expand Down
9 changes: 5 additions & 4 deletions inc/Layouts/LayoutManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class LayoutManager {
* @brief Set the size of the space
* @param size
*/

void setSize(const Point &size);
/**
* @fn int LayoutManager::Space::getSubspaceCount()
Expand Down Expand Up @@ -204,15 +205,16 @@ class LayoutManager {
int pos_x,
int pos_y,
int gap,
int border_size,
int bar_height);
int border_size);
virtual ~LayoutManager() = default;
/**
* @fn virtual void LayoutManager::updateGeometry()
* @brief updateGeometry this method is a residual from the old implementation.
* it should be removed in the future.
*/
virtual void updateGeometry() = 0;
virtual void updateGeometry(int sizeX, int sizeY, int posX, int posY) = 0;
virtual void reSize(const Point &size, const Point &pos) = 0;
virtual void recursiveResize(const Point &size, const Point &pos, Space *space) = 0;
/**
* @fn virtual Space * LayoutManager::findSpace(Client *client)
* @brief find the space that contains the client
Expand Down Expand Up @@ -245,7 +247,6 @@ class LayoutManager {
int screen_height_;
int gap_;
int border_size_;
int bar_height_;
int space_count_;
Display *display_;
Window rootWindow_;
Expand Down
4 changes: 3 additions & 1 deletion inc/Layouts/TreeLayoutManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class TreeLayoutManager :public LayoutManager {
* @fn void TreeLayoutManager::updateGeometry()
* @brief this method is an old implementation and should be removed
*/
void updateGeometry() override;
void updateGeometry(int sizeX, int sizeY, int posX, int posY) override;
void reSize(const Point &size, const Point &pos) override;
void recursiveResize(const Point &size, const Point &pos, Space *space) override;
/**
* @fn Space* TreeLayoutManager::findSpace(Client* client)
* @brief find the space containing the client
Expand Down
27 changes: 24 additions & 3 deletions src/Bars/Bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,34 @@ void Bar::init(ConfigDataBar *config, TSBarsData *ts) {
display = WindowManager::getInstance()->getDisplay();
int screen = DefaultScreen(display);
root = RootWindow(display, screen);
unsigned int size_x = DisplayWidth(display, screen);
unsigned int size_y = configData->getBarHeight();
int posX = 0;
int posY = 0;
if (configData->getBarPosition() == "top") {
sizeY = configData->getBarSize();
sizeX = DisplayWidth(display, screen);
posX = 0;
posY = 0;
} else if (configData->getBarPosition() == "bottom") {
sizeY = configData->getBarSize();
sizeX = DisplayWidth(display, screen);
posX = 0;
posY = DisplayHeight(display, screen) - sizeY;
} else if (configData->getBarPosition() == "left") {
sizeY = DisplayHeight(display, screen);
sizeX = configData->getBarSize();
posX = 0;
posY = 0;
} else if (configData->getBarPosition() == "right") {
sizeY = DisplayHeight(display, screen);
sizeX = configData->getBarSize();
posX = DisplayWidth(display, screen) - sizeX;
posY = 0;
}
unsigned int bg = configData->getBarBackgroundColor();
unsigned int fg = configData->getBarFontColor();
unsigned int border = configData->getBarBorderColor();
unsigned int borderSize = configData->getBarBorderSize();
window = XCreateSimpleWindow(display, root, 0, 0,size_x, size_y, borderSize, 0, bg);
window = XCreateSimpleWindow(display, root, posX, posY,sizeX, sizeY, borderSize, 0, bg);
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
this->draw();
Expand Down
21 changes: 20 additions & 1 deletion src/Bars/Bars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Bars/Bars.hpp"
#include "Bars/Bar.hpp"
#include "Config/ConfigDataBars.hpp"
#include "Config/ConfigDataBar.hpp"
#include "Bars/TSBarsData.hpp"
#include "WindowManager.hpp"
#include <string>
Expand All @@ -41,7 +42,25 @@ void Bars::init(ConfigDataBars *config,
for (auto &bar : this->configData->getBars()) {
std::unique_ptr<Bar> newBar = std::make_unique<Bar>();
newBar->init(bar, this->tsData);
Logger::GetInstance()->Log("Bar [" + std::to_string(this->bars.size()) + "] on window [" + std::to_string(newBar->getWindow()) + "] initialized",L_INFO);
if (bar->getBarPosition() == "top") {
this->spaceN += bar->getBarSize();
} else if (bar->getBarPosition() == "bottom") {
this->spaceS += bar->getBarSize();
} else if (bar->getBarPosition() == "left") {
this->spaceW += bar->getBarSize();
} else if (bar->getBarPosition() == "right") {
this->spaceE += bar->getBarSize();
}
Logger::GetInstance()->Log("Bar ["
+ std::to_string(this->bars.size())
+ "] on window ["
+ std::to_string(newBar->getWindow())
+ "] initialized\t"
+ bar->getBarPosition()
+ " "
+ std::to_string(newBar->getSizeX())
+ " x "
+ std::to_string(newBar->getSizeY()),L_INFO);
this->bars.push_back(std::move(newBar));
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/Config/ConfigDataBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <sstream>

ConfigDataBar::ConfigDataBar() :
barHeight_(20),
barSize_(20),
barFont_("Arial"),
barFontColor_(0x00ff00),
barFontSize_(12),
Expand All @@ -44,10 +44,10 @@ void ConfigDataBar::configInit(const Json::Value &root) {
if (root.empty() || !root.isObject()) {
throw std::runtime_error("Invalid configuration file");
}
if (root["Height"].empty() || !root["Height"].isInt()) {
Logger::GetInstance()->Log("ConfigDataBar::Height is empty or not an integer",L_ERROR);
if (root["Bar_Size"].empty() || !root["Bar_Size"].isInt()) {
Logger::GetInstance()->Log("ConfigDataBar::Bar_Size is empty or not an integer",L_ERROR);
} else {
barHeight_ = root["Height"].asInt();
barSize_ = root["Bar_Size"].asInt();
}
if (root["Font"].empty() || !root["Font"].isString()) {
Logger::GetInstance()->Log("ConfigDataBar::Font is empty or not a string", L_ERROR);
Expand Down Expand Up @@ -82,17 +82,23 @@ void ConfigDataBar::configInit(const Json::Value &root) {
} else {
barBorderSize_ = root["Border_Size"].asInt();
}
if (root["Position"].empty() || !root["Position"].isString()) {
Logger::GetInstance()->Log("ConfigDataBar::Position is empty or not a string",L_ERROR);
} else {
barPosition_ = root["Position"].asString();
}
std::stringstream msg;
msg << "Bar :\t Height [" << barHeight_ << "] Font [" << barFont_ << "] FontSize [" << barFontSize_ << "] BorderSize [" << barBorderSize_ << "]";
msg << "Bar :\t Height [" << barSize_ << "] Font [" << barFont_ << "] FontSize [" << barFontSize_ << "] BorderSize [" << barBorderSize_ << "]";
Logger::GetInstance()->Log(msg.str(),L_INFO);
}
Json::Value ConfigDataBar::configSave() {
return Json::Value();
}
int ConfigDataBar::getBarHeight() const { return barHeight_; }
int ConfigDataBar::getBarSize() const { return barSize_; }
const std::string &ConfigDataBar::getBarFont() const { return barFont_; }
unsigned int ConfigDataBar::getBarFontColor() const { return barFontColor_; }
int ConfigDataBar::getBarFontSize() const { return barFontSize_; }
unsigned int ConfigDataBar::getBarBackgroundColor() const { return barBackgroundColor_; }
int ConfigDataBar::getBarBorderSize() const { return barBorderSize_; }
unsigned int ConfigDataBar::getBarBorderColor() const { return barBorderColor_; }
const std::string &ConfigDataBar::getBarPosition() const { return barPosition_; }
4 changes: 4 additions & 0 deletions src/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ int Group::getBorderSize() const { return borderSize_; }
int Group::getGap() const { return gap_; }
unsigned long Group::getInactiveColor() const { return inactiveColor_; }
unsigned long Group::getActiveColor() const { return activeColor_; }

void Group::resize(int sizeX, int sizeY, int posX, int posY) {
layoutManager_->updateGeometry(sizeX, sizeY, posX, posY);
}
5 changes: 2 additions & 3 deletions src/Layouts/LayoutManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ LayoutManager::LayoutManager(Display *display,
int pos_x,
int pos_y,
int borderSize,
int gap,
int barHeight) :
int gap) :
display_(display),
rootWindow_(root),
space_count_(0),
border_size_(borderSize),
gap_(gap),
bar_height_(barHeight),
rootSpace_(nullptr),
screen_height_(0),
screen_width_(0) {}
Expand Down Expand Up @@ -78,3 +76,4 @@ void LayoutManager::Space::setLeft( std::unique_ptr<Space> left) { this->left_ =
Client *LayoutManager::Space::getClient() const { return client_; }
void LayoutManager::Space::setClient(Client *client) { Space::client_ = client; }
int LayoutManager::Space::getSubspaceCount() const {return subspace_count_; }

67 changes: 62 additions & 5 deletions src/Layouts/TreeLayoutManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @date 2024-02-07
*/
#include "Layouts/TreeLayoutManager.hpp"
#include "Logger.hpp"
TreeLayoutManager::TreeLayoutManager(Display *display,
Window root,
int sizeX,
Expand All @@ -34,9 +35,9 @@ TreeLayoutManager::TreeLayoutManager(Display *display,
int borderSize,
int gap,
int barHeight) :
LayoutManager(display, root, sizeX, sizeY, posX, posY, gap, borderSize, barHeight) {
Point pos(posX, posY + barHeight);
Point size(sizeX - borderSize, sizeY - barHeight - borderSize);
LayoutManager(display, root, sizeX, sizeY, posX, posY, gap, borderSize) {
Point pos(posX, posY);
Point size(sizeX - borderSize, sizeY - borderSize);
this->rootSpace_ = new Space(pos, size, 0);
}
void deleteSpace(LayoutManager::Space *space) {
Expand All @@ -51,7 +52,9 @@ void deleteSpace(LayoutManager::Space *space) {
TreeLayoutManager::~TreeLayoutManager() {
deleteSpace(rootSpace_);
}
void TreeLayoutManager::updateGeometry() { }
void TreeLayoutManager::updateGeometry(int sizeX, int sizeY, int posX, int posY) {
reSize(Point(sizeX,sizeY),Point(posX,posY));
}
LayoutManager::Space * TreeLayoutManager::findSpaceRecursive(Client *client, LayoutManager::Space * space) {
if (space->getClient() == client) {
return space;
Expand Down Expand Up @@ -128,10 +131,20 @@ void TreeLayoutManager::addClientRecursive(Client* client, Space* space) {
}
}
void TreeLayoutManager::placeClientInSpace(Client* client, Space* space) {
Logger::GetInstance()->Log("Placing client in space ["
+ std::to_string(space->getSize().x)
+ " x "
+ std::to_string(space->getSize().y)
+ "] Pos ["
+ std::to_string(space->getPos().x)
+ " x"
+ std::to_string(space->getPos().y)
+ "]", L_INFO);
client->move(space->getPos().x + border_size_ + gap_ / 2, space->getPos().y + border_size_ + gap_ / 2);
client->resize(space->getSize().x - (border_size_ * 2)- gap_, space->getSize().y - (border_size_ * 2) - gap_);
client->restack();
space->setClient(client);
if (space->getClient() != client)
space->setClient(client);
}
void TreeLayoutManager::splitSpace(Client* client, Space* space, bool splitAlongX) {
Point sizeLeft, sizeRight;
Expand Down Expand Up @@ -178,3 +191,47 @@ void TreeLayoutManager::growSpaceX(Client *client) {
space->getParent()->getRight()->setSize(Point(space->getParent()->getRight()->getSize().x - 1, space->getParent()->getRight()->getSize().y));
}
}

void TreeLayoutManager::reSize(const LayoutManager::Point &size, const LayoutManager::Point &pos) {
if (rootSpace_->getSize().x == size.x && rootSpace_->getSize().y == size.y) {
return;
}
Logger::GetInstance()->Log("Resizing to [" + std::to_string(size.x) + "x" + std::to_string(size.y) + "]", L_INFO);
recursiveResize(size, pos,rootSpace_);
}

void TreeLayoutManager::recursiveResize(const LayoutManager::Point &size, const LayoutManager::Point &pos,LayoutManager::Space * space) {
LayoutManager::Point oldSize = space->getSize();
LayoutManager::Point oldPos = space->getPos();
space->setSize(size);
space->setPos(pos);
Logger::GetInstance()->Log("Resizing subspace from "
+ std::to_string(oldSize.x)
+ " x "
+ std::to_string(oldSize.y)
+ " to "
+ std::to_string(space->getSize().x)
+ " x "
+ std::to_string(space->getSize().y), L_INFO);
if (space->getClient() != nullptr) {
Logger::GetInstance()->Log("Replacing client in space", L_INFO);
placeClientInSpace(space->getClient(), space);
}
if (space->getLeft() != nullptr) {
Point leftPos = pos;
Point leftSize = size;
Point rightPos = pos;
Point rightSize = size;
if (space->getLeft()->getSize().x == oldSize.x) {
leftSize.y = size.y / 2;
rightSize.y = size.y - leftSize.y;
rightPos.y = pos.y + leftSize.y;
} else {
leftSize.x = size.x / 2;
rightSize.x = size.x - leftSize.x;
rightPos.x = pos.x + leftSize.x;
}
recursiveResize(leftSize, leftPos, space->getLeft().get());
recursiveResize(rightSize, rightPos, space->getRight().get());
}
}
Loading

0 comments on commit 7d2130c

Please sign in to comment.