pinnacle/api/rust/examples/example_config.rs

196 lines
5.1 KiB
Rust
Raw Normal View History

2023-10-21 02:32:08 +02:00
// You should glob import these to prevent your config from getting cluttered.
2023-10-20 04:44:33 +02:00
use pinnacle_api::prelude::*;
use pinnacle_api::*;
2023-10-20 00:43:37 +02:00
2023-10-19 06:05:07 +02:00
fn main() {
2023-10-21 02:32:08 +02:00
// Connect to the Pinnacle server.
// This needs to be called before you start calling any config functions.
2023-10-21 00:56:34 +02:00
pinnacle_api::connect().unwrap();
2023-10-20 00:43:37 +02:00
2023-10-21 00:56:34 +02:00
let mod_key = Modifier::Ctrl;
2023-10-20 04:44:33 +02:00
2023-10-21 00:56:34 +02:00
let terminal = "alacritty";
2023-10-20 00:43:37 +02:00
2023-10-21 00:56:34 +02:00
process::set_env("MOZ_ENABLE_WAYLAND", "1");
2023-10-21 02:32:08 +02:00
// You must create a callback_vec to hold your callbacks.
// Rust is not Lua, so it takes a bit more work to get captures working.
//
// Anything that requires a callback will also require a mut reference to this struct.
//
// Additionally, all callbacks also take in `&mut CallbackVec`.
// This allows you to call functions that need callbacks within other callbacks.
2023-10-21 02:15:49 +02:00
let mut callback_vec = CallbackVec::new();
2023-10-21 02:32:08 +02:00
// Keybinds.
2023-10-21 02:15:49 +02:00
input::mousebind(
&[mod_key],
MouseButton::Left,
MouseEdge::Press,
move |_| {
window::begin_move(MouseButton::Left);
},
&mut callback_vec,
);
2023-10-21 00:56:34 +02:00
input::mousebind(
&[mod_key],
MouseButton::Right,
MouseEdge::Press,
2023-10-21 02:15:49 +02:00
move |_| {
2023-10-21 00:56:34 +02:00
window::begin_resize(MouseButton::Right);
},
2023-10-21 02:15:49 +02:00
&mut callback_vec,
2023-10-21 00:56:34 +02:00
);
2023-10-20 04:44:33 +02:00
2023-10-21 02:15:49 +02:00
input::keybind(
&[mod_key, Modifier::Alt],
'q',
|_| pinnacle::quit(),
&mut callback_vec,
);
2023-10-20 04:44:33 +02:00
2023-10-21 02:15:49 +02:00
input::keybind(
&[mod_key, Modifier::Alt],
'c',
move |_| {
if let Some(window) = window::get_focused() {
window.close();
}
},
&mut callback_vec,
);
2023-10-21 00:56:34 +02:00
2023-10-21 02:15:49 +02:00
input::keybind(
&[mod_key],
xkbcommon::xkb::keysyms::KEY_Return,
move |_| {
process::spawn(vec![terminal]).unwrap();
},
&mut callback_vec,
);
2023-10-21 00:56:34 +02:00
input::keybind(
&[mod_key, Modifier::Alt],
xkbcommon::xkb::keysyms::KEY_space,
2023-10-21 02:15:49 +02:00
move |_| {
if let Some(window) = window::get_focused() {
2023-10-21 00:56:34 +02:00
window.toggle_floating();
2023-10-20 04:44:33 +02:00
}
2023-10-21 00:56:34 +02:00
},
2023-10-21 02:15:49 +02:00
&mut callback_vec,
2023-10-21 00:56:34 +02:00
);
2023-10-20 04:44:33 +02:00
2023-10-21 02:15:49 +02:00
input::keybind(
&[mod_key],
'f',
move |_| {
if let Some(window) = window::get_focused() {
window.toggle_fullscreen();
}
},
&mut callback_vec,
);
2023-10-21 00:56:34 +02:00
2023-10-21 02:15:49 +02:00
input::keybind(
&[mod_key],
'm',
move |_| {
if let Some(window) = window::get_focused() {
window.toggle_maximized();
}
},
&mut callback_vec,
);
2023-10-21 00:56:34 +02:00
let tags = ["1", "2", "3", "4", "5"];
2023-10-21 02:15:49 +02:00
output::connect_for_all(
move |output, _| {
tag::add(&output, tags.as_slice());
tag::get("1", Some(&output)).unwrap().toggle();
},
&mut callback_vec,
);
2023-10-21 00:56:34 +02:00
2023-10-21 02:32:08 +02:00
let mut layout_cycler = tag::layout_cycler(&[
Layout::MasterStack,
Layout::Dwindle,
Layout::Spiral,
Layout::CornerTopLeft,
Layout::CornerTopRight,
Layout::CornerBottomLeft,
Layout::CornerBottomRight,
]);
input::keybind(
&[mod_key],
xkbcommon::xkb::keysyms::KEY_space,
move |_| {
(layout_cycler.next)(None);
},
&mut callback_vec,
);
input::keybind(
&[mod_key, Modifier::Shift],
xkbcommon::xkb::keysyms::KEY_space,
move |_| {
(layout_cycler.prev)(None);
},
&mut callback_vec,
);
// Keybinds for tags
2023-10-21 00:56:34 +02:00
for tag_name in tags.iter().map(|t| t.to_string()) {
let t = tag_name.clone();
2023-10-21 02:15:49 +02:00
input::keybind(
&[mod_key],
tag_name.chars().next().unwrap(),
move |_| {
tag::get(&t, None).unwrap().switch_to();
},
&mut callback_vec,
);
2023-10-21 00:56:34 +02:00
let t = tag_name.clone();
input::keybind(
&[mod_key, Modifier::Shift],
tag_name.chars().next().unwrap(),
2023-10-21 02:15:49 +02:00
move |_| {
2023-10-21 00:56:34 +02:00
tag::get(&t, None).unwrap().toggle();
},
2023-10-21 02:15:49 +02:00
&mut callback_vec,
2023-10-21 00:56:34 +02:00
);
let t = tag_name.clone();
input::keybind(
2023-10-20 04:44:33 +02:00
&[mod_key, Modifier::Alt],
2023-10-21 00:56:34 +02:00
tag_name.chars().next().unwrap(),
2023-10-21 02:15:49 +02:00
move |_| {
if let Some(window) = window::get_focused() {
2023-10-21 00:56:34 +02:00
window.move_to_tag(&tag::get(&t, None).unwrap());
2023-10-20 04:44:33 +02:00
}
},
2023-10-21 02:15:49 +02:00
&mut callback_vec,
2023-10-20 00:43:37 +02:00
);
2023-10-21 00:56:34 +02:00
let t = tag_name.clone();
input::keybind(
&[mod_key, Modifier::Shift, Modifier::Alt],
tag_name.chars().next().unwrap(),
2023-10-21 02:15:49 +02:00
move |_| {
2023-10-21 00:56:34 +02:00
if let Some(window) = window::get_focused() {
window.toggle_tag(&tag::get(&t, None).unwrap());
}
},
2023-10-21 02:15:49 +02:00
&mut callback_vec,
2023-10-21 00:56:34 +02:00
);
}
2023-10-20 04:44:33 +02:00
2023-10-21 02:32:08 +02:00
// At the very end of your config, you will need to start listening to Pinnacle in order for
// your callbacks to be correctly called.
//
// This will not return unless an error occurs.
2023-10-21 02:15:49 +02:00
pinnacle_api::listen(callback_vec);
2023-10-19 06:05:07 +02:00
}