From 4b77e8d7dd133e9512256bfe134ed5fa881ec744 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Sun, 26 Nov 2023 11:18:14 +0200 Subject: [PATCH] reimplement throttling with frame callbacks --- src/bar.rs | 54 ++++++++++++++++++++++++++++------------------------ src/state.rs | 8 ++++---- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/bar.rs b/src/bar.rs index f8434ef..b88abcc 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -20,7 +20,8 @@ pub struct Bar { pub output: Output, hidden: bool, mapped: bool, - frame_cb: Option, + throttle: Option, + throttled: bool, width: u32, height: u32, scale120: Option, @@ -65,7 +66,8 @@ impl Bar { output, hidden: true, mapped: false, - frame_cb: None, + throttle: None, + throttled: false, width: 0, height: state.shared_state.config.height, scale120: None, @@ -136,7 +138,14 @@ impl Bar { } pub fn frame(&mut self, conn: &mut Connection, ss: &mut SharedState) { - assert!(self.mapped); + if !self.mapped { + return; + } + + if self.throttle.is_some() { + self.throttled = true; + return; + } let (pix_width, pix_height, scale_f) = match self.scale120 { Some(scale120) => ( @@ -326,24 +335,23 @@ impl Bar { self.surface .attach(conn, Some(buffer.into_wl_buffer()), 0, 0); self.surface.damage(conn, 0, 0, i32::MAX, i32::MAX); - self.surface.commit(conn); - } - pub fn request_frame(&mut self, conn: &mut Connection) { - if self.mapped && !self.hidden && self.frame_cb.is_none() { - self.frame_cb = Some(self.surface.frame_with_cb(conn, |ctx| { - if let Some(bar) = ctx - .state - .bars - .iter_mut() - .find(|bar| bar.frame_cb == Some(ctx.proxy)) - { - bar.frame_cb = None; + self.throttle = Some(self.surface.frame_with_cb(conn, |ctx| { + if let Some(bar) = ctx + .state + .bars + .iter_mut() + .find(|bar| bar.throttle == Some(ctx.proxy)) + { + bar.throttle = None; + if bar.throttled { + bar.throttled = false; bar.frame(ctx.conn, &mut ctx.state.shared_state); } - })); - self.surface.commit(conn); - } + } + })); + + self.surface.commit(conn); } pub fn show(&mut self, conn: &mut Connection, shared_state: &SharedState) { @@ -547,12 +555,8 @@ fn layer_surface_cb(ctx: EventCtx) { assert_ne!(args.width, 0); bar.width = args.width; bar.layer_surface.ack_configure(ctx.conn, args.serial); - if bar.mapped { - bar.request_frame(ctx.conn); - } else { - bar.mapped = true; - bar.frame(ctx.conn, &mut ctx.state.shared_state); - } + bar.mapped = true; + bar.frame(ctx.conn, &mut ctx.state.shared_state); } zwlr_layer_surface_v1::Event::Closed => { let bar_index = ctx @@ -579,6 +583,6 @@ fn fractional_scale_cb(ctx: EventCtx) { .unwrap(); if bar.scale120 != Some(scale120) { bar.scale120 = Some(scale120); - bar.request_frame(ctx.conn); + bar.frame(ctx.conn, &mut ctx.state.shared_state); } } diff --git a/src/state.rs b/src/state.rs index 6556298..279aac3 100644 --- a/src/state.rs +++ b/src/state.rs @@ -139,7 +139,7 @@ impl State { pub fn draw_all(&mut self, conn: &mut Connection) { for bar in &mut self.bars { - bar.request_frame(conn); + bar.frame(conn, &mut self.shared_state); } } @@ -210,21 +210,21 @@ impl State { pub fn tags_updated(&mut self, conn: &mut Connection, output: Option) { self.for_each_bar(output, |bar, ss| { bar.set_tags(ss.wm_info_provider.get_tags(&bar.output)); - bar.request_frame(conn); + bar.frame(conn, ss); }); } pub fn layout_name_updated(&mut self, conn: &mut Connection, output: Option) { self.for_each_bar(output, |bar, ss| { bar.set_layout_name(ss.wm_info_provider.get_layout_name(&bar.output)); - bar.request_frame(conn); + bar.frame(conn, ss); }); } pub fn mode_name_updated(&mut self, conn: &mut Connection, output: Option) { self.for_each_bar(output, |bar, ss| { bar.set_mode_name(ss.wm_info_provider.get_mode_name(&bar.output)); - bar.request_frame(conn); + bar.frame(conn, ss); }); } }