Skip to content

Commit

Permalink
fix: Prevent "Do not reset image view" being reset
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Feb 6, 2024
1 parent 75cb308 commit 4e06ca7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn init(gfx: &mut Graphics, plugins: &mut Plugins) -> OculanteState {
Err(e) => {
warn!("Settings failed to load: {e}. This may happen after application updates. Generating a fresh file.");
state.persistent_settings = Default::default();
state.persistent_settings.save();
state.persistent_settings.save_blocking();
}
}

Expand Down Expand Up @@ -717,7 +717,6 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
state.persistent_settings.last_open_directory = dir.to_path_buf();
}
state.current_path = Some(p);
_ = state.persistent_settings.save();
}

// check if a new texture has been sent
Expand Down Expand Up @@ -813,6 +812,9 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
FrameSource::Animation => {
state.redraw = true;
}
FrameSource::CompareResult => {
state.redraw = false;
}
}

if let Some(tex) = &mut state.current_texture {
Expand Down
4 changes: 2 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl PersistentSettings {
}

// save settings in a thread so we don't block
pub fn save(&self) {
pub fn save_threaded(&self) {
let settings = self.clone();
std::thread::spawn(move || {
_ = save(&settings);
Expand All @@ -112,7 +112,7 @@ impl PersistentSettings {
fn save(s: &PersistentSettings) -> Result<()> {
let local_dir = dirs::data_local_dir().ok_or(anyhow!("Can't get local dir"))?;
let f = File::create(local_dir.join(".oculante"))?;
Ok(serde_json::to_writer_pretty(f, s)?)
Ok(serde_json::to_writer(f, s)?)
}

pub fn set_system_theme(ctx: &Context) {
Expand Down
15 changes: 4 additions & 11 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
load_image_from_path, next_image, prev_image, send_extended_info, set_title, solo_channel,
toggle_fullscreen, unpremult, ColorChannel, ImageExt,
},
FrameSource,
};

const ICON_SIZE: f32 = 24.;
Expand Down Expand Up @@ -177,14 +178,12 @@ impl EguiExt for Ui {
}
}


/// Proof-of-concept funtion to draw texture completely with egui
#[allow(unused)]
pub fn image_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) {
if let Some(texture) = &state.current_texture {
let tex_id = gfx.egui_register_texture(texture);


let image_rect = Rect::from_center_size(
Pos2::new(
state.image_geometry.offset.x
Expand All @@ -207,9 +206,8 @@ pub fn image_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) {
tex_id.id,
image_rect,
Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)),
Color32::WHITE
Color32::WHITE,
);

}

// state.image_geometry.scale;
Expand Down Expand Up @@ -385,22 +383,17 @@ pub fn info_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) {
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.is_loaded = false;
state.current_image = None;
state
.player
.load(&path, state.message_channel.0.clone());
.load_advanced(&path, Some(FrameSource::CompareResult), state.message_channel.0.clone());
state.current_path = Some(path);
state.persistent_settings.keep_view = true;
}
}
if ui.button("Clear").clicked() {
state.compare_list.clear();
}
}
if state.is_loaded {
state.persistent_settings.keep_view = false;
}

});
});

Expand Down
34 changes: 29 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ pub struct Player {
}

impl Player {
/// Create a new Player
pub fn new(image_sender: Sender<Frame>, cache_size: usize, max_texture_size: u32) -> Player {
let (stop_sender, _): (Sender<()>, Receiver<()>) = mpsc::channel();
Player {
Expand Down Expand Up @@ -226,14 +227,23 @@ impl Player {
}
}

pub fn load(&mut self, img_location: &Path, message_sender: Sender<Message>) {
pub fn load_advanced(
&mut self,
img_location: &Path,
forced_frame_source: Option<FrameSource>,
message_sender: Sender<Message>,
) {
debug!("Stopping player on load");
self.stop();
let (stop_sender, stop_receiver): (Sender<()>, Receiver<()>) = mpsc::channel();
self.stop_sender = stop_sender;

if let Some(cached_image) = self.cache.get(img_location) {
_ = self.image_sender.send(Frame::new_still(cached_image));
let mut frame = Frame::new_still(cached_image);
if let Some(fs) = forced_frame_source {
frame.source = fs;
}
_ = self.image_sender.send(frame);
info!("Cache hit for {}", img_location.display());
return;
}
Expand All @@ -244,6 +254,7 @@ impl Player {
message_sender,
stop_receiver,
self.max_texture_size,
forced_frame_source,
);

if let Ok(meta) = std::fs::metadata(img_location) {
Expand All @@ -253,6 +264,10 @@ impl Player {
}
}

pub fn load(&mut self, img_location: &Path, message_sender: Sender<Message>) {
self.load_advanced(img_location, None, message_sender);
}

pub fn stop(&self) {
_ = self.stop_sender.send(());
}
Expand All @@ -264,6 +279,7 @@ pub fn send_image_threaded(
message_sender: Sender<Message>,
stop_receiver: Receiver<()>,
max_texture_size: u32,
forced_frame_source: Option<FrameSource>,
) {
let loc = img_location.to_owned();

Expand All @@ -278,7 +294,10 @@ pub fn send_image_threaded(
// .send(Frame::new_reset(f.buffer.clone()));

let mut first = true;
for f in frame_receiver.iter() {
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() {
info!("Stopped from receiver.");
return;
Expand All @@ -301,6 +320,7 @@ pub fn send_image_threaded(
);

let mut frame = f;

let op = ImageOperation::Resize {
dimensions: new_dimensions,
aspect: true,
Expand Down Expand Up @@ -369,6 +389,7 @@ pub enum FrameSource {
AnimationStart,
Still,
EditResult,
CompareResult,
}

/// A single frame
Expand Down Expand Up @@ -792,9 +813,12 @@ pub fn compare_next(state: &mut OculanteState) {
state.image_geometry = geo.clone();
state.is_loaded = false;
state.current_image = None;
state.player.load(path, state.message_channel.0.clone());
state.player.load_advanced(
path,
Some(FrameSource::CompareResult),
state.message_channel.0.clone(),
);
state.current_path = Some(path.clone());
state.persistent_settings.keep_view = true;
}
}
}
Expand Down

0 comments on commit 4e06ca7

Please sign in to comment.