From ff8c5e243c13e52a39c6335031e741ac1dac6fd3 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Mon, 28 Aug 2023 17:53:03 +0300 Subject: move some code from state.rs to bar.rs --- src/bar.rs | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- src/state.rs | 109 +++------------------------------------------ 2 files changed, 131 insertions(+), 119 deletions(-) (limited to 'src') diff --git a/src/bar.rs b/src/bar.rs index 7cf53b9..2817e26 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -18,24 +18,24 @@ use crate::wm_info_provider::Tag; pub struct Bar { pub output: Output, - pub hidden: bool, - pub mapped: bool, - pub frame_cb: Option, - pub width: u32, - pub height: u32, - pub scale120: Option, + hidden: bool, + mapped: bool, + frame_cb: Option, + width: u32, + height: u32, + scale120: Option, pub surface: WlSurface, - pub layer_surface: ZwlrLayerSurfaceV1, - pub viewport: WpViewport, - pub fractional_scale: Option, - pub blocks_btns: ButtonManager<(Option, Option)>, - pub tags: Vec, - pub layout_name: Option, - pub mode_name: Option, - pub tags_btns: ButtonManager, - pub tags_computed: Vec<(ColorPair, ComputedText)>, - pub layout_name_computed: Option, - pub mode_computed: Option, + layer_surface: ZwlrLayerSurfaceV1, + viewport: WpViewport, + fractional_scale: Option, + blocks_btns: ButtonManager<(Option, Option)>, + tags: Vec, + layout_name: Option, + mode_name: Option, + tags_btns: ButtonManager, + tags_computed: Vec<(ColorPair, ComputedText)>, + layout_name_computed: Option, + mode_computed: Option, } #[derive(Debug, PartialEq)] @@ -45,6 +45,55 @@ pub struct ColorPair { } impl Bar { + pub fn new(conn: &mut Connection, state: &State, output: Output) -> Self { + let surface = state.wl_compositor.create_surface(conn); + + let fractional_scale = state + .fractional_scale_manager + .map(|mgr| mgr.get_fractional_scale_with_cb(conn, surface, fractional_scale_cb)); + + let layer_surface = state.layer_shell.get_layer_surface_with_cb( + conn, + surface, + Some(output.wl), + zwlr_layer_shell_v1::Layer::Top, + wayrs_client::cstr!("i3bar-river").into(), + layer_surface_cb, + ); + + Self { + output, + hidden: true, + mapped: false, + frame_cb: None, + width: 0, + height: state.shared_state.config.height, + scale120: None, + surface, + viewport: state.viewporter.get_viewport(conn, surface), + fractional_scale, + layer_surface, + blocks_btns: Default::default(), + tags: Vec::new(), + layout_name: None, + mode_name: None, + tags_btns: Default::default(), + tags_computed: Vec::new(), + layout_name_computed: None, + mode_computed: None, + } + } + + pub fn destroy(self, conn: &mut Connection) { + self.output.destroy(conn); + self.surface.destroy(conn); + self.layer_surface.destroy(conn); + self.viewport.destroy(conn); + if let Some(fs) = self.fractional_scale { + fs.destroy(conn); + } + } + pub fn set_tags(&mut self, tags: Vec) { self.tags = tags; self.tags_btns.clear(); @@ -479,3 +528,61 @@ pub fn compute_tag_label(label: &str, config: &Config) -> ComputedText { }, ) } + +fn layer_surface_cb( + conn: &mut Connection, + state: &mut State, + layer_surface: zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, + event: zwlr_layer_surface_v1::Event, +) { + match event { + zwlr_layer_surface_v1::Event::Configure(args) => { + let bar = state + .bars + .iter_mut() + .find(|bar| bar.layer_surface == layer_surface) + .unwrap(); + if bar.hidden { + return; + } + assert_ne!(args.width, 0); + bar.width = args.width; + bar.layer_surface.ack_configure(conn, args.serial); + if bar.mapped { + bar.request_frame(conn); + } else { + bar.mapped = true; + bar.frame(conn, &mut state.shared_state); + } + } + zwlr_layer_surface_v1::Event::Closed => { + let bar_index = state + .bars + .iter() + .position(|bar| bar.layer_surface == layer_surface) + .unwrap(); + state.drop_bar(conn, bar_index); + } + _ => (), + } +} + +fn fractional_scale_cb( + conn: &mut Connection, + state: &mut State, + fractional_scale: WpFractionalScaleV1, + event: wp_fractional_scale_v1::Event, +) { + let wp_fractional_scale_v1::Event::PreferredScale(scale120) = event else { + return; + }; + let bar = state + .bars + .iter_mut() + .find(|b| b.fractional_scale == Some(fractional_scale)) + .unwrap(); + if bar.scale120 != Some(scale120) { + bar.scale120 = Some(scale120); + bar.request_frame(conn); + } +} diff --git a/src/state.rs b/src/state.rs index 3a8eada..ba2c3e6 100644 --- a/src/state.rs +++ b/src/state.rs @@ -20,10 +20,10 @@ use crate::{ }; pub struct State { - wl_compositor: WlCompositor, - layer_shell: ZwlrLayerShellV1, - viewporter: WpViewporter, - fractional_scale_manager: Option, + pub wl_compositor: WlCompositor, + pub layer_shell: ZwlrLayerShellV1, + pub viewporter: WpViewporter, + pub fractional_scale_manager: Option, seats: Seats, pointers: Vec, @@ -152,42 +152,7 @@ impl State { wm.new_ouput(conn, output.wl); } - let surface = self.wl_compositor.create_surface(conn); - - let fractional_scale = self - .fractional_scale_manager - .map(|mgr| mgr.get_fractional_scale_with_cb(conn, surface, fractional_scale_cb)); - - let layer_surface = self.layer_shell.get_layer_surface_with_cb( - conn, - surface, - Some(output.wl), - zwlr_layer_shell_v1::Layer::Top, - wayrs_client::cstr!("i3bar-river").into(), - layer_surface_cb, - ); - - let mut bar = Bar { - output, - hidden: self.hidden, - mapped: false, - frame_cb: None, - width: 0, - height: self.shared_state.config.height, - scale120: None, - surface, - viewport: self.viewporter.get_viewport(conn, surface), - fractional_scale, - layer_surface, - blocks_btns: Default::default(), - tags: Vec::new(), - layout_name: None, - mode_name: None, - tags_btns: Default::default(), - tags_computed: Vec::new(), - layout_name_computed: None, - mode_computed: None, - }; + let mut bar = Bar::new(conn, self, output); if !self.hidden { bar.show(conn, &self.shared_state); @@ -196,14 +161,12 @@ impl State { self.bars.push(bar); } - fn drop_bar(&mut self, conn: &mut Connection, bar_index: usize) { + pub fn drop_bar(&mut self, conn: &mut Connection, bar_index: usize) { let bar = self.bars.swap_remove(bar_index); if let Some(wm) = &mut self.shared_state.wm_info_provider { wm.output_removed(conn, bar.output.wl); } - bar.surface.destroy(conn); - bar.layer_surface.destroy(conn); - bar.output.destroy(conn); + bar.destroy(conn); } pub fn toggle_visibility(&mut self, conn: &mut Connection) { @@ -321,44 +284,6 @@ fn wl_registry_cb(conn: &mut Connection, state: &mut State, event: &wl_re } } -fn layer_surface_cb( - conn: &mut Connection, - state: &mut State, - layer_surface: zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, - event: zwlr_layer_surface_v1::Event, -) { - match event { - zwlr_layer_surface_v1::Event::Configure(args) => { - let bar = state - .bars - .iter_mut() - .find(|bar| bar.layer_surface == layer_surface) - .unwrap(); - if bar.hidden { - return; - } - assert_ne!(args.width, 0); - bar.width = args.width; - bar.layer_surface.ack_configure(conn, args.serial); - if bar.mapped { - bar.request_frame(conn); - } else { - bar.mapped = true; - bar.frame(conn, &mut state.shared_state); - } - } - zwlr_layer_surface_v1::Event::Closed => { - let bar_index = state - .bars - .iter() - .position(|bar| bar.layer_surface == layer_surface) - .unwrap(); - state.drop_bar(conn, bar_index); - } - _ => (), - } -} - fn wl_pointer_cb( conn: &mut Connection, state: &mut State, @@ -474,26 +399,6 @@ fn wl_pointer_cb( } } -fn fractional_scale_cb( - conn: &mut Connection, - state: &mut State, - fractional_scale: WpFractionalScaleV1, - event: wp_fractional_scale_v1::Event, -) { - let wp_fractional_scale_v1::Event::PreferredScale(scale120) = event else { - return; - }; - let bar = state - .bars - .iter_mut() - .find(|b| b.fractional_scale == Some(fractional_scale)) - .unwrap(); - if bar.scale120 != Some(scale120) { - bar.scale120 = Some(scale120); - bar.request_frame(conn); - } -} - #[derive(Debug, Default, Clone, Copy)] pub struct ScrollFrame { stop: bool, -- cgit v1.3.1