confirm dialog when opening rom if emulator already running
This commit is contained in:
parent
977cd30040
commit
6f23ed0377
2 changed files with 48 additions and 2 deletions
|
@ -14,6 +14,8 @@ use cacao::{
|
|||
Event, EventMask, EventMonitor,
|
||||
},
|
||||
core_foundation::base::TCFTypeRef,
|
||||
filesystem::ModalResponse,
|
||||
foundation::{NSInteger, NSString},
|
||||
};
|
||||
use cpal::Stream;
|
||||
use frontend_common::{
|
||||
|
@ -24,6 +26,11 @@ use gb_emu_lib::{
|
|||
connect::{EmulatorMessage, JoypadButtons, JoypadState, RendererMessage, ResolutionData},
|
||||
renderer::{RendererBackend, RendererBackendManager, WindowOptions},
|
||||
};
|
||||
use objc::{
|
||||
class, msg_send, msg_send_id,
|
||||
rc::{Id, Owned},
|
||||
runtime::Object,
|
||||
};
|
||||
use raw_window_handle::{
|
||||
AppKitDisplayHandle, AppKitWindowHandle, HasRawDisplayHandle, HasRawWindowHandle,
|
||||
RawDisplayHandle, RawWindowHandle,
|
||||
|
@ -184,6 +191,43 @@ impl CacaoWindowManager {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn can_open_new_rom(&mut self) -> bool {
|
||||
if if let Some(handles) = &self.handles {
|
||||
handles.sender.send(EmulatorMessage::Pause).unwrap();
|
||||
let title = NSString::new("Emulator running!");
|
||||
let message = NSString::new("Stopping may result in lost data");
|
||||
let close = NSString::new("Continue");
|
||||
let continue_ = NSString::new("Stop");
|
||||
let escape_keycode = NSString::new("\x1b");
|
||||
|
||||
let out: ModalResponse = unsafe {
|
||||
let mut alert: Id<Object, Owned> = msg_send_id![class!(NSAlert), new];
|
||||
let _: () = msg_send![&mut alert, setMessageText: &*title];
|
||||
let _: () = msg_send![&mut alert, setInformativeText: &*message];
|
||||
let _: () = msg_send![&mut alert, addButtonWithTitle: &*continue_];
|
||||
let close_button: cacao::foundation::id =
|
||||
msg_send![&mut alert, addButtonWithTitle: &*close];
|
||||
let _: () = msg_send![&mut *close_button, setKeyEquivalent: &*escape_keycode];
|
||||
|
||||
let out: NSInteger = msg_send![&*alert, runModal];
|
||||
out.into()
|
||||
};
|
||||
|
||||
handles.sender.send(EmulatorMessage::Start).unwrap();
|
||||
matches!(out, ModalResponse::FirstButtonReturned)
|
||||
} else {
|
||||
true
|
||||
} {
|
||||
let _ = self.handles.take();
|
||||
for (_, w) in self.windows.drain() {
|
||||
w.close();
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn message(&mut self, message: EmuWindowMessage) {
|
||||
match message {
|
||||
EmuWindowMessage::Closing { window_type } => {
|
||||
|
|
|
@ -56,7 +56,7 @@ pub(crate) struct TwincUiApp {
|
|||
|
||||
impl TwincUiApp {
|
||||
fn open_dialog(&self) {
|
||||
if self.current_game.read().unwrap().is_emulator_running() {
|
||||
if !self.current_game.write().unwrap().can_open_new_rom() {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,9 @@ impl AppDelegate for TwincUiApp {
|
|||
if let Some(url) = urls.first() {
|
||||
if url.scheme() == "file" {
|
||||
if let Ok(path) = url.to_file_path() {
|
||||
dispatch(AppMessage::Core(CoreMessage::OpenRom(path)));
|
||||
if self.current_game.write().unwrap().can_open_new_rom() {
|
||||
dispatch(AppMessage::Core(CoreMessage::OpenRom(path)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue