feat: zoom items on ctrl+scroll
This commit is contained in:
parent
8e74a08704
commit
825ec0c3e3
3 changed files with 292 additions and 3 deletions
|
|
@ -20,6 +20,7 @@ mod mime_app;
|
|||
pub mod mime_icon;
|
||||
mod mounter;
|
||||
mod mouse_area;
|
||||
mod scroll_area;
|
||||
mod operation;
|
||||
mod spawn_detached;
|
||||
use tab::Location;
|
||||
|
|
|
|||
218
src/scroll_area.rs
Normal file
218
src/scroll_area.rs
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
//! A container for capturing mouse wheel events.
|
||||
|
||||
use cosmic::{
|
||||
iced_core::{
|
||||
event::{self, Event},
|
||||
layout,
|
||||
mouse::{self},
|
||||
overlay,
|
||||
renderer::{self},
|
||||
widget::{Operation, OperationOutputWrapper, Tree},
|
||||
Clipboard, Layout, Length, Rectangle, Shell, Size, Widget,
|
||||
},
|
||||
widget::Id,
|
||||
Element, Renderer, Theme,
|
||||
};
|
||||
|
||||
/// Emit messages on mouse wheel events.
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct ScrollArea<'a, Message> {
|
||||
id: Id,
|
||||
content: Element<'a, Message>,
|
||||
on_scroll: Option<Box<dyn Fn(Option<mouse::ScrollDelta>) -> Option<Message> + 'a>>,
|
||||
should_propogate_events: bool,
|
||||
}
|
||||
|
||||
impl<'a, Message> ScrollArea<'a, Message> {
|
||||
/// The message to emit on a forward button release.
|
||||
#[must_use]
|
||||
pub fn on_scroll(
|
||||
mut self,
|
||||
message: impl Fn(Option<mouse::ScrollDelta>) -> Option<Message> + 'a,
|
||||
) -> Self {
|
||||
self.on_scroll = Some(Box::new(message));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the widget's unique identifier.
|
||||
#[must_use]
|
||||
pub fn with_id(mut self, id: Id) -> Self {
|
||||
self.id = id;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> ScrollArea<'a, Message> {
|
||||
/// Creates a [`ScrollArea`] with the given content.
|
||||
pub fn new(content: impl Into<Element<'a, Message>>, should_propogate_events: bool) -> Self {
|
||||
ScrollArea {
|
||||
id: Id::unique(),
|
||||
content: content.into(),
|
||||
on_scroll: None,
|
||||
should_propogate_events,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> Widget<Message, Theme, Renderer> for ScrollArea<'a, Message>
|
||||
where
|
||||
Message: Clone,
|
||||
{
|
||||
fn children(&self) -> Vec<Tree> {
|
||||
vec![Tree::new(&self.content)]
|
||||
}
|
||||
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(std::slice::from_mut(&mut self.content));
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
self.content.as_widget().size()
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&self,
|
||||
tree: &mut Tree,
|
||||
renderer: &Renderer,
|
||||
limits: &layout::Limits,
|
||||
) -> layout::Node {
|
||||
self.content
|
||||
.as_widget()
|
||||
.layout(&mut tree.children[0], renderer, limits)
|
||||
}
|
||||
|
||||
fn operate(
|
||||
&self,
|
||||
tree: &mut Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
operation: &mut dyn Operation<OperationOutputWrapper<Message>>,
|
||||
) {
|
||||
self.content
|
||||
.as_widget()
|
||||
.operate(&mut tree.children[0], layout, renderer, operation);
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
&mut self,
|
||||
tree: &mut Tree,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
renderer: &Renderer,
|
||||
clipboard: &mut dyn Clipboard,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
viewport: &Rectangle,
|
||||
) -> event::Status {
|
||||
if let event::Status::Captured = self.content.as_widget_mut().on_event(
|
||||
&mut tree.children[0],
|
||||
event.clone(),
|
||||
layout,
|
||||
cursor,
|
||||
renderer,
|
||||
clipboard,
|
||||
shell,
|
||||
viewport,
|
||||
) {
|
||||
return event::Status::Captured;
|
||||
}
|
||||
|
||||
update(self, &event, layout, cursor, shell)
|
||||
}
|
||||
|
||||
fn mouse_interaction(
|
||||
&self,
|
||||
tree: &Tree,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
renderer: &Renderer,
|
||||
) -> mouse::Interaction {
|
||||
self.content.as_widget().mouse_interaction(
|
||||
&tree.children[0],
|
||||
layout,
|
||||
cursor,
|
||||
viewport,
|
||||
renderer,
|
||||
)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
tree: &Tree,
|
||||
renderer: &mut Renderer,
|
||||
theme: &Theme,
|
||||
renderer_style: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
) {
|
||||
self.content.as_widget().draw(
|
||||
&tree.children[0],
|
||||
renderer,
|
||||
theme,
|
||||
renderer_style,
|
||||
layout,
|
||||
cursor,
|
||||
viewport,
|
||||
);
|
||||
}
|
||||
|
||||
fn overlay<'b>(
|
||||
&'b mut self,
|
||||
tree: &'b mut Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
|
||||
self.content
|
||||
.as_widget_mut()
|
||||
.overlay(&mut tree.children[0], layout, renderer)
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
Some(self.id.clone())
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> From<ScrollArea<'a, Message>> for Element<'a, Message>
|
||||
where
|
||||
Message: 'a + Clone,
|
||||
Renderer: 'a + renderer::Renderer,
|
||||
Theme: 'a,
|
||||
{
|
||||
fn from(area: ScrollArea<'a, Message>) -> Element<'a, Message> {
|
||||
Element::new(area)
|
||||
}
|
||||
}
|
||||
|
||||
/// Processes the given [`Event`] and updates the [`State`] of a [`ScrollArea`]
|
||||
/// accordingly.
|
||||
fn update<Message: Clone>(
|
||||
widget: &mut ScrollArea<'_, Message>,
|
||||
event: &Event,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
) -> event::Status {
|
||||
let layout_bounds = layout.bounds();
|
||||
if !cursor.is_over(layout_bounds) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
event::Status::Ignored
|
||||
}
|
||||
76
src/tab.rs
76
src/tab.rs
|
|
@ -63,7 +63,7 @@ use crate::{
|
|||
menu,
|
||||
mime_app::{mime_apps, MimeApp},
|
||||
mime_icon::{mime_for_path, mime_icon},
|
||||
mouse_area,
|
||||
mouse_area, scroll_area::ScrollArea,
|
||||
};
|
||||
use unix_permissions_ext::UNIXPermissionsExt;
|
||||
use uzers::{get_group_by_gid, get_user_by_uid};
|
||||
|
|
@ -3372,6 +3372,9 @@ impl Tab {
|
|||
mouse_area = mouse_area.on_right_press(Message::ContextMenu);
|
||||
}
|
||||
|
||||
let mouse_area = ScrollArea::new(mouse_area, true)
|
||||
.on_scroll(respond_to_scroll_direction);
|
||||
|
||||
let mut popover = widget::popover(mouse_area);
|
||||
|
||||
if let Some(point) = self.context_menu {
|
||||
|
|
@ -3577,6 +3580,24 @@ impl Tab {
|
|||
}
|
||||
}
|
||||
|
||||
fn respond_to_scroll_direction(delta: Option<cosmic::iced_core::mouse::ScrollDelta>) -> Option<Message> {
|
||||
let delta_y = match delta {
|
||||
Some(cosmic::iced_core::mouse::ScrollDelta::Lines { y, .. }) => y,
|
||||
Some(cosmic::iced_core::mouse::ScrollDelta::Pixels { y, .. }) => y,
|
||||
None => 0.0,
|
||||
};
|
||||
|
||||
if delta_y > 0.0 {
|
||||
return Some(Message::ScrollUp);
|
||||
}
|
||||
|
||||
if delta_y < 0.0 {
|
||||
return Some(Message::ScrollDown);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{fs, io, path::PathBuf};
|
||||
|
|
@ -3589,8 +3610,9 @@ mod tests {
|
|||
use super::{scan_path, Location, Message, Tab};
|
||||
use crate::{
|
||||
app::test_utils::{
|
||||
assert_eq_tab_path, 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,
|
||||
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,
|
||||
},
|
||||
config::{IconSizes, TabConfig},
|
||||
};
|
||||
|
|
@ -3823,6 +3845,54 @@ mod tests {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_scroll_up_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::ScrollUp, Modifiers::CTRL, should_zoom);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_scroll_up_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::ScrollUp, Modifiers::empty(), should_not_zoom);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
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);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
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);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn tab_empty_history_does_nothing_on_prev_next() -> io::Result<()> {
|
||||
let fs = simple_fs(0, NUM_NESTED, NUM_DIRS, 0, NAME_LEN)?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue