window: Add Home/End shortcuts for first/last img

This is the same as in EOG.
Also add PageUp/Down for image navigation from EOG.
Cleanup the navigation enums as well.
This commit is contained in:
Sophie Herold 2023-02-06 19:14:01 +01:00
parent 169dd454cc
commit 69889820cd
6 changed files with 65 additions and 13 deletions

View file

@ -102,6 +102,18 @@
<property name="accelerator">Right</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">First Image</property>
<property name="action-name">win.first</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Last Image</property>
<property name="action-name">win.last</property>
</object>
</child>
</object>
</child>
<child>

View file

@ -180,8 +180,10 @@ impl LpApplication {
self.set_accels_for_action("win.toggle-fullscreen", &["F11"]);
self.set_accels_for_action("win.set-background", &["<Ctrl>F8"]);
self.set_accels_for_action("win.previous", &["Left"]);
self.set_accels_for_action("win.next", &["Right"]);
self.set_accels_for_action("win.previous", &["Left", "Page_Down"]);
self.set_accels_for_action("win.next", &["Right", "Page_Up"]);
self.set_accels_for_action("win.first", &["Home"]);
self.set_accels_for_action("win.last", &["End"]);
self.set_accels_for_action("win.zoom-to(1.0)", &["1", "KP_1", "<Ctrl>1", "<Ctrl>KP_1"]);
self.set_accels_for_action("win.zoom-to(2.0)", &["2", "KP_2", "<Ctrl>2", "<Ctrl>KP_2"]);

View file

@ -207,6 +207,16 @@ impl LpFileModel {
.collect()
}
/// Return first path
pub fn first(&self) -> Option<PathBuf> {
self.imp().files.borrow().first().cloned()
}
/// Returns last path
pub fn last(&self) -> Option<PathBuf> {
self.imp().files.borrow().last().cloned()
}
/// Currently sorts by name
fn sort(files: &mut IndexSet<PathBuf>) {
files.sort_by(|x, y| util::compare_by_name(x, y));

View file

@ -123,3 +123,15 @@ pub async fn spawn<T: Debug + Send + 'static>(
.spawn(async_global_executor::spawn_blocking(f))?
.await)
}
#[derive(Debug, Clone, Copy)]
pub enum Position {
First,
Last,
}
#[derive(Debug, Clone, Copy)]
pub enum Direction {
Back,
Forward,
}

View file

@ -21,6 +21,7 @@ use crate::deps::*;
use crate::file_model::LpFileModel;
use crate::i18n::*;
use crate::thumbnail::Thumbnail;
use crate::util::{Direction, Position};
use crate::widgets::{LpImage, LpImagePage, LpSlidingView};
use adw::prelude::*;
@ -270,12 +271,11 @@ impl LpImageView {
}
/// Move forward or backwards
pub fn navigate(&self, direction: adw::NavigationDirection) {
pub fn navigate(&self, direction: Direction) {
if let Some(current_path) = self.current_path() {
let new_path = match direction {
adw::NavigationDirection::Forward => self.model().after(&current_path),
adw::NavigationDirection::Back => self.model().before(&current_path),
_ => unimplemented!("Navigation direction should only be back or forward."),
Direction::Forward => self.model().after(&current_path),
Direction::Back => self.model().before(&current_path),
};
if let Some(new_path) = new_path {
@ -284,6 +284,18 @@ impl LpImageView {
}
}
/// Jump to position
pub fn jump(&self, position: Position) {
let new_path = match position {
Position::First => self.model().first(),
Position::Last => self.model().last(),
};
if let Some(new_path) = new_path {
self.navigate_to_path(&new_path);
}
}
/// Used for drag and drop
fn navigate_to_path(&self, new_path: &Path) {
let sliding_view = self.sliding_view();

View file

@ -30,7 +30,7 @@ use std::cell::RefCell;
use std::path::{Path, PathBuf};
use crate::config;
use crate::util;
use crate::util::{self, Direction, Position};
use crate::widgets::{LpImage, LpImagePage, LpImageView, LpPropertiesView};
mod imp {
@ -99,15 +99,19 @@ mod imp {
});
klass.install_action("win.next", None, move |win, _, _| {
win.imp()
.image_view
.navigate(adw::NavigationDirection::Forward);
win.imp().image_view.navigate(Direction::Forward);
});
klass.install_action("win.previous", None, move |win, _, _| {
win.imp()
.image_view
.navigate(adw::NavigationDirection::Back);
win.imp().image_view.navigate(Direction::Back);
});
klass.install_action("win.first", None, move |win, _, _| {
win.imp().image_view.jump(Position::First);
});
klass.install_action("win.last", None, move |win, _, _| {
win.imp().image_view.jump(Position::Last);
});
klass.install_action("win.zoom-out", None, move |win, _, _| {