Skip to content

Commit

Permalink
Show alerted workspace and app
Browse files Browse the repository at this point in the history
  • Loading branch information
2hdddg committed Jan 28, 2024
1 parent 52a4c6d commit c8467b9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
21 changes: 17 additions & 4 deletions config/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,35 @@ local function render_workspaces(displayName)
local workspaces = {}
for _, workspace in pairs(display.workspaces) do
local app_name = ""
local app_alert = ""
for _, app in pairs(workspace.applications) do
if app.focus then app_name = app.name end
if app.alert then
end
if app.alert and app_alert == "" then
app_alert = app.name
end
end
if #workspace.applications == 0 then app_name = "<desktop>" end
boxcolor = BLACK_BR
if workspace.alert then
boxcolor = RED
end
if workspace.focus then
boxcolor = YELLOW
end
local items = {
wsbox(label{label=" " .. zen.u.html_escape(workspace.name) .. " "}, boxcolor),
wsbox(label{label=zen.u.html_escape(app_name)}, boxcolor),
}
if app_alert ~= "" and app_alert ~= app_name then
table.insert(items, wsbox(label{label=zen.u.html_escape(app_alert)}, RED))
end
local workspace = {
type = "flex",
direction = "row",
padding = { right = 1 },
items = {
wsbox(label{label=" " .. zen.u.html_escape(workspace.name) .. " "}, boxcolor),
wsbox(label{label=zen.u.html_escape(app_name)}, boxcolor),
},
items = items,
tag = workspace.name
}
table.insert(workspaces, workspace)
Expand Down
7 changes: 6 additions & 1 deletion src/PowerSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ std::shared_ptr<PowerSource> PowerSource::Create(std::shared_ptr<MainLoop> mainl

bool PowerSource::Initialize() {
m_drawn = m_published = false;
m_sourceState = PowerState{.IsPluggedIn = false, .IsCharging = false, .Capacity = 0};
m_sourceState = PowerState{
.IsAlerted = false,
.IsPluggedIn = false,
.IsCharging = false,
.Capacity = 0,
};
// TODO: Probe that these exists
m_ac = "/sys/class/power_supply/AC/online";
// TODO: Always BAT0?
Expand Down
3 changes: 3 additions & 0 deletions src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,20 @@ void ScriptContextImpl::Publish(const std::string_view name, const Displays& dis
auto applicationTable = m_lua.create_table();
applicationTable["name"] = application.name;
applicationTable["focus"] = application.isFocused;
applicationTable["alert"] = application.isAlerted;
applicationTable["appid"] = application.appId;
applicationsTable.add(applicationTable);
}
workspaceTable["name"] = workspace.name;
workspaceTable["focus"] = workspace.isFocused;
workspaceTable["alert"] = workspace.isAlerted;
workspaceTable["applications"] = applicationsTable;
workspacesTable.add(workspaceTable);
}
displayTable["workspaces"] = workspacesTable;
spdlog::debug("Display {} focus: {}", display.name, display.isFocused);
displayTable["focus"] = display.isFocused;
displayTable["alert"] = display.isAlerted;
displaysTable[display.name] = displayTable;
}
m_lua["zen"][name] = displaysTable;
Expand Down
7 changes: 5 additions & 2 deletions src/ScriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ struct Application {
std::string name;
std::string appId;
bool isFocused;
bool isAlerted;
};

struct Workspace {
Workspace(const std::string name_) : name(name_), isFocused(false) {}
Workspace(const std::string name_) : name(name_), isFocused(false), isAlerted(false) {}
std::string name;
bool isFocused; // Has the focused application
bool isAlerted; // Has an alerted application
std::vector<Application> applications;
};

struct Display {
Display(const std::string& name_) : name(name_), isFocused(false) {}
Display(const std::string& name_) : name(name_), isFocused(false), isAlerted(false) {}
std::string name;
bool isFocused; // Has the focused workspace
bool isAlerted; // Has an alerted workspace (or application)
std::vector<Workspace> workspaces;
};

Expand Down
16 changes: 15 additions & 1 deletion src/SwayCompositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,19 @@ static std::string GetString(basic_json<> node, const char *attr) {
return v.get<std::string>();
}

static bool GetBool(basic_json<> node, const char *attr, bool whenMissing) {
auto x = node[attr];
if (x.is_null()) return whenMissing;
if (!x.is_boolean()) return whenMissing; // TODO: Warn
return x.get<bool>();
}

static std::string GetName(basic_json<> node) { return GetString(node, "name"); }

static bool GetFocused(basic_json<> node) { return node["focused"].get<bool>(); }

static bool GetUrgent(basic_json<> node) { return GetBool(node, "urgent", false); }

static void ParseBarStateUpdateEvent(const std::string &payload, bool &visibleByModifier) {
auto rootNode = json::parse(payload, Filter, false /*ignore exceptions*/);
if (rootNode.is_discarded()) {
Expand Down Expand Up @@ -118,7 +127,8 @@ static void ParseApplication(Workspace &workspace, nlohmann::basic_json<> applic
}
auto application = Application{.name = GetName(applicationNode),
.appId = GetString(applicationNode, "app_id"),
.isFocused = false};
.isFocused = false,
.isAlerted = false};
int applicationId = applicationNode["id"].get<int>();
// In sway the only focused app is the one in the focused workspace. When that app is focused
// the workspace is not. To simplify usage this considers the focused app in a non focused
Expand All @@ -129,6 +139,8 @@ static void ParseApplication(Workspace &workspace, nlohmann::basic_json<> applic
} else {
application.isFocused = applicationId == nextFocusId;
}
application.isAlerted = GetUrgent(applicationNode);
workspace.isAlerted = workspace.isAlerted || application.isAlerted;
workspace.applications.push_back(std::move(application));
}

Expand Down Expand Up @@ -171,6 +183,7 @@ static std::optional<Displays> ParseTree(const std::string &payload) {
continue;
}
auto workspace = Workspace(GetName(workspaceNode));
workspace.isAlerted = GetUrgent(workspaceNode);
// Better way?
auto focusNode = workspaceNode["focus"];
int nextFocusId = -1;
Expand All @@ -187,6 +200,7 @@ static std::optional<Displays> ParseTree(const std::string &payload) {
ParseApplication(workspace, applicationNode, nextFocusId);
}
display.isFocused = display.isFocused || workspace.isFocused;
display.isAlerted = display.isAlerted || workspace.isAlerted;
display.workspaces.push_back(std::move(workspace));
}
displays.push_back(std::move(display));
Expand Down

0 comments on commit c8467b9

Please sign in to comment.