Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hyprland/workspaces: Improve the workspaces persistency logic #2967

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions include/modules/hyprland/workspaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@
std::string output() const { return m_output; };
bool isActive() const { return m_isActive; };
bool isSpecial() const { return m_isSpecial; };
bool isPersistent() const { return m_isPersistent; };
bool isPersistent() const { return m_isPersistentRule || m_isPersistentConfig; };
bool isPersistentConfig() const { return m_isPersistentConfig; };
aruhier marked this conversation as resolved.
Show resolved Hide resolved
bool isPersistentRule() const { return m_isPersistentRule; };
bool isVisible() const { return m_isVisible; };
bool isEmpty() const { return m_windows == 0; };
bool isUrgent() const { return m_isUrgent; };

bool handleClicked(GdkEventButton* bt) const;
void setActive(bool value = true) { m_isActive = value; };
void setPersistent(bool value = true) { m_isPersistent = value; };
void setPersistentRule(bool value = true) { m_isPersistentRule = value; };
void setPersistentConfig(bool value = true) { m_isPersistentConfig = value; };
void setUrgent(bool value = true) { m_isUrgent = value; };
void setVisible(bool value = true) { m_isVisible = value; };
void setWindows(uint value) { m_windows = value; };
Expand All @@ -101,8 +104,9 @@
uint m_windows;
bool m_isActive = false;
bool m_isSpecial = false;
bool m_isPersistent = false;
bool m_isPersistentRule = false; // represents the persistent state in hyprland

Check warning on line 107 in include/modules/hyprland/workspaces.hpp

View workflow job for this annotation

GitHub Actions / build

include/modules/hyprland/workspaces.hpp:107:8 [readability-identifier-naming]

invalid case style for private member 'm_isPersistentRule'
bool m_isPersistentConfig = false; // represents the persistent state in the Waybar config

Check warning on line 108 in include/modules/hyprland/workspaces.hpp

View workflow job for this annotation

GitHub Actions / build

include/modules/hyprland/workspaces.hpp:108:8 [readability-identifier-naming]

invalid case style for private member 'm_isPersistentConfig'
bool m_isUrgent = false;

Check warning on line 109 in include/modules/hyprland/workspaces.hpp

View workflow job for this annotation

GitHub Actions / build

include/modules/hyprland/workspaces.hpp:109:8 [readability-identifier-naming]

invalid case style for private member 'm_isUrgent'
bool m_isVisible = false;

std::map<WindowAddress, std::string> m_windowMap;
Expand Down
43 changes: 29 additions & 14 deletions src/modules/hyprland/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void Workspaces::onWorkspaceCreated(std::string const &workspaceName,
(showSpecial() || !name.starts_with("special")) && !isDoubleSpecial(workspaceName)) {
for (Json::Value const &rule : workspaceRules) {
if (rule["workspaceString"].asString() == workspaceName) {
workspaceJson["persistent"] = rule["persistent"].asBool();
workspaceJson["persistent-rule"] = rule["persistent"].asBool();
break;
}
}
Expand Down Expand Up @@ -587,8 +587,7 @@ std::optional<std::string> Workspace::closeWindow(WindowAddress const &addr) {
void Workspaces::createWorkspace(Json::Value const &workspace_data,
Json::Value const &clients_data) {
auto workspaceName = workspace_data["name"].asString();
spdlog::debug("Creating workspace {}, persistent: {}", workspaceName,
workspace_data["persistent"].asBool() ? "true" : "false");
spdlog::debug("Creating workspace {}", workspaceName);

// avoid recreating existing workspaces
auto workspace = std::find_if(
Expand All @@ -600,7 +599,22 @@ void Workspaces::createWorkspace(Json::Value const &workspace_data,

if (workspace != m_workspaces.end()) {
// don't recreate workspace, but update persistency if necessary
(*workspace)->setPersistent(workspace_data["persistent"].asBool());
const auto keys = workspace_data.getMemberNames();

const auto *k = "persistent-rule";
if (std::find(keys.begin(), keys.end(), k) != keys.end()) {
spdlog::debug("Set dynamic persistency of workspace {} to: {}", workspaceName,
workspace_data[k].asBool() ? "true" : "false");
(*workspace)->setPersistentRule(workspace_data[k].asBool());
}

k = "persistent-config";
if (std::find(keys.begin(), keys.end(), k) != keys.end()) {
spdlog::debug("Set config persistency of workspace {} to: {}", workspaceName,
workspace_data[k].asBool() ? "true" : "false");
(*workspace)->setPersistentConfig(workspace_data[k].asBool());
}

return;
}

Expand All @@ -624,16 +638,16 @@ void Workspaces::removeWorkspace(std::string const &name) {
return;
}

if ((*workspace)->isPersistent()) {
spdlog::trace("Not removing persistent workspace {}", name);
if ((*workspace)->isPersistentConfig()) {
spdlog::trace("Not removing config persistent workspace {}", name);
return;
}

m_box.remove(workspace->get()->button());
m_workspaces.erase(workspace);
}

Json::Value createPersistentWorkspaceData(std::string const &name, std::string const &monitor) {
Json::Value createMonitorWorkspaceData(std::string const &name, std::string const &monitor) {
spdlog::trace("Creating persistent workspace: {} on monitor {}", name, monitor);
Json::Value workspaceData;
try {
Expand All @@ -646,7 +660,6 @@ Json::Value createPersistentWorkspaceData(std::string const &name, std::string c
workspaceData["name"] = name;
workspaceData["monitor"] = monitor;
workspaceData["windows"] = 0;
workspaceData["persistent"] = true;
return workspaceData;
}

Expand Down Expand Up @@ -699,7 +712,8 @@ void Workspaces::loadPersistentWorkspacesFromConfig(Json::Value const &clientsJs
}

for (auto const &workspace : persistentWorkspacesToCreate) {
auto const workspaceData = createPersistentWorkspaceData(workspace, m_bar.output->name);
auto workspaceData = createMonitorWorkspaceData(workspace, m_bar.output->name);
workspaceData["persistent-config"] = true;
m_workspacesToCreate.emplace_back(workspaceData, clientsJson);
}
}
Expand All @@ -724,7 +738,8 @@ void Workspaces::loadPersistentWorkspacesFromWorkspaceRules(const Json::Value &c
// 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
auto workspaceData = createPersistentWorkspaceData(workspace, m_bar.output->name);
auto workspaceData = createMonitorWorkspaceData(workspace, m_bar.output->name);
workspaceData["persistent-rule"] = true;
m_workspacesToCreate.emplace_back(workspaceData, clientsJson);
} else {
m_workspacesToRemove.emplace_back(workspace);
Expand Down Expand Up @@ -774,10 +789,9 @@ void Workspaces::initializeWorkspaces() {
if (m_persistentWorkspaceConfig.isObject()) {
// a persistent workspace config is defined, so use that instead of workspace rules
loadPersistentWorkspacesFromConfig(clientsJson);
} else {
// no persistent workspaces config defined, use Hyprland's workspace rules
loadPersistentWorkspacesFromWorkspaceRules(clientsJson);
}
// load Hyprland's workspace rules
loadPersistentWorkspacesFromWorkspaceRules(clientsJson);
}

void Workspaces::extendOrphans(int workspaceId, Json::Value const &clientsJson) {
Expand Down Expand Up @@ -812,7 +826,8 @@ Workspace::Workspace(const Json::Value &workspace_data, Workspaces &workspace_ma
m_output(workspace_data["monitor"].asString()), // TODO:allow using monitor desc
m_windows(workspace_data["windows"].asInt()),
m_isActive(true),
m_isPersistent(workspace_data["persistent"].asBool()) {
m_isPersistentRule(workspace_data["persistent-rule"].asBool()),
m_isPersistentConfig(workspace_data["persistent-config"].asBool()) {
if (m_name.starts_with("name:")) {
m_name = m_name.substr(5);
} else if (m_name.starts_with("special")) {
Expand Down
Loading