Skip to content

Commit

Permalink
make sure app is exited
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Sep 3, 2023
1 parent 7dc55d3 commit 614a8a3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum AppEvent {
pub(crate) enum UserEvent {
AppUpdate,
Idle,
QuitApp,
}

pub(crate) enum AppUpdateEvent {
Expand Down Expand Up @@ -145,3 +146,9 @@ impl Application {
}
}
}

pub fn quit_app() {
Application::with_event_loop_proxy(|proxy| {
let _ = proxy.send_event(UserEvent::QuitApp);
});
}
30 changes: 19 additions & 11 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ impl ApplicationHandle {
UserEvent::Idle => {
self.idle();
}
UserEvent::QuitApp => {
control_flow.set_exit();
}
}
}

Expand All @@ -61,7 +64,7 @@ impl ApplicationHandle {
self.new_window(event_loop, view_fn, config)
}
AppUpdateEvent::CloseWindow { window_id } => {
self.close_window(window_id);
self.close_window(window_id, control_flow);
}
AppUpdateEvent::RequestTimer { timer } => {
self.request_timer(timer, control_flow);
Expand All @@ -85,8 +88,7 @@ impl ApplicationHandle {
&mut self,
window_id: winit::window::WindowId,
event: WindowEvent,
#[cfg(target_os = "macos")] _control_flow: &mut ControlFlow,
#[cfg(not(target_os = "macos"))] control_flow: &mut ControlFlow,
control_flow: &mut ControlFlow,
) {
let window_handle = match self.window_handles.get_mut(&window_id) {
Some(window_handle) => window_handle,
Expand All @@ -106,15 +108,10 @@ impl ApplicationHandle {
window_handle.position(point);
}
WindowEvent::CloseRequested => {
window_handle.window = None;
self.close_window(window_id, control_flow);
}
WindowEvent::Destroyed => {
window_handle.destroy();
self.window_handles.remove(&window_id);
#[cfg(not(target_os = "macos"))]
if self.window_handles.is_empty() {
control_flow.set_exit();
}
self.close_window(window_id, control_flow);
}
WindowEvent::DroppedFile(_) => {}
WindowEvent::HoveredFile(_) => {}
Expand Down Expand Up @@ -208,9 +205,20 @@ impl ApplicationHandle {
self.window_handles.insert(window_id, window_handle);
}

fn close_window(&mut self, window_id: WindowId) {
fn close_window(
&mut self,
window_id: WindowId,
#[cfg(target_os = "macos")] _control_flow: &mut ControlFlow,
#[cfg(not(target_os = "macos"))] control_flow: &mut ControlFlow,
) {
if let Some(handle) = self.window_handles.get_mut(&window_id) {
handle.window = None;
handle.destroy();
}
self.window_handles.remove(&window_id);
#[cfg(not(target_os = "macos"))]
if self.window_handles.is_empty() {
control_flow.set_exit();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub mod views;
pub mod window;
mod window_handle;

pub use app::{launch, AppEvent, Application};
pub use app::{launch, quit_app, AppEvent, Application};
pub use context::ViewContext;
pub use floem_reactive as reactive;
pub use floem_renderer::cosmic_text;
Expand Down
13 changes: 5 additions & 8 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
sync::Arc,
time::{Duration, Instant},
};
use std::time::{Duration, Instant};

use floem_reactive::{with_scope, RwSignal, Scope};
use floem_renderer::Renderer;
Expand Down Expand Up @@ -45,7 +42,7 @@ use crate::{
/// - processing all requests to update the animation state from the reactive system
/// - requesting a new animation frame from the backend
pub(crate) struct WindowHandle {
pub(crate) window: Option<Arc<winit::window::Window>>,
pub(crate) window: Option<winit::window::Window>,
/// Reactive Scope for this WindowHandle
scope: Scope,
pub(crate) view: Box<dyn View>,
Expand All @@ -66,7 +63,6 @@ impl WindowHandle {
window: winit::window::Window,
view_fn: impl FnOnce(winit::window::WindowId) -> Box<dyn View> + 'static,
) -> Self {
let window = Arc::new(window);
let window_id = window.id();
let id = Id::next();
set_current_view(id);
Expand Down Expand Up @@ -892,8 +888,9 @@ impl WindowHandle {

#[cfg(target_os = "linux")]
fn show_context_menu(&self, menu: Menu, _platform_menu: winit::menu::Menu, pos: Option<Point>) {
self.context_menu
.set(Some((menu, pos.unwrap_or(self.cursor_position))));
let pos = pos.unwrap_or(self.cursor_position);
let pos = Point::new(pos.x / self.app_state.scale, pos.y / self.app_state.scale);
self.context_menu.set(Some((menu, pos)));
}

pub(crate) fn menu_action(&mut self, id: usize) {
Expand Down

0 comments on commit 614a8a3

Please sign in to comment.