Skip to content

Commit

Permalink
reimplement throttling with frame callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Nov 26, 2023
1 parent 7221482 commit 4b77e8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
54 changes: 29 additions & 25 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub struct Bar {
pub output: Output,
hidden: bool,
mapped: bool,
frame_cb: Option<WlCallback>,
throttle: Option<WlCallback>,
throttled: bool,
width: u32,
height: u32,
scale120: Option<u32>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -136,7 +138,14 @@ impl Bar {
}

pub fn frame(&mut self, conn: &mut Connection<State>, 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) => (
Expand Down Expand Up @@ -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<State>) {
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<State>, shared_state: &SharedState) {
Expand Down Expand Up @@ -547,12 +555,8 @@ fn layer_surface_cb(ctx: EventCtx<State, ZwlrLayerSurfaceV1>) {
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
Expand All @@ -579,6 +583,6 @@ fn fractional_scale_cb(ctx: EventCtx<State, WpFractionalScaleV1>) {
.unwrap();
if bar.scale120 != Some(scale120) {
bar.scale120 = Some(scale120);
bar.request_frame(ctx.conn);
bar.frame(ctx.conn, &mut ctx.state.shared_state);
}
}
8 changes: 4 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl State {

pub fn draw_all(&mut self, conn: &mut Connection<Self>) {
for bar in &mut self.bars {
bar.request_frame(conn);
bar.frame(conn, &mut self.shared_state);
}
}

Expand Down Expand Up @@ -210,21 +210,21 @@ impl State {
pub fn tags_updated(&mut self, conn: &mut Connection<Self>, output: Option<WlOutput>) {
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<Self>, output: Option<WlOutput>) {
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<Self>, output: Option<WlOutput>) {
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);
});
}
}
Expand Down

0 comments on commit 4b77e8d

Please sign in to comment.