fix: special workspaces with the same name as regular workspaces caused the latter to be destroyed

pull/2864/head
Brenno Lemos 2024-01-24 20:05:29 -03:00
parent 0948a407d0
commit 1bf97f532e
2 changed files with 23 additions and 13 deletions

View File

@ -22,6 +22,9 @@
using WindowAddress = std::string; using WindowAddress = std::string;
static const std::string SPECIAL_QUALIFIER_PREFIX = "special:";
static const int SPECIAL_QUALIFIER_PREFIX_LEN = SPECIAL_QUALIFIER_PREFIX.length();
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
class Workspaces; class Workspaces;
@ -135,6 +138,7 @@ class Workspaces : public AModule, public EventHandler {
void onEvent(const std::string& e) override; void onEvent(const std::string& e) override;
void updateWindowCount(); void updateWindowCount();
void sortWorkspaces(); void sortWorkspaces();
auto locateWorkspace(const std::string& workspaceName);
void createWorkspace(Json::Value const& workspace_data, void createWorkspace(Json::Value const& workspace_data,
Json::Value const& clients_data = Json::Value::nullRef); Json::Value const& clients_data = Json::Value::nullRef);
void removeWorkspace(std::string const& name); void removeWorkspace(std::string const& name);

View File

@ -514,16 +514,28 @@ std::optional<std::string> Workspace::closeWindow(WindowAddress const &addr) {
return std::nullopt; return std::nullopt;
} }
auto Workspaces::locateWorkspace(const std::string &workspaceName) {
bool isSpecial = workspaceName.starts_with(SPECIAL_QUALIFIER_PREFIX);
std::string unqualifiedWorkspaceName;
if (isSpecial) {
unqualifiedWorkspaceName = workspaceName.substr(SPECIAL_QUALIFIER_PREFIX_LEN);
}
return std::find_if(
m_workspaces.begin(), m_workspaces.end(),
[workspaceName, isSpecial, unqualifiedWorkspaceName](std::unique_ptr<Workspace> const &w) {
return (isSpecial && w->isSpecial() && unqualifiedWorkspaceName == w->name()) ||
workspaceName == w->name();
});
}
void Workspaces::createWorkspace(Json::Value const &workspace_data, void Workspaces::createWorkspace(Json::Value const &workspace_data,
Json::Value const &clients_data) { Json::Value const &clients_data) {
// avoid recreating existing workspaces // avoid recreating existing workspaces
auto workspaceName = workspace_data["name"].asString(); auto workspaceName = workspace_data["name"].asString();
auto workspace = std::find_if(
m_workspaces.begin(), m_workspaces.end(), auto workspace = locateWorkspace(workspaceName);
[workspaceName](std::unique_ptr<Workspace> const &w) {
return (workspaceName.starts_with("special:") && workspaceName.substr(8) == w->name()) ||
workspaceName == w->name();
});
if (workspace != m_workspaces.end()) { if (workspace != m_workspaces.end()) {
if (workspace_data["persistent"].asBool() and !(*workspace)->isPersistent()) { if (workspace_data["persistent"].asBool() and !(*workspace)->isPersistent()) {
@ -541,10 +553,7 @@ void Workspaces::createWorkspace(Json::Value const &workspace_data,
} }
void Workspaces::removeWorkspace(std::string const &name) { void Workspaces::removeWorkspace(std::string const &name) {
auto workspace = auto workspace = locateWorkspace(name);
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();
});
if (workspace == m_workspaces.end()) { if (workspace == m_workspaces.end()) {
// happens when a workspace on another monitor is destroyed // happens when a workspace on another monitor is destroyed
@ -1016,9 +1025,6 @@ void WindowCreationPayload::clearWorkspaceName() {
// special qualifier. The reasoning is that not all of Hyprland's IPC events // special qualifier. The reasoning is that not all of Hyprland's IPC events
// use this qualifier, so it's better to be consistent about our uses. // use this qualifier, so it's better to be consistent about our uses.
static const std::string SPECIAL_QUALIFIER_PREFIX = "special:";
static const int SPECIAL_QUALIFIER_PREFIX_LEN = SPECIAL_QUALIFIER_PREFIX.length();
if (m_workspaceName.starts_with(SPECIAL_QUALIFIER_PREFIX)) { if (m_workspaceName.starts_with(SPECIAL_QUALIFIER_PREFIX)) {
m_workspaceName = m_workspaceName.substr( m_workspaceName = m_workspaceName.substr(
SPECIAL_QUALIFIER_PREFIX_LEN, m_workspaceName.length() - SPECIAL_QUALIFIER_PREFIX_LEN); SPECIAL_QUALIFIER_PREFIX_LEN, m_workspaceName.length() - SPECIAL_QUALIFIER_PREFIX_LEN);