Add mode configuration support

This commit is contained in:
Dustin Thomas 2025-03-07 21:18:30 -06:00
parent 7fb1c70544
commit 4f0d743e7f
Signed by: cptlobster
GPG key ID: 33D607425C830B4C
3 changed files with 65 additions and 10 deletions

View file

@ -4,12 +4,15 @@ version = "0.1"
# Startup applications (these will be called with exec)
# To add commands to be executed on config reloads too, create an exec_always entry)
exec = [
{ command = "uwsm finalize SWAYSOCK I3SOCK XCURSOR_SIZE XCURSOR_THEME", no-startup-id = true},
{ command = "dex --autostart --environment sway", no-startup-id = true },
"swayidle -w timeout 300 $lock_sh before-sleep $lock_sh",
{ command = "nm-applet --sm-disable", no-startup-id = true },
{ command = "blueman-applet", no-startup-id = true },
{ command = "mako", no-startup-id = true },
{ command = "swaybg -i $bg_file -m fill", no-startup-id = true }
{ command = "swaybg -i $bg_file -m fill", no-startup-id = true },
{ command = "/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1", no-startup-id = true },
{ command = "kanshi", no-startup-id = true }
]
tiling-drag = true
@ -30,11 +33,11 @@ size = 12
# for_window rules
[[for-window]]
rule = { class = "(?i)gscreenshot" }
rule = [{ class = "(?i)gscreenshot" }]
floating = "enable"
[[for-window]]
rule = { window-role = "Toplevel" }
rule = [{ window-role = "Toplevel" }]
floating = "enable"
[set]
@ -58,12 +61,12 @@ refresh_i3status = "pkill -RTMIN+10 i3blocks"
# kill currently focused window
"$mod+Shift+Q".kill = {}
# launch applications
"$mod+Return".exec = "alacritty"
"$mod+Shift+space".exec = "rofi -combi-modi window,drun,ssh -show combi"
"Mod4+Control+Mod1+Shift+l".exec = "xdg-open https://www.linkedin.com"
"$mod+Return".exec = "uwsm app -- alacritty"
"$mod+Shift+space".exec = "rofi -show drun -run-command \"uwsm app -- {cmd}\""
"Mod4+Control+Mod1+Shift+l".exec = "uwsm app -- xdg-open https://www.linkedin.com"
# Screenshots
"$mod+Control+s".exec = "gscreenshot"
"$mod+Shift+s".exec = "gscreenshot -s -c -n"
"$mod+Shift+s".exec = "uwsm app -- grim -g \"$(slurp -d)\" - | wl-copy -t image/png"
"$mod+Mod1+s".exec = "gscreenshot -c -n"
# focus windows
"$mod+Up".focus.directional = "up"
@ -110,6 +113,9 @@ refresh_i3status = "pkill -RTMIN+10 i3blocks"
"$mod+Shift+8".move.container = 8
"$mod+Shift+9".move.container = 9
"$mod+Shift+0".move.container = 10
"$mod+Shift+Tab".move.container = "scratchpad"
# show scratchpad
"$mod+Tab".scratchpad = {}
# media controls
"XF86AudioRaiseVolume".exec = { command = "$ch_vol_sh +5% && $refresh_i3status", no-startup-id = true }
"XF86AudioLowerVolume".exec = { command = "$ch_vol_sh -5% && $refresh_i3status", no-startup-id = true }
@ -121,7 +127,6 @@ refresh_i3status = "pkill -RTMIN+10 i3blocks"
"XF86AudioPrev".exec = { command = "playerctl previous", no-startup-id = true }
# config
"$mod+Shift+c".reload = {}
#"$mod+Shift+r".restart = true
# power options
"$mod+l".exec = { command = "$lock_sh", no-startup-id = true }
"$mod+Shift+e".exec = "rofi -show p -modi p:rofi-power-menu"

View file

@ -66,6 +66,8 @@ pub struct Config {
/// Default orientation and workspace layout
#[serde(default)]
default: Option<Defaults>,
#[serde(default)]
modes: Option<Modes>,
/// User-defined bindsym commands
#[serde(default)]
bindsym: Option<HashMap<String, KeylessBindsym>>,
@ -76,6 +78,38 @@ pub struct Config {
bar: Option<Bar>,
}
#[derive(PartialEq, Eq, Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Modes (HashMap<String, ModeCfg>);
impl Display for Modes {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
for (k, v) in self.0.iter() {
log::debug!("Converting mode {}...", k);
let header = format!("# Configuration for mode {}", k);
write!(f, "{}\nmode {} {{\n{}\n}}\n", header, k, indent(&v.to_string(), 4))?;
}
Ok(())
}
}
#[derive(PartialEq, Eq, Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ModeCfg {
// User defined bindsym commands for this mode
bindsym: Option<HashMap<String, KeylessBindsym>>,
// User defined bindcode commands for this mode
bindcode: Option<HashMap<String, KeylessBindsym>>,
}
impl Display for ModeCfg {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let bindsym = stringify_bindsyms(&self.bindsym);
let bindcode = stringify_bindcodes(&self.bindcode);
write!(f, "{}{}", bindsym, bindcode)
}
}
#[derive(PartialEq, Eq, Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Defaults {
@ -151,6 +185,11 @@ impl Bar {
}
}
fn indent(content: &str, level: u8) -> String {
let ind = (0..level).map(|_| " ").collect::<String>();
content.lines().map(|s| format!("{}{}", ind, s)).collect::<Vec<String>>().join("\n")
}
fn with_comment_header(section: String, header: String) -> String {
let comment = header.lines().map(|l| format!("# {l}")).collect::<Vec<String>>().join("\n");
format!("{}\n{}\n\n", comment, section)
@ -266,6 +305,14 @@ fn stringify_bar (bar: &Option<Bar>) -> String {
}
}
fn stringify_modes (modes: &Option<Modes>) -> String {
log::debug!("Converting modes...");
match modes {
Some(m) => with_comment_header(m.to_string(), "Mode configuration".to_string()),
None => String::new()
}
}
impl Display for Config {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
let header =
@ -274,12 +321,13 @@ impl Display for Config {
\nwill need to run `sway -c [config file] -C` to do so.\
\n\
\nFor more information, please visit https://github.com/cptlobster/swayconf.";
write!(f, "{}{}{}{}{}{}{}{}",
write!(f, "{}{}{}{}{}{}{}{}{}",
with_comment_header(String::new(), header.to_string()),
stringify_sets(&self.set),
stringify_exec(&self.exec),
stringify_exec_always(&self.exec_always),
stringify_defaults(&self.default),
stringify_modes(&self.modes),
stringify_bindsyms(&self.bindsym),
stringify_bindcodes(&self.bindcode),
stringify_bar(&self.bar)

View file

@ -15,7 +15,7 @@
use serde::{Deserialize, Serialize};
use strum::Display;
use crate::sway::{criteria, options};
use crate::sway::{options};
use crate::sway::criteria::CriteriaVec;
use crate::sway::options::{bind, exec, focus, layout, mov, resize, ArgMap};
@ -77,6 +77,8 @@ pub enum Runtime {
Layout(layout::LayoutParams),
#[strum(to_string = "max_render_time {0}")]
MaxRenderTime(options::MaxRenderTimeOpts),
#[strum(to_string = "mode {0}")]
Mode(String),
#[strum(to_string = "move {0}")]
Move(mov::MoveParams),
Nop,