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 --Outputs
---@field GetOutputProps { output_name: string } ---@field GetOutputProps { output_name: string }
--Tags --Tags
---@field GetTagsByName { tag_name: string } ---@field GetTagProps { tag_id: TagId }
---@field GetTagOutput { tag_id: TagId }
---@field GetTagActive { tag_id: TagId }
---@field GetTagName { tag_id: TagId }
---@alias Request _Request | "GetWindows" | "GetOutputs" ---@alias Request _Request | "GetWindows" | "GetOutputs" | "GetTags"
---@class IncomingMsg ---@class IncomingMsg
---@field CallCallback { callback_id: integer, args: Args } ---@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[]? } ---@field OutputProps { make: string?, model: string?, loc: integer[]?, res: integer[]?, refresh_rate: integer?, physical_size: integer[]?, focused: boolean?, tag_ids: integer[]? }
--Tags --Tags
---@field Tags { tag_ids: TagId[] } ---@field Tags { tag_ids: TagId[] }
---@field TagActive { active: boolean? } ---@field TagProps { active: boolean?, name: string?, output_name: string? }
---@field TagName { name: string? }

View file

@ -297,13 +297,13 @@ end
---@return Output|nil ---@return Output|nil
function output.get_for_tag(tag) function output.get_for_tag(tag)
SendRequest({ SendRequest({
GetTagOutput = { GetTagProps = {
tag_id = tag:id(), tag_id = tag:id(),
}, },
}) })
local response = ReadMsg() 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 if output_name == nil then
return nil 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. ---@return boolean|nil active `true` if the tag is active, `false` if not, and `nil` if the tag doesn't exist.
function tg:active() function tg:active()
SendRequest({ SendRequest({
GetTagActive = { GetTagProps = {
tag_id = self._id, tag_id = self._id,
}, },
}) })
local response = ReadMsg() local response = ReadMsg()
local active = response.RequestResponse.response.TagActive.active local active = response.RequestResponse.response.TagProps.active
return active return active
end end
@ -56,13 +56,13 @@ end
---@return string|nil name The name of this tag, or nil if it doesn't exist. ---@return string|nil name The name of this tag, or nil if it doesn't exist.
function tg:name() function tg:name()
SendRequest({ SendRequest({
GetTagName = { GetTagProps = {
tag_id = self._id, tag_id = self._id,
}, },
}) })
local response = ReadMsg() local response = ReadMsg()
local name = response.RequestResponse.response.TagName.name local name = response.RequestResponse.response.TagProps.name
return name return name
end end
@ -74,7 +74,7 @@ end
---Set this tag's layout. ---Set this tag's layout.
---@param layout Layout ---@param layout Layout
function tg:set_layout(layout) -- TODO: output param function tg:set_layout(layout)
local name = self:name() local name = self:name()
if name ~= nil then if name ~= nil then
tag.set_layout(name, layout) tag.set_layout(name, layout)
@ -266,11 +266,29 @@ end
---@param name string The name of the tags you want. ---@param name string The name of the tags you want.
---@return Tag[] ---@return Tag[]
function tag.get_by_name(name) function tag.get_by_name(name)
SendRequest({ SendRequest("GetTags")
GetTagsByName = {
tag_name = name, 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() local response = ReadMsg()

View file

@ -113,6 +113,25 @@ require("pinnacle").setup(function(pinnacle)
for _, tg in pairs(tags) do for _, tg in pairs(tags) do
print(tg:name()) print(tg:name())
print((tg:output() and tg:output():name()) or "nil output") 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
end) end)

View file

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

View file

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