hyprland/workspaces: break up doUpdate

master
Austin Horstman 2024-05-24 13:53:39 -05:00
parent 9fe51af6b0
commit d73051e980
No known key found for this signature in database
2 changed files with 45 additions and 38 deletions

View File

@ -181,7 +181,13 @@ class Workspaces : public AModule, public EventHandler {
int windowRewritePriorityFunction(std::string const& window_rule); int windowRewritePriorityFunction(std::string const& window_rule);
// Update methods
void doUpdate(); void doUpdate();
void removeWorkspacesToRemove();
void createWorkspacesToCreate();
std::vector<std::string> getVisibleWorkspaces();
void updateWorkspaceStates(const std::vector<std::string>& visibleWorkspaces);
bool updateWindowsToCreate();
void extendOrphans(int workspaceId, Json::Value const& clientsJson); void extendOrphans(int workspaceId, Json::Value const& clientsJson);
void registerOrphanWindow(WindowCreationPayload create_window_payload); void registerOrphanWindow(WindowCreationPayload create_window_payload);

View File

@ -69,13 +69,9 @@ auto Workspaces::parseConfig(const Json::Value &config) -> void {
populateBoolConfig(config, "move-to-monitor", m_moveToMonitor); populateBoolConfig(config, "move-to-monitor", m_moveToMonitor);
populateSortByConfig(config); populateSortByConfig(config);
populateIgnoreWorkspacesConfig(config); populateIgnoreWorkspacesConfig(config);
populatePersistentWorkspacesConfig(config); populatePersistentWorkspacesConfig(config);
populateFormatWindowSeparatorConfig(config); populateFormatWindowSeparatorConfig(config);
populateWindowRewriteConfig(config); populateWindowRewriteConfig(config);
} }
@ -195,14 +191,31 @@ auto Workspaces::registerIpc() -> void {
void Workspaces::doUpdate() { void Workspaces::doUpdate() {
std::unique_lock lock(m_mutex); std::unique_lock lock(m_mutex);
// remove workspaces that wait to be removed removeWorkspacesToRemove();
for (auto &elem : m_workspacesToRemove) { createWorkspacesToCreate();
removeWorkspace(elem);
std::vector<std::string> visibleWorkspaces = getVisibleWorkspaces();
updateWorkspaceStates(visibleWorkspaces);
updateWindowCount();
sortWorkspaces();
bool anyWindowCreated = updateWindowsToCreate();
if (anyWindowCreated) {
dp.emit();
}
}
void Workspaces::removeWorkspacesToRemove() {
for (const auto &workspaceName : m_workspacesToRemove) {
removeWorkspace(workspaceName);
} }
m_workspacesToRemove.clear(); m_workspacesToRemove.clear();
}
// add workspaces that wait to be created void Workspaces::createWorkspacesToCreate() {
for (auto &[workspaceData, clientsData] : m_workspacesToCreate) { for (const auto &[workspaceData, clientsData] : m_workspacesToCreate) {
createWorkspace(workspaceData, clientsData); createWorkspace(workspaceData, clientsData);
} }
if (!m_workspacesToCreate.empty()) { if (!m_workspacesToCreate.empty()) {
@ -210,63 +223,55 @@ void Workspaces::doUpdate() {
sortWorkspaces(); sortWorkspaces();
} }
m_workspacesToCreate.clear(); m_workspacesToCreate.clear();
}
// get all active workspaces std::vector<std::string> Workspaces::getVisibleWorkspaces() {
spdlog::trace("Getting active workspaces");
auto monitors = gIPC->getSocket1JsonReply("monitors");
std::vector<std::string> visibleWorkspaces; std::vector<std::string> visibleWorkspaces;
for (Json::Value &monitor : monitors) { auto monitors = gIPC->getSocket1JsonReply("monitors");
for (const auto &monitor : monitors) {
auto ws = monitor["activeWorkspace"]; auto ws = monitor["activeWorkspace"];
if (ws.isObject() && (ws["name"].isString())) { if (ws.isObject() && ws["name"].isString()) {
visibleWorkspaces.push_back(ws["name"].asString()); visibleWorkspaces.push_back(ws["name"].asString());
} }
auto sws = monitor["specialWorkspace"]; auto sws = monitor["specialWorkspace"];
auto name = sws["name"].asString(); auto name = sws["name"].asString();
if (sws.isObject() && (sws["name"].isString()) && !name.empty()) { if (sws.isObject() && sws["name"].isString() && !name.empty()) {
visibleWorkspaces.push_back(!name.starts_with("special:") ? name : name.substr(8)); visibleWorkspaces.push_back(!name.starts_with("special:") ? name : name.substr(8));
} }
} }
return visibleWorkspaces;
}
spdlog::trace("Updating workspace states"); void Workspaces::updateWorkspaceStates(const std::vector<std::string> &visibleWorkspaces) {
auto updated_workspaces = gIPC->getSocket1JsonReply("workspaces"); auto updatedWorkspaces = gIPC->getSocket1JsonReply("workspaces");
for (auto &workspace : m_workspaces) { for (auto &workspace : m_workspaces) {
// active
workspace->setActive(workspace->name() == m_activeWorkspaceName || workspace->setActive(workspace->name() == m_activeWorkspaceName ||
workspace->name() == m_activeSpecialWorkspaceName); workspace->name() == m_activeSpecialWorkspaceName);
// disable urgency if workspace is active
if (workspace->name() == m_activeWorkspaceName && workspace->isUrgent()) { if (workspace->name() == m_activeWorkspaceName && workspace->isUrgent()) {
workspace->setUrgent(false); workspace->setUrgent(false);
} }
// visible
workspace->setVisible(std::find(visibleWorkspaces.begin(), visibleWorkspaces.end(), workspace->setVisible(std::find(visibleWorkspaces.begin(), visibleWorkspaces.end(),
workspace->name()) != visibleWorkspaces.end()); workspace->name()) != visibleWorkspaces.end());
// set workspace icon
std::string &workspaceIcon = m_iconsMap[""]; std::string &workspaceIcon = m_iconsMap[""];
if (m_withIcon) { if (m_withIcon) {
workspaceIcon = workspace->selectIcon(m_iconsMap); workspaceIcon = workspace->selectIcon(m_iconsMap);
} }
auto updatedWorkspace = std::find_if(
// update m_output updatedWorkspaces.begin(), updatedWorkspaces.end(), [&workspace](const auto &w) {
auto updated_workspace =
std::find_if(updated_workspaces.begin(), updated_workspaces.end(), [&workspace](auto &w) {
auto wNameRaw = w["name"].asString(); auto wNameRaw = w["name"].asString();
auto wName = wNameRaw.starts_with("special:") ? wNameRaw.substr(8) : wNameRaw; auto wName = wNameRaw.starts_with("special:") ? wNameRaw.substr(8) : wNameRaw;
return wName == workspace->name(); return wName == workspace->name();
}); });
if (updatedWorkspace != updatedWorkspaces.end()) {
if (updated_workspace != updated_workspaces.end()) { workspace->setOutput((*updatedWorkspace)["monitor"].asString());
workspace->setOutput((*updated_workspace)["monitor"].asString());
} }
workspace->update(m_format, workspaceIcon); workspace->update(m_format, workspaceIcon);
} }
}
spdlog::trace("Updating window count"); bool Workspaces::updateWindowsToCreate() {
bool anyWindowCreated = false; bool anyWindowCreated = false;
std::vector<WindowCreationPayload> notCreated; std::vector<WindowCreationPayload> notCreated;
for (auto &windowPayload : m_windowsToCreate) { for (auto &windowPayload : m_windowsToCreate) {
bool created = false; bool created = false;
for (auto &workspace : m_workspaces) { for (auto &workspace : m_workspaces) {
@ -285,13 +290,9 @@ void Workspaces::doUpdate() {
} }
} }
} }
if (anyWindowCreated) {
dp.emit();
}
m_windowsToCreate.clear(); m_windowsToCreate.clear();
m_windowsToCreate = notCreated; m_windowsToCreate = notCreated;
return anyWindowCreated;
} }
auto Workspaces::update() -> void { auto Workspaces::update() -> void {