From 2d42f01b8a94a0ee14774da778325680987ed5ad Mon Sep 17 00:00:00 2001 From: Ottatop Date: Thu, 20 Jul 2023 16:05:26 -0500 Subject: [PATCH] Simplify tag in API --- api/lua/msg.lua | 10 +++------- api/lua/output.lua | 4 ++-- api/lua/tag.lua | 38 ++++++++++++++++++++++++++++---------- api/lua/test_config.lua | 19 +++++++++++++++++++ src/api/msg.rs | 11 ++++------- src/state.rs | 38 ++++++++++++-------------------------- 6 files changed, 68 insertions(+), 52 deletions(-) diff --git a/api/lua/msg.lua b/api/lua/msg.lua index dbc74db..9f1aac9 100644 --- a/api/lua/msg.lua +++ b/api/lua/msg.lua @@ -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? } diff --git a/api/lua/output.lua b/api/lua/output.lua index b18ef61..0e7ef27 100644 --- a/api/lua/output.lua +++ b/api/lua/output.lua @@ -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 diff --git a/api/lua/tag.lua b/api/lua/tag.lua index e7f7746..1879085 100644 --- a/api/lua/tag.lua +++ b/api/lua/tag.lua @@ -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() diff --git a/api/lua/test_config.lua b/api/lua/test_config.lua index b7ab3db..0fc204b 100644 --- a/api/lua/test_config.lua +++ b/api/lua/test_config.lua @@ -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) diff --git a/src/api/msg.rs b/src/api/msg.rs index e9e3e2a..b612257 100644 --- a/src/api/msg.rs +++ b/src/api/msg.rs @@ -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, }, - TagActive { + TagProps { active: Option, - }, - TagName { name: Option, + output_name: Option, }, } diff --git a/src/state.rs b/src/state.rs index a483647..00f5e03 100644 --- a/src/state.rs +++ b/src/state.rs @@ -479,12 +479,11 @@ impl State { ) .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::>(); crate::api::send_to_client( @@ -495,35 +494,22 @@ impl State { ) .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");