Move functions around

This commit is contained in:
Ottatop 2023-07-20 15:22:22 -05:00
parent 83fd3ec978
commit 9475c9965d
6 changed files with 91 additions and 74 deletions

View file

@ -37,7 +37,6 @@
--Outputs
---@field GetOutputProps { output_name: string }
--Tags
---@field GetTagsByOutput { output_name: string }
---@field GetTagsByName { tag_name: string }
---@field GetTagOutput { tag_id: TagId }
---@field GetTagActive { tag_id: TagId }
@ -65,7 +64,7 @@
--Outputs
---@field Output { output_name: OutputName? }
---@field Outputs { output_names: OutputName[] }
---@field OutputProps { make: string?, model: string?, loc: integer[]?, res: integer[]?, refresh_rate: integer?, physical_size: integer[]?, focused: boolean? }
---@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? }

View file

@ -5,9 +5,15 @@
-- SPDX-License-Identifier: MPL-2.0
---@class Output A display.
---@field name string The name of this output (or rather, of its connector).
---@field private _name string The name of this output (or rather, of its connector).
local op = {}
---Get this output's name. This is something like "eDP-1" or "HDMI-A-0".
---@return string
function op:name()
return self._name
end
---Get all tags on this output. See `tag.get_on_output`.
---@return Tag[]
function op:tags()
@ -31,7 +37,7 @@ end
function op:make()
SendRequest({
GetOutputProps = {
output_name = self.name,
output_name = self._name,
},
})
@ -45,7 +51,7 @@ end
function op:model()
SendRequest({
GetOutputProps = {
output_name = self.name,
output_name = self._name,
},
})
@ -59,7 +65,7 @@ end
function op:loc()
SendRequest({
GetOutputProps = {
output_name = self.name,
output_name = self._name,
},
})
@ -77,7 +83,7 @@ end
function op:res()
SendRequest({
GetOutputProps = {
output_name = self.name,
output_name = self._name,
},
})
@ -96,7 +102,7 @@ end
function op:refresh_rate()
SendRequest({
GetOutputProps = {
output_name = self.name,
output_name = self._name,
},
})
@ -110,7 +116,7 @@ end
function op:physical_size()
SendRequest({
GetOutputProps = {
output_name = self.name,
output_name = self._name,
},
})
@ -128,7 +134,7 @@ end
function op:focused()
SendRequest({
GetOutputProps = {
output_name = self.name,
output_name = self._name,
},
})
@ -138,15 +144,17 @@ function op:focused()
end
---This is an internal global function used to create an output object from an output name.
---@param props Output
---@param output_name string The name of the output.
---@return Output
function NewOutput(props)
local function new_output(output_name)
---@type Output
local o = { _name = output_name }
-- Copy functions over
for k, v in pairs(op) do
props[k] = v
o[k] = v
end
return props
return o
end
------------------------------------------------------
@ -174,7 +182,7 @@ function output.get_by_name(name)
for _, output_name in pairs(output_names) do
if output_name == name then
return NewOutput({ name = output_name })
return new_output(output_name)
end
end
@ -195,7 +203,7 @@ function output.get_by_model(model)
---@type Output[]
local outputs = {}
for _, output_name in pairs(output_names) do
local o = NewOutput({ name = output_name })
local o = new_output(output_name)
if o:model() == model then
table.insert(outputs, o)
end
@ -219,7 +227,7 @@ function output.get_by_res(width, height)
---@type Output
local outputs = {}
for _, output_name in pairs(output_names) do
local o = NewOutput({ name = output_name })
local o = new_output(output_name)
if o:res() and o:res().w == width and o:res().h == height then
table.insert(outputs, o)
end
@ -254,7 +262,7 @@ function output.get_focused()
local output_names = response.RequestResponse.response.Outputs.output_names
for _, output_name in pairs(output_names) do
local o = NewOutput({ name = output_name })
local o = new_output(output_name)
if o:focused() then
return o
end
@ -275,7 +283,7 @@ function output.connect_for_all(func)
---@param args Args
table.insert(CallbackTable, function(args)
local args = args.ConnectForAllOutputs
func(NewOutput({ name = args.output_name }))
func(new_output(args.output_name))
end)
SendMsg({
ConnectForAllOutputs = {
@ -284,4 +292,24 @@ function output.connect_for_all(func)
})
end
---Get the output the specified tag is on.
---@param tag Tag
---@return Output|nil
function output.get_for_tag(tag)
SendRequest({
GetTagOutput = {
tag_id = tag:id(),
},
})
local response = ReadMsg()
local output_name = response.RequestResponse.response.Output.output_name
if output_name == nil then
return nil
else
return new_output(output_name)
end
end
return output

View file

@ -16,18 +16,26 @@ local tag = {}
---| "CornerBottomRight" # One main corner window in the bottom right with a column of windows on the left and a row on the top.
---@class Tag
---@field private id integer The internal id of this tag.
---@field private _id integer The internal id of this tag.
local tg = {}
---@param props Tag
---@param tag_id integer
---@return Tag
local function new_tag(props)
local function new_tag(tag_id)
---@type Tag
local t = { _id = tag_id }
-- Copy functions over
for k, v in pairs(tg) do
props[k] = v
t[k] = v
end
return props
return t
end
---Get this tag's internal id.
---@return integer
function tg:id()
return self._id
end
---Get this tag's active status.
@ -35,7 +43,7 @@ end
function tg:active()
SendRequest({
GetTagActive = {
tag_id = self.id,
tag_id = self._id,
},
})
@ -49,7 +57,7 @@ end
function tg:name()
SendRequest({
GetTagName = {
tag_id = self.id,
tag_id = self._id,
},
})
@ -61,20 +69,7 @@ end
---Get this tag's output.
---@return Output|nil output The output this tag is on, or nil if the tag doesn't exist.
function tg:output()
SendRequest({
GetTagOutput = {
tag_id = self.id,
},
})
local response = ReadMsg()
local output_name = response.RequestResponse.response.Output.output_name
if output_name == nil then
return nil
else
return NewOutput({ name = output_name })
end
return require("output").get_for_tag(self)
end
---Set this tag's layout.
@ -110,7 +105,7 @@ function tag.add(output, ...)
SendMsg({
AddTags = {
output_name = output.name,
output_name = output:name(),
tag_names = tag_names,
},
})
@ -132,7 +127,7 @@ end
function tag.add_table(output, names)
SendMsg({
AddTags = {
output_name = output.name,
output_name = output:name(),
tag_names = names,
},
})
@ -155,7 +150,7 @@ function tag.toggle(name, output)
if output ~= nil then
SendMsg({
ToggleTag = {
output_name = output.name,
output_name = output:name(),
tag_name = name,
},
})
@ -164,7 +159,7 @@ function tag.toggle(name, output)
if op ~= nil then
SendMsg({
ToggleTag = {
output_name = op.name,
output_name = op:name(),
tag_name = name,
},
})
@ -188,7 +183,7 @@ function tag.switch_to(name, output)
if output ~= nil then
SendMsg({
SwitchToTag = {
output_name = output.name,
output_name = output:name(),
tag_name = name,
},
})
@ -197,7 +192,7 @@ function tag.switch_to(name, output)
if op ~= nil then
SendMsg({
SwitchToTag = {
output_name = op.name,
output_name = op:name(),
tag_name = name,
},
})
@ -213,7 +208,7 @@ function tag.set_layout(name, layout, output)
if output ~= nil then
SendMsg({
SetLayout = {
output_name = output.name,
output_name = output:name(),
tag_name = name,
layout = layout,
},
@ -223,7 +218,7 @@ function tag.set_layout(name, layout, output)
if op ~= nil then
SendMsg({
SetLayout = {
output_name = op.name,
output_name = op:name(),
tag_name = name,
layout = layout,
},
@ -244,20 +239,24 @@ end
---@return Tag[]
function tag.get_on_output(output)
SendRequest({
GetTagsByOutput = {
output_name = output.name,
GetOutputProps = {
output_name = output:name(),
},
})
local response = ReadMsg()
local tag_ids = response.RequestResponse.response.Tags.tag_ids
local tag_ids = response.RequestResponse.response.OutputProps.tag_ids
---@type Tag[]
local tags = {}
if tag_ids == nil then
return tags
end
for _, tag_id in pairs(tag_ids) do
table.insert(tags, new_tag({ id = tag_id }))
table.insert(tags, new_tag(tag_id))
end
return tags
@ -281,7 +280,7 @@ function tag.get_by_name(name)
local tags = {}
for _, tag_id in pairs(tag_ids) do
table.insert(tags, new_tag({ id = tag_id }))
table.insert(tags, new_tag(tag_id))
end
return tags

View file

@ -109,11 +109,11 @@ require("pinnacle").setup(function(pinnacle)
print("----------------------")
-- local tags = tag.get_on_output(output.get_focused())
-- for _, tg in pairs(tags) do
-- print(tg:name())
-- print(tg:output() and tg:output().name or "nil output")
-- end
local tags = tag.get_on_output(output.get_focused() --[[@as Output]])
for _, tg in pairs(tags) do
print(tg:name())
print((tg:output() and tg:output():name()) or "nil output")
end
end)
-- Tags ---------------------------------------------------------------------------

View file

@ -106,7 +106,6 @@ pub enum Request {
GetOutputs,
GetOutputProps { output_name: String },
// Tags
GetTagsByOutput { output_name: String }, // TODO: move into props
GetTagsByName { tag_name: String },
GetTagOutput { tag_id: TagId },
GetTagActive { tag_id: TagId },
@ -224,6 +223,7 @@ pub enum RequestResponse {
physical_size: Option<(i32, i32)>,
/// Whether the output is focused or not.
focused: Option<bool>,
tag_ids: Option<Vec<TagId>>,
},
Tags {
tag_ids: Vec<TagId>,

View file

@ -457,6 +457,11 @@ impl<B: Backend> State<B> {
.focused_output
.as_ref()
.and_then(|foc_op| output.map(|op| op == foc_op));
let tag_ids = output.as_ref().map(|output| {
output.with_state(|state| {
state.tags.iter().map(|tag| tag.id()).collect::<Vec<_>>()
})
});
crate::api::send_to_client(
&mut stream,
&OutgoingMsg::RequestResponse {
@ -468,26 +473,12 @@ impl<B: Backend> State<B> {
refresh_rate,
physical_size,
focused,
tag_ids,
},
},
)
.expect("failed to send to client");
}
Request::GetTagsByOutput { output_name } => {
let output = self.space.outputs().find(|op| op.name() == output_name);
if let Some(output) = output {
let tag_ids = output.with_state(|state| {
state.tags.iter().map(|tag| tag.id()).collect::<Vec<_>>()
});
crate::api::send_to_client(
&mut stream,
&OutgoingMsg::RequestResponse {
response: RequestResponse::Tags { tag_ids },
},
)
.expect("failed to send to client");
}
}
Request::GetTagsByName { tag_name } => {
let tag_ids = self
.space