Convert tag_name to use tag_id instead

This commit is contained in:
Ottatop 2023-07-21 11:38:46 -05:00
parent afc0818805
commit 3ddd57c63f
6 changed files with 210 additions and 244 deletions

View file

@ -10,20 +10,20 @@
---@field SetKeybind { key: Keys, modifiers: Modifier[], callback_id: integer } ---@field SetKeybind { key: Keys, modifiers: Modifier[], callback_id: integer }
---@field SetMousebind { button: integer } ---@field SetMousebind { button: integer }
--Windows --Windows
---@field CloseWindow { window_id: integer } ---@field CloseWindow { window_id: WindowId }
---@field ToggleFloating { window_id: integer } ---@field ToggleFloating { window_id: WindowId }
---@field SetWindowSize { window_id: integer, width: integer?, height: integer? } ---@field SetWindowSize { window_id: WindowId, width: integer?, height: integer? }
---@field MoveWindowToTag { window_id: integer, tag_id: string } ---@field MoveWindowToTag { window_id: WindowId, tag_id: TagId }
---@field ToggleTagOnWindow { window_id: integer, tag_id: string } ---@field ToggleTagOnWindow { window_id: WindowId, tag_id: TagId }
-- --
---@field Spawn { command: string[], callback_id: integer? } ---@field Spawn { command: string[], callback_id: integer? }
---@field Request Request ---@field Request Request
--Tags --Tags
---@field ToggleTag { output_name: string, tag_name: string } ---@field ToggleTag { tag_id: TagId }
---@field SwitchToTag { output_name: string, tag_name: string } ---@field SwitchToTag { tag_id: TagId }
---@field AddTags { output_name: string, tag_names: string[] } ---@field AddTags { output_name: string, tag_names: string[] }
---@field RemoveTags { output_name: string, tag_names: string[] } ---@field RemoveTags { tag_ids: TagId[] }
---@field SetLayout { output_name: string, tag_name: string, layout: Layout } ---@field SetLayout { tag_id: TagId, layout: Layout }
--Outputs --Outputs
---@field ConnectForAllOutputs { callback_id: integer } ---@field ConnectForAllOutputs { callback_id: integer }

View file

@ -32,6 +32,16 @@ local function new_tag(tag_id)
return t return t
end end
---Switch to this tag.
function tg:switch_to()
tag.switch_to(self)
end
---Toggle this tag.
function tg:toggle()
tag.toggle(self)
end
---Get this tag's internal id. ---Get this tag's internal id.
---@return integer ---@return integer
function tg:id() function tg:id()
@ -75,10 +85,7 @@ end
---Set this tag's layout. ---Set this tag's layout.
---@param layout Layout ---@param layout Layout
function tg:set_layout(layout) function tg:set_layout(layout)
local name = self:name() tag.set_layout(self, layout)
if name ~= nil then
tag.set_layout(name, layout)
end
end end
----------------------------------------------------------- -----------------------------------------------------------
@ -138,27 +145,13 @@ end
---tag.toggle("2", op) ---tag.toggle("2", op)
----- will cause windows on both tags 1 and 2 to be displayed at the same time. ----- will cause windows on both tags 1 and 2 to be displayed at the same time.
---``` ---```
---@param name string The name of the tag. ---@param t Tag
---@param output Output? The output. function tag.toggle(t)
function tag.toggle(name, output) SendMsg({
if output ~= nil then ToggleTag = {
SendMsg({ tag_id = t:id(),
ToggleTag = { },
output_name = output:name(), })
tag_name = name,
},
})
else
local op = require("output").get_focused()
if op ~= nil then
SendMsg({
ToggleTag = {
output_name = op:name(),
tag_name = name,
},
})
end
end
end end
---Switch to a tag on the specified output, deactivating any other active tags on it. ---Switch to a tag on the specified output, deactivating any other active tags on it.
@ -171,54 +164,25 @@ end
---```lua ---```lua
---tag.switch_to("3") -- Switches to and displays *only* windows on tag 3 ---tag.switch_to("3") -- Switches to and displays *only* windows on tag 3
---``` ---```
---@param name string The name of the tag. ---@param t Tag
---@param output Output? The output. function tag.switch_to(t)
function tag.switch_to(name, output) SendMsg({
if output ~= nil then SwitchToTag = {
SendMsg({ tag_id = t:id(),
SwitchToTag = { },
output_name = output:name(), })
tag_name = name,
},
})
else
local op = require("output").get_focused()
if op ~= nil then
SendMsg({
SwitchToTag = {
output_name = op:name(),
tag_name = name,
},
})
end
end
end end
---Set a layout for the tag on the specified output. If there is none, set it for the tag on the currently focused one. ---Set a layout for the specified tag.
---@param name string The name of the tag. ---@param t Tag
---@param layout Layout The layout. ---@param layout Layout
---@param output Output? The output. function tag.set_layout(t, layout)
function tag.set_layout(name, layout, output) SendMsg({
if output ~= nil then SetLayout = {
SendMsg({ tag_id = t:id(),
SetLayout = { layout = layout,
output_name = output:name(), },
tag_name = name, })
layout = layout,
},
})
else
local op = require("output").get_focused()
if op ~= nil then
SendMsg({
SetLayout = {
output_name = op:name(),
tag_name = name,
layout = layout,
},
})
end
end
end end
---Get all tags on the specified output. ---Get all tags on the specified output.

View file

@ -138,11 +138,17 @@ require("pinnacle").setup(function(pinnacle)
-- Tags --------------------------------------------------------------------------- -- Tags ---------------------------------------------------------------------------
output.connect_for_all(function(op) output.connect_for_all(function(op)
-- op:add_tags("1", "2", "3", "4", "5") op:add_tags("1", "2", "3", "4", "5")
-- Same as tag.add(op, "1", "2", "3", "4", "5") -- Same as tag.add(op, "1", "2", "3", "4", "5")
local tags_table = { "Terminal", "Browser", "Code", "Email", "Potato" } -- local tags_table = { "Terminal", "Browser", "Code", "Email", "Potato" }
op:add_tags(tags_table) -- op:add_tags(tags_table)
for _, t in pairs(tag.get_by_name("1")) do
if t:output() and t:output():focused() then
t:toggle()
end
end
end) end)
---@type Layout[] ---@type Layout[]
@ -205,66 +211,146 @@ require("pinnacle").setup(function(pinnacle)
end) end)
input.keybind({ mod_key }, keys.KEY_1, function() input.keybind({ mod_key }, keys.KEY_1, function()
tag.switch_to("1") for _, t in pairs(tag.get_by_name("1")) do
if t:output() and t:output():focused() then
t:switch_to()
end
end
end) end)
input.keybind({ mod_key }, keys.KEY_2, function() input.keybind({ mod_key }, keys.KEY_2, function()
tag.switch_to("2") for _, t in pairs(tag.get_by_name("2")) do
if t:output() and t:output():focused() then
t:switch_to()
end
end
end) end)
input.keybind({ mod_key }, keys.KEY_3, function() input.keybind({ mod_key }, keys.KEY_3, function()
tag.switch_to("3") for _, t in pairs(tag.get_by_name("3")) do
if t:output() and t:output():focused() then
t:switch_to()
end
end
end) end)
input.keybind({ mod_key }, keys.KEY_4, function() input.keybind({ mod_key }, keys.KEY_4, function()
tag.switch_to("4") for _, t in pairs(tag.get_by_name("4")) do
if t:output() and t:output():focused() then
t:switch_to()
end
end
end) end)
input.keybind({ mod_key }, keys.KEY_5, function() input.keybind({ mod_key }, keys.KEY_5, function()
tag.switch_to("5") for _, t in pairs(tag.get_by_name("5")) do
if t:output() and t:output():focused() then
t:switch_to()
end
end
end) end)
input.keybind({ mod_key, "Shift" }, keys.KEY_1, function() input.keybind({ mod_key, "Shift" }, keys.KEY_1, function()
tag.toggle("1") for _, t in pairs(tag.get_by_name("1")) do
if t:output() and t:output():focused() then
t:toggle()
end
end
end) end)
input.keybind({ mod_key, "Shift" }, keys.KEY_2, function() input.keybind({ mod_key, "Shift" }, keys.KEY_2, function()
tag.toggle("2") for _, t in pairs(tag.get_by_name("2")) do
if t:output() and t:output():focused() then
t:toggle()
end
end
end) end)
input.keybind({ mod_key, "Shift" }, keys.KEY_3, function() input.keybind({ mod_key, "Shift" }, keys.KEY_3, function()
tag.toggle("3") for _, t in pairs(tag.get_by_name("3")) do
if t:output() and t:output():focused() then
t:toggle()
end
end
end) end)
input.keybind({ mod_key, "Shift" }, keys.KEY_4, function() input.keybind({ mod_key, "Shift" }, keys.KEY_4, function()
tag.toggle("4") for _, t in pairs(tag.get_by_name("4")) do
if t:output() and t:output():focused() then
t:toggle()
end
end
end) end)
input.keybind({ mod_key, "Shift" }, keys.KEY_5, function() input.keybind({ mod_key, "Shift" }, keys.KEY_5, function()
tag.toggle("5") for _, t in pairs(tag.get_by_name("5")) do
if t:output() and t:output():focused() then
t:toggle()
end
end
end) end)
input.keybind({ mod_key, "Alt" }, keys.KEY_1, function() input.keybind({ mod_key, "Alt" }, keys.KEY_1, function()
window.get_focused():move_to_tag("1") for _, t in pairs(tag.get_by_name("1")) do
if t:output() and t:output():focused() then
window.get_focused():move_to_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Alt" }, keys.KEY_2, function() input.keybind({ mod_key, "Alt" }, keys.KEY_2, function()
window.get_focused():move_to_tag("2") for _, t in pairs(tag.get_by_name("2")) do
if t:output() and t:output():focused() then
window.get_focused():move_to_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Alt" }, keys.KEY_3, function() input.keybind({ mod_key, "Alt" }, keys.KEY_3, function()
window.get_focused():move_to_tag("3") for _, t in pairs(tag.get_by_name("3")) do
if t:output() and t:output():focused() then
window.get_focused():move_to_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Alt" }, keys.KEY_4, function() input.keybind({ mod_key, "Alt" }, keys.KEY_4, function()
window.get_focused():move_to_tag("4") for _, t in pairs(tag.get_by_name("4")) do
if t:output() and t:output():focused() then
window.get_focused():move_to_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Alt" }, keys.KEY_5, function() input.keybind({ mod_key, "Alt" }, keys.KEY_5, function()
window.get_focused():move_to_tag("5") for _, t in pairs(tag.get_by_name("5")) do
if t:output() and t:output():focused() then
window.get_focused():move_to_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_1, function() input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_1, function()
window.get_focused():toggle_tag("1") for _, t in pairs(tag.get_by_name("1")) do
if t:output() and t:output():focused() then
window.get_focused():toggle_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_2, function() input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_2, function()
window.get_focused():toggle_tag("2") for _, t in pairs(tag.get_by_name("2")) do
if t:output() and t:output():focused() then
window.get_focused():toggle_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_3, function() input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_3, function()
window.get_focused():toggle_tag("3") for _, t in pairs(tag.get_by_name("3")) do
if t:output() and t:output():focused() then
window.get_focused():toggle_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_4, function() input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_4, function()
window.get_focused():toggle_tag("4") for _, t in pairs(tag.get_by_name("4")) do
if t:output() and t:output():focused() then
window.get_focused():toggle_tag(t)
end
end
end) end)
input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_5, function() input.keybind({ mod_key, "Shift", "Alt" }, keys.KEY_5, function()
window.get_focused():toggle_tag("5") for _, t in pairs(tag.get_by_name("5")) do
if t:output() and t:output():focused() then
window.get_focused():toggle_tag(t)
end
end
end) end)
end) end)

View file

@ -46,12 +46,12 @@ end
---window.get_focused():move_to_tag("5") ---window.get_focused():move_to_tag("5")
----- ...will make the window only appear on tag 5. ----- ...will make the window only appear on tag 5.
---``` ---```
---@param name string The name of the tag. ---@param t Tag
function win:move_to_tag(name) function win:move_to_tag(t)
SendMsg({ SendMsg({
MoveWindowToTag = { MoveWindowToTag = {
window_id = self.id, window_id = self.id,
tag_id = name, tag_id = t:id(),
}, },
}) })
end end
@ -66,12 +66,12 @@ end
---window.get_focused():toggle_tag("2") ---window.get_focused():toggle_tag("2")
----- ...will also make the window appear on tag 2. ----- ...will also make the window appear on tag 2.
---``` ---```
---@param name string The name of the tag. ---@param t Tag
function win:toggle_tag(name) function win:toggle_tag(t)
SendMsg({ SendMsg({
ToggleTagOnWindow = { ToggleTagOnWindow = {
window_id = self.id, window_id = self.id,
tag_id = name, tag_id = t:id(),
}, },
}) })
end end

View file

@ -40,21 +40,19 @@ pub enum Msg {
}, },
MoveWindowToTag { MoveWindowToTag {
window_id: WindowId, window_id: WindowId,
tag_id: String, tag_id: TagId,
}, },
ToggleTagOnWindow { ToggleTagOnWindow {
window_id: WindowId, window_id: WindowId,
tag_id: String, tag_id: TagId,
}, },
// Tag management // Tag management
ToggleTag { ToggleTag {
output_name: String, tag_id: TagId,
tag_name: String,
}, },
SwitchToTag { SwitchToTag {
output_name: String, tag_id: TagId,
tag_name: String,
}, },
AddTags { AddTags {
/// The name of the output you want these tags on. /// The name of the output you want these tags on.
@ -63,12 +61,10 @@ pub enum Msg {
}, },
RemoveTags { RemoveTags {
/// The name of the output you want these tags removed from. /// The name of the output you want these tags removed from.
output_name: String, tag_ids: Vec<TagId>,
tag_names: Vec<String>,
}, },
SetLayout { SetLayout {
output_name: String, tag_id: TagId,
tag_name: String,
layout: Layout, layout: Layout,
}, },

View file

@ -159,110 +159,49 @@ impl<B: Backend> State<B> {
window.toplevel().send_pending_configure(); window.toplevel().send_pending_configure();
} }
Msg::MoveWindowToTag { window_id, tag_id } => { Msg::MoveWindowToTag { window_id, tag_id } => {
if let Some(window) = window_id.window(self) { let Some(window) = window_id.window(self) else { return };
window.with_state(|state| { let Some(tag) = tag_id.tag(self) else { return };
self.focus_state window.with_state(|state| {
.focused_output state.tags = vec![tag.clone()];
.as_ref() });
.unwrap() let Some(output) = tag.output(self) else { return };
.with_state(|op_state| { self.re_layout(&output);
let tag = op_state.tags.iter().find(|tag| tag.name() == tag_id);
if let Some(tag) = tag {
state.tags = vec![tag.clone()];
}
});
});
let output = self.focus_state.focused_output.clone().unwrap();
self.re_layout(&output);
}
} }
Msg::ToggleTagOnWindow { window_id, tag_id } => { Msg::ToggleTagOnWindow { window_id, tag_id } => {
if let Some(window) = window_id.window(self) { let Some(window) = window_id.window(self) else { return };
window.with_state(|state| { let Some(tag) = tag_id.tag(self) else { return };
self.focus_state
.focused_output
.as_ref()
.unwrap()
.with_state(|op_state| {
let tag = op_state.tags.iter().find(|tag| tag.name() == tag_id);
if let Some(tag) = tag {
if state.tags.contains(tag) {
state.tags.retain(|tg| tg != tag);
} else {
state.tags.push(tag.clone());
}
}
});
});
let output = self.focus_state.focused_output.clone().unwrap(); window.with_state(|state| {
self.re_layout(&output); if state.tags.contains(&tag) {
} state.tags.retain(|tg| tg != &tag);
} else {
state.tags.push(tag.clone());
}
});
let Some(output) = tag.output(self) else { return };
self.re_layout(&output);
} }
Msg::ToggleTag { Msg::ToggleTag { tag_id } => {
output_name,
tag_name,
} => {
tracing::debug!("ToggleTag"); tracing::debug!("ToggleTag");
if let Some(tag) = tag_id.tag(self) {
let output = self tag.set_active(!tag.active());
.space if let Some(output) = tag.output(self) {
.outputs() self.re_layout(&output);
.find(|op| op.name() == output_name) }
.cloned();
if let Some(output) = output {
output.with_state(|state| {
if let Some(tag) = state.tags.iter_mut().find(|tag| tag.name() == tag_name)
{
tracing::debug!("Setting tag {tag:?} to {}", !tag.active());
tag.set_active(!tag.active());
}
});
self.re_layout(&output);
} }
} }
Msg::SwitchToTag { Msg::SwitchToTag { tag_id } => {
output_name, let Some(tag) = tag_id.tag(self) else { return };
tag_name, let Some(output) = tag.output(self) else { return };
} => { output.with_state(|state| {
let output = self for op_tag in state.tags.iter_mut() {
.space op_tag.set_active(false);
.outputs() }
.find(|op| op.name() == output_name) tag.set_active(true);
.cloned(); });
if let Some(output) = output { self.re_layout(&output);
output.with_state(|state| {
if !state.tags.iter().any(|tag| tag.name() == tag_name) {
// TODO: notify error
return;
}
for tag in state.tags.iter_mut() {
tag.set_active(false);
}
let Some(tag) = state
.tags
.iter_mut()
.find(|tag| tag.name() == tag_name)
else {
unreachable!()
};
tag.set_active(true);
tracing::debug!(
"focused tags: {:?}",
state
.tags
.iter()
.filter(|tag| tag.active())
.map(|tag| tag.name())
.collect::<Vec<_>>()
);
});
self.re_layout(&output);
}
} }
// TODO: add output
Msg::AddTags { Msg::AddTags {
output_name, output_name,
tag_names, tag_names,
@ -278,39 +217,20 @@ impl<B: Backend> State<B> {
}); });
} }
} }
Msg::RemoveTags { Msg::RemoveTags { tag_ids } => {
output_name, let tags = tag_ids.into_iter().filter_map(|tag_id| tag_id.tag(self));
tag_names, for tag in tags {
} => { let Some(output) = tag.output(self) else { continue };
if let Some(output) = self
.space
.outputs()
.find(|output| output.name() == output_name)
{
output.with_state(|state| { output.with_state(|state| {
state.tags.retain(|tag| !tag_names.contains(&tag.name())); state.tags.retain(|tg| tg != &tag);
}); });
} }
} }
Msg::SetLayout { Msg::SetLayout { tag_id, layout } => {
output_name, let Some(tag) = tag_id.tag(self) else { return };
tag_name, tag.set_layout(layout);
layout, let Some(output) = tag.output(self) else { return };
} => { self.re_layout(&output);
let output = self
.space
.outputs()
.find(|op| op.name() == output_name)
.cloned();
if let Some(output) = output {
output.with_state(|state| {
if let Some(tag) = state.tags.iter_mut().find(|tag| tag.name() == tag_name)
{
tag.set_layout(layout);
}
});
self.re_layout(&output);
}
} }
Msg::ConnectForAllOutputs { callback_id } => { Msg::ConnectForAllOutputs { callback_id } => {