Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zjeffer committed Dec 28, 2023
1 parent 3cd2b29 commit a7ec8d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
4 changes: 3 additions & 1 deletion include/modules/hyprland/workspaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class Workspaces : public AModule, public EventHandler {

void onWindowTitleEvent(std::string const& payload);

void onConfigReloaded();

int windowRewritePriorityFunction(std::string const& window_rule);

bool m_allOutputs = false;
Expand All @@ -173,7 +175,7 @@ class Workspaces : public AModule, public EventHandler {
{"NUMBER", SortMethod::NUMBER},
{"DEFAULT", SortMethod::DEFAULT}};

void initialize_persistent_workspaces();
void initializePersistentWorkspaces();

std::string m_format;

Expand Down
59 changes: 32 additions & 27 deletions src/modules/hyprland/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ auto Workspaces::registerIpc() -> void {
gIPC->registerForIPC("closewindow", this);
gIPC->registerForIPC("movewindow", this);
gIPC->registerForIPC("urgent", this);
gIPC->registerForIPC("configreloaded", this);

if (windowRewriteConfigUsesTitle()) {
spdlog::info(
Expand Down Expand Up @@ -231,6 +232,11 @@ auto Workspaces::update() -> void {
m_windowsToCreate.clear();
m_windowsToCreate = notCreated;

// print workspaces
for (auto &workspace : m_workspaces) {
spdlog::info("Workspace {}", workspace->name());
}

AModule::update();
}

Expand Down Expand Up @@ -280,6 +286,8 @@ void Workspaces::onEvent(const std::string &ev) {
onWorkspaceRenamed(payload);
} else if (eventName == "windowtitle") {
onWindowTitleEvent(payload);
} else if (eventName == "configreloaded") {
onConfigReloaded();
}

dp.emit();
Expand All @@ -304,6 +312,14 @@ void Workspaces::onWorkspaceCreated(std::string const &payload) {
if (name == payload &&
(allOutputs() || m_bar.output->name == workspaceJson["monitor"].asString()) &&
(showSpecial() || !name.starts_with("special")) && !isDoubleSpecial(payload)) {
auto const workspaceRules = gIPC->getSocket1JsonReply("workspacerules");
for (Json::Value const &rule : workspaceRules) {
if (rule["workspaceString"].asString() == payload) {
workspaceJson["persistent"] = rule["persistent"].asBool();
break;
}
}

m_workspacesToCreate.push_back(workspaceJson);
break;
}
Expand Down Expand Up @@ -430,6 +446,11 @@ void Workspaces::onWindowTitleEvent(std::string const &payload) {
}
}

void Workspaces::onConfigReloaded() {
initializePersistentWorkspaces();
updateWindowCount();
}

void Workspaces::updateWindowCount() {
const Json::Value workspacesJson = gIPC->getSocket1JsonReply("workspaces");
for (auto &workspace : m_workspaces) {
Expand Down Expand Up @@ -486,6 +507,7 @@ std::optional<std::string> Workspace::closeWindow(WindowAddress const &addr) {

void Workspaces::createWorkspace(Json::Value const &workspace_data,
Json::Value const &clients_data) {
spdlog::debug("Creating workspace {}", workspace_data["name"].asString());
// avoid recreating existing workspaces
auto workspaceName = workspace_data["name"].asString();
auto workspace = std::find_if(
Expand All @@ -496,7 +518,9 @@ void Workspaces::createWorkspace(Json::Value const &workspace_data,
});

if (workspace != m_workspaces.end()) {
// don't recreate workspace, but update persistency if necessary
if (workspace_data["persistent"].asBool() and !(*workspace)->isPersistent()) {
spdlog::info("Setting workspace {} as persistent", workspaceName);
(*workspace)->setPersistent();
}
return;
Expand All @@ -511,6 +535,7 @@ void Workspaces::createWorkspace(Json::Value const &workspace_data,
}

void Workspaces::removeWorkspace(std::string const &name) {
spdlog::debug("Removing workspace {}", name);
auto workspace =
std::find_if(m_workspaces.begin(), m_workspaces.end(), [&](std::unique_ptr<Workspace> &x) {
return (name.starts_with("special:") && name.substr(8) == x->name()) || name == x->name();
Expand All @@ -525,20 +550,22 @@ void Workspaces::removeWorkspace(std::string const &name) {
m_workspaces.erase(workspace);
}

void Workspaces::initialize_persistent_workspaces() {
// initialize persistent workspaces
void Workspaces::initializePersistentWorkspaces() {
auto const workspaceRules = gIPC->getSocket1JsonReply("workspacerules");
for (Json::Value const &rule : workspaceRules) {
if (!rule["workspaceString"].isString()) {
spdlog::warn("Invalid workspaceString, skipping: {}", rule);
spdlog::warn("Workspace rules: invalid workspaceString, skipping: {}", rule);
continue;
}
auto const &workspace = rule["workspaceString"].asString();
auto const &monitor = rule["monitor"].asString();
if (!rule["persistent"].asBool()) {
continue;
}
// assume if a monitor is not passed, display it persistently on every monitor
// create this workspace persistently if:
// 1. allOutputs is true
// 2. the rule's monitor is the current monitor
// 3. no monitor is specified in the rule => assume it needs to be persistent on every monitor
if (allOutputs() || m_bar.output->name == monitor || monitor.empty()) {
// => persistent workspace should be shown on this monitor
Json::Value workspaceData;
Expand Down Expand Up @@ -573,7 +600,7 @@ void Workspaces::init() {
}
}

initialize_persistent_workspaces();
initializePersistentWorkspaces();

updateWindowCount();

Expand Down Expand Up @@ -603,28 +630,6 @@ Workspace::Workspace(const Json::Value &workspace_data, Workspaces &workspace_ma
m_isSpecial = true;
}

/* auto const workspaceRules = gIPC->getSocket1JsonReply("workspacerules");
for (Json::Value const &rule : workspaceRules) {
// check if this rule is for this workspaceString
auto const &workspaceString = rule["workspaceString"];
if (workspaceString.isInt() && workspaceString.asInt() == id_) {
is_persistent_ = rule["persistent"].asBool();
spdlog::info("Persistent value for workspaceString {} is {}", workspaceString.asInt(),
is_persistent_);
break;
}
if (workspaceString.isString() && workspaceString.asString().starts_with("name:") &&
workspaceString.asString().substr(5) == name_) {
if (rule["persistent"].isBool()) {
is_persistent_ = rule["persistent"].asBool();
spdlog::info("Persistent value for workspaceString {} is {}", workspaceString.asString(),
is_persistent_);
break;
}
spdlog::warn("Invalid persistent value for workspaceString {}", workspaceString.asString());
}
} */

m_button.add_events(Gdk::BUTTON_PRESS_MASK);
m_button.signal_button_press_event().connect(sigc::mem_fun(*this, &Workspace::handleClicked),
false);
Expand Down

0 comments on commit a7ec8d7

Please sign in to comment.