chore: collapse scroll_area into mouse_area
This commit is contained in:
parent
b332fc6b1c
commit
a192d93f4b
5 changed files with 133 additions and 296 deletions
116
src/tab.rs
116
src/tab.rs
|
|
@ -7,8 +7,9 @@ use cosmic::{
|
|||
},
|
||||
alignment::{Horizontal, Vertical},
|
||||
clipboard::dnd::DndAction,
|
||||
event,
|
||||
futures::SinkExt,
|
||||
keyboard::Modifiers,
|
||||
keyboard::{self, Modifiers},
|
||||
subscription::{self, Subscription},
|
||||
//TODO: export in cosmic::widget
|
||||
widget::{
|
||||
|
|
@ -24,7 +25,7 @@ use cosmic::{
|
|||
Rectangle,
|
||||
Size,
|
||||
},
|
||||
iced_core::widget::tree,
|
||||
iced_core::{mouse::ScrollDelta, widget::tree},
|
||||
iced_style::rule,
|
||||
theme,
|
||||
widget::{
|
||||
|
|
@ -64,7 +65,6 @@ use crate::{
|
|||
mime_app::{mime_apps, MimeApp},
|
||||
mime_icon::{mime_for_path, mime_icon},
|
||||
mouse_area,
|
||||
scroll_area::ScrollArea,
|
||||
};
|
||||
use unix_permissions_ext::UNIXPermissionsExt;
|
||||
use uzers::{get_group_by_gid, get_user_by_uid};
|
||||
|
|
@ -829,8 +829,6 @@ pub enum Message {
|
|||
ZoomDefault,
|
||||
ZoomIn,
|
||||
ZoomOut,
|
||||
ScrollUp,
|
||||
ScrollDown,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
|
|
@ -1549,16 +1547,6 @@ impl Tab {
|
|||
Message::AddNetworkDrive => {
|
||||
commands.push(Command::AddNetworkDrive);
|
||||
}
|
||||
Message::ScrollUp => {
|
||||
if mod_ctrl {
|
||||
self.update(Message::ZoomIn, modifiers);
|
||||
}
|
||||
}
|
||||
Message::ScrollDown => {
|
||||
if mod_ctrl {
|
||||
self.update(Message::ZoomOut, modifiers);
|
||||
}
|
||||
}
|
||||
Message::ClickRelease(click_i_opt) => {
|
||||
if click_i_opt == self.clicked.take() {
|
||||
return commands;
|
||||
|
|
@ -3365,7 +3353,8 @@ impl Tab {
|
|||
.on_press(move |_point_opt| Message::Click(None))
|
||||
.on_release(|_| Message::ClickRelease(None))
|
||||
.on_back_press(move |_point_opt| Message::GoPrevious)
|
||||
.on_forward_press(move |_point_opt| Message::GoNext);
|
||||
.on_forward_press(move |_point_opt| Message::GoNext)
|
||||
.on_scroll(respond_to_scroll_direction);
|
||||
|
||||
if self.context_menu.is_some() {
|
||||
mouse_area = mouse_area.on_right_press(move |_point_opt| Message::ContextMenu(None));
|
||||
|
|
@ -3374,9 +3363,6 @@ impl Tab {
|
|||
}
|
||||
|
||||
let should_propogate_events = true;
|
||||
let mouse_area = ScrollArea::new(mouse_area, should_propogate_events)
|
||||
.on_scroll(respond_to_scroll_direction);
|
||||
|
||||
let mut popover = widget::popover(mouse_area);
|
||||
|
||||
if let Some(point) = self.context_menu {
|
||||
|
|
@ -3582,21 +3568,22 @@ impl Tab {
|
|||
}
|
||||
}
|
||||
|
||||
fn respond_to_scroll_direction(
|
||||
delta: Option<cosmic::iced_core::mouse::ScrollDelta>,
|
||||
) -> Option<Message> {
|
||||
pub fn respond_to_scroll_direction(delta: ScrollDelta, modifiers: Modifiers) -> Option<Message> {
|
||||
if !modifiers.control() {
|
||||
return None;
|
||||
}
|
||||
|
||||
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,
|
||||
ScrollDelta::Lines { y, .. } => y,
|
||||
ScrollDelta::Pixels { y, .. } => y,
|
||||
};
|
||||
|
||||
if delta_y > 0.0 {
|
||||
return Some(Message::ScrollUp);
|
||||
return Some(Message::ZoomIn);
|
||||
}
|
||||
|
||||
if delta_y < 0.0 {
|
||||
return Some(Message::ScrollDown);
|
||||
return Some(Message::ZoomOut);
|
||||
}
|
||||
|
||||
None
|
||||
|
|
@ -3606,15 +3593,15 @@ fn respond_to_scroll_direction(
|
|||
mod tests {
|
||||
use std::{fs, io, path::PathBuf};
|
||||
|
||||
use cosmic::iced_runtime::keyboard::Modifiers;
|
||||
use cosmic::{iced::mouse::ScrollDelta, iced_runtime::keyboard::Modifiers};
|
||||
use log::{debug, trace};
|
||||
use tempfile::TempDir;
|
||||
use test_log::test;
|
||||
|
||||
use super::{scan_path, Location, Message, Tab};
|
||||
use super::{scan_path, respond_to_scroll_direction, Location, Message, Tab};
|
||||
use crate::{
|
||||
app::test_utils::{
|
||||
assert_eq_tab_path, assert_scroll_affects_item_zoom, empty_fs, eq_path_item,
|
||||
assert_eq_tab_path, assert_zoom_affects_item_size, 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,
|
||||
},
|
||||
|
|
@ -3850,66 +3837,55 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn tab_scroll_up_with_ctrl_modifier_zooms() -> io::Result<()> {
|
||||
fn tab_zoom_in_increases_item_view_size() -> 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);
|
||||
let should_affect_size = true;
|
||||
assert_zoom_affects_item_size(&mut tab, Message::ZoomIn, should_affect_size);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn tab_zoom_out_decreases_item_view_size() -> 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_affect_size = true;
|
||||
assert_zoom_affects_item_size(&mut tab, Message::ZoomOut, should_affect_size);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tab_scroll_up_with_ctrl_modifier_zooms() -> io::Result<()> {
|
||||
let message_maybe = respond_to_scroll_direction(ScrollDelta::Pixels { x: 0.0, y: 1.0 }, Modifiers::CTRL);
|
||||
assert!(!message_maybe.is_none());
|
||||
assert!(matches!(message_maybe.unwrap(), Message::ZoomIn));
|
||||
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,
|
||||
);
|
||||
|
||||
let message_maybe = respond_to_scroll_direction(ScrollDelta::Pixels { x: 0.0, y: 1.0 }, Modifiers::empty());
|
||||
assert!(message_maybe.is_none());
|
||||
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,
|
||||
);
|
||||
|
||||
let message_maybe = respond_to_scroll_direction(ScrollDelta::Pixels { x: 0.0, y: -1.0 }, Modifiers::CTRL);
|
||||
assert!(!message_maybe.is_none());
|
||||
assert!(matches!(message_maybe.unwrap(), Message::ZoomOut));
|
||||
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,
|
||||
);
|
||||
|
||||
let message_maybe = respond_to_scroll_direction(ScrollDelta::Pixels { x: 0.0, y: -1.0 }, Modifiers::empty());
|
||||
assert!(message_maybe.is_none());
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
|
|
@ -4059,7 +4035,7 @@ impl<M> Widget<M, cosmic::Theme, cosmic::Renderer> for ArcElementWrapper<M> {
|
|||
_clipboard: &mut dyn cosmic::iced_core::Clipboard,
|
||||
_shell: &mut cosmic::iced_core::Shell<'_, M>,
|
||||
_viewport: &Rectangle,
|
||||
) -> cosmic::iced_core::event::Status {
|
||||
) -> event::Status {
|
||||
self.0.lock().unwrap().as_widget_mut().on_event(
|
||||
_state, _event, _layout, _cursor, _renderer, _clipboard, _shell, _viewport,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue