image_view: Clear the carousel when creating a new model

This way we properly handle switching directories within the
same window.
This commit is contained in:
Christopher Davis 2022-06-28 21:28:55 -04:00
parent 3557b15034
commit 8acf76bd5e

View file

@ -160,8 +160,7 @@ impl LpImageView {
// 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`.
// //
// TODO: Properly loading a new model with the same view, // TODO: Loading a file from the same directory
// & loading a file from the same directory
fn build_model_from_file(&self, file: &gio::File) { fn build_model_from_file(&self, file: &gio::File) {
let imp = self.imp(); let imp = self.imp();
let carousel = &imp.carousel; let carousel = &imp.carousel;
@ -173,6 +172,11 @@ impl LpImageView {
if let Some(ref parent) = file.parent() { if let Some(ref parent) = file.parent() {
if let Some(ref m) = *model { if let Some(ref m) = *model {
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
while carousel.n_pages() > 0 {
carousel.remove(&carousel.nth_page(0));
}
*model = Some(LpFileModel::from_directory(parent)); *model = Some(LpFileModel::from_directory(parent));
log::debug!("new model created"); log::debug!("new model created");
} else { } else {