Implement unmount button

This commit is contained in:
Jeremy Soller 2024-04-24 13:59:55 -06:00
parent 8c3af501ca
commit 37a6f37f7f
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
5 changed files with 77 additions and 29 deletions

View file

@ -182,6 +182,7 @@ pub enum Message {
Modifiers(Modifiers),
MoveToTrash(Option<Entity>),
MounterItems(MounterKey, MounterItems),
NavBarClose(Entity),
NavBarContext(Entity),
NavMenuAction(NavMenuAction),
NewItem(Option<Entity>, bool),
@ -734,7 +735,13 @@ impl Application for App {
cosmic::app::Message::App(Message::DndDropNav(entity, data, action))
})
.on_context(|entity| cosmic::app::Message::App(Message::NavBarContext(entity)))
.on_close(|entity| cosmic::app::Message::App(Message::NavBarClose(entity)))
.context_menu(self.nav_context_menu(self.nav_bar_context_id))
.close_icon(
widget::icon::from_name("media-eject-symbolic")
.size(16)
.icon(),
)
.into_container();
if !self.core().is_condensed() {
@ -1065,15 +1072,7 @@ impl Application for App {
let mut entity = self
.nav_model
.insert()
.text(format!(
"{} ({})",
item.name(),
if item.is_mounted() {
"mounted"
} else {
"not mounted"
}
))
.text(item.name())
.data(MounterData(key, item.clone()));
if let Some(path) = item.path() {
entity = entity.data(Location::Path(path.clone()));
@ -1081,6 +1080,9 @@ impl Application for App {
if let Some(icon) = item.icon() {
entity = entity.icon(widget::icon::icon(icon).size(16));
}
if item.is_mounted() {
entity = entity.closable();
}
}
}
Message::NewItem(entity_opt, dir) => {
@ -1650,6 +1652,14 @@ impl Application for App {
}
}
Message::NavBarClose(entity) => {
if let Some(data) = self.nav_model.data::<MounterData>(entity) {
if let Some(mounter) = self.mounters.get(&data.0) {
return mounter.unmount(data.1.clone()).map(|_| message::none());
}
}
}
// Tracks which nav bar item to show a context menu for.
Message::NavBarContext(entity) => {
self.nav_bar_context_id = entity;

View file

@ -24,6 +24,7 @@ fn gio_icon_to_path(icon: &gio::Icon, size: u16) -> Option<PathBuf> {
enum Cmd {
Rescan,
Mount(MounterItem),
Unmount(MounterItem),
}
enum Event {
@ -189,6 +190,34 @@ impl Gvfs {
);
}
}
Cmd::Unmount(mounter_item) => {
let MounterItem::Gvfs(item) = mounter_item else { continue };
let ItemKind::Mount = item.kind else { continue };
for (i, mount) in monitor.mounts().into_iter().enumerate() {
if i != item.index {
continue;
}
let name = MountExt::name(&mount);
if item.name != name {
log::warn!("trying to unmount mount {} failed: name is {:?} when {:?} was expected", i, name, item.name);
continue;
}
//TODO: do eject instead of unmount?
log::info!("unmount {}", name);
MountExt::unmount_with_operation(
&mount,
gio::MountUnmountFlags::NONE,
//TODO: gio::MountOperation needed for network shares with auth
gio::MountOperation::NONE,
gio::Cancellable::NONE,
move |result| {
log::info!("unmount {}: result {:?}", name, result);
},
);
}
}
}
}
});
@ -213,6 +242,17 @@ impl Mounter for Gvfs {
)
}
fn unmount(&self, item: MounterItem) -> Command<()> {
let command_tx = self.command_tx.clone();
Command::perform(
async move {
command_tx.send(Cmd::Unmount(item)).unwrap();
()
},
|x| x,
)
}
fn subscription(&self) -> subscription::Subscription<MounterItems> {
let command_tx = self.command_tx.clone();
let event_rx = self.event_rx.clone();

View file

@ -51,6 +51,7 @@ pub type MounterItems = Vec<MounterItem>;
pub trait Mounter {
//TODO: send result
fn mount(&self, item: MounterItem) -> Command<()>;
fn unmount(&self, item: MounterItem) -> Command<()>;
fn subscription(&self) -> subscription::Subscription<MounterItems>;
}