From 1b14c81bbf8b7e1743a665e7fd7ef4f0b62a5741 Mon Sep 17 00:00:00 2001 From: paolobettelini Date: Mon, 19 Jun 2023 12:13:21 +0200 Subject: [PATCH] Changed the workspace separation --- README.md | 4 +++- hyprload.toml | 2 +- src/main.cpp | 60 +++++++++++++++++++++++++-------------------------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index aa52973..3369f71 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,9 @@ It also provides the following config values Keep in mind that if you're using, for example, the `wlr/workspaces` widgets in [waybar](https://github.com/Alexays/Waybar), this will require a change to your config. You should set `all-outputs` to `false`, and adjust the icon mapping. -If your workspace-per-monitor count is 10, the first monitor will have workspaces 1-10, the second 11-20 and so on. They will be accessed via numbers 1-10 while your mouse is on a given monitor. +# Workings +When you are on monitor `(ID=N)` and want to move to workspace `W`, +the actual workspace you go to is given by `(W - 1) * monitors + N + 1`. # Special thanks - [hyprsome](https://github.com/sopa0/hyprsome): An earlier project of similar nature diff --git a/hyprload.toml b/hyprload.toml index 461f608..e08f277 100644 --- a/hyprload.toml +++ b/hyprload.toml @@ -1,6 +1,6 @@ [split-monitor-workspaces] description = "Split monitor workspaces" -version = "1.0.0" +version = "1.1.0" author = "Duckonaut" [split-monitor-workspaces.build] diff --git a/src/main.cpp b/src/main.cpp index 023da5b..b8b4df6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,23 +15,30 @@ const std::string k_workspaceCount = "plugin:split-monitor-workspaces:count"; const CColor s_pluginColor = {0x61 / 255.0f, 0xAF / 255.0f, 0xEF / 255.0f, 1.0f}; -std::map> g_vMonitorWorkspaceMap; - static HOOK_CALLBACK_FN* e_monitorAddedHandle = nullptr; static HOOK_CALLBACK_FN* e_monitorRemovedHandle = nullptr; -const std::string& getWorkspaceFromMonitor(CMonitor* monitor, const std::string& workspace) +const std::string getWorkspaceFromMonitor(CMonitor* monitor, const std::string& workspace) { - int workspaceIndex = std::stoi(workspace); - if (workspaceIndex - 1 < 0) { + int workspaceIndex = 1; + try { + workspaceIndex = std::stoi(workspace); + } catch (...) { return workspace; } - if (workspaceIndex - 1 >= g_vMonitorWorkspaceMap[monitor->ID].size()) { + // Checks + int workspaceCount = g_pConfigManager->getConfigValuePtrSafe(k_workspaceCount)->intValue; + if (workspaceIndex < 1 || workspaceIndex > workspaceCount) { return workspace; } - return g_vMonitorWorkspaceMap[monitor->ID][workspaceIndex - 1]; + // Compute workspace index + std::size_t monitors = g_pCompositor->m_vMonitors.size(); + + int actualIndex = (workspaceIndex - 1) * monitors + monitor->ID + 1; + + return std::to_string(actualIndex); } void monitorWorkspace(std::string workspace) @@ -57,29 +64,24 @@ void monitorMoveToWorkspaceSilent(std::string workspace) void mapWorkspacesToMonitors() { - g_vMonitorWorkspaceMap.clear(); - - int workspaceIndex = 1; - - for (auto& monitor : g_pCompositor->m_vMonitors) { - int workspaceCount = g_pConfigManager->getConfigValuePtrSafe(k_workspaceCount)->intValue; - std::string logMessage = - "[split-monitor-workspaces] Mapping workspaces " + std::to_string(workspaceIndex) + "-" + std::to_string(workspaceIndex + workspaceCount - 1) + " to monitor " + monitor->szName; - - HyprlandAPI::addNotification(PHANDLE, logMessage, s_pluginColor, 5000); + std::size_t monitors = g_pCompositor->m_vMonitors.size(); + for (auto& workspace : g_pCompositor->m_vWorkspaces) { + int workIndex = 0; + try { + workIndex = std::stoi(workspace->m_szName); + } catch (...) { + continue; + } - for (int i = workspaceIndex; i < workspaceIndex + workspaceCount; i++) { - std::string workspaceName = std::to_string(i); - g_vMonitorWorkspaceMap[monitor->ID].push_back(workspaceName); - HyprlandAPI::invokeHyprctlCommand("keyword", "workspace " + workspaceName + "," + monitor->szName); - CWorkspace* workspace = g_pCompositor->getWorkspaceByName(workspaceName); + // Compute correct monitor (Reverse computation) + int monitor = ((workIndex % monitors) + monitors - 1) % monitors; - if (workspace != nullptr) { - g_pCompositor->moveWorkspaceToMonitor(workspace, monitor.get()); - } - } - HyprlandAPI::invokeHyprctlCommand("dispatch", "workspace " + std::to_string(workspaceIndex)); - workspaceIndex += workspaceCount; + std::string cmd = "moveworkspacetomonitor " + + std::to_string(workIndex) + " " + + std::to_string(monitor); + + HyprlandAPI::addNotification(PHANDLE, cmd, s_pluginColor, 5000); + HyprlandAPI::invokeHyprctlCommand("dispatch", cmd); } } @@ -119,6 +121,4 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) APICALL EXPORT void PLUGIN_EXIT() { HyprlandAPI::addNotification(PHANDLE, "[split-monitor-workspaces] Unloaded successfully!", s_pluginColor, 5000); - - g_vMonitorWorkspaceMap.clear(); }