Skip to content

Commit

Permalink
Implement unmount button
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Apr 24, 2024
1 parent 8c3af50 commit 37a6f37
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 29 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ slotmap = "1.0.7"

[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic.git"
branch = "nav-bar-close-icon"
default-features = false
features = ["multi-window", "tokio", "winit"]

Expand Down Expand Up @@ -77,10 +78,6 @@ test-log = "0.2"
# libcosmic = { path = "../libcosmic" }
# cosmic-config = { path = "../libcosmic/cosmic-config" }
# cosmic-theme = { path = "../libcosmic/cosmic-theme" }
# libcosmic = { git = "https://github.com/pop-os/libcosmic//", branch = "dnd" }
# cosmic-config = { git = "https://github.com/pop-os/libcosmic//", branch = "dnd" }
# cosmic-theme = { git = "https://github.com/pop-os/libcosmic//", branch = "dnd" }

# [patch.'https://github.com/pop-os/smithay-clipboard']
# smithay-clipboard = { git = "https://github.com/pop-os/smithay-clipboard//", rev = "2f2430b" }
# smithay-clipboard = { path = "../smithay-clipboard" }
28 changes: 19 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -1065,22 +1072,17 @@ 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()));
}
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) => {
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions src/mounter/gvfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
},
);
}
}
}
}
});
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/mounter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

Expand Down

0 comments on commit 37a6f37

Please sign in to comment.