chore(clippy): app.rs, mime_app.rs

This commit is contained in:
Josh Megnauth 2025-01-27 02:17:50 -05:00 • committed by Jeremy Soller
parent 2e378ab261
commit cfe045827d
2 changed files with 97 additions and 117 deletions

View file

@ -49,7 +49,7 @@ use std::{
collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}, collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
env, fmt, fs, io, env, fmt, fs, io,
num::NonZeroU16, num::NonZeroU16,
path::PathBuf, path::{Path, PathBuf},
process, process,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
time::{self, Instant}, time::{self, Instant},
@ -678,7 +678,6 @@ impl App {
let mut overlaps: HashMap<_, _> = self let mut overlaps: HashMap<_, _> = self
.windows .windows
.keys() .keys()
.into_iter()
.map(|k| (*k, (0., 0., 0., 0.))) .map(|k| (*k, (0., 0., 0., 0.)))
.collect(); .collect();
let mut sorted_overlaps: Vec<_> = self.overlap.values().collect(); let mut sorted_overlaps: Vec<_> = self.overlap.values().collect();
@ -690,7 +689,7 @@ impl App {
let tr = tr.intersects(overlap); let tr = tr.intersects(overlap);
let bl = bl.intersects(overlap); let bl = bl.intersects(overlap);
let br = br.intersects(overlap); let br = br.intersects(overlap);
let Some((top, left, bottom, right)) = overlaps.get_mut(&w_id) else { let Some((top, left, bottom, right)) = overlaps.get_mut(w_id) else {
continue; continue;
}; };
if tl && tr { if tl && tr {
@ -802,12 +801,9 @@ impl App {
} }
fn remove_window(&mut self, id: &window::Id) { fn remove_window(&mut self, id: &window::Id) {
match self.windows.remove(id) { if let Some(WindowKind::Desktop(entity)) = self.windows.remove(id) {
Some(WindowKind::Desktop(entity)) => { // Remove the tab from the tab model
// Remove the tab from the tab model self.tab_model.remove(entity);
self.tab_model.remove(entity);
}
_ => {}
} }
} }
@ -817,7 +813,7 @@ impl App {
let Some(tab) = self.tab_model.data::<Tab>(entity) else { let Some(tab) = self.tab_model.data::<Tab>(entity) else {
return Task::none(); return Task::none();
}; };
let Some(ref items) = tab.items_opt() else { let Some(items) = tab.items_opt() else {
return Task::none(); return Task::none();
}; };
for item in items.iter() { for item in items.iter() {
@ -1753,7 +1749,7 @@ impl Application for App {
return self.update(message); return self.update(message);
} }
if let Some(data) = self.nav_model.data::<MounterData>(entity).clone() { if let Some(data) = self.nav_model.data::<MounterData>(entity) {
if let Some(mounter) = MOUNTERS.get(&data.0) { if let Some(mounter) = MOUNTERS.get(&data.0) {
return mounter.mount(data.1.clone()).map(|_| message::none()); return mounter.mount(data.1.clone()).map(|_| message::none());
} }
@ -1770,14 +1766,11 @@ impl Application for App {
} }
fn on_context_drawer(&mut self) -> Task<Self::Message> { fn on_context_drawer(&mut self) -> Task<Self::Message> {
match self.context_page { if let ContextPage::Preview(..) = self.context_page {
ContextPage::Preview(..) => { // Persist state of preview page
// Persist state of preview page if self.core.window.show_context != self.config.show_details {
if self.core.window.show_context != self.config.show_details { return self.update(Message::Preview(None));
return self.update(Message::Preview(None));
}
} }
_ => {}
} }
Task::none() Task::none()
} }
@ -1937,12 +1930,14 @@ impl Application for App {
} }
} }
Message::DesktopViewOptions => { Message::DesktopViewOptions => {
let mut settings = window::Settings::default(); let mut settings = window::Settings {
settings.decorations = true; decorations: true,
settings.min_size = Some(Size::new(360.0, 180.0)); min_size: Some(Size::new(360.0, 180.0)),
settings.resizable = true; resizable: true,
settings.size = Size::new(480.0, 444.0); size: Size::new(480.0, 444.0),
settings.transparent = true; transparent: true,
..Default::default()
};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
@ -2140,11 +2135,9 @@ impl Application for App {
let mut still_mounted = false; let mut still_mounted = false;
for item in mounter_items.iter() { for item in mounter_items.iter() {
if let Some(path) = item.path() { if let Some(path) = item.path() {
if path == old_path { if path == old_path && item.is_mounted() {
if item.is_mounted() { still_mounted = true;
still_mounted = true; break;
break;
}
} }
} }
} }
@ -2289,7 +2282,7 @@ impl Application for App {
let mut contains_change = false; let mut contains_change = false;
for event in events.iter() { for event in events.iter() {
for event_path in event.paths.iter() { for event_path in event.paths.iter() {
if event_path.starts_with(&path) { if event_path.starts_with(path) {
match event.kind { match event.kind {
notify::EventKind::Modify( notify::EventKind::Modify(
notify::event::ModifyKind::Metadata(_), notify::event::ModifyKind::Metadata(_),
@ -2303,16 +2296,17 @@ impl Application for App {
for item in items.iter_mut() { for item in items.iter_mut() {
if item.path_opt() == Some(event_path) { if item.path_opt() == Some(event_path) {
//TODO: reload more, like mime types? //TODO: reload more, like mime types?
match fs::metadata(&event_path) { match fs::metadata(event_path) {
Ok(new_metadata) => match &mut item Ok(new_metadata) => {
.metadata if let ItemMetadata::Path {
{
ItemMetadata::Path {
metadata, metadata,
.. ..
} => *metadata = new_metadata, } = &mut item.metadata
_ => {} {
}, *metadata = new_metadata
}
}
Err(err) => { Err(err) => {
log::warn!("failed to reload metadata for {:?}: {}", path, err); log::warn!("failed to reload metadata for {:?}: {}", path, err);
} }
@ -2423,43 +2417,32 @@ impl Application for App {
Message::OpenItemLocation(entity_opt) => { Message::OpenItemLocation(entity_opt) => {
return Task::batch(self.selected_paths(entity_opt).into_iter().filter_map( return Task::batch(self.selected_paths(entity_opt).into_iter().filter_map(
|path| { |path| {
if let Some(parent) = path.parent() { path.parent().map(Path::to_path_buf).map(|parent| {
Some(self.open_tab( self.open_tab(Location::Path(parent), true, Some(vec![path]))
Location::Path(parent.to_path_buf()), })
true,
Some(vec![path]),
))
} else {
None
}
}, },
)) ))
} }
Message::OpenWithBrowse => match self.dialog_pages.pop_front() { Message::OpenWithBrowse => match self.dialog_pages.pop_front() {
Some(DialogPage::OpenWith { Some(DialogPage::OpenWith {
mime, store_opt, .. mime,
store_opt: Some(app),
..
}) => { }) => {
if let Some(app) = store_opt { let url = format!("mime:///{mime}");
let url = format!("mime:///{mime}"); if let Some(mut command) = app.command(Some(url.clone().into())) {
if let Some(mut command) = app.command(Some(url.clone().into())) { match spawn_detached(&mut command) {
match spawn_detached(&mut command) { Ok(()) => {}
Ok(()) => {} Err(err) => {
Err(err) => { log::warn!("failed to open {:?} with {:?}: {}", url, app.id, err)
log::warn!(
"failed to open {:?} with {:?}: {}",
url,
app.id,
err
)
}
} }
} else {
log::warn!(
"failed to open {:?} with {:?}: failed to get command",
url,
app.id
);
} }
} else {
log::warn!(
"failed to open {:?} with {:?}: failed to get command",
url,
app.id
);
} }
} }
Some(dialog_page) => { Some(dialog_page) => {
@ -2542,7 +2525,7 @@ impl Application for App {
Message::PendingCancelAll => { Message::PendingCancelAll => {
for (id, (_, controller)) in self.pending_operations.iter() { for (id, (_, controller)) in self.pending_operations.iter() {
controller.cancel(); controller.cancel();
self.progress_operations.remove(&id); self.progress_operations.remove(id);
} }
} }
Message::PendingComplete(id, op_sel) => { Message::PendingComplete(id, op_sel) => {
@ -2636,12 +2619,14 @@ impl Application for App {
let selected_paths = self.selected_paths(entity_opt); let selected_paths = self.selected_paths(entity_opt);
let mut commands = Vec::with_capacity(selected_paths.len()); let mut commands = Vec::with_capacity(selected_paths.len());
for path in selected_paths { for path in selected_paths {
let mut settings = window::Settings::default(); let mut settings = window::Settings {
settings.decorations = true; decorations: true,
settings.min_size = Some(Size::new(360.0, 180.0)); min_size: Some(Size::new(360.0, 180.0)),
settings.resizable = true; resizable: true,
settings.size = Size::new(480.0, 600.0); size: Size::new(480.0, 600.0),
settings.transparent = true; transparent: true,
..Default::default()
};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
@ -2982,17 +2967,14 @@ impl Application for App {
return self.open_tab(location, true, None); return self.open_tab(location, true, None);
} }
Message::TabRescan(entity, location, parent_item_opt, items, selection_paths) => { Message::TabRescan(entity, location, parent_item_opt, items, selection_paths) => {
match self.tab_model.data_mut::<Tab>(entity) { if let Some(tab) = self.tab_model.data_mut::<Tab>(entity) {
Some(tab) => { if location == tab.location {
if location == tab.location { tab.parent_item_opt = parent_item_opt;
tab.parent_item_opt = parent_item_opt; tab.set_items(items);
tab.set_items(items); if let Some(selection_paths) = selection_paths {
if let Some(selection_paths) = selection_paths { tab.select_paths(selection_paths);
tab.select_paths(selection_paths);
}
} }
} }
_ => (),
} }
} }
Message::TabView(entity_opt, view) => { Message::TabView(entity_opt, view) => {
@ -3275,26 +3257,23 @@ impl Application for App {
// Applies selected nav bar context menu operation. // Applies selected nav bar context menu operation.
Message::NavMenuAction(action) => match action { Message::NavMenuAction(action) => match action {
NavMenuAction::Open(entity) => { NavMenuAction::Open(entity) => {
match self if let Some(path) = self
.nav_model .nav_model
.data::<Location>(entity) .data::<Location>(entity)
.and_then(|x| x.path_opt()) .and_then(|x| x.path_opt())
.map(|x| x.to_path_buf()) .map(|x| x.to_path_buf())
{ {
Some(path) => { self.open_file(&path);
self.open_file(&path);
}
None => {}
} }
} }
NavMenuAction::OpenWith(entity) => { NavMenuAction::OpenWith(entity) => {
match self if let Some(path) = self
.nav_model .nav_model
.data::<Location>(entity) .data::<Location>(entity)
.and_then(|x| x.path_opt()) .and_then(|x| x.path_opt())
.map(|x| x.to_path_buf()) .map(|x| x.to_path_buf())
{ {
Some(path) => match tab::item_from_path(&path, IconSizes::default()) { match tab::item_from_path(&path, IconSizes::default()) {
Ok(item) => { Ok(item) => {
return self.update(Message::DialogPush(DialogPage::OpenWith { return self.update(Message::DialogPush(DialogPage::OpenWith {
path: path.to_path_buf(), path: path.to_path_buf(),
@ -3311,8 +3290,7 @@ impl Application for App {
Err(err) => { Err(err) => {
log::warn!("failed to get item for path {:?}: {}", path, err); log::warn!("failed to get item for path {:?}: {}", path, err);
} }
}, }
None => {}
} }
} }
NavMenuAction::OpenInNewTab(entity) => { NavMenuAction::OpenInNewTab(entity) => {
@ -3329,8 +3307,7 @@ impl Application for App {
// Open the selected path in a new cosmic-files window. // Open the selected path in a new cosmic-files window.
NavMenuAction::OpenInNewWindow(entity) => { NavMenuAction::OpenInNewWindow(entity) => {
if let Some(&Location::Path(ref path)) = self.nav_model.data::<Location>(entity) if let Some(Location::Path(path)) = self.nav_model.data::<Location>(entity) {
{
match env::current_exe() { match env::current_exe() {
Ok(exe) => match process::Command::new(&exe).arg(path).spawn() { Ok(exe) => match process::Command::new(&exe).arg(path).spawn() {
Ok(_child) => {} Ok(_child) => {}
@ -3391,16 +3368,15 @@ impl Application for App {
log::info!("output {}: created", output.id()); log::info!("output {}: created", output.id());
let surface_id = WindowId::unique(); let surface_id = WindowId::unique();
match self.surface_ids.insert(output.clone(), surface_id) { if let Some(old_surface_id) =
Some(old_surface_id) => { self.surface_ids.insert(output.clone(), surface_id)
//TODO: remove old surface? {
log::warn!( //TODO: remove old surface?
"output {}: already had surface ID {:?}", log::warn!(
output.id(), "output {}: already had surface ID {:?}",
old_surface_id output.id(),
); old_surface_id
} );
None => {}
} }
let display = match output_info_opt { let display = match output_info_opt {
@ -3470,7 +3446,7 @@ impl Application for App {
} }
Message::Cosmic(cosmic) => { Message::Cosmic(cosmic) => {
// Forward cosmic messages // Forward cosmic messages
return Task::perform(async move { cosmic }, |cosmic| message::cosmic(cosmic)); return Task::perform(async move { cosmic }, message::cosmic);
} }
Message::None => {} Message::None => {}
#[cfg(all(feature = "desktop", feature = "wayland"))] #[cfg(all(feature = "desktop", feature = "wayland"))]
@ -4148,7 +4124,7 @@ impl Application for App {
let running = count; let running = count;
// Adjust the progress bar so it does not jump around when operations finish // Adjust the progress bar so it does not jump around when operations finish
for id in self.progress_operations.iter() { for id in self.progress_operations.iter() {
if self.complete_operations.contains_key(&id) { if self.complete_operations.contains_key(id) {
total_progress += 1.0; total_progress += 1.0;
count += 1; count += 1;
} }
@ -4241,8 +4217,7 @@ impl Application for App {
self.tab_model.active_data::<Tab>(), self.tab_model.active_data::<Tab>(),
&self.config, &self.config,
&self.key_binds, &self.key_binds,
) )]
.into()]
} }
fn header_end(&self) -> Vec<Element<Self::Message>> { fn header_end(&self) -> Vec<Element<Self::Message>> {
@ -4371,16 +4346,16 @@ impl Application for App {
return if let Some(margin) = self.margin.get(&id) { return if let Some(margin) = self.margin.get(&id) {
if margin.0 >= 0. || margin.2 >= 0. { if margin.0 >= 0. || margin.2 >= 0. {
tab_column = widget::column::with_children(vec![ tab_column = widget::column::with_children(vec![
vertical_space().height(margin.0 as f32).into(), vertical_space().height(margin.0).into(),
tab_column.into(), tab_column.into(),
vertical_space().height(margin.2 as f32).into(), vertical_space().height(margin.2).into(),
]) ])
} }
if margin.1 >= 0. || margin.3 >= 0. { if margin.1 >= 0. || margin.3 >= 0. {
Element::from(widget::row::with_children(vec![ Element::from(widget::row::with_children(vec![
horizontal_space().width(margin.1 as f32).into(), horizontal_space().width(margin.1).into(),
tab_column.into(), tab_column.into(),
horizontal_space().width(margin.3 as f32).into(), horizontal_space().width(margin.3).into(),
])) ]))
} else { } else {
tab_column.into() tab_column.into()
@ -4748,7 +4723,6 @@ pub(crate) mod test_utils {
use crate::{ use crate::{
config::{IconSizes, TabConfig}, config::{IconSizes, TabConfig},
mounter::MounterMap,
tab::Item, tab::Item,
}; };
@ -4986,8 +4960,8 @@ pub(crate) mod test_utils {
assert!( assert!(
entries entries
.into_iter() .into_iter()
.zip(tab.items_opt().clone().unwrap_or(&empty)) .zip(tab.items_opt().unwrap_or(&empty))
.all(|(a, b)| eq_path_item(&a, &b)), .all(|(a, b)| eq_path_item(&a, b)),
"Path ({}) and Tab path ({}) don't have equal contents", "Path ({}) and Tab path ({}) don't have equal contents",
path.display(), path.display(),
tab_path.display() tab_path.display()

View file

@ -278,12 +278,12 @@ impl MimeAppCache {
pub fn get(&self, key: &Mime) -> &[MimeApp] { pub fn get(&self, key: &Mime) -> &[MimeApp] {
static EMPTY: Vec<MimeApp> = Vec::new(); static EMPTY: Vec<MimeApp> = Vec::new();
self.cache.get(key).unwrap_or_else(|| &EMPTY) self.cache.get(key).unwrap_or(&EMPTY)
} }
pub fn icons(&self, key: &Mime) -> &[widget::icon::Handle] { pub fn icons(&self, key: &Mime) -> &[widget::icon::Handle] {
static EMPTY: Vec<widget::icon::Handle> = Vec::new(); static EMPTY: Vec<widget::icon::Handle> = Vec::new();
self.icons.get(key).unwrap_or_else(|| &EMPTY) self.icons.get(key).unwrap_or(&EMPTY)
} }
pub fn terminal(&self) -> Option<&MimeApp> { pub fn terminal(&self) -> Option<&MimeApp> {
@ -348,3 +348,9 @@ impl MimeAppCache {
} }
} }
} }
impl Default for MimeAppCache {
fn default() -> Self {
Self::new()
}
}