Refactoring

This commit is contained in:
2026-03-05 16:15:10 +01:00
parent c2c695ff45
commit 095c242fcf
2 changed files with 50 additions and 54 deletions

View File

@@ -20,9 +20,7 @@ void HyprlandService::setConfigFilePath(std::string path) {
configFilePath = path; configFilePath = path;
}; };
std::string HyprlandService::getConfigFilePath() { std::string HyprlandService::getConfigFilePath() { return configFilePath; };
return configFilePath;
};
std::list<Workspace> HyprlandService::getWorkspaces() { std::list<Workspace> HyprlandService::getWorkspaces() {
json j = json::parse(ShellService::exec(HYPRCTL_BINARY " workspaces -j")); json j = json::parse(ShellService::exec(HYPRCTL_BINARY " workspaces -j"));
@@ -44,7 +42,8 @@ std::optional<Workspace> HyprlandService::getWorkspace(int id) {
} }
Workspace HyprlandService::getCurrentWorkspace() { Workspace HyprlandService::getCurrentWorkspace() {
json j = json::parse(ShellService::exec(HYPRCTL_BINARY " activeworkspace -j")); json j =
json::parse(ShellService::exec(HYPRCTL_BINARY " activeworkspace -j"));
return j.get<Workspace>(); return j.get<Workspace>();
}; };
@@ -84,15 +83,17 @@ std::list<WindowRule> HyprlandService::getWindowRules() {
}; };
void HyprlandService::setClientFloating(Client c) { void HyprlandService::setClientFloating(Client c) {
ShellService::exec(HYPRCTL_BINARY " dispatch setfloating address:" + c.address); ShellService::exec(HYPRCTL_BINARY " dispatch setfloating address:" +
c.address);
}; };
void HyprlandService::setClientTiled(Client c) { void HyprlandService::setClientTiled(Client c) {
ShellService::exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address); ShellService::exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address);
} }
//on = true -> creates a window rule to ENABLE floating mode for currently active workspace // on = true -> creates a window rule to ENABLE floating mode for currently
//on = false -> creates a window rule to DISABLE floating mode for currently active workspace // active workspace on = false -> creates a window rule to DISABLE floating mode
// for currently active workspace
void HyprlandService::setFloatingRule(bool on) { void HyprlandService::setFloatingRule(bool on) {
WindowRule rule{.tile = !on, .workspaceID = getCurrentWorkspace().id}; WindowRule rule{.tile = !on, .workspaceID = getCurrentWorkspace().id};
auto conflictingRule = findConflictingRule(rule); auto conflictingRule = findConflictingRule(rule);
@@ -100,19 +101,16 @@ void HyprlandService::setFloatingRule(bool on) {
removeRule(conflictingRule.value()); removeRule(conflictingRule.value());
} }
FileService::appendToFile( FileService::appendToFile(getConfigFilePath(), rule.toString());
getConfigFilePath(),
rule.toString()
);
}; };
std::optional<WindowRule> HyprlandService::findConflictingRule(WindowRule subject) { std::optional<WindowRule>
HyprlandService::findConflictingRule(WindowRule subject) {
std::list<WindowRule> rules = getWindowRules(); std::list<WindowRule> rules = getWindowRules();
int id = getCurrentWorkspace().id; int id = getCurrentWorkspace().id;
for (auto &rule : rules) { for (auto &rule : rules) {
if (rule.tile == !subject.tile && rule.workspaceID == subject.workspaceID) if (rule.tile == !subject.tile && rule.workspaceID == subject.workspaceID) {
{
return rule; return rule;
} }
} }
@@ -126,8 +124,7 @@ void HyprlandService::removeRule(WindowRule rule) {
int foundIndex = -1; int foundIndex = -1;
for (auto &it : rules) { for (auto &it : rules) {
if (it.toString() == rule.toString()) if (it.toString() == rule.toString()) {
{
foundIndex = index; foundIndex = index;
break; break;
} }
@@ -135,10 +132,7 @@ void HyprlandService::removeRule(WindowRule rule) {
} }
if (foundIndex != -1) { if (foundIndex != -1) {
FileService::deleteNthLine( FileService::deleteNthLine(getConfigFilePath(), foundIndex);
getConfigFilePath(),
foundIndex
);
} }
// else: rule not found, do nothing // else: rule not found, do nothing
@@ -181,7 +175,8 @@ void HyprlandService::moveToWorkspace(int workspaceId) {
setClientTiled(getActiveClient()); setClientTiled(getActiveClient());
} }
ShellService::exec(HYPRCTL_BINARY " dispatch movetoworkspace " + std::to_string(workspaceId)); ShellService::exec(HYPRCTL_BINARY " dispatch movetoworkspace " +
std::to_string(workspaceId));
} }
void HyprlandService::toggleFloating() { void HyprlandService::toggleFloating() {

View File

@@ -23,15 +23,16 @@ std::string ShellService::exec(const std::string& command) {
return result; return result;
}; };
std::string ShellService::getHomePath() std::string ShellService::getHomePath() {
{
char *home = getenv("HOME"); char *home = getenv("HOME");
if (home && *home) { if (home && *home) {
return std::string(home) + std::string("/"); return std::string(home) + std::string("/");
} }
// Fallback: try to get home directory from passwd if HOME is not set // Fallback: try to get home directory from passwd if HOME is not set
// This assumes that the 'htt' process is ran with the user as the process owner. Should it be run with systemd or some other wonky method, it will behave unexpectedly // This assumes that the 'htt' process is ran with the user as the process
// owner. Should it be run with systemd or some other wonky method, it will
// behave unexpectedly
struct passwd *pw = getpwuid(getuid()); struct passwd *pw = getpwuid(getuid());
if (pw && pw->pw_dir) { if (pw && pw->pw_dir) {
return std::string(pw->pw_dir); return std::string(pw->pw_dir);