Skip to content

Commit

Permalink
feat: More love for compare mode ui, option to remove current image
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Jun 9, 2024
1 parent 57299c4 commit 68a5a48
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 34 deletions.
7 changes: 6 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ fn main() {
println!("Build script");
// #[cfg(windows)]
match std::process::Command::new("convert")
.args(vec!["res/oculante.png", "-define", "icon:auto-resize=16,32,48,64,128,256", "icon.ico"])
.args(vec![
"res/oculante.png",
"-define",
"icon:auto-resize=16,32,48,64,128,256",
"icon.ico",
])
.spawn()
{
Ok(_b) => println!("Converted icon"),
Expand Down
2 changes: 1 addition & 1 deletion src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
sync::mpsc::{self, Receiver, Sender},
};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct ImageGeometry {
/// The scale of the displayed image
pub scale: f32,
Expand Down
5 changes: 3 additions & 2 deletions src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub fn open_image(img_location: &Path) -> Result<Receiver<Frame>> {
jp2_image.width(),
jp2_image.height(),
jp2_image.get_pixels(Some(255))?.data,
).context("Can't decode jp2k buffer")?;
)
.context("Can't decode jp2k buffer")?;
_ = sender.send(Frame::new_still(image_buffer));
return Ok(receiver);
}
Expand Down Expand Up @@ -457,7 +458,7 @@ pub fn open_image(img_location: &Path) -> Result<Receiver<Frame>> {
let contents = std::fs::read(&img_location)?;

let decoder = image::codecs::webp::WebPDecoder::new(std::io::Cursor::new(contents))?;
if ! decoder.has_animation() {
if !decoder.has_animation() {
let img = image::open(img_location)?;
_ = sender.send(Frame::new_still(img.to_rgba8()));
return Ok(receiver);
Expand Down
24 changes: 19 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ fn main() -> Result<(), String> {
window_config.lazy_loop = !settings.force_redraw;
window_config.decorations = !settings.borderless;
if settings.window_geometry != Default::default() {
window_config.width = settings.window_geometry.1.0 as u32;
window_config.height = settings.window_geometry.1.1 as u32;
window_config.width = settings.window_geometry.1 .0 as u32;
window_config.height = settings.window_geometry.1 .1 as u32;
}
debug!("Loaded settings.");
if settings.zen_mode {
Expand All @@ -135,7 +135,6 @@ fn main() -> Result<(), String> {
window_config = window_config.set_title(&title_string);
}
window_config.min_size = Some(settings.min_window_size);

}
Err(e) => {
error!("Could not load settings: {e}");
Expand Down Expand Up @@ -298,7 +297,7 @@ fn init(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins) -> OculanteSta
ctx.set_pixels_per_point(app.window().dpi() as f32);
let mut fonts = FontDefinitions::default();

ctx.options_mut(|o|o.zoom_with_keyboard = false);
ctx.options_mut(|o| o.zoom_with_keyboard = false);

fonts
.font_data
Expand Down Expand Up @@ -538,6 +537,12 @@ fn event(app: &mut App, state: &mut OculanteState, evt: Event) {
state.send_message_info("Deleted image");
}
}
if key_pressed(app, state, ClearImage) {
state.current_path = None;
state.current_image = None;
state.current_texture = None;
state.image_info = None;
}
if key_pressed(app, state, ZoomIn) {
let delta = zoomratio(3.5, state.image_geometry.scale);
let new_scale = state.image_geometry.scale + delta;
Expand Down Expand Up @@ -643,7 +648,9 @@ fn event(app: &mut App, state: &mut OculanteState, evt: Event) {
Event::Drop(file) => {
if let Some(p) = file.path {
if let Some(ext) = p.extension() {
if SUPPORTED_EXTENSIONS.contains(&ext.to_string_lossy().to_string().to_lowercase().as_str()) {
if SUPPORTED_EXTENSIONS
.contains(&ext.to_string_lossy().to_string().to_lowercase().as_str())
{
state.is_loaded = false;
state.current_image = None;
state.player.load(&p, state.message_channel.0.clone());
Expand Down Expand Up @@ -893,6 +900,13 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
state.redraw = true;
}
FrameSource::CompareResult => {
debug!("Received compare result");

// always reset if first image
if state.current_texture.is_none() {
state.reset_image = true;
}

state.redraw = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct PersistentSettings {
pub fit_image_on_window_resize: bool,
pub zoom_multiplier: f32,
pub borderless: bool,
pub min_window_size: (u32,u32)
pub min_window_size: (u32, u32),
}

impl Default for PersistentSettings {
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Default for PersistentSettings {
fit_image_on_window_resize: false,
zoom_multiplier: 1.0,
borderless: false,
min_window_size: (100,100)
min_window_size: (100, 100),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/shortcuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum InputEvent {
PanUp,
PanDown,
DeleteFile,
ClearImage,
LosslessRotateRight,
LosslessRotateLeft,
Copy,
Expand Down Expand Up @@ -180,7 +181,7 @@ pub fn key_pressed(app: &mut App, state: &mut OculanteState, command: InputEvent
// early out if just one key is pressed, and it's a modifier
if app.keyboard.alt() || app.keyboard.shift() || app.keyboard.ctrl() {
if app.keyboard.down.len() == 1 {
debug!("just modifier down");
log::trace!("alt/shift/ctrl modifier down");
return false;
}
}
Expand Down
117 changes: 99 additions & 18 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ use notan::{
egui::{self, *},
prelude::{App, Graphics},
};
use std::{collections::BTreeSet, ops::RangeInclusive, path::PathBuf, time::Instant};
use std::{
collections::BTreeSet,
ops::RangeInclusive,
path::PathBuf,
time::{Duration, Instant},
};
use strum::IntoEnumIterator;
const PANEL_WIDTH: f32 = 240.0;
const PANEL_WIDGET_OFFSET: f32 = 10.0;
Expand Down Expand Up @@ -368,30 +373,90 @@ pub fn info_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) {
);
}
ui.collapsing("Compare", |ui| {


if state.persistent_settings.max_cache == 0 {
ui.label("Warning! Set your cache to more than 0 in settings for this to be fast.");
}

ui.vertical_centered_justified(|ui| {
if let Some(p) = &(state.current_path).clone() {
if ui.button("Add/update current image").clicked() {
state.compare_list.insert(p.clone(), state.image_geometry.clone());
if let Some(p) = &(state.current_path).clone() {

if ui.button(&format!("{FOLDER} Open another image..."))
.clicked()
{
#[cfg(feature = "file_open")]
crate::browse_for_image_path(state);
#[cfg(not(feature = "file_open"))]
ui.ctx().memory_mut(|w| w.open_popup(Id::new("OPEN")));
}


let mut compare_list: Vec<(PathBuf, ImageGeometry)> = state.compare_list.clone().into_iter().collect();
compare_list.sort_by(|a,b| a.0.cmp(&b.0));
for (path, geo) in compare_list {
if ui.selectable_label(p==&path, path.file_name().map(|f| f.to_string_lossy().to_string()).unwrap_or_default().to_string()).clicked(){
state.image_geometry = geo.clone();
state
.player
.load_advanced(&path, Some(FrameSource::CompareResult), state.message_channel.0.clone());
state.current_path = Some(path);



// if ui.button("Add/update current image").clicked() {
// state.compare_list.insert(p.clone(), state.image_geometry.clone());
// }


let mut compare_list: Vec<(PathBuf, ImageGeometry)> = state.compare_list.clone().into_iter().collect();
compare_list.sort_by(|a,b| a.0.cmp(&b.0));
for (path, geo) in compare_list {


ui.horizontal(|ui|{

if ui.button(X).clicked() {
state.compare_list.remove(&path);
}


ui.vertical_centered_justified(|ui| {

if ui.selectable_label(p==&path, path.file_name().map(|f| f.to_string_lossy().to_string()).unwrap_or_default().to_string()).clicked(){
state.image_geometry = geo.clone();
state
.player
.load_advanced(&path, Some(FrameSource::CompareResult), state.message_channel.0.clone());
ui.ctx().request_repaint();
ui.ctx().request_repaint_after(Duration::from_millis(500));

state.current_path = Some(path);
state.image_info = None;
}
});


});


}

if let Some(path) = &state.current_path {
if let Some(geo) = state.compare_list.get(path) {
if state.image_geometry != *geo {
if ui.button(RichText::new(format!("{ARROWS_CLOCKWISE} Update position")).color(Color32::YELLOW)).clicked() {
state.compare_list.insert(p.clone(), state.image_geometry.clone());
}
}
} else {
if ui.button(format!("{PLUS} Add current image")).clicked() {
state.compare_list.insert(p.clone(), state.image_geometry.clone());
}
}
}

if !state.compare_list.is_empty() {

if ui.button(format!("{TRASH} Clear all")).clicked() {
state.compare_list.clear();
}
}

}
if ui.button("Clear").clicked() {
state.compare_list.clear();
}
}

});
});
});

ui.collapsing("Alpha tools", |ui| {
Expand Down Expand Up @@ -2064,6 +2129,22 @@ pub fn main_menu(ui: &mut Ui, state: &mut OculanteState, app: &mut App, gfx: &mu
}
}

if state.current_texture.is_some() {
if tooltip(
unframed_button(PLACEHOLDER, ui),
"Clear image",
&lookup(&state.persistent_settings.shortcuts, &ClearImage),
ui,
)
.clicked()
{
state.current_path = None;
state.current_image = None;
state.current_texture = None;
state.image_info = None;
}
}

drag_area(ui, state, app);

ui.add_space(ui.available_width() - 32.);
Expand Down
14 changes: 10 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,19 @@ impl Player {
self.stop_sender = stop_sender;

if let Some(cached_image) = self.cache.get(img_location) {
debug!("Cache hit for {}", img_location.display());

let mut frame = Frame::new_still(cached_image);
if let Some(fs) = forced_frame_source {
debug!("Frame source set to {:?}", fs);
frame.source = fs;
}
_ = self.image_sender.send(frame);
debug!("Cache hit for {}", img_location.display());
return;
}

debug!("Image not in cache.");

send_image_threaded(
img_location,
self.image_sender.clone(),
Expand Down Expand Up @@ -286,15 +290,13 @@ pub fn send_image_threaded(

match open_image(&loc) {
Ok(frame_receiver) => {
debug!("Got a frame receiver from opening image");
// _ = texture_sender
// .clone()
// .send(Frame::new_reset(f.buffer.clone()));

let mut first = true;
for mut f in frame_receiver.iter() {
if let Some(ref fs) = forced_frame_source {
f.source = fs.clone();
}
if stop_receiver.try_recv().is_ok() {
debug!("Stopped from receiver.");
return;
Expand All @@ -318,6 +320,10 @@ pub fn send_image_threaded(
as u32,
);

if let Some(ref fs) = forced_frame_source {
f.source = fs.clone();
}

let mut frame = f;

let op = ImageOperation::Resize {
Expand Down

0 comments on commit 68a5a48

Please sign in to comment.