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

Feat: Add new optional "default command" for auto-launching apps on new workspaces #3559

Merged
11 changes: 9 additions & 2 deletions src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ void CCompositor::moveWorkspaceToMonitor(CWorkspace* pWorkspace, CMonitor* pMoni

Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with new {}", nextWorkspaceOnMonitorID);

g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID);
g_pCompositor->createNewWorkspace(nextWorkspaceOnMonitorID, POLDMON->ID, "", false);
}

Debug::log(LOG, "moveWorkspaceToMonitor: Plugging gap with existing {}", nextWorkspaceOnMonitorID);
Expand Down Expand Up @@ -2431,7 +2431,7 @@ bool CCompositor::cursorOnReservedArea() {
return !VECINRECT(CURSORPOS, XY1.x, XY1.y, XY2.x, XY2.y);
}

CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, const std::string& name) {
CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, const std::string& name, bool isEmpty) {
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
const auto NAME = name == "" ? std::to_string(id) : name;
auto monID = monid;

Expand All @@ -2447,6 +2447,13 @@ CWorkspace* CCompositor::createNewWorkspace(const int& id, const int& monid, con
PWORKSPACE->m_iID = id;
PWORKSPACE->m_iMonitorID = monID;

if (isEmpty) {
const SWorkspaceRule workspaceRule = g_pConfigManager->getWorkspaceRuleFor(PWORKSPACE);
if (!workspaceRule.onCreatedEmptyRunCmd.empty()) {
g_pKeybindManager->spawn(workspaceRule.onCreatedEmptyRunCmd);
}
}

return PWORKSPACE;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Compositor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class CCompositor {
Vector2D parseWindowVectorArgsRelative(const std::string&, const Vector2D&);
void forceReportSizesToWindowsOnWorkspace(const int&);
bool cursorOnReservedArea();
CWorkspace* createNewWorkspace(const int&, const int&, const std::string& name = ""); // will be deleted next frame if left empty and unfocused!
CWorkspace* createNewWorkspace(const int&, const int&, const std::string& name = "", bool isEmpty = true); // will be deleted next frame if left empty and unfocused!
void renameWorkspace(const int&, const std::string& name = "");
void setActiveMonitor(CMonitor*);
bool isWorkspaceSpecial(const int&);
Expand Down
6 changes: 6 additions & 0 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../managers/KeybindManager.hpp"

#include <string.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Expand Down Expand Up @@ -1172,6 +1173,9 @@ void CConfigManager::handleWorkspaceRules(const std::string& command, const std:
rules = value.substr(WORKSPACE_DELIM + 1);
}

const static std::string ruleOnCreatedEmtpy = "on-created-empty:";
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
const static int ruleOnCreatedEmtpyLen = ruleOnCreatedEmtpy.length();

auto assignRule = [&](std::string rule) {
size_t delim = std::string::npos;
if ((delim = rule.find("gapsin:")) != std::string::npos)
Expand All @@ -1194,6 +1198,8 @@ void CConfigManager::handleWorkspaceRules(const std::string& command, const std:
wsRule.isDefault = configStringToInt(rule.substr(delim + 8));
else if ((delim = rule.find("persistent:")) != std::string::npos)
wsRule.isPersistent = configStringToInt(rule.substr(delim + 11));
else if ((delim = rule.find(ruleOnCreatedEmtpy)) != std::string::npos)
wsRule.onCreatedEmptyRunCmd = cleanCmdForWorkspace(name, rule.substr(delim + ruleOnCreatedEmtpyLen));
};

size_t pos = 0;
Expand Down
1 change: 1 addition & 0 deletions src/config/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct SWorkspaceRule {
std::optional<int> rounding;
std::optional<int> decorate;
std::optional<int> shadow;
std::string onCreatedEmptyRunCmd;
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
};

struct SMonitorAdditionalReservedArea {
Expand Down
39 changes: 39 additions & 0 deletions src/helpers/MiscFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,45 @@ int getWorkspaceIDFromString(const std::string& in, std::string& outName) {
return result;
}

std::string cleanCmdForWorkspace(const std::string& inWorkspaceName, std::string dirtyCmd) {

vaxerski marked this conversation as resolved.
Show resolved Hide resolved

std::string cmd = removeBeginEndSpacesTabs(dirtyCmd);

if (!cmd.empty()) {
std::string rules;
const std::string workspaceRule = "workspace " + inWorkspaceName;

if (cmd[0] == '[') {
const int closingBracketIdx = cmd.find_last_of(']');
auto tmpRules = cmd.substr(1, closingBracketIdx - 1);
cmd = cmd.substr(closingBracketIdx + 1);

auto rulesList = CVarList(tmpRules, 0, ';');

bool hadWorkspaceRule = false;
rulesList.map([&](std::string& rule) {
if (rule.find("workspace") == 0) {
rule = workspaceRule;
hadWorkspaceRule = true;
}
});

if (!hadWorkspaceRule) {
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
rulesList.append(workspaceRule);
}

rules = "[" + rulesList.join(";") + "]";
} else {
rules = "[" + workspaceRule + "]";
}

return rules + " " + cmd;
}

return {};
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
}

float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2) {
const float DX = std::max({0.0, p1.x - vec.x, vec.x - p2.x});
const float DY = std::max({0.0, p1.y - vec.y, vec.y - p2.y});
Expand Down
1 change: 1 addition & 0 deletions src/helpers/MiscFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ std::string removeBeginEndSpacesTabs(std::string);
bool isNumber(const std::string&, bool allowfloat = false);
bool isDirection(const std::string&);
int getWorkspaceIDFromString(const std::string&, std::string&);
std::string cleanCmdForWorkspace(const std::string&, std::string);
float vecToRectDistanceSquared(const Vector2D& vec, const Vector2D& p1, const Vector2D& p2);
void logSystemInfo();
std::string execAndGet(const char*);
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/VarList.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <functional>
#include <vector>
#include <string>
#include "../macros.hpp"
Expand All @@ -20,6 +21,15 @@ class CVarList {

std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const;

void map(std::function<void(std::string&)> func) {
for (auto& s : m_vArgs)
func(s);
}

void append(const std::string arg) {
m_vArgs.emplace_back(arg);
}

std::string operator[](const size_t& idx) const {
if (idx >= m_vArgs.size())
return "";
Expand Down
14 changes: 9 additions & 5 deletions src/managers/KeybindManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "KeybindManager.hpp"
#include "../render/decorations/CHyprGroupBarDecoration.hpp"
#include "debug/Log.hpp"
#include "helpers/VarList.hpp"

#include <regex>

Expand Down Expand Up @@ -810,7 +812,7 @@ void CKeybindManager::changeworkspace(std::string args) {
const auto PCURRENTWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace);
const bool EXPLICITPREVIOUS = args.starts_with("previous");

if (args.starts_with("previous")) {
if (EXPLICITPREVIOUS) {
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
// Do nothing if there's no previous workspace, otherwise switch to it.
if (PCURRENTWORKSPACE->m_sPrevWorkspace.iID == -1) {
Debug::log(LOG, "No previous workspace to change to");
Expand Down Expand Up @@ -842,9 +844,10 @@ void CKeybindManager::changeworkspace(std::string args) {
g_pInputManager->m_bEmptyFocusCursorSet = false;

auto pWorkspaceToChangeTo = g_pCompositor->getWorkspaceByID(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo);
if (!pWorkspaceToChangeTo)
if (!pWorkspaceToChangeTo) {
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
pWorkspaceToChangeTo = g_pCompositor->createNewWorkspace(BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.iID : workspaceToChangeTo, PMONITOR->ID,
BISWORKSPACECURRENT ? PCURRENTWORKSPACE->m_sPrevWorkspace.name : workspaceName);
}

if (!BISWORKSPACECURRENT && pWorkspaceToChangeTo->m_bIsSpecialWorkspace) {
PMONITOR->setSpecialWorkspace(pWorkspaceToChangeTo);
Expand Down Expand Up @@ -939,7 +942,7 @@ void CKeybindManager::moveActiveToWorkspace(std::string args) {
pMonitor = g_pCompositor->getMonitorFromID(pWorkspace->m_iMonitorID);
g_pCompositor->setActiveMonitor(pMonitor);
} else {
pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName);
pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName, false);
pMonitor = g_pCompositor->getMonitorFromID(pWorkspace->m_iMonitorID);
g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace);
}
Expand Down Expand Up @@ -990,7 +993,7 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) {
if (pWorkspace) {
g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace);
} else {
pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName);
pWorkspace = g_pCompositor->createNewWorkspace(WORKSPACEID, PWINDOW->m_iMonitorID, workspaceName, false);
g_pCompositor->moveWindowToWorkspaceSafe(PWINDOW, pWorkspace);
}

Expand Down Expand Up @@ -1467,8 +1470,9 @@ void CKeybindManager::toggleSpecialWorkspace(std::string args) {
Debug::log(LOG, "Toggling special workspace {} to open", workspaceID);
auto PSPECIALWORKSPACE = g_pCompositor->getWorkspaceByID(workspaceID);

if (!PSPECIALWORKSPACE)
if (!PSPECIALWORKSPACE) {
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
PSPECIALWORKSPACE = g_pCompositor->createNewWorkspace(workspaceID, PMONITOR->ID, workspaceName);
}

PMONITOR->setSpecialWorkspace(PSPECIALWORKSPACE);
}
Expand Down