Allow new tag helpers for window API

This commit is contained in:
Ottatop 2023-08-21 20:47:51 -05:00
parent 7154a0bb95
commit fd09e20ced
2 changed files with 21 additions and 59 deletions

View file

@ -19,10 +19,12 @@ local tag_module = {}
---@field private _id integer The internal id of this tag.
local tag = {}
---***You probably don't need to use this function.***
---
---Create a tag from `Tag|TagTable|TagTableNamed|string`.
---@param tb Tag|TagTable|TagTableNamed|string
---@return Tag|nil
local function create_tag_from_params(tb)
function tag_module.create_tag_from_params(tb)
-- If creating from a tag object, just return the obj
if tb.id then
return tb --[[@as Tag]]
@ -232,7 +234,7 @@ end
---@param t Tag|TagTable|TagTableNamed|string
---@see Tag.toggle — The corresponding object method
function tag_module.toggle(t)
local t = create_tag_from_params(t)
local t = tag_module.create_tag_from_params(t)
if t then
SendMsg({
@ -269,7 +271,7 @@ end
---@param t Tag|TagTable|TagTableNamed|string
---@see Tag.switch_to — The corresponding object method
function tag_module.switch_to(t)
local t = create_tag_from_params(t)
local t = tag_module.create_tag_from_params(t)
if t then
SendMsg({
@ -305,7 +307,7 @@ end
---@param layout Layout The layout.
---@see Tag.set_layout — The corresponding object method
function tag_module.set_layout(t, layout)
local t = create_tag_from_params(t)
local t = tag_module.create_tag_from_params(t)
if t then
SendMsg({
@ -337,12 +339,13 @@ end
---end
---```
---@param params TagTable|TagTableNamed|string
---@return Tag|nil
---
---@see TagModule.get_on_output
---@see TagModule.get_by_name
---@see TagModule.get_all
function tag_module.get(params)
return create_tag_from_params(params)
return tag_module.create_tag_from_params(params)
end
---Get all tags on the specified output.

View file

@ -243,79 +243,38 @@ function window_module.get_all()
end
---Toggle the tag with the given name and (optional) output for the specified window.
---You can also provide a tag object instead of a name and output.
---
---@param w Window
---@param name string
---@param output Output?
---@overload fun(w: Window, t: Tag)
---@param t Tag|TagTable|TagTableNamed|string
---@see Window.toggle_tag — The corresponding object method
function window_module.toggle_tag(w, name, output)
if type(name) == "table" then
SendMsg({
ToggleTagOnWindow = {
window_id = w:id(),
tag_id = name--[[@as Tag]]:id(),
},
})
return
end
function window_module.toggle_tag(w, t)
local t = require("tag").create_tag_from_params(t)
local output = output or require("output").get_focused()
if output == nil then
return
end
local tags = require("tag").get_by_name(name)
for _, t in pairs(tags) do
if t:output() and t:output():name() == output:name() then
if t then
SendMsg({
ToggleTagOnWindow = {
window_id = w:id(),
tag_id = t:id(),
},
})
return
end
end
end
---Move the specified window to the tag with the given name and (optional) output.
---You can also provide a tag object instead of a name and output.
---
---@param w Window
---@param name string
---@param output Output?
---@overload fun(w: Window, t: Tag)
---@param t Tag|TagTable|TagTableNamed|string
---@see Window.move_to_tag — The corresponding object method
function window_module.move_to_tag(w, name, output)
if type(name) == "table" then
SendMsg({
MoveWindowToTag = {
window_id = w:id(),
tag_id = name--[[@as Tag]]:id(),
},
})
return
end
function window_module.move_to_tag(w, t)
local t = require("tag").create_tag_from_params(t)
local output = output or require("output").get_focused()
if output == nil then
return
end
local tags = require("tag").get_by_name(name)
for _, t in pairs(tags) do
if t:output() and t:output():name() == output:name() then
if t then
SendMsg({
MoveWindowToTag = {
window_id = w:id(),
tag_id = t:id(),
},
})
return
end
end
end