Skip to content

Commit

Permalink
✨(ImageShower): add file drag and drop functionality
Browse files Browse the repository at this point in the history
This update introduces the ability to drag and drop files into the image shower, displaying information about the dropped files and handling their data. The new `ui_file_drag_and_drop` method manages the UI for this feature, including visual feedback during the drag operation and a window to show the details of the dropped files.

Signed-off-by: Benign X <1341398182@qq.com>
  • Loading branch information
W-Mai committed Dec 1, 2024
1 parent 7350fce commit 89d234a
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/image_shower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct MyEguiApp {
height: u32,
image_data: Option<Vec<Color32>>,

dropped_files: Vec<egui::DroppedFile>,

context: AppContext,
}

Expand Down Expand Up @@ -75,6 +77,8 @@ impl MyEguiApp {
height,
image_data,

dropped_files: Default::default(),

context,
}
}
Expand Down Expand Up @@ -196,9 +200,89 @@ impl eframe::App for MyEguiApp {
);
}
});

self.ui_file_drag_and_drop(ctx);
}

fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, &self.context);
}
}

impl MyEguiApp {
fn ui_file_drag_and_drop(&mut self, ctx: &egui::Context) {
use std::fmt::Write as _;

if !ctx.input(|i| i.raw.hovered_files.is_empty()) {
let text = ctx.input(|i| {
let mut text = "Dropping files:\n".to_owned();
for file in &i.raw.hovered_files {
if let Some(path) = &file.path {
write!(text, "\n{}", path.display()).ok();
} else if !file.mime.is_empty() {
write!(text, "\n{}", file.mime).ok();
} else {
text += "\n???";
}
}
text
});

let painter = ctx.layer_painter(egui::LayerId::new(
egui::Order::Foreground,
egui::Id::new("file_drop_target"),
));

let screen_rect = ctx.screen_rect();
painter.rect_filled(screen_rect, 0.0, Color32::from_black_alpha(192));
painter.text(
screen_rect.center(),
egui::Align2::CENTER_CENTER,
text,
egui::TextStyle::Heading.resolve(&ctx.style()),
Color32::WHITE,
);
}

// Collect dropped files:
ctx.input(|i| {
if !i.raw.dropped_files.is_empty() {
self.dropped_files = i.raw.dropped_files.clone();
}
});

// Show dropped files (if any):
if !self.dropped_files.is_empty() {
let mut open = true;
egui::Window::new("Dropped files")
.open(&mut open)
.show(ctx, |ui| {
for file in &self.dropped_files {
let mut info = if let Some(path) = &file.path {
path.display().to_string()
} else if !file.name.is_empty() {
file.name.clone()
} else {
"???".to_owned()
};

let mut additional_info = vec![];
if !file.mime.is_empty() {
additional_info.push(format!("type: {}", file.mime));
}
if let Some(bytes) = &file.bytes {
additional_info.push(format!("{} bytes", bytes.len()));
}
if !additional_info.is_empty() {
info += &format!(" ({})", additional_info.join(", "));
}

ui.label(info);
}
});
if !open {
self.dropped_files.clear();
}
}
}
}

0 comments on commit 89d234a

Please sign in to comment.