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

View File

@@ -23,15 +23,16 @@ std::string ShellService::exec(const std::string& command) {
return result;
};
std::string ShellService::getHomePath()
{
std::string ShellService::getHomePath() {
char *home = getenv("HOME");
if (home && *home) {
return std::string(home) + std::string("/");
}
// 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());
if (pw && pw->pw_dir) {
return std::string(pw->pw_dir);