Simplify tag in API

This commit is contained in:
Ottatop 2023-07-20 16:05:26 -05:00
parent 9475c9965d
commit 2d42f01b8a
6 changed files with 68 additions and 52 deletions

View file

@ -37,12 +37,9 @@
--Outputs
---@field GetOutputProps { output_name: string }
--Tags
---@field GetTagsByName { tag_name: string }
---@field GetTagOutput { tag_id: TagId }
---@field GetTagActive { tag_id: TagId }
---@field GetTagName { tag_id: TagId }
---@field GetTagProps { tag_id: TagId }
---@alias Request _Request | "GetWindows" | "GetOutputs"
---@alias Request _Request | "GetWindows" | "GetOutputs" | "GetTags"
---@class IncomingMsg
---@field CallCallback { callback_id: integer, args: Args }
@ -67,5 +64,4 @@
---@field OutputProps { make: string?, model: string?, loc: integer[]?, res: integer[]?, refresh_rate: integer?, physical_size: integer[]?, focused: boolean?, tag_ids: integer[]? }
--Tags
---@field Tags { tag_ids: TagId[] }
---@field TagActive { active: boolean? }
---@field TagName { name: string? }
---@field TagProps { active: boolean?, name: string?, output_name: string? }

View file

@ -297,13 +297,13 @@ end
---@return Output|nil
function output.get_for_tag(tag)
SendRequest({
GetTagOutput = {
GetTagProps = {
tag_id = tag:id(),
},
})
local response = ReadMsg()
local output_name = response.RequestResponse.response.Output.output_name
local output_name = response.RequestResponse.response.TagProps.output_name
if output_name == nil then
return nil

View file

@ -42,13 +42,13 @@ end
---@return boolean|nil active `true` if the tag is active, `false` if not, and `nil` if the tag doesn't exist.
function tg:active()
SendRequest({
GetTagActive = {
GetTagProps = {
tag_id = self._id,
},
})
local response = ReadMsg()
local active = response.RequestResponse.response.TagActive.active
local active = response.RequestResponse.response.TagProps.active
return active
end
@ -56,13 +56,13 @@ end
---@return string|nil name The name of this tag, or nil if it doesn't exist.
function tg:name()
SendRequest({
GetTagName = {
GetTagProps = {
tag_id = self._id,
},
})
local response = ReadMsg()
local name = response.RequestResponse.response.TagName.name
local name = response.RequestResponse.response.TagProps.name
return name
end
@ -74,7 +74,7 @@ end
---Set this tag's layout.
---@param layout Layout
function tg:set_layout(layout) -- TODO: output param
function tg:set_layout(layout)
local name = self:name()
if name ~= nil then
tag.set_layout(name, layout)
@ -266,11 +266,29 @@ end
---@param name string The name of the tags you want.
---@return Tag[]
function tag.get_by_name(name)
SendRequest({
GetTagsByName = {
tag_name = name,
},
})
SendRequest("GetTags")
local response = ReadMsg()
local tag_ids = response.RequestResponse.response.Tags.tag_ids
---@type Tag[]
local tags = {}
for _, tag_id in pairs(tag_ids) do
local t = new_tag(tag_id)
if t:name() == name then
table.insert(tags, t)
end
end
return tags
end
---Get all tags across all ouptuts.
---@return Tag[]
function tag.get_all()
SendRequest("GetTags")
local response = ReadMsg()

View file

@ -113,6 +113,25 @@ require("pinnacle").setup(function(pinnacle)
for _, tg in pairs(tags) do
print(tg:name())
print((tg:output() and tg:output():name()) or "nil output")
print(tg:active())
end
print("----------------------")
local tags = tag.get_by_name("2")
for _, tg in pairs(tags) do
print(tg:name())
print((tg:output() and tg:output():name()) or "nil output")
print(tg:active())
end
print("----------------------")
local tags = tag.get_all()
for _, tg in pairs(tags) do
print(tg:name())
print((tg:output() and tg:output():name()) or "nil output")
print(tg:active())
end
end)

View file

@ -106,10 +106,8 @@ pub enum Request {
GetOutputs,
GetOutputProps { output_name: String },
// Tags
GetTagsByName { tag_name: String },
GetTagOutput { tag_id: TagId },
GetTagActive { tag_id: TagId },
GetTagName { tag_id: TagId },
GetTags,
GetTagProps { tag_id: TagId },
}
#[derive(Debug, PartialEq, Eq, Copy, Clone, serde::Serialize, serde::Deserialize)]
@ -228,10 +226,9 @@ pub enum RequestResponse {
Tags {
tag_ids: Vec<TagId>,
},
TagActive {
TagProps {
active: Option<bool>,
},
TagName {
name: Option<String>,
output_name: Option<String>,
},
}

View file

@ -479,12 +479,11 @@ impl<B: Backend> State<B> {
)
.expect("failed to send to client");
}
Request::GetTagsByName { tag_name } => {
Request::GetTags => {
let tag_ids = self
.space
.outputs()
.flat_map(|op| op.with_state(|state| state.tags.clone()))
.filter(|tag| tag.name() == tag_name)
.map(|tag| tag.id())
.collect::<Vec<_>>();
crate::api::send_to_client(
@ -495,35 +494,22 @@ impl<B: Backend> State<B> {
)
.expect("failed to send to client");
}
Request::GetTagOutput { tag_id } => {
let output_name = tag_id
.tag(self)
Request::GetTagProps { tag_id } => {
let tag = tag_id.tag(self);
let output_name = tag
.as_ref()
.and_then(|tag| tag.output(self))
.map(|output| output.name());
let active = tag.as_ref().map(|tag| tag.active());
let name = tag.as_ref().map(|tag| tag.name());
crate::api::send_to_client(
&mut stream,
&OutgoingMsg::RequestResponse {
response: RequestResponse::Output { output_name },
},
)
.expect("failed to send to client");
}
Request::GetTagActive { tag_id } => {
let active = tag_id.tag(self).map(|tag| tag.active());
crate::api::send_to_client(
&mut stream,
&OutgoingMsg::RequestResponse {
response: RequestResponse::TagActive { active },
},
)
.expect("failed to send to client");
}
Request::GetTagName { tag_id } => {
let name = tag_id.tag(self).map(|tag| tag.name());
crate::api::send_to_client(
&mut stream,
&OutgoingMsg::RequestResponse {
response: RequestResponse::TagName { name },
response: RequestResponse::TagProps {
active,
name,
output_name,
},
},
)
.expect("failed to send to client");