Skip to content

Commit

Permalink
add enter shortcut to dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Jan 28, 2024
1 parent f5b12f9 commit 9c4894b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
29 changes: 24 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl App {
let close = self
.dialog_manager
.get_proxy()
.spawn_dialog("Unsaved changes", move |ui| {
.spawn_dialog("Unsaved changes", move |ui, enter| {
ui.label(
"You have unsaved changes are you sure you want to close this image?",
);
Expand All @@ -249,6 +249,11 @@ impl App {
return Some(false);
}

if *enter {
*enter = false;
return Some(true);
}

None
})
.inner
Expand Down Expand Up @@ -287,11 +292,14 @@ impl App {
let error = error.clone();
self.dialog_manager
.get_proxy()
.spawn_dialog("Error", move |ui| {
.spawn_dialog("Error", move |ui, enter| {
ui.label(&error);
ui.with_layout(egui::Layout::left_to_right(egui::Align::LEFT), |ui| {
if ui.button("Ok").clicked() {
Some(())
} else if *enter {
*enter = false;
Some(())
} else {
None
}
Expand All @@ -305,7 +313,7 @@ impl App {
if self.op_queue.undo_stack().is_edited() {
thread::spawn(move || {
let close = dialog_proxy
.spawn_dialog("Unsaved changes", move |ui| {
.spawn_dialog("Unsaved changes", move |ui, enter| {
ui.label(
"You have unsaved changes are you sure you want to close this image?",
);
Expand All @@ -320,6 +328,11 @@ impl App {
return Some(false);
}

if *enter {
*enter = false;
return Some(true);
}

None
},
)
Expand Down Expand Up @@ -381,7 +394,7 @@ impl App {
self.metadata_ui(ctx);
self.crop_ui(ctx);

self.dialog_manager.update(ctx, self.size);
self.dialog_manager.update(ctx, self.size, &mut self.enter);
}

pub fn main_area(&mut self, wgpu: &WgpuState, ctx: &egui::Context) {
Expand Down Expand Up @@ -1146,7 +1159,7 @@ pub fn delete(
dialog_proxy: DialogProxy,
proxy: EventLoopProxy<UserEvent>,
) {
dialog_proxy.spawn_dialog("Move to trash", move |ui| {
dialog_proxy.spawn_dialog("Move to trash", move |ui, enter| {
ui.label("Are you sure you want to move this to trash?");

ui.with_layout(egui::Layout::left_to_right(egui::Align::LEFT), |ui| {
Expand All @@ -1159,6 +1172,12 @@ pub fn delete(
return Some(());
}

if *enter {
*enter = false;
let _ = proxy.send_event(UserEvent::QueueDelete(path.clone()));
return Some(());
}

None
})
.inner
Expand Down
14 changes: 7 additions & 7 deletions src/app/dialog_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use winit::event_loop::EventLoopProxy;

use crate::util::{p2, UserEvent};

trait UiClosure: Fn(&mut egui::Ui) -> bool + Send {}
impl<T: Fn(&mut egui::Ui) -> bool + Send> UiClosure for T {}
trait UiClosure: Fn(&mut egui::Ui, &mut bool) -> bool + Send {}
impl<T: Fn(&mut egui::Ui, &mut bool) -> bool + Send> UiClosure for T {}

struct Dialog {
name: &'static str,
Expand Down Expand Up @@ -39,7 +39,7 @@ impl DialogManager {
self.proxy.clone()
}

pub fn update(&mut self, ctx: &egui::Context, size: Vector2<f32>) {
pub fn update(&mut self, ctx: &egui::Context, size: Vector2<f32>, enter: &mut bool) {
let mut rng = thread_rng();
while let Ok(dialog) = self.receiver.try_recv() {
self.dialogs.insert(rng.gen(), dialog);
Expand All @@ -56,7 +56,7 @@ impl DialogManager {
.pivot(egui::Align2::CENTER_CENTER)
.default_pos(p2(Point2::from_vec(size / 2.0)))
.open(&mut open)
.show(ctx, |ui| done = (dialog.closure)(ui));
.show(ctx, |ui| done = (dialog.closure)(ui, enter));
if !open || done {
self.dialogs.remove(&key);
}
Expand All @@ -73,12 +73,12 @@ pub struct DialogProxy {
impl DialogProxy {
pub fn spawn_dialog<T, F>(&self, name: &'static str, closure: F) -> DialogHandle<T>
where
F: Fn(&mut egui::Ui) -> Option<T> + Send + 'static,
F: Fn(&mut egui::Ui, &mut bool) -> Option<T> + Send + 'static,
T: 'static + Send,
{
let (tx, rx) = mpsc::channel();
let outer = move |ui: &mut egui::Ui| {
let value = (closure)(ui);
let outer = move |ui: &mut egui::Ui, enter: &mut bool| {
let value = (closure)(ui, enter);
match value {
Some(value) => {
let _ = tx.send(value);
Expand Down
14 changes: 12 additions & 2 deletions src/app/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ impl App {
)
.clicked()
{
save_image::open(self.current_filename.clone(), self.proxy.clone(), wgpu);
if let Some(image) = self.image_view.as_ref() {
if let Some(path) = &image.path {
let buf = path.to_path_buf();
self.queue(Op::LoadPath(buf, false));
}
}
ui.close_menu();
}

Expand Down Expand Up @@ -333,9 +338,14 @@ impl App {

self.dialog_manager.get_proxy().spawn_dialog(
"About",
move |ui| -> Option<()> {
move |ui, enter| -> Option<()> {
ui.label(&about);

if *enter {
*enter = false;
return Some(());
}

ui.button("Ok").clicked().then_some(())
},
);
Expand Down
7 changes: 6 additions & 1 deletion src/app/op_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl OpQueue {

if edited_prompt {
let close = dialog_proxy
.spawn_dialog("Unsaved changes", move |ui| {
.spawn_dialog("Unsaved changes", move |ui, enter| {
ui.label(
"You have unsaved changes are you sure you want to close this image?",
);
Expand All @@ -334,6 +334,11 @@ impl OpQueue {
return Some(false);
}

if *enter {
*enter = false;
return Some(true);
}

None
})
.inner
Expand Down
14 changes: 12 additions & 2 deletions src/app/save_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn save(

pub fn get_jpeg_quality(dialog_proxy: DialogProxy) -> Option<u8> {
dialog_proxy
.spawn_dialog("Export settings", |ui| {
.spawn_dialog("Export settings", |ui, enter| {
let mut preferences = PREFERENCES.lock().unwrap();
let mut output = None;
egui::Grid::new("jpeg export settings grid").show(ui, |ui| {
Expand All @@ -160,6 +160,11 @@ pub fn get_jpeg_quality(dialog_proxy: DialogProxy) -> Option<u8> {
}
},
);

if *enter {
*enter = false;
output = Some(Some(preferences.jpeg_quality))
}
});
output
})
Expand All @@ -169,7 +174,7 @@ pub fn get_jpeg_quality(dialog_proxy: DialogProxy) -> Option<u8> {

pub fn get_webp_quality(dialog_proxy: DialogProxy) -> Option<(f32, bool)> {
dialog_proxy
.spawn_dialog("Export settings", |ui| {
.spawn_dialog("Export settings", |ui, enter| {
let mut preferences = PREFERENCES.lock().unwrap();
let mut output = None;
egui::Grid::new("webp export settings grid").show(ui, |ui| {
Expand Down Expand Up @@ -205,6 +210,11 @@ pub fn get_webp_quality(dialog_proxy: DialogProxy) -> Option<(f32, bool)> {
}
},
);

if *enter {
*enter = false;
output = Some(Some((preferences.webp_quality, preferences.webp_lossy)))
}
});
output
})
Expand Down

0 comments on commit 9c4894b

Please sign in to comment.