Skip to content

Commit

Permalink
allow disabling bar on certain outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Aug 3, 2023
1 parent f837f89 commit 1af0661
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ show_layout_name = true
# WM-specific options
[wm.river]
max_tag = 9 # Show only the first nine tags

# Per output overrides
# [output.your-output-name]
# right now only "enable" option is available
# enable = false
#
# You can have any number of overrides
# [output.eDP-1]
# enable = false
```

## How progressive short mode and rounded corners work
Expand Down
18 changes: 18 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::protocol::zwlr_layer_surface_v1;
use anyhow::{Context, Result};
use pangocairo::pango::FontDescription;
use serde::{de, Deserialize};
use std::collections::HashMap;
use std::fs::read_to_string;
use std::ops::Deref;
use std::path::PathBuf;
Expand Down Expand Up @@ -44,6 +45,8 @@ pub struct Config {
pub show_layout_name: bool,
// wm-specific
pub wm: WmConfig,
// overrides
pub output: HashMap<String, OutputOverrides>,
}

impl Default for Config {
Expand Down Expand Up @@ -84,6 +87,8 @@ impl Default for Config {
wm: WmConfig {
river: RiverConfig { max_tag: 9 },
},

output: HashMap::new(),
}
}
}
Expand All @@ -110,6 +115,13 @@ impl Config {
}
})
}

pub fn output_enabled(&self, output: &str) -> bool {
self.output
.get(output)
.and_then(|o| o.enable)
.unwrap_or(true)
}
}

fn config_dir() -> Option<PathBuf> {
Expand Down Expand Up @@ -144,6 +156,12 @@ pub struct RiverConfig {
pub max_tag: u8,
}

#[derive(Debug, Deserialize)]
pub struct OutputOverrides {
#[serde(default)]
enable: Option<bool>,
}

#[derive(Debug)]
pub struct Font(pub FontDescription);

Expand Down
64 changes: 49 additions & 15 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub struct State {
seats: Seats,
pointers: Vec<Pointer>,

// Outputs that haven't yet advertised their names
pending_outputs: Vec<PendingOutput>,

pub hidden: bool,
pub bars: Vec<Bar>,

Expand All @@ -47,6 +50,12 @@ struct Pointer {
scroll_frame: ScrollFrame,
}

struct PendingOutput {
wl: WlOutput,
reg_name: u32,
scale: u32,
}

impl State {
pub fn new(conn: &mut Connection<Self>, globals: &Globals) -> Self {
let mut error = Ok(());
Expand Down Expand Up @@ -77,6 +86,8 @@ impl State {
seats: Seats::bind(conn, globals),
pointers: Vec::new(),

pending_outputs: Vec::new(),

hidden: false,
bars: Vec::new(),

Expand Down Expand Up @@ -135,11 +146,23 @@ impl State {
}

fn bind_output(&mut self, conn: &mut Connection<Self>, global: &Global) {
let output = global
.bind_with_cb(conn, 2..=4, wl_output_cb)
.expect("could not bind wl_output");
self.pending_outputs.push(PendingOutput {
wl: global
.bind_with_cb(conn, 2..=4, wl_output_cb)
.expect("could not bind wl_output"),
reg_name: global.name,
scale: 1,
});
}

self.shared_state.wm_info_provider.new_ouput(conn, output);
fn register_output(&mut self, conn: &mut Connection<Self>, output: PendingOutput, name: &str) {
if !self.shared_state.config.output_enabled(name) {
return;
}

self.shared_state
.wm_info_provider
.new_ouput(conn, output.wl);

let surface = self.wl_compositor.create_surface(conn);

Expand All @@ -150,15 +173,15 @@ impl State {
let layer_surface = self.layer_shell.get_layer_surface_with_cb(
conn,
surface,
Some(output),
Some(output.wl),
zwlr_layer_shell_v1::Layer::Top,
wayrs_client::cstr!("i3bar-river").into(),
layer_surface_cb,
);

let mut bar = Bar {
output,
output_reg_name: global.name,
output: output.wl,
output_reg_name: output.reg_name,
hidden: self.hidden,
mapped: false,
frame_cb: None,
Expand Down Expand Up @@ -256,18 +279,29 @@ fn wl_registry_cb(conn: &mut Connection<State>, state: &mut State, event: &wl_re
}

fn wl_output_cb(
_: &mut Connection<State>,
conn: &mut Connection<State>,
state: &mut State,
output: WlOutput,
event: wl_output::Event,
) {
if let wl_output::Event::Scale(scale) = event {
let bar = state
.bars
.iter_mut()
.find(|bar| bar.output == output)
.unwrap();
bar.scale = scale as u32;
match event {
wl_output::Event::Name(name) => {
let i = state
.pending_outputs
.iter()
.position(|o| o.wl == output)
.unwrap();
let output = state.pending_outputs.swap_remove(i);
state.register_output(conn, output, name.to_str().expect("invalid output name"));
}
wl_output::Event::Scale(scale) => {
if let Some(bar) = state.bars.iter_mut().find(|bar| bar.output == output) {
bar.scale = scale as u32;
} else if let Some(output) = state.pending_outputs.iter_mut().find(|o| o.wl == output) {
output.scale = scale as u32;
}
}
_ => (),
}
}

Expand Down

0 comments on commit 1af0661

Please sign in to comment.