refactor: take window representation directly from old workspace on movewindow event

pull/2529/head
Brenno Lemos 2023-09-22 21:12:42 -03:00
parent 6663ca3d75
commit 258ab8b147
2 changed files with 32 additions and 37 deletions

View File

@ -48,17 +48,14 @@ class Workspace {
void set_name(std::string value) { name_ = value; }; void set_name(std::string value) { name_ = value; };
bool contains_window(WindowAddress addr) { return window_map_.contains(addr); } bool contains_window(WindowAddress addr) { return window_map_.contains(addr); }
void insert_window(WindowAddress addr, std::string window_repr); void insert_window(WindowAddress addr, std::string window_repr);
void remove_window(WindowAddress addr) { window_map_.erase(addr); } std::string remove_window(WindowAddress addr);
void initialize_window_map(const Json::Value& clients_data); void initialize_window_map(const Json::Value& clients_data);
bool on_window_opened(WindowAddress& addr, std::string& workspace_name, bool on_window_opened(WindowAddress& addr, std::string& workspace_name, std::string window_repr);
const Json::Value& clients_data);
bool on_window_opened(WindowAddress& addr, std::string& workspace_name, std::string& window_class, bool on_window_opened(WindowAddress& addr, std::string& workspace_name, std::string& window_class,
std::string& window_title); std::string& window_title);
bool on_window_closed(WindowAddress& addr); std::optional<std::string> on_window_closed(WindowAddress& addr);
bool on_window_moved(WindowAddress& addr, std::string& workspace_name,
const Json::Value& clients_data);
void update(const std::string& format, const std::string& icon); void update(const std::string& format, const std::string& icon);

View File

@ -7,6 +7,7 @@
#include <algorithm> #include <algorithm>
#include <charconv> #include <charconv>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include "util/rewrite_string.hpp" #include "util/rewrite_string.hpp"
@ -257,16 +258,23 @@ void Workspaces::on_window_moved(std::string payload) {
std::string workspace_name = std::string workspace_name =
payload.substr(next_comma_idx + 1, payload.length() - next_comma_idx); payload.substr(next_comma_idx + 1, payload.length() - next_comma_idx);
int changes = 0; std::string window_repr;
const Json::Value clients_json = gIPC->getSocket1JsonReply("clients");
// Take the window's representation from the old workspace...
for (auto &workspace : workspaces_) { for (auto &workspace : workspaces_) {
if (workspace->on_window_moved(window_address, workspace_name, clients_json)) { try {
changes++; window_repr = workspace->on_window_closed(window_address).value();
if (changes == 2) { break;
break; } catch (const std::bad_optional_access &e) {
} // window was not found in this workspace
continue;
}
}
// ...and add it to the new workspace
for (auto &workspace : workspaces_) {
if (workspace->on_window_opened(window_address, workspace_name, window_repr)) {
break;
} }
} }
} }
@ -318,22 +326,18 @@ void Workspace::insert_window(WindowAddress addr, std::string window_class) {
} }
}; };
std::string Workspace::remove_window(WindowAddress addr) {
std::string window_repr = window_map_[addr];
window_map_.erase(addr);
return window_repr;
}
bool Workspace::on_window_opened(WindowAddress &addr, std::string &workspace_name, bool Workspace::on_window_opened(WindowAddress &addr, std::string &workspace_name,
const Json::Value &clients_data) { std::string window_repr) {
if (workspace_name == name()) { if (workspace_name == name()) {
for (auto client : clients_data) { window_map_.emplace(addr, window_repr);
// substr(2, ...) is necessary because Hyprland's JSON follows this format: return true;
// 0x{ADDR}
// While Hyprland's IPC follows this format:
// {ADDR}
auto client_address = client["address"].asString().substr(2, addr.length());
if (client_address == addr) {
std::string window_class = client["class"].asString();
insert_window(addr, window_class);
return true;
}
}
return false;
} else { } else {
return false; return false;
} }
@ -349,20 +353,14 @@ bool Workspace::on_window_opened(WindowAddress &addr, std::string &workspace_nam
} }
} }
bool Workspace::on_window_closed(WindowAddress &addr) { std::optional<std::string> Workspace::on_window_closed(WindowAddress &addr) {
if (window_map_.contains(addr)) { if (window_map_.contains(addr)) {
remove_window(addr); return remove_window(addr);
return true;
} else { } else {
return false; return {};
} }
} }
bool Workspace::on_window_moved(WindowAddress &addr, std::string &workspace_name,
const Json::Value &clients_data) {
return on_window_opened(addr, workspace_name, clients_data) || on_window_closed(addr);
}
void Workspaces::create_workspace(Json::Value &workspace_data, const Json::Value &clients_data) { void Workspaces::create_workspace(Json::Value &workspace_data, const Json::Value &clients_data) {
// replace the existing persistent workspace if it exists // replace the existing persistent workspace if it exists
auto workspace = std::find_if( auto workspace = std::find_if(