Skip to content

Commit

Permalink
Enhancement: Add File Dialog to Browse for Images
Browse files Browse the repository at this point in the history
Add the ability to use a file browse dialog to open a file from another folder
on disk using Ctrl+O / F1. This is useful when testing or launching the tool
from a shortcut, where it may not be started with a filename.

This makes use of the "nfd" crate (which uses the "nativedialog" library
under the hood, requiring a working C/C++ compiler for compilation to work)

Limitations:
* The file dialog allows selecting any file types, and not just supported
  image file formats.
  • Loading branch information
Aligorith committed Dec 29, 2021
1 parent 5a4504e commit 1fbb1a8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ usvg = "0.14.1"
tiny-skia = "0.5.1"
log = "0.4"
env_logger = "0.8"
nfd = "0.0.4"

[dependencies.libavif-image]
version = "0.6.0"
Expand Down
Binary file modified resource/usage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified resource_dev/Usage Panel/Emulsion Usage.odt
Binary file not shown.
2 changes: 2 additions & 0 deletions src/input_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use lazy_static::lazy_static;

pub static TOGGLE_FULLSCREEN_NAME: &str = "toggle_fullscreen";
pub static ESCAPE_NAME: &str = "escape";
pub static IMG_BROWSE_NAME: &str = "img_browse";
pub static IMG_NEXT_NAME: &str = "img_next";
pub static IMG_PREV_NAME: &str = "img_prev";
pub static IMG_ORIG_NAME: &str = "img_orig";
Expand All @@ -34,6 +35,7 @@ lazy_static! {
let mut m = HashMap::new();
m.insert(TOGGLE_FULLSCREEN_NAME, vec!["F11", "Return"]);
m.insert(ESCAPE_NAME, vec!["Escape"]);
m.insert(IMG_BROWSE_NAME, vec!["F1", "CmdCtrl+O"]);
m.insert(IMG_NEXT_NAME, vec!["D", "Right", "PageDown"]);
m.insert(IMG_PREV_NAME, vec!["A", "Left", "PageUp"]);
m.insert(IMG_ORIG_NAME, vec!["Q", "1"]);
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ fn main() {
bottom_bar.slider.set_on_value_change(move || {
picture_widget.jump_to_index(slider.value());
});
// TODO: Right-click to launch file browser?
}
{
let picture_widget = picture_widget.clone();
Expand Down
19 changes: 19 additions & 0 deletions src/widgets/picture_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::rc::{Rc, Weak};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

extern crate nfd;
use nfd::Response;

use gelatin::cgmath::{Matrix4, Vector2, Vector3};
use gelatin::glium::glutin::event::{ElementState, ModifiersState, MouseButton};
use gelatin::glium::{
Expand Down Expand Up @@ -619,6 +622,22 @@ impl PictureWidget {
}
}
}
if triggered!(IMG_BROWSE_NAME) {
match nfd::open_file_dialog(None, None).expect("file dialog error") {
// TODO: Restrict to only supported image types
Response::Okay(file_path) => {
println!("Selected File Path = {:?}", file_path);
let file_path_buf = PathBuf::from(file_path.clone());

borrowed.playback_manager.request_load(LoadRequest::FilePath(file_path_buf.clone()));
borrowed.render_validity.invalidate();
},

Response::Cancel => println!("File browse cancelled"),
_ => (),
}

}
if let Some(img_path) = borrowed.playback_manager.shown_file_path() {
if let Some(folder_path) = img_path.parent() {
let img_and_folder = (img_path.to_str(), folder_path.to_str());
Expand Down

0 comments on commit 1fbb1a8

Please sign in to comment.