Skip to content

Commit

Permalink
fix: zoom state
Browse files Browse the repository at this point in the history
  • Loading branch information
fireyy committed Nov 22, 2023
1 parent 2bc4872 commit 3ffed27
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 42 deletions.
19 changes: 1 addition & 18 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 @@ -38,6 +38,7 @@ serde = { workspace = true }
egui = { workspace = true }
cc_files = { workspace = true }
egui_extras = { workspace = true }
image = { version = "0.24.6", default-features = false, features = ["png", "jpeg", "gif", "webp"] }
rfd = "0.10"
egui-notify = "0.6"
tracing = { workspace = true }
Expand Down
9 changes: 1 addition & 8 deletions crates/cc_files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
egui_extras = { workspace = true }
reqwest = { workspace = true }
tokio-stream = { version = "0.1.11", default-features = false }
image = { version = "0.24.6", default-features = false, features = ["png", "jpeg", "gif", "webp"] }
egui = { workspace = true }
cc_runtime = { workspace = true }
tokio = "1.22.0"
anyhow = "1.0.66"
serde = { workspace = true }
enum-map = { version = "2", features = ["serde"] }
infer = { workspace = true }
tracing = { workspace = true }
crossbeam-channel = { workspace = true }
tracing = { workspace = true }
34 changes: 18 additions & 16 deletions src/widgets/file_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use cc_storage::Object;
use super::confirm::ConfirmAction;
use super::toasts::ToastKind;
use crate::global;
use crate::util;
use cc_ui::icon;

#[derive(PartialEq)]
Expand All @@ -21,8 +20,6 @@ pub struct FileView {
pub is_preview: bool,
pub img_zoom: f32,
pub img_default_zoom: f32,
pub img_scroll: Option<eframe::emath::Pos2>,
pub disp_rect: util::Rect,
}

impl FileView {
Expand All @@ -31,8 +28,6 @@ impl FileView {
is_preview: false,
img_zoom: 1.0,
img_default_zoom: 1.0,
img_scroll: Some(eframe::emath::Pos2::new(0.0, 0.0)),
disp_rect: util::Rect::default(),
}
}

Expand Down Expand Up @@ -73,11 +68,26 @@ impl FileView {
.auto_shrink([false; 2])
.show(ui, |ui| {
if !url.is_empty() {
let mut size: egui::Vec2 = egui::Vec2::ZERO;
let mut image = egui::Image::from_uri(url);
image = image.fit_to_original_size(self.img_zoom);
if let Ok(img) =
image.load_for_size(ui.ctx(), ui.available_size())
{
if let Some(size2) = img.size() {
self.img_default_zoom =
if size2.x > ui.available_width() {
ui.available_width() / size2.x
} else {
1.0
};
size = size2;
}
}
if self.img_zoom == 1.0 {
image = image.max_size(ui.available_size());
self.img_zoom = self.img_default_zoom;
}
size *= self.img_zoom;
image = image.fit_to_exact_size(size);
ui.add_sized(ui.available_size(), image);
}
});
Expand Down Expand Up @@ -128,7 +138,7 @@ impl FileView {
.on_hover_text("Zoom to window size")
.clicked()
{
self.img_zoom = 1.0;
self.img_zoom = self.img_default_zoom;
}
if ui
.button(icon::ZOOM_OUT)
Expand Down Expand Up @@ -219,11 +229,3 @@ impl FileView {
self.is_preview = true;
}
}

// pub fn create_image(ctx: &egui::Context, buf: &Buffer) -> TextureHandle {
// let (size, pixels) =
// buf.render_to_rgba(Rectangle::from(0, 0, buf.get_width(), buf.get_height()));
// let color_image =
// ColorImage::from_rgba_premultiplied([size.width as usize, size.height as usize], &pixels);
// ctx.load_texture("my_texture", color_image, TextureOptions::NEAREST)
// }

0 comments on commit 3ffed27

Please sign in to comment.