Skip to content

Commit

Permalink
rename popup to dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Jan 27, 2024
1 parent 361132b commit a6a0dbd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 63 deletions.
38 changes: 21 additions & 17 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ use resize::Resize;

pub mod preferences;

pub mod popup_manager;
pub mod dialog_manager;

use self::{
dialog_manager::{DialogManager, DialogProxy},
image_view::{crop_renderer, image_renderer},
popup_manager::{PopupManager, PopupProxy},
preferences::PREFERENCES,
undo_stack::UndoFrame,
};
Expand All @@ -69,7 +69,7 @@ pub struct App {
pub crop_renderer: crop_renderer::Renderer,
pub image_view: Option<Box<ImageView>>,
pub modifiers: ModifiersState,
popup_manager: PopupManager,
dialog_manager: DialogManager,
size: Vec2<f32>,
fullscreen: bool,
top_bar_size: f32,
Expand Down Expand Up @@ -227,9 +227,9 @@ impl App {
}
Output::Close => {
let close = self
.popup_manager
.dialog_manager
.get_proxy()
.spawn_popup("Unsaved changes", move |ui| {
.spawn_dialog("Unsaved changes", move |ui| {
ui.label(
"You have unsaved changes are you sure you want to close this image?",
);
Expand Down Expand Up @@ -278,9 +278,9 @@ impl App {
}
UserEvent::ErrorMessage(error) => {
let error = error.clone();
self.popup_manager
self.dialog_manager
.get_proxy()
.spawn_popup("Error", move |ui| {
.spawn_dialog("Error", move |ui| {
ui.label(&error);
ui.with_layout(egui::Layout::left_to_right(egui::Align::LEFT), |ui| {
if ui.button("Ok").clicked() {
Expand All @@ -294,11 +294,11 @@ impl App {
}
UserEvent::Exit => {
let exit = self.exit.clone();
let popup_proxy = self.popup_manager.get_proxy();
let dialog_proxy = self.dialog_manager.get_proxy();
if self.op_queue.undo_stack().is_edited() {
thread::spawn(move || {
let close = popup_proxy
.spawn_popup("Unsaved changes", move |ui| {
let close = dialog_proxy
.spawn_dialog("Unsaved changes", move |ui| {
ui.label(
"You have unsaved changes are you sure you want to close this image?",
);
Expand Down Expand Up @@ -374,7 +374,7 @@ impl App {
self.metadata_ui(ctx);
self.crop_ui(ctx);

self.popup_manager.update(ctx, self.size);
self.dialog_manager.update(ctx, self.size);
}

pub fn main_area(&mut self, wgpu: &WgpuState, ctx: &egui::Context) {
Expand Down Expand Up @@ -404,7 +404,7 @@ impl App {
if let Some(ref path) = view.path {
delete(
path.clone(),
self.popup_manager.get_proxy(),
self.dialog_manager.get_proxy(),
self.proxy.clone(),
);
}
Expand Down Expand Up @@ -1117,16 +1117,16 @@ impl App {
}

pub fn new(wgpu: &WgpuState, proxy: EventLoopProxy<UserEvent>, size: [f32; 2]) -> Self {
let popup_manager = PopupManager::new(proxy.clone());
let dialog_manager = DialogManager::new(proxy.clone());
App {
exit: Arc::new(AtomicBool::new(false)),
delay: Duration::MAX,
modifiers: ModifiersState::empty(),
image_renderer: image_renderer::Renderer::new(wgpu),
crop_renderer: crop_renderer::Renderer::new(wgpu),
image_view: None,
op_queue: OpQueue::new(proxy.clone(), popup_manager.get_proxy()),
popup_manager,
op_queue: OpQueue::new(proxy.clone(), dialog_manager.get_proxy()),
dialog_manager,
size: Vec2::from(size),
fullscreen: false,
top_bar_size: TOP_BAR_SIZE,
Expand All @@ -1145,8 +1145,12 @@ impl App {
}
}

pub fn delete(path: std::path::PathBuf, popup_proxy: PopupProxy, proxy: EventLoopProxy<UserEvent>) {
popup_proxy.spawn_popup("Move to trash", move |ui| {
pub fn delete(
path: std::path::PathBuf,
dialog_proxy: DialogProxy,
proxy: EventLoopProxy<UserEvent>,
) {
dialog_proxy.spawn_dialog("Move to trash", move |ui| {
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 Down
52 changes: 26 additions & 26 deletions src/app/popup_manager.rs → src/app/dialog_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,66 @@ use crate::{util::UserEvent, vec2::Vec2};
trait UiClosure: Fn(&mut egui::Ui) -> bool + Send {}
impl<T: Fn(&mut egui::Ui) -> bool + Send> UiClosure for T {}

struct Popup {
struct Dialog {
name: &'static str,
closure: Box<dyn UiClosure>,
}

pub struct PopupManager {
popups: HashMap<usize, Popup>,
receiver: mpsc::Receiver<Popup>,
proxy: PopupProxy,
pub struct DialogManager {
dialogs: HashMap<usize, Dialog>,
receiver: mpsc::Receiver<Dialog>,
proxy: DialogProxy,
}

impl PopupManager {
impl DialogManager {
pub fn new(proxy: EventLoopProxy<UserEvent>) -> Self {
let popups = HashMap::new();
let dialogs = HashMap::new();
let (sender, receiver) = mpsc::channel();

Self {
popups,
dialogs,
receiver,
proxy: PopupProxy { sender, proxy },
proxy: DialogProxy { sender, proxy },
}
}

pub fn get_proxy(&self) -> PopupProxy {
pub fn get_proxy(&self) -> DialogProxy {
self.proxy.clone()
}

pub fn update(&mut self, ctx: &egui::Context, size: Vec2<f32>) {
let mut rng = thread_rng();
while let Ok(popup) = self.receiver.try_recv() {
self.popups.insert(rng.gen(), popup);
while let Ok(dialog) = self.receiver.try_recv() {
self.dialogs.insert(rng.gen(), dialog);
}

for key in self.popups.keys().copied().collect::<Vec<_>>() {
let popup = &self.popups[&key];
for key in self.dialogs.keys().copied().collect::<Vec<_>>() {
let dialog = &self.dialogs[&key];
let mut open = true;
let mut done = false;
egui::Window::new(popup.name)
egui::Window::new(dialog.name)
.id(egui::Id::new(key))
.collapsible(false)
.resizable(false)
.pivot(egui::Align2::CENTER_CENTER)
.default_pos(size / 2.0)
.open(&mut open)
.show(ctx, |ui| done = (popup.closure)(ui));
.show(ctx, |ui| done = (dialog.closure)(ui));
if !open || done {
self.popups.remove(&key);
self.dialogs.remove(&key);
}
}
}
}

#[derive(Clone)]
pub struct PopupProxy {
sender: mpsc::Sender<Popup>,
pub struct DialogProxy {
sender: mpsc::Sender<Dialog>,
proxy: EventLoopProxy<UserEvent>,
}

impl PopupProxy {
pub fn spawn_popup<T, F>(&self, name: &'static str, closure: F) -> PopupHandle<T>
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,
T: 'static + Send,
Expand All @@ -87,20 +87,20 @@ impl PopupProxy {
}
};

let popup = Popup {
let dialog = Dialog {
name,
closure: Box::new(outer),
};

let _ = self.sender.send(popup);
let _ = self.sender.send(dialog);
let _ = self.proxy.send_event(UserEvent::Wake);
PopupHandle(rx)
DialogHandle(rx)
}
}

pub struct PopupHandle<T>(mpsc::Receiver<T>);
pub struct DialogHandle<T>(mpsc::Receiver<T>);

impl<T> PopupHandle<T> {
impl<T> DialogHandle<T> {
pub fn wait(self) -> Option<T> {
self.0.recv().ok()
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl App {
if let Some(ref path) = view.path {
super::delete(
path.clone(),
self.popup_manager.get_proxy(),
self.dialog_manager.get_proxy(),
self.proxy.clone(),
);
}
Expand Down Expand Up @@ -331,7 +331,7 @@ impl App {
&format!("GPU backend: {:?}", info.backend),
);

self.popup_manager.get_proxy().spawn_popup(
self.dialog_manager.get_proxy().spawn_dialog(
"About",
move |ui| -> Option<()> {
ui.label(&about);
Expand Down
16 changes: 8 additions & 8 deletions src/app/op_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use self::imageops::{adjust_saturation_in_place, brighten_in_place};
use super::{
cache::Cache,
clipboard,
dialog_manager::DialogProxy,
image_list::ImageList,
image_view::ImageView,
load_image::{load_from_bytes, load_uncached},
popup_manager::PopupProxy,
save_image,
};
use crate::{
Expand Down Expand Up @@ -98,14 +98,14 @@ pub struct OpQueue {
working: bool,
loading_info: Arc<Mutex<LoadingInfo>>,
proxy: EventLoopProxy<UserEvent>,
popup_proxy: PopupProxy,
dialog_proxy: DialogProxy,
stack: UndoStack,
pub cache: Arc<Cache>,
pub image_list: ImageList,
}

impl OpQueue {
pub fn new(proxy: EventLoopProxy<UserEvent>, popup_proxy: PopupProxy) -> Self {
pub fn new(proxy: EventLoopProxy<UserEvent>, dialog_proxy: DialogProxy) -> Self {
const CACHE_SIZE: usize = 1_000_000_000;
let cache = Arc::new(Cache::new(CACHE_SIZE));
let loading_info = Arc::new(Mutex::new(LoadingInfo::default()));
Expand All @@ -114,7 +114,7 @@ impl OpQueue {
working: false,
image_list: ImageList::new(cache.clone(), proxy.clone(), loading_info.clone()),
loading_info,
popup_proxy,
dialog_proxy,
stack: UndoStack::new(),
proxy,
cache,
Expand Down Expand Up @@ -150,7 +150,7 @@ impl OpQueue {
if let Some(view) = view {
save_image::save(
self.proxy.clone(),
self.popup_proxy.clone(),
self.dialog_proxy.clone(),
path,
view.image_data.clone(),
view.rotation(),
Expand Down Expand Up @@ -288,7 +288,7 @@ impl OpQueue {
let cache = self.cache.clone();
let proxy = self.proxy.clone();
let loading_info = self.loading_info.clone();
let popup_proxy = self.popup_proxy.clone();
let dialog_proxy = self.dialog_proxy.clone();
thread::spawn(move || {
let mut path = path_buf.clone();
if path.is_dir() {
Expand Down Expand Up @@ -319,8 +319,8 @@ impl OpQueue {
}

if edited_prompt {
let close = popup_proxy
.spawn_popup("Unsaved changes", move |ui| {
let close = dialog_proxy
.spawn_dialog("Unsaved changes", move |ui| {
ui.label(
"You have unsaved changes are you sure you want to close this image?",
);
Expand Down
20 changes: 10 additions & 10 deletions src/app/save_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use image::{
use winit::event_loop::EventLoopProxy;

use super::{
dialog_manager::DialogProxy,
op_queue::{Output, UserEventLoopProxyExt},
popup_manager::PopupProxy,
preferences::PREFERENCES,
};
use crate::{
Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn open(name: String, proxy: EventLoopProxy<UserEvent>, wgpu: &WgpuState) {

pub fn save(
proxy: EventLoopProxy<UserEvent>,
popup_proxy: PopupProxy,
dialog_proxy: DialogProxy,
mut path: PathBuf,
image_data: Arc<RwLock<Arc<ImageData>>>,
rotation: i32,
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn save(
let res = match ext.as_str() {
"png" => save_with_format(path, &frames[0], ImageOutputFormat::Png),
"jpg" | "jpeg" | "jpe" | "jif" | "jfif" => {
let quality = match get_jpeg_quality(popup_proxy.clone()) {
let quality = match get_jpeg_quality(dialog_proxy.clone()) {
Some(quality) => quality,
None => {
proxy.send_output(Output::Done);
Expand All @@ -103,7 +103,7 @@ pub fn save(
"tiff" | "tif" => tiff(path, &frames[0]),
"gif" => gif(path, frames),
"webp" => {
let (quality, lossy) = match get_webp_quality(popup_proxy.clone()) {
let (quality, lossy) = match get_webp_quality(dialog_proxy.clone()) {
Some(quality) => quality,
None => {
proxy.send_output(Output::Done);
Expand Down Expand Up @@ -131,9 +131,9 @@ pub fn save(
});
}

pub fn get_jpeg_quality(popup_proxy: PopupProxy) -> Option<u8> {
popup_proxy
.spawn_popup("Export settings", |ui| {
pub fn get_jpeg_quality(dialog_proxy: DialogProxy) -> Option<u8> {
dialog_proxy
.spawn_dialog("Export settings", |ui| {
let mut preferences = PREFERENCES.lock().unwrap();
let mut output = None;
egui::Grid::new("jpeg export settings grid").show(ui, |ui| {
Expand Down Expand Up @@ -167,9 +167,9 @@ pub fn get_jpeg_quality(popup_proxy: PopupProxy) -> Option<u8> {
.flatten()
}

pub fn get_webp_quality(popup_proxy: PopupProxy) -> Option<(f32, bool)> {
popup_proxy
.spawn_popup("Export settings", |ui| {
pub fn get_webp_quality(dialog_proxy: DialogProxy) -> Option<(f32, bool)> {
dialog_proxy
.spawn_dialog("Export settings", |ui| {
let mut preferences = PREFERENCES.lock().unwrap();
let mut output = None;
egui::Grid::new("webp export settings grid").show(ui, |ui| {
Expand Down

0 comments on commit a6a0dbd

Please sign in to comment.