Add mounter abstraction, enable minimal GVfs support

This commit is contained in:
Jeremy Soller 2024-04-22 09:54:00 -06:00
parent 8913c4198b
commit 02b6cda872
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
8 changed files with 254 additions and 19 deletions

View file

@ -48,6 +48,7 @@ use crate::{
fl, home_dir,
key_bind::key_binds,
menu, mime_app,
mounter::{mounters, Mounters},
operation::Operation,
spawn_detached::spawn_detached,
tab::{self, HeadingOptions, ItemMetadata, Location, Tab},
@ -268,6 +269,7 @@ pub struct App {
dialog_text_input: widget::Id,
key_binds: HashMap<KeyBind, Action>,
modifiers: Modifiers,
mounters: Mounters,
pending_operation_id: u64,
pending_operations: BTreeMap<u64, (Operation, f32)>,
complete_operations: BTreeMap<u64, Operation>,
@ -727,6 +729,34 @@ impl Application for App {
.data(Location::Trash)
});
//TODO: dynamic mount list
let mounters = mounters();
for (mounter_name, mounter) in mounters.iter() {
println!("Mounter {}", mounter_name);
match mounter.items() {
Ok(items) => {
for item in items {
let name = item.name();
println!(" - {}", name);
let icon = item.icon(16);
let dir_opt = item.path();
nav_model = nav_model.insert(move |mut b| {
b = b
.text(name.clone())
.icon(widget::icon::icon(icon.clone()).size(16));
match &dir_opt {
Some(dir) => b.data(Location::Path(dir.clone())),
None => b,
}
});
}
}
Err(err) => {
log::warn!("failed to get items: {}", err);
}
}
}
let mut app = App {
core,
nav_model: nav_model.build(),
@ -741,6 +771,7 @@ impl Application for App {
dialog_text_input: widget::Id::unique(),
key_binds: key_binds(),
modifiers: Modifiers::empty(),
mounters,
pending_operation_id: 0,
pending_operations: BTreeMap::new(),
complete_operations: BTreeMap::new(),

View file

@ -19,6 +19,7 @@ mod localize;
mod menu;
mod mime_app;
pub mod mime_icon;
mod mounter;
mod mouse_area;
mod operation;
mod spawn_detached;

54
src/mounter/gvfs.rs Normal file
View file

@ -0,0 +1,54 @@
use cosmic::widget;
use gio::prelude::*;
use gio::{Mount, ThemedIcon, Volume, VolumeMonitor};
use std::{error::Error, path::PathBuf};
use super::{Mounter, MounterItem, MounterItems};
pub struct Gvfs {
monitor: VolumeMonitor,
}
impl Gvfs {
pub fn new() -> Self {
let monitor = VolumeMonitor::get();
Self { monitor }
}
}
impl Mounter for Gvfs {
fn items(&self) -> Result<MounterItems, Box<dyn Error>> {
let mut items = MounterItems::new();
for mount in self.monitor.mounts() {
items.push(Box::new(mount));
}
Ok(items)
}
}
impl MounterItem for Mount {
fn name(&self) -> String {
MountExt::name(self).to_string()
}
fn icon(&self, size: u16) -> widget::icon::Handle {
let icon = MountExt::symbolic_icon(self);
if let Some(themed_icon) = icon.downcast_ref::<ThemedIcon>() {
for name in themed_icon.names() {
let named = widget::icon::from_name(name.as_str()).size(size);
if let Some(path) = named.path() {
return widget::icon::from_path(path);
}
}
}
//TODO: handle more gio icon types
widget::icon::from_name("folder-symbolic")
.size(size)
.handle()
}
fn path(&self) -> Option<PathBuf> {
MountExt::root(self).path()
}
}

33
src/mounter/mod.rs Normal file
View file

@ -0,0 +1,33 @@
use cosmic::widget;
use std::error::Error;
use std::{collections::BTreeMap, path::PathBuf, sync::Arc};
#[cfg(feature = "gvfs")]
mod gvfs;
pub trait MounterItem {
fn name(&self) -> String;
fn icon(&self, size: u16) -> widget::icon::Handle;
fn path(&self) -> Option<PathBuf>;
}
pub type MounterItems = Vec<Box<dyn MounterItem>>;
pub trait Mounter {
fn items(&self) -> Result<MounterItems, Box<dyn Error>>;
}
pub type MounterKey = &'static str;
pub type MounterMap = BTreeMap<MounterKey, Box<dyn Mounter>>;
pub type Mounters = Arc<MounterMap>;
pub fn mounters() -> Mounters {
let mut mounters = MounterMap::new();
#[cfg(feature = "gvfs")]
{
mounters.insert("gvfs", Box::new(gvfs::Gvfs::new()));
}
Mounters::new(mounters)
}