use super::*;
impl Runtime {
pub(super) fn dispatch(
&mut self,
event_loop: &ActiveEventLoop,
event: Event,
force_redraw: bool,
) -> bool {
let mut cx = self.event_context();
let Some(window) = &mut self.window else {
return false;
};
window.view.event(&event, &mut cx);
self.apply_event_context(event_loop, cx, force_redraw, true)
}
pub(super) fn apply_event_context(
&mut self,
event_loop: &ActiveEventLoop,
mut cx: EventContext,
force_redraw: bool,
announce_focus: bool,
) -> bool {
let relaunch_requested = cx.relaunch.is_some();
if let Some(request) = cx.relaunch.take() {
self.relaunch_request = Some(request);
}
if cx.exit && !self.quit_phase_active {
self.pending_quit = Some(if relaunch_requested {
QuitReason::Relaunch
} else {
QuitReason::Explicit
});
return false;
}
let entity_notifications = std::mem::take(&mut cx.entity_notifications);
let notify_all_entities = cx.notify_all_entities;
let global_notifications = std::mem::take(&mut cx.global_notifications);
let notify_all_globals = cx.notify_all_globals;
if !enqueue_global_notifications(
&mut self.pending_global_notifications,
&mut self.pending_global_notification_types,
&mut self.pending_all_globals,
&global_notifications,
notify_all_globals,
) {
self.fail(
event_loop,
AppError::View(format!(
"one effect cycle cannot retain more than {MAX_PENDING_GLOBAL_NOTIFICATIONS} pending global notifications"
)),
);
return false;
}
if !enqueue_entity_events(&mut self.pending_entity_events, &mut cx.entity_events) {
self.fail(
event_loop,
AppError::View(format!(
"one effect cycle cannot retain more than {MAX_PENDING_ENTITY_EVENTS} pending entity events"
)),
);
return false;
}
if self.targeted_actions.len() + cx.targeted_actions.len()
> crate::MAX_PENDING_TARGETED_ACTIONS
{
self.fail(
event_loop,
AppError::View(format!(
"one effect cycle cannot retain more than {} cross-window actions",
crate::MAX_PENDING_TARGETED_ACTIONS
)),
);
return false;
}
self.targeted_actions.extend(cx.targeted_actions.drain(..));
for request in &mut cx.open_windows {
let Some(anchor) = request.popover_anchor_element else {
continue;
};
let Some(bounds) = self
.window
.as_ref()
.and_then(|state| state.ui.element_bounds(anchor))
else {
self.fail(
event_loop,
AppError::View(format!(
"system popover anchor {anchor:?} is not mounted in its parent window"
)),
);
return false;
};
let Some(popover) = request.options.popover.as_mut() else {
self.fail(
event_loop,
AppError::Window(WindowCommandError::InvalidPopoverConfiguration.to_string()),
);
return false;
};
popover.anchor_rect = bounds;
}
self.pending_windows.extend(cx.open_windows.drain(..));
if cx.close_current_window
&& let Some(handle) = self.current_handle()
{
self.close_requests.push(handle);
}
self.close_requests.append(&mut cx.close_windows);
self.focus_requests.append(&mut cx.focus_windows);
self.invalidate_requests.append(&mut cx.invalidate_windows);
if self.window_commands.len() + cx.window_commands.len() > MAX_PENDING_WINDOW_COMMANDS {
self.fail(
event_loop,
AppError::View(format!(
"one effect cycle cannot retain more than {MAX_PENDING_WINDOW_COMMANDS} pending window commands"
)),
);
return false;
}
self.window_commands.append(&mut cx.window_commands);
enqueue_platform_requests(&mut self.platform_requests, &mut cx.platform_requests);
let actions = std::mem::take(&mut cx.actions);
let form_submissions = std::mem::take(&mut cx.form_submissions);
let menus = cx.menus.take();
let window_menus = cx.window_menus.take();
let native_popup_menus = std::mem::take(&mut cx.native_popup_menus);
let mut focus_changed = false;
if let Some(state) = &mut self.window {
let previous_focus = state.ui.focused();
let mut deferred_focus = false;
let selection_changed =
cx.clear_text_selection && state.ui.clear_static_text_selection();
if let Some(request) = cx.focus {
match request {
Some(id) => {
if state.ui.is_focusable(id) {
state.pending_focus = None;
state.ui.focus(id);
} else {
state.pending_focus = Some(id);
state.view_dirty = true;
deferred_focus = true;
}
}
None => {
state.pending_focus = None;
state.ui.blur();
}
}
}
focus_changed = previous_focus != state.ui.focused();
if focus_changed {
self.pending_input = None;
}
#[cfg(target_os = "macos")]
if focus_changed
&& state.ui.focused().is_some()
&& let Some(host) = &state.native_host
{
host.focus_framework();
}
if cx.invalidate {
state.view_dirty = true;
}
if (force_redraw
|| cx.invalidate
|| focus_changed
|| deferred_focus
|| selection_changed)
&& state.scheduler.invalidate()
{
state.window.request_redraw();
}
}
if focus_changed && announce_focus {
let focused = self.window.as_ref().and_then(|state| state.ui.focused());
let mut focus_cx = self.event_context();
if let Some(window) = &mut self.window {
window
.view
.event(&Event::FocusChanged(focused), &mut focus_cx);
}
if !self.apply_event_context(event_loop, focus_cx, false, false) {
return false;
}
}
#[cfg(target_os = "macos")]
if focus_changed {
self.sync_native_menu_state();
}
if let Some(menus) = menus
&& !self.replace_menus(event_loop, menus)
{
return false;
}
if let Some(window_menus) = window_menus
&& !self.replace_current_window_menus(event_loop, window_menus)
{
return false;
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
for popup in native_popup_menus {
if !self.show_current_native_popup_menu(event_loop, popup) {
return false;
}
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
debug_assert!(native_popup_menus.is_empty());
for action in actions {
if self.invoke_action(event_loop, &action).is_none() {
return false;
}
}
for form in form_submissions {
if !self.invoke_form_submission(event_loop, form, None) {
return false;
}
}
self.invalidate_entity_observers(&entity_notifications, notify_all_entities);
self.invalidate_global_observers(&global_notifications, notify_all_globals);
true
}
pub(super) fn finalize_process_services(&mut self) {
if self.process_services_finalized {
return;
}
self.process_services_finalized = true;
global_shortcut::clear_global_shortcut_handler_proxy();
tray::clear_tray_handler_proxy();
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
self.global_shortcuts.clear();
self.tray_icons.clear();
#[cfg(any(
target_os = "macos",
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
{
self.single_instance.take();
}
#[cfg(target_os = "macos")]
{
self.menu_host.take();
self.mac_application_host.take();
for (_, dialog) in self.active_platform_dialogs.drain() {
dialog.native.cancel();
}
}
#[cfg(target_os = "windows")]
{
self.windows_menu_host.take();
self._windows_power_monitor.take();
}
#[cfg(target_os = "linux")]
{
self._linux_power_monitor.take();
}
#[cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
self.active_platform_dialogs.clear();
self.foreground_tasks.shutdown();
self.background_tasks.shutdown();
self.image_workers.shutdown();
}
pub(super) fn invoke_action(
&mut self,
event_loop: &ActiveEventLoop,
action: &AnyAction,
) -> Option<bool> {
let Some(window) = &mut self.window else {
return Some(false);
};
let path = window.ui.focus_path();
let mut dispatch = std::mem::take(&mut window.action_dispatch_scratch);
window
.ui
.collect_action_dispatch(&path, action.type_id(), &mut dispatch);
for binding in dispatch.iter().copied() {
let listener = self
.window
.as_ref()
.and_then(|window| window.listeners.action_listener(binding.key));
let Some(listener) = listener else {
continue;
};
let mut cx = self.event_context();
if let Some(window) = &mut self.window {
listener(window.view.as_any_mut(), action.as_any(), &mut cx);
}
let propagate = cx.propagate_action;
let stopped = cx.stop_event_propagation;
if !self.apply_event_context(event_loop, cx, false, true) {
return None;
}
let consumed = match binding.phase {
crate::DispatchPhase::Capture => stopped,
crate::DispatchPhase::Bubble => !propagate,
};
if consumed {
dispatch.clear();
if let Some(window) = &mut self.window {
window.action_dispatch_scratch = dispatch;
}
return Some(true);
}
}
dispatch.clear();
if let Some(window) = &mut self.window {
window.action_dispatch_scratch = dispatch;
}
Some(false)
}
pub(super) fn action_available(&self, action: &AnyAction) -> bool {
let Some(window) = &self.window else {
return false;
};
let path = window.ui.focus_path();
window.ui.action_available(&path, action.type_id())
}
#[cfg(target_os = "macos")]
pub(super) fn invoke_dock_menu_action(
&mut self,
event_loop: &ActiveEventLoop,
action_id: usize,
) {
let item = self
.dock_menu_actions
.get(action_id)
.filter(|item| !item.disabled)
.map(|item| (item.action.clone(), item.os_action));
let Some((action, os_action)) = item else {
return;
};
let target = self
.active_window
.or_else(|| self.focus_history.last().copied());
if let Some(target) = target
&& !self.activate_window(target)
{
return;
}
let handled = action
.as_ref()
.is_some_and(|action| self.invoke_action(event_loop, action).unwrap_or(true));
if !handled && let Some(os_action) = os_action {
self.invoke_os_action(event_loop, os_action);
}
if target.is_some() {
self.deactivate_window();
}
self.process_window_commands(event_loop);
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(super) fn active_menu_declaration(&self) -> &[Menu] {
let Some(active) = self.active_window else {
return &self.menus;
};
if self.current_window.is_some_and(|(id, _)| id == active) {
return self.config.window_menus.as_deref().unwrap_or(&self.menus);
}
self.windows
.get(&active)
.and_then(|entry| entry.config.window_menus.as_deref())
.unwrap_or(&self.menus)
}
#[cfg(target_os = "macos")]
pub(super) fn install_active_mac_menu(&mut self, event_loop: &ActiveEventLoop) -> bool {
let menus = self.active_menu_declaration().to_vec();
let host = match MacMenuHost::new(&menus, self.event_proxy.clone()) {
Ok(host) => host,
Err(error) => {
self.fail(event_loop, AppError::Platform(error));
return false;
}
};
self.menu_actions = collect_menu_actions(&menus);
self.menu_host = Some(host);
self.sync_active_native_menu_state();
true
}
pub(super) fn replace_menus(&mut self, event_loop: &ActiveEventLoop, menus: Vec<Menu>) -> bool {
if let Err(error) = validate_menus(&menus) {
self.fail(event_loop, AppError::Platform(error.to_string()));
return false;
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
let _ = event_loop;
#[cfg(target_os = "windows")]
let next_host = if menus.is_empty() {
None
} else {
match windows_menu::WindowsMenuHost::new(&menus, self.event_proxy.clone()) {
Ok(host) => Some(host),
Err(error) => {
self.fail(event_loop, AppError::Platform(error));
return false;
}
}
};
self.menus = menus;
#[cfg(target_os = "macos")]
{
if !self.install_active_mac_menu(event_loop) {
return false;
}
}
#[cfg(target_os = "windows")]
{
self.windows_menu_host.take();
if let Some(next_host) = &next_host {
for entry in self.windows.values() {
if entry.config.window_menus.is_some() {
continue;
}
if let Err(error) = next_host.attach(&entry.state.window) {
self.fail(event_loop, AppError::Platform(error));
return false;
}
}
if self.config.window_menus.is_none()
&& let Some(window) = &self.window
&& let Err(error) = next_host.attach(&window.window)
{
self.fail(event_loop, AppError::Platform(error));
return false;
}
}
self.windows_menu_host = next_host;
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
let _ = menus;
true
}
pub(super) fn replace_current_window_menus(
&mut self,
event_loop: &ActiveEventLoop,
menus: Option<Vec<Menu>>,
) -> bool {
if let Some(menus) = menus.as_deref()
&& let Err(error) = validate_menus(menus)
{
self.fail(event_loop, AppError::Platform(error.to_string()));
return false;
}
let Some((_window_id, _)) = self.current_window else {
return false;
};
#[cfg(target_os = "windows")]
let next_host = match menus.as_deref() {
Some([]) | None => None,
Some(menus) => {
match windows_menu::WindowsMenuHost::new(menus, self.event_proxy.clone()) {
Ok(host) => Some(host),
Err(error) => {
self.fail(event_loop, AppError::Platform(error));
return false;
}
}
}
};
#[cfg(target_os = "windows")]
{
let Some(state) = self.window.as_mut() else {
return false;
};
if self.config.window_menus.is_none()
&& let Some(host) = &self.windows_menu_host
&& let Err(error) = host.detach(&state.window)
{
self.fail(event_loop, AppError::Platform(error));
return false;
}
state.window_menu_host.take();
if menus.is_some() {
if let Some(host) = &next_host
&& let Err(error) = host.attach(&state.window)
{
self.fail(event_loop, AppError::Platform(error));
return false;
}
state.window_menu_host = next_host;
} else if let Some(host) = &self.windows_menu_host
&& let Err(error) = host.attach(&state.window)
{
self.fail(event_loop, AppError::Platform(error));
return false;
}
}
self.config.window_menus = menus;
#[cfg(target_os = "macos")]
if self.active_window == Some(_window_id) && !self.install_active_mac_menu(event_loop) {
return false;
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
let _ = (event_loop, _window_id);
true
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(super) fn show_current_native_popup_menu(
&mut self,
event_loop: &ActiveEventLoop,
request: crate::event::NativePopupMenuRequest,
) -> bool {
if self.pending_native_popup_menus.len() == MAX_PENDING_NATIVE_POPUP_MENUS {
self.fail(
event_loop,
AppError::View(format!(
"the application cannot retain more than {MAX_PENDING_NATIVE_POPUP_MENUS} selected native popup menus"
)),
);
return false;
}
let Some(handle) = self.current_handle() else {
return false;
};
let actions = collect_menu_actions(std::slice::from_ref(&request.menu));
static NEXT_POPUP_MENU_ID: AtomicU64 = AtomicU64::new(1);
let popup_id = NEXT_POPUP_MENU_ID.fetch_add(1, Ordering::Relaxed).max(1);
#[cfg(target_os = "macos")]
let states = {
let contexts = self
.window
.as_ref()
.map(|window| window.ui.key_context_stack())
.unwrap_or_default();
actions
.iter()
.map(|item| MacMenuItemState {
disabled: item.disabled,
action_available: item
.action
.as_ref()
.is_some_and(|action| self.action_available(action))
|| item
.os_action
.is_some_and(|action| self.os_action_available(action)),
checked: item.checked,
shortcut: item.action.as_ref().and_then(|action| {
self.keymap.shortcut_for_action_value(action, &contexts)
}),
})
.collect::<Vec<_>>()
};
#[cfg(target_os = "macos")]
let native_focus_active = self
.window
.as_ref()
.and_then(|window| window.native_host.as_ref())
.is_some_and(MacNativeHost::native_focus_active);
self.pending_native_popup_menus.insert(
popup_id,
PendingNativePopupMenu {
window: handle,
actions,
},
);
#[cfg(target_os = "windows")]
tray::install_native_menu_handlers(self.event_proxy.clone());
let result = {
let Some(window) = self.window.as_ref() else {
self.pending_native_popup_menus.remove(&popup_id);
return false;
};
#[cfg(target_os = "macos")]
{
crate::macos_menu::show_popup_menu(
&request.menu,
popup_id,
&window.window,
request.position,
&states,
native_focus_active,
self.event_proxy.clone(),
)
}
#[cfg(target_os = "windows")]
{
windows_menu::show_popup_menu(
&request.menu,
popup_id,
&window.window,
request.position,
)
}
};
let shown = match result {
Ok(shown) => shown,
Err(error) => {
self.pending_native_popup_menus.remove(&popup_id);
self.fail(event_loop, AppError::Platform(error));
return false;
}
};
if !shown {
self.pending_native_popup_menus.remove(&popup_id);
return true;
}
if self
.event_proxy
.send_event(RuntimeEvent::NativePopupMenuClosed(popup_id))
.is_err()
{
self.pending_native_popup_menus.remove(&popup_id);
}
true
}
pub(super) fn os_action_available(&self, action: OsAction) -> bool {
let window = self.window.as_ref();
let input_focused = window.is_some_and(|window| window.ui.focused_text_input().is_some());
match action {
OsAction::Cut => {
input_focused
&& window
.expect("input focus requires a current window")
.ui
.selected_input_text()
.is_some_and(|selection| !selection.is_empty())
}
OsAction::Copy => window.is_some_and(|window| {
window
.ui
.selected_text()
.is_some_and(|selection| !selection.is_empty())
}),
OsAction::Paste => input_focused,
OsAction::SelectAll => {
input_focused || window.is_some_and(|window| window.ui.has_selectable_text())
}
OsAction::Undo => {
input_focused && window.is_some_and(|window| window.ui.input_can_undo())
}
OsAction::Redo => {
input_focused && window.is_some_and(|window| window.ui.input_can_redo())
}
OsAction::About => cfg!(any(target_os = "macos", target_os = "windows")),
OsAction::ShowHelp => cfg!(target_os = "macos"),
OsAction::HideApplication
| OsAction::ShowAllApplications
| OsAction::Quit
| OsAction::BringAllToFront => !self.window_handles.is_empty(),
OsAction::HideOtherApplications => cfg!(target_os = "macos"),
OsAction::CloseWindow => window.is_some() && self.config.is_closable,
OsAction::MinimizeWindow => window.is_some() && self.config.is_minimizable,
OsAction::ZoomWindow => {
window.is_some() && self.config.is_resizable && self.config.is_maximizable
}
OsAction::ToggleFullscreen => window.is_some(),
}
}
pub(super) fn invoke_os_action(
&mut self,
event_loop: &ActiveEventLoop,
action: OsAction,
) -> bool {
match action {
OsAction::Copy => {
let selected = self
.window
.as_ref()
.and_then(|window| window.ui.selected_text());
if let Some(selected) = selected {
let _ = self.write_clipboard_text(selected.as_ref());
return true;
}
false
}
OsAction::Cut => {
let selected = self
.window
.as_ref()
.and_then(|window| window.ui.selected_input_text());
let Some(selected) = selected else {
return false;
};
let copied = self.write_clipboard_text(selected.as_ref());
if copied {
let result = self
.window
.as_mut()
.map(|window| window.ui.input_backspace())
.unwrap_or_default();
self.apply_input_result(event_loop, result, true);
}
true
}
OsAction::Paste => {
if self
.window
.as_ref()
.and_then(|window| window.ui.focused_text_input())
.is_none()
{
return false;
}
let pasted = self.read_clipboard_text();
if let Some(value) = pasted {
let result = self
.window
.as_mut()
.map(|window| window.ui.input_replace(&value))
.unwrap_or_default();
self.apply_input_result(event_loop, result, true);
}
true
}
OsAction::SelectAll => {
if !self.os_action_available(OsAction::SelectAll) {
return false;
}
let result = self
.window
.as_mut()
.map_or_else(InputResult::default, |window| {
if window.ui.focused_text_input().is_some() {
window.ui.input_select_all()
} else {
InputResult {
repaint: window.ui.select_all_static_text(),
change: None,
}
}
});
self.apply_input_result(event_loop, result, false);
true
}
OsAction::Undo => {
if self
.window
.as_ref()
.and_then(|window| window.ui.focused_text_input())
.is_none()
{
return false;
}
let result = self
.window
.as_mut()
.map(|window| window.ui.input_undo())
.unwrap_or_default();
self.apply_input_result(event_loop, result, true)
}
OsAction::Redo => {
if self
.window
.as_ref()
.and_then(|window| window.ui.focused_text_input())
.is_none()
{
return false;
}
let result = self
.window
.as_mut()
.map(|window| window.ui.input_redo())
.unwrap_or_default();
self.apply_input_result(event_loop, result, true)
}
OsAction::About => {
if !DesktopIntegrationSupport::current().native_about_panel
|| self.platform_requests.len() == crate::MAX_PENDING_PLATFORM_REQUESTS
{
return false;
}
let Ok(request) = PlatformRequest::show_about_panel(AboutPanelOptions::default())
else {
return false;
};
self.platform_requests.push_back(request);
true
}
OsAction::HideOtherApplications | OsAction::ShowHelp => false,
OsAction::HideApplication | OsAction::ShowAllApplications => {
let visible = action == OsAction::ShowAllApplications;
let mut handles = self.window_handles.keys().copied().collect::<Vec<_>>();
handles.sort_unstable();
self.window_commands.extend(
handles
.into_iter()
.map(|handle| WindowCommand::SetVisible(handle, visible)),
);
true
}
OsAction::Quit => {
self.pending_quit = Some(QuitReason::Explicit);
true
}
OsAction::CloseWindow => {
if !self.config.is_closable {
return false;
}
let Some(handle) = self.current_handle() else {
return false;
};
self.close_requests.push(handle);
true
}
OsAction::MinimizeWindow => {
if !self.config.is_minimizable {
return false;
}
let Some(handle) = self.current_handle() else {
return false;
};
self.window_commands.push(WindowCommand::Minimize(handle));
true
}
OsAction::ZoomWindow => {
if !self.config.is_resizable || !self.config.is_maximizable {
return false;
}
let Some(handle) = self.current_handle() else {
return false;
};
self.window_commands.push(WindowCommand::Zoom(handle));
true
}
OsAction::ToggleFullscreen => {
let Some(handle) = self.current_handle() else {
return false;
};
self.window_commands
.push(WindowCommand::ToggleFullscreen(handle));
true
}
OsAction::BringAllToFront => {
let mut handles = self.window_handles.keys().copied().collect::<Vec<_>>();
handles.sort_unstable();
self.focus_requests.extend(handles);
true
}
}
}
#[cfg(target_os = "macos")]
pub(super) fn sync_native_menu_state(&self) {
let Some(host) = &self.menu_host else {
return;
};
let contexts = self
.window
.as_ref()
.map(|window| window.ui.key_context_stack())
.unwrap_or_default();
let states =
self.menu_actions
.iter()
.map(|item| MacMenuItemState {
disabled: item.disabled,
action_available: item
.action
.as_ref()
.is_some_and(|action| self.action_available(action))
|| item
.os_action
.is_some_and(|action| self.os_action_available(action)),
checked: item.checked,
shortcut: item.action.as_ref().and_then(|action| {
self.keymap.shortcut_for_action_value(action, &contexts)
}),
})
.collect::<Vec<_>>();
let native_focus_active = self
.window
.as_ref()
.and_then(|window| window.native_host.as_ref())
.is_some_and(MacNativeHost::native_focus_active);
let close_match = self.keymap.bindings_for_input(
&[Keystroke::new(
Key::Character("w".to_owned()),
Modifiers::SUPER,
)],
&contexts,
);
let keymap_claims_close = close_match.pending
|| close_match
.bindings
.iter()
.any(|binding| self.action_available(binding.action()));
host.update(&states, native_focus_active, keymap_claims_close);
}
#[cfg(target_os = "macos")]
pub(super) fn sync_active_native_menu_state(&mut self) {
if self.window.is_some() {
self.sync_native_menu_state();
return;
}
let Some(window_id) = self.active_window else {
return;
};
if self.activate_window(window_id) {
self.sync_native_menu_state();
self.deactivate_window();
}
}
}