mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 12:50:04 -04:00
Revert change to winit event loop in egui_glium (#756)
* Revert change to winit event loop in egui_glium This reverts https://github.com/emilk/egui/pull/631 Fixes https://github.com/emilk/egui/issues/755 * Add example of file dialogs and file drag-and-drop * fix ci
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# Changelog for eframe
|
||||
All notable changes to the `eframe` and `epi` crates.
|
||||
|
||||
NOTE: [`egui_web`](egui_web/CHANGELOG.md), [`egui-winit`](egui-winit/CHANGELOG.md) and [`egui_glium`](egui_glium/CHANGELOG.md) have their own changelogs!
|
||||
|
||||
|
||||
## Unreleased
|
||||
* `Frame` now provides `set_decorations` to set whether to show window decorations.
|
||||
|
||||
@@ -36,6 +36,7 @@ egui_web = { version = "0.14.0", path = "../egui_web", default-features = false
|
||||
|
||||
[dev-dependencies]
|
||||
image = { version = "0.23", default-features = false, features = ["png"] }
|
||||
rfd = "0.5.0"
|
||||
|
||||
[features]
|
||||
default = ["default_fonts"]
|
||||
|
||||
100
eframe/examples/file_dialog.rs
Normal file
100
eframe/examples/file_dialog.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use eframe::{egui, epi};
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {
|
||||
dropped_files: Vec<egui::DroppedFile>,
|
||||
picked_path: Option<String>,
|
||||
}
|
||||
|
||||
impl epi::App for MyApp {
|
||||
fn name(&self) -> &str {
|
||||
"Native file dialogs and drag-and-drop files"
|
||||
}
|
||||
|
||||
fn update(&mut self, ctx: &egui::CtxRef, _frame: &mut epi::Frame<'_>) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.label("Drag-and-drop files onto the window!");
|
||||
|
||||
if cfg!(target_os = "macos") {
|
||||
// Awaiting fix of winit bug: https://github.com/rust-windowing/winit/pull/2027
|
||||
} else if ui.button("Open file…").clicked() {
|
||||
if let Some(path) = rfd::FileDialog::new().pick_file() {
|
||||
self.picked_path = Some(path.display().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(picked_path) = &self.picked_path {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Picked file:");
|
||||
ui.monospace(picked_path);
|
||||
});
|
||||
}
|
||||
|
||||
// Show dropped files (if any):
|
||||
if !self.dropped_files.is_empty() {
|
||||
ui.group(|ui| {
|
||||
ui.label("Dropped files:");
|
||||
|
||||
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()
|
||||
};
|
||||
if let Some(bytes) = &file.bytes {
|
||||
info += &format!(" ({} bytes)", bytes.len());
|
||||
}
|
||||
ui.label(info);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
self.detect_files_being_dropped(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
impl MyApp {
|
||||
fn detect_files_being_dropped(&mut self, ctx: &egui::CtxRef) {
|
||||
use egui::*;
|
||||
|
||||
// Preview hovering files:
|
||||
if !ctx.input().raw.hovered_files.is_empty() {
|
||||
let mut text = "Dropping files:\n".to_owned();
|
||||
for file in &ctx.input().raw.hovered_files {
|
||||
if let Some(path) = &file.path {
|
||||
text += &format!("\n{}", path.display());
|
||||
} else if !file.mime.is_empty() {
|
||||
text += &format!("\n{}", file.mime);
|
||||
} else {
|
||||
text += "\n???";
|
||||
}
|
||||
}
|
||||
|
||||
let painter =
|
||||
ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("file_drop_target")));
|
||||
|
||||
let screen_rect = ctx.input().screen_rect();
|
||||
painter.rect_filled(screen_rect, 0.0, Color32::from_black_alpha(192));
|
||||
painter.text(
|
||||
screen_rect.center(),
|
||||
Align2::CENTER_CENTER,
|
||||
text,
|
||||
TextStyle::Heading,
|
||||
Color32::WHITE,
|
||||
);
|
||||
}
|
||||
|
||||
// Collect dropped files:
|
||||
if !ctx.input().raw.dropped_files.is_empty() {
|
||||
self.dropped_files = ctx.input().raw.dropped_files.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let options = eframe::NativeOptions::default();
|
||||
eframe::run_native(Box::new(MyApp::default()), options);
|
||||
}
|
||||
@@ -61,6 +61,6 @@ pub fn start_web(canvas_id: &str, app: Box<dyn epi::App>) -> Result<(), wasm_bin
|
||||
|
||||
/// Call from `fn main` like this: `eframe::run_native(Box::new(MyEguiApp::default()))`
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn run_native(app: Box<dyn epi::App>, native_options: epi::NativeOptions) {
|
||||
pub fn run_native(app: Box<dyn epi::App>, native_options: epi::NativeOptions) -> ! {
|
||||
egui_glium::run(app, &native_options)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user