imageView: Fix two panics when file not exists

This commit is contained in:
Sophie Herold 2022-11-04 19:28:48 +01:00
parent 369b71b15c
commit 165861568c
2 changed files with 16 additions and 16 deletions

View file

@ -18,11 +18,13 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
use crate::deps::*; use crate::deps::*;
use crate::i18n::*;
use crate::util; use crate::util;
use gio::prelude::*; use gio::prelude::*;
use gio::subclass::prelude::*; use gio::subclass::prelude::*;
use anyhow::Context;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use std::cell::RefCell; use std::cell::RefCell;
@ -68,7 +70,7 @@ glib::wrapper! {
} }
impl LpFileModel { impl LpFileModel {
pub fn from_directory(directory: &gio::File) -> Self { pub fn from_directory(directory: &gio::File) -> anyhow::Result<Self> {
let model = glib::Object::new::<Self>(&[]); let model = glib::Object::new::<Self>(&[]);
{ {
@ -85,7 +87,7 @@ impl LpFileModel {
gio::FileQueryInfoFlags::NONE, gio::FileQueryInfoFlags::NONE,
gio::Cancellable::NONE, gio::Cancellable::NONE,
) )
.unwrap(); .context(i18n("Directory does not exist."))?;
// Filter out non-images; For now we support "all" image types. // Filter out non-images; For now we support "all" image types.
enumerator.for_each(|info| { enumerator.for_each(|info| {
@ -106,7 +108,7 @@ impl LpFileModel {
model.imp().directory.set(directory.clone()).unwrap(); model.imp().directory.set(directory.clone()).unwrap();
} }
model Ok(model)
} }
pub fn directory(&self) -> Option<gio::File> { pub fn directory(&self) -> Option<gio::File> {

View file

@ -154,15 +154,9 @@ impl LpImageView {
} }
} }
self.build_model_from_file(file); self.build_model_from_file(file)?;
self.notify("active-file"); self.notify("active-file");
// TODO: rework width stuff
// let width = imp.picture.image_width();
// let height = imp.picture.image_height();
// log::debug!("Image dimensions: {} x {}", width, height);
// Ok((width, height))
Ok(()) Ok(())
} }
@ -170,7 +164,7 @@ impl LpImageView {
// that holds a `gio::File` for each child within the same directory as the // that holds a `gio::File` for each child within the same directory as the
// file we pass. This model will update with changes to the directory, // file we pass. This model will update with changes to the directory,
// and in turn we'll update our `adw::Carousel`. // and in turn we'll update our `adw::Carousel`.
fn build_model_from_file(&self, file: &gio::File) { fn build_model_from_file(&self, file: &gio::File) -> anyhow::Result<()> {
let imp = self.imp(); let imp = self.imp();
let carousel = &imp.carousel; let carousel = &imp.carousel;
@ -183,27 +177,31 @@ impl LpImageView {
if m.directory().map_or(false, |f| !f.equal(parent)) { if m.directory().map_or(false, |f| !f.equal(parent)) {
// Clear the carousel before creating the new model // Clear the carousel before creating the new model
self.clear_carousel(false); self.clear_carousel(false);
*model = Some(LpFileModel::from_directory(parent)); *model = Some(LpFileModel::from_directory(parent)?);
log::debug!("new model created"); log::debug!("new model created");
} else { } else {
log::debug!("Re-using old model and navigating to the current file"); log::debug!("Re-using old model and navigating to the current file");
self.navigate_to_file(m, file); self.navigate_to_file(m, file);
return; return Ok(());
} }
} else { } else {
*model = Some(LpFileModel::from_directory(parent)); *model = Some(LpFileModel::from_directory(parent)?);
log::debug!("new model created"); log::debug!("new model created");
} }
} }
} }
if let Some(model) = imp.model.borrow().as_ref() { if let Some(model) = imp.model.borrow().as_ref() {
let index = model.index_of(file).unwrap(); let index = model
.index_of(file)
.context(i18n("File not found in model. (TODO: bad message)"))?;
log::debug!("Currently at file {index} in the directory"); log::debug!("Currently at file {index} in the directory");
carousel.append(&LpImagePage::from_file(file)); carousel.append(&LpImagePage::from_file(file));
self.fill_carousel(model, index); self.fill_carousel(model, index);
self.update_action_state(model, index); self.update_action_state(model, index);
} }
Ok(())
} }
pub fn navigate(&self, direction: adw::NavigationDirection) { pub fn navigate(&self, direction: adw::NavigationDirection) {
@ -402,7 +400,7 @@ impl LpImageView {
) )
.await; .await;
let _ = match wallpaper::WallpaperRequest::default() match wallpaper::WallpaperRequest::default()
.set_on(wallpaper::SetOn::Background) .set_on(wallpaper::SetOn::Background)
.show_preview(true) .show_preview(true)
.identifier(id) .identifier(id)