diff --git a/src/lib.rs b/src/lib.rs index 3af21b8..1a60a6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,8 @@ mod mime_app; pub mod mime_icon; mod mounter; mod mouse_area; -mod scroll_area; mod operation; +mod scroll_area; mod spawn_detached; use tab::Location; pub mod tab; diff --git a/src/scroll_area.rs b/src/scroll_area.rs index cb9b709..ac72a59 100644 --- a/src/scroll_area.rs +++ b/src/scroll_area.rs @@ -1,4 +1,4 @@ -//! A container for capturing mouse wheel events. +//! A container for reacting to scroll events. use cosmic::{ iced_core::{ @@ -14,7 +14,7 @@ use cosmic::{ Element, Renderer, Theme, }; -/// Emit messages on mouse wheel events. +/// Emit messages on scroll events. Optionally continue propogating scroll events. #[allow(missing_debug_implementations)] pub struct ScrollArea<'a, Message> { id: Id, @@ -24,7 +24,7 @@ pub struct ScrollArea<'a, Message> { } impl<'a, Message> ScrollArea<'a, Message> { - /// The message to emit on a forward button release. + /// The message to emit on a scroll. #[must_use] pub fn on_scroll( mut self, @@ -189,8 +189,7 @@ where } } -/// Processes the given [`Event`] and updates the [`State`] of a [`ScrollArea`] -/// accordingly. +/// Processes the given [`Event`] and emit any messages produced by [ScrollArea::on_scroll]. fn update( widget: &mut ScrollArea<'_, Message>, event: &Event, @@ -203,15 +202,18 @@ fn update( return event::Status::Ignored; } - if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event { - if let Some(message) = widget.on_scroll.as_ref() { - if let Some(msg) = message(Some(delta.clone())) { - shell.publish(msg); - if !widget.should_propogate_events { - return event::Status::Captured; + match event { + Event::Mouse(mouse::Event::WheelScrolled { delta }) => { + if let Some(on_scroll) = widget.on_scroll.as_ref() { + if let Some(message) = on_scroll(Some(delta.clone())) { + shell.publish(message); + if !widget.should_propogate_events { + return event::Status::Captured; + } } } } + _ => {} } event::Status::Ignored diff --git a/src/tab.rs b/src/tab.rs index 3d880b1..646c298 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -63,7 +63,8 @@ use crate::{ menu, mime_app::{mime_apps, MimeApp}, mime_icon::{mime_for_path, mime_icon}, - mouse_area, scroll_area::ScrollArea, + mouse_area, + scroll_area::ScrollArea, }; use unix_permissions_ext::UNIXPermissionsExt; use uzers::{get_group_by_gid, get_user_by_uid}; @@ -3581,7 +3582,9 @@ impl Tab { } } -fn respond_to_scroll_direction(delta: Option) -> Option { +fn respond_to_scroll_direction( + delta: Option, +) -> Option { let delta_y = match delta { Some(cosmic::iced_core::mouse::ScrollDelta::Lines { y, .. }) => y, Some(cosmic::iced_core::mouse::ScrollDelta::Pixels { y, .. }) => y, @@ -3612,8 +3615,8 @@ mod tests { use crate::{ app::test_utils::{ assert_eq_tab_path, assert_scroll_affects_item_zoom, empty_fs, eq_path_item, - filter_dirs, read_dir_sorted, simple_fs, tab_click_new, NAME_LEN, NUM_DIRS, - NUM_FILES, NUM_HIDDEN, NUM_NESTED, + filter_dirs, read_dir_sorted, simple_fs, tab_click_new, NAME_LEN, NUM_DIRS, NUM_FILES, + NUM_HIDDEN, NUM_NESTED, }, config::{IconSizes, TabConfig}, }; @@ -3852,7 +3855,7 @@ mod tests { let path = fs.path(); let mut tab = Tab::new(Location::Path(path.into()), TabConfig::default()); - + let should_zoom = true; assert_scroll_affects_item_zoom(&mut tab, Message::ScrollUp, Modifiers::CTRL, should_zoom); @@ -3866,7 +3869,12 @@ mod tests { let mut tab = Tab::new(Location::Path(path.into()), TabConfig::default()); let should_not_zoom = false; - assert_scroll_affects_item_zoom(&mut tab, Message::ScrollUp, Modifiers::empty(), should_not_zoom); + assert_scroll_affects_item_zoom( + &mut tab, + Message::ScrollUp, + Modifiers::empty(), + should_not_zoom, + ); Ok(()) } @@ -3875,10 +3883,15 @@ mod tests { fn tab_scroll_down_with_ctrl_modifier_zooms() -> io::Result<()> { let fs = simple_fs(0, NUM_NESTED, NUM_DIRS, 0, NAME_LEN)?; let path = fs.path(); - + let mut tab = Tab::new(Location::Path(path.into()), TabConfig::default()); let should_zoom = true; - assert_scroll_affects_item_zoom(&mut tab, Message::ScrollDown, Modifiers::CTRL, should_zoom); + assert_scroll_affects_item_zoom( + &mut tab, + Message::ScrollDown, + Modifiers::CTRL, + should_zoom, + ); Ok(()) } @@ -3887,10 +3900,15 @@ mod tests { fn tab_scroll_down_without_ctrl_modifier_does_not_zoom() -> io::Result<()> { let fs = simple_fs(0, NUM_NESTED, NUM_DIRS, 0, NAME_LEN)?; let path = fs.path(); - + let mut tab = Tab::new(Location::Path(path.into()), TabConfig::default()); let should_not_zoom = false; - assert_scroll_affects_item_zoom(&mut tab, Message::ScrollDown, Modifiers::empty(), should_not_zoom); + assert_scroll_affects_item_zoom( + &mut tab, + Message::ScrollDown, + Modifiers::empty(), + should_not_zoom, + ); Ok(()) }