summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorMax Verevkin <maxxverrr@gmail.com>2022-07-18 23:55:16 +0300
committerMax Verevkin <maxxverrr@gmail.com>2022-07-18 23:55:16 +0300
commit6d2a9b349470f905025c0a6be027a8ba8227b972 (patch)
treed0093cfdde18142dcb9238b15e4eb849bd5cd76b /src/main.rs
parentaa8c17a178aa2478230d3172cba8bfe03ea7333d (diff)
downloadi3bar-mango-6d2a9b349470f905025c0a6be027a8ba8227b972.tar.gz
refactor
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs181
1 files changed, 88 insertions, 93 deletions
diff --git a/src/main.rs b/src/main.rs
index 5633807..a77d1be 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,5 @@
+#![allow(clippy::explicit_auto_deref)] // TODO: remove once https://github.com/rust-lang/rust-clippy/issues/9101 is resloved
+
#[macro_use]
extern crate log;
#[macro_use]
@@ -17,9 +19,6 @@ use smithay_client_toolkit::{
WaylandSource,
};
-use std::cell::RefCell;
-use std::rc::Rc;
-
mod bar_state;
mod button_manager;
mod color;
@@ -64,38 +63,37 @@ fn main() {
)
.expect("Initial roundtrip failed!");
- let bar_state = Rc::new(RefCell::new(BarState::new(&env)));
+ let mut bar_state = BarState::new(&env);
let env_handle = env.clone();
- let bar_state_handle = Rc::clone(&bar_state);
- let output_handler = move |output: wl_output::WlOutput, info: &OutputInfo| {
- if info.obsolete {
- info!("Output removed");
- bar_state_handle.borrow_mut().remove_surface(info.id);
- output.release();
- } else {
- info!("Output detected");
- let surface = env_handle.create_surface().detach();
- let pool = env_handle
- .create_auto_pool()
- .expect("Failed to create a memory pool!");
- bar_state_handle
- .borrow_mut()
- .add_surface(&output, info.id, surface, pool);
- }
- };
+ let output_handler =
+ move |output: wl_output::WlOutput, info: &OutputInfo, bar_state: &mut BarState| {
+ if info.obsolete {
+ info!("Output removed");
+ bar_state.remove_surface(info.id);
+ output.release();
+ } else {
+ info!("Output detected");
+ let surface = env_handle.create_surface().detach();
+ let pool = env_handle
+ .create_auto_pool()
+ .expect("Failed to create a memory pool!");
+ bar_state.add_surface(&output, info.id, surface, pool);
+ }
+ };
// Process currently existing outputs
for output in env.get_all_outputs() {
if let Some(info) = with_output_info(&output, Clone::clone) {
- output_handler(output, &info);
+ output_handler(output, &info, &mut bar_state);
}
}
// Setup a listener for changes
// The listener will live for as long as we keep this handle alive
- let _listner_handle =
- env.listen_for_outputs(move |output, info, _| output_handler(output, info));
+ let _listner_handle = env.listen_for_outputs(move |output, info, mut data| {
+ output_handler(output, info, data.get().unwrap())
+ });
// Right now river only supports one seat
let cursor_theme = pointer::ThemeManager::init(
@@ -108,95 +106,92 @@ fn main() {
if seat_data.has_pointer && !seat_data.defunct {
let pointer = seat.get_pointer();
let themed_pointer = cursor_theme.theme_pointer(pointer.detach());
- let bar_state_handle = bar_state.clone();
let seat = seat.clone();
let mut pos = (0.0, 0.0);
let mut cur_surface = None;
- pointer.quick_assign(move |_, event, _| match event {
- wl_pointer::Event::Enter {
- serial,
- surface,
- surface_x: y,
- surface_y: x,
- } => {
- let _ = themed_pointer.set_cursor("default", Some(serial));
- cur_surface = Some(surface);
- pos = (x, y);
- }
- wl_pointer::Event::Leave {
- serial: _,
- surface: _,
- } => {
- cur_surface = None;
- }
- wl_pointer::Event::Motion {
- time: _,
- surface_x: x,
- surface_y: y,
- } => {
- pos = (x, y);
- }
- wl_pointer::Event::Button {
- serial: _,
- time: _,
- button,
- state,
- } if state == wl_pointer::ButtonState::Pressed => {
- if let Some(cur_surface) = &cur_surface {
- bar_state_handle.borrow_mut().handle_click(
- cur_surface,
- &seat,
- pos.0,
- pos.1,
- button.into(),
- );
+ pointer.quick_assign(move |_, event, mut data| {
+ let bar_state: &mut BarState = data.get().unwrap();
+ match event {
+ wl_pointer::Event::Enter {
+ serial,
+ surface,
+ surface_x: y,
+ surface_y: x,
+ } => {
+ let _ = themed_pointer.set_cursor("default", Some(serial));
+ cur_surface = Some(surface);
+ pos = (x, y);
}
- }
- wl_pointer::Event::Axis {
- time: _,
- axis,
- value,
- } if axis == wl_pointer::Axis::VerticalScroll => {
- if let Some(cur_surface) = &cur_surface {
- bar_state_handle.borrow_mut().handle_click(
- cur_surface,
- &seat,
- pos.0,
- pos.1,
- if value > 0.0 {
- PointerBtn::WheelDown
- } else {
- PointerBtn::WheelUp
- },
- );
+ wl_pointer::Event::Leave {
+ serial: _,
+ surface: _,
+ } => {
+ cur_surface = None;
+ }
+ wl_pointer::Event::Motion {
+ time: _,
+ surface_x: x,
+ surface_y: y,
+ } => {
+ pos = (x, y);
}
+ wl_pointer::Event::Button {
+ serial: _,
+ time: _,
+ button,
+ state,
+ } if state == wl_pointer::ButtonState::Pressed => {
+ if let Some(cur_surface) = &cur_surface {
+ bar_state.handle_click(
+ cur_surface,
+ &seat,
+ pos.0,
+ pos.1,
+ button.into(),
+ );
+ }
+ }
+ wl_pointer::Event::Axis {
+ time: _,
+ axis,
+ value,
+ } if axis == wl_pointer::Axis::VerticalScroll => {
+ if let Some(cur_surface) = &cur_surface {
+ bar_state.handle_click(
+ cur_surface,
+ &seat,
+ pos.0,
+ pos.1,
+ if value > 0.0 {
+ PointerBtn::WheelDown
+ } else {
+ PointerBtn::WheelUp
+ },
+ );
+ }
+ }
+ _ => (),
}
- _ => (),
});
}
});
}
- let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap();
+ let mut event_loop = calloop::EventLoop::<BarState>::try_new().unwrap();
WaylandSource::new(queue)
.quick_insert(event_loop.handle())
.unwrap();
- if let Some(cmd) = &bar_state.borrow().status_cmd {
- let bar_state_handle = Rc::clone(&bar_state);
- cmd.quick_insert(event_loop.handle(), bar_state_handle);
+ if let Some(cmd) = &bar_state.status_cmd {
+ cmd.quick_insert(event_loop.handle());
}
loop {
- {
- // Using a new scope in order to be sure that `bar_state` will not be borrowed while
- // dispatching the events
- bar_state.borrow_mut().handle_events();
- }
+ bar_state.handle_events();
display.flush().unwrap();
- if let Err(e) = event_loop.dispatch(None, &mut ()) {
- bar_state.borrow_mut().set_error(e.to_string());
+ if let Err(e) = event_loop.dispatch(None, &mut bar_state) {
+ bar_state.set_error(e.to_string());
}
}
}