use std::{
any::{Any, TypeId},
cell::{Ref, RefMut},
future::Future,
path::{Path, PathBuf},
sync::Arc,
};
use bitflags::bitflags;
use thiserror::Error;
use crate::{
Action, AnyAction, Assets, Display, DisplayId, Displays, ElementId, Entity, EntityId,
EventEmitter, FocusHandle, Global, KeyboardLayout, Menu, Point, Size, Vector, View,
WindowHandle, WindowOptions,
clipboard::{ClipboardError, ClipboardItem, ClipboardService, ClipboardTarget},
entity::{EntityEvent, MAX_ENTITY_EVENTS_PER_CALLBACK, MAX_ENTITY_NOTIFICATIONS_PER_EVENT},
foreground::{AsyncViewContext, ForegroundTaskSpawnError, ForegroundTaskSpawner, Task},
global::{GlobalStore, MAX_GLOBAL_NOTIFICATIONS_PER_EVENT},
platform::{
PathPromptOptions, PathPromptResponse, PlatformError, PlatformRequest, PlatformResponse,
PromptButton, PromptLevel, SavePathOptions, SavePathResponse, SystemNotification,
},
runtime::{
MAX_SYSTEM_WINDOW_TABS, WindowAppearance, WindowBackgroundAppearance, WindowCommand,
WindowCommandError, WindowRequest, validate_window_bounds, validate_window_document_path,
validate_window_position, validate_window_size, validate_window_tabbing_identifier,
validate_window_title,
},
};
pub const MAX_TARGETED_ACTIONS_PER_EVENT: usize = 256;
pub const MAX_PENDING_TARGETED_ACTIONS: usize = 1_024;
#[derive(Clone, Debug, PartialEq)]
pub enum Event {
CloseRequested,
PointerMoved(Point),
PointerLeft,
MouseButton {
button: MouseButton,
pressed: bool,
},
Click(crate::ElementId),
ContextMenu(ContextMenuEvent),
Dismiss(crate::ElementId),
Scroll(Vector),
MousePressure(MousePressureEvent),
Pinch(PinchEvent),
Rotation(RotationEvent),
SmartMagnify(SmartMagnifyEvent),
Touch(TouchEvent),
KeyDown {
key: Key,
key_char: Option<Key>,
modifiers: Modifiers,
repeat: bool,
},
KeyUp {
key: Key,
key_char: Option<Key>,
modifiers: Modifiers,
},
TextInput(String),
ModifiersChanged(Modifiers),
FocusChanged(Option<ElementId>),
Focused(bool),
AppearanceChanged(WindowAppearance),
Moved {
logical_position: Point,
scale_factor: f32,
},
FilesHovered(DroppedFiles),
FilesHoverCancelled,
FilesDropped(DroppedFiles),
ExternalDragEnded(ExternalDragEndEvent),
Resized {
logical_size: Size,
scale_factor: f32,
},
}
pub const MAX_FORM_FIELDS: usize = 256;
pub const MAX_VALIDATION_ISSUES: usize = 256;
pub const MAX_VALIDATION_MESSAGE_BYTES: usize = 4 * 1024;
pub const MAX_FORM_SUBMISSIONS_PER_EVENT: usize = 64;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FormField {
id: ElementId,
value: Arc<str>,
}
impl FormField {
pub(crate) fn new(id: ElementId, value: Arc<str>) -> Self {
Self { id, value }
}
pub const fn id(&self) -> ElementId {
self.id
}
pub fn value(&self) -> &str {
&self.value
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FormSubmitEvent {
form: ElementId,
trigger: Option<ElementId>,
fields: Arc<[FormField]>,
truncated: bool,
}
impl FormSubmitEvent {
pub(crate) fn new(
form: ElementId,
trigger: Option<ElementId>,
fields: Vec<FormField>,
truncated: bool,
) -> Self {
debug_assert!(fields.len() <= MAX_FORM_FIELDS);
Self {
form,
trigger,
fields: Arc::from(fields),
truncated,
}
}
pub const fn form(&self) -> ElementId {
self.form
}
pub const fn trigger(&self) -> Option<ElementId> {
self.trigger
}
pub fn fields(&self) -> &[FormField] {
&self.fields
}
pub fn value(&self, id: impl Into<ElementId>) -> Option<&str> {
let id = id.into();
self.fields
.iter()
.find(|field| field.id == id)
.map(FormField::value)
}
pub const fn is_truncated(&self) -> bool {
self.truncated
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ValidationIssue {
id: ElementId,
message: Option<Arc<str>>,
message_truncated: bool,
}
impl ValidationIssue {
pub(crate) fn new(id: ElementId, message: Option<Arc<str>>, message_truncated: bool) -> Self {
Self {
id,
message,
message_truncated,
}
}
pub const fn id(&self) -> ElementId {
self.id
}
pub fn message(&self) -> Option<&str> {
self.message.as_deref()
}
pub const fn is_message_truncated(&self) -> bool {
self.message_truncated
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ValidationReport {
form: ElementId,
trigger: Option<ElementId>,
issues: Arc<[ValidationIssue]>,
truncated: bool,
}
impl ValidationReport {
pub(crate) fn new(
form: ElementId,
trigger: Option<ElementId>,
issues: Vec<ValidationIssue>,
truncated: bool,
) -> Self {
debug_assert!(issues.len() <= MAX_VALIDATION_ISSUES);
Self {
form,
trigger,
issues: Arc::from(issues),
truncated,
}
}
pub const fn form(&self) -> ElementId {
self.form
}
pub const fn trigger(&self) -> Option<ElementId> {
self.trigger
}
pub fn issues(&self) -> &[ValidationIssue] {
&self.issues
}
pub fn first(&self) -> Option<&ValidationIssue> {
self.issues.first()
}
pub const fn is_truncated(&self) -> bool {
self.truncated
}
}
pub const MAX_DROPPED_FILES: usize = 4_096;
pub const MAX_EXTERNAL_DRAG_FILES: usize = 4_096;
pub const MAX_EXTERNAL_DRAG_PATH_BYTES: usize = 16 * 1024;
pub const MAX_EXTERNAL_DRAG_TOTAL_PATH_BYTES: usize = 8 * 1024 * 1024;
pub const MAX_EXTERNAL_DRAG_TEXT_BYTES: usize = 1024 * 1024;
pub const MAX_EXTERNAL_DRAG_URL_BYTES: usize = 16 * 1024;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct DroppedFiles {
paths: Arc<[PathBuf]>,
truncated: bool,
}
impl DroppedFiles {
pub fn new(paths: impl IntoIterator<Item = PathBuf>) -> Self {
let mut paths = paths.into_iter();
let retained = paths.by_ref().take(MAX_DROPPED_FILES).collect::<Vec<_>>();
Self {
paths: Arc::from(retained),
truncated: paths.next().is_some(),
}
}
pub fn paths(&self) -> &[PathBuf] {
&self.paths
}
pub fn is_empty(&self) -> bool {
self.paths.is_empty()
}
pub fn is_truncated(&self) -> bool {
self.truncated
}
pub(crate) fn from_retained(paths: Vec<PathBuf>, truncated: bool) -> Self {
debug_assert!(paths.len() <= MAX_DROPPED_FILES);
Self {
paths: Arc::from(paths),
truncated,
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct FileDragPaths {
entries: Arc<[(PathBuf, bool)]>,
truncated: bool,
}
impl FileDragPaths {
pub fn new(entries: impl IntoIterator<Item = (PathBuf, bool)>) -> Self {
let mut entries = entries.into_iter();
let mut retained = Vec::with_capacity(entries.size_hint().0.min(MAX_EXTERNAL_DRAG_FILES));
let mut retained_bytes = 0_usize;
let mut truncated = false;
for (path, is_directory) in entries.by_ref().take(MAX_EXTERNAL_DRAG_FILES) {
let path_bytes = path.as_os_str().as_encoded_bytes().len();
let next_bytes = retained_bytes.saturating_add(path_bytes);
if path_bytes == 0
|| path_bytes > MAX_EXTERNAL_DRAG_PATH_BYTES
|| next_bytes > MAX_EXTERNAL_DRAG_TOTAL_PATH_BYTES
{
truncated = true;
continue;
}
retained_bytes = next_bytes;
retained.push((path, is_directory));
}
truncated |= entries.next().is_some();
Self {
entries: Arc::from(retained),
truncated,
}
}
pub fn files(paths: impl IntoIterator<Item = PathBuf>) -> Self {
Self::new(paths.into_iter().map(|path| (path, false)))
}
pub fn directories(paths: impl IntoIterator<Item = PathBuf>) -> Self {
Self::new(paths.into_iter().map(|path| (path, true)))
}
pub fn entries(&self) -> &[(PathBuf, bool)] {
&self.entries
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn is_truncated(&self) -> bool {
self.truncated
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ExternalDragText {
text: Arc<str>,
truncated: bool,
}
impl ExternalDragText {
pub fn new(text: impl Into<Arc<str>>) -> Self {
let text = text.into();
if text.len() <= MAX_EXTERNAL_DRAG_TEXT_BYTES {
return Self {
text,
truncated: false,
};
}
let mut end = MAX_EXTERNAL_DRAG_TEXT_BYTES;
while !text.is_char_boundary(end) {
end -= 1;
}
Self {
text: Arc::from(&text[..end]),
truncated: true,
}
}
pub fn as_str(&self) -> &str {
&self.text
}
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
pub fn is_truncated(&self) -> bool {
self.truncated
}
pub(crate) fn from_bounded(text: String, truncated: bool) -> Self {
debug_assert!(text.len() <= MAX_EXTERNAL_DRAG_TEXT_BYTES);
Self {
text: Arc::from(text),
truncated,
}
}
}
impl From<&str> for ExternalDragText {
fn from(text: &str) -> Self {
Self::new(text)
}
}
impl From<String> for ExternalDragText {
fn from(text: String) -> Self {
Self::new(text)
}
}
impl From<Arc<str>> for ExternalDragText {
fn from(text: Arc<str>) -> Self {
Self::new(text)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExternalDragUrl(Arc<str>);
#[derive(Clone, Debug, Error, Eq, PartialEq)]
pub enum ExternalDragUrlError {
#[error("external drag URL is empty")]
Empty,
#[error(
"external drag URL is {bytes} bytes; the maximum is {MAX_EXTERNAL_DRAG_URL_BYTES} bytes"
)]
TooLarge { bytes: usize },
#[error("external drag URL must have an absolute RFC 3986 scheme")]
InvalidScheme,
#[error("external drag URL contains whitespace or control characters")]
InvalidCharacter,
}
impl ExternalDragUrl {
pub fn new(url: impl Into<Arc<str>>) -> Result<Self, ExternalDragUrlError> {
let url = url.into();
if url.is_empty() {
return Err(ExternalDragUrlError::Empty);
}
if url.len() > MAX_EXTERNAL_DRAG_URL_BYTES {
return Err(ExternalDragUrlError::TooLarge { bytes: url.len() });
}
if url
.chars()
.any(|character| character.is_whitespace() || character.is_control())
{
return Err(ExternalDragUrlError::InvalidCharacter);
}
let Some((scheme, remainder)) = url.split_once(':') else {
return Err(ExternalDragUrlError::InvalidScheme);
};
let mut characters = scheme.chars();
if remainder.is_empty()
|| !characters
.next()
.is_some_and(|character| character.is_ascii_alphabetic())
|| !characters.all(|character| {
character.is_ascii_alphanumeric() || matches!(character, '+' | '-' | '.')
})
{
return Err(ExternalDragUrlError::InvalidScheme);
}
Ok(Self(url))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
pub type DroppedText = ExternalDragText;
pub type DroppedUrl = ExternalDragUrl;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExternalDragPayload {
Files(FileDragPaths),
Text(ExternalDragText),
Url(ExternalDragUrl),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExternalDragOperation {
Cancelled,
Copied,
Moved,
Linked,
Deleted,
Other,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ExternalDragEndEvent {
pub source: ElementId,
pub operation: ExternalDragOperation,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DragStartEvent {
pub origin: Point,
pub position: Point,
pub modifiers: Modifiers,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DragOrigin {
Internal(ElementId),
CrossWindow {
window: WindowHandle,
source: ElementId,
},
External,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DropEvent {
pub position: Point,
pub modifiers: Modifiers,
pub origin: DragOrigin,
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum MouseButton {
#[default]
Left,
Right,
Middle,
Back,
Forward,
Other(u16),
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum DispatchPhase {
#[default]
Bubble,
Capture,
}
impl DispatchPhase {
pub const fn bubble(self) -> bool {
matches!(self, Self::Bubble)
}
pub const fn capture(self) -> bool {
matches!(self, Self::Capture)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct MouseDownEvent {
pub button: MouseButton,
pub position: Point,
pub modifiers: Modifiers,
pub click_count: usize,
pub first_mouse: bool,
}
impl MouseDownEvent {
pub const fn is_focusing(self) -> bool {
matches!(self.button, MouseButton::Left)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct MouseUpEvent {
pub button: MouseButton,
pub position: Point,
pub modifiers: Modifiers,
pub click_count: usize,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct MouseMoveEvent {
pub position: Point,
pub pressed_button: Option<MouseButton>,
pub modifiers: Modifiers,
}
impl MouseMoveEvent {
pub const fn dragging(self) -> bool {
self.pressed_button.is_some()
}
pub fn dragging_button(self, button: MouseButton) -> bool {
matches!(self.pressed_button, Some(pressed) if pressed == button)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct MouseExitEvent {
pub position: Point,
pub pressed_button: Option<MouseButton>,
pub modifiers: Modifiers,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PointerPhase {
Down,
Move,
Up,
Cancel,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PointerEvent {
pub phase: PointerPhase,
pub position: Point,
pub origin: Point,
pub delta: Vector,
pub button: MouseButton,
pub modifiers: Modifiers,
}
pub const MAX_SCROLL_PIXELS_PER_EVENT: f32 = 1_048_576.0;
pub const MAX_SCROLL_LINES_PER_EVENT: f32 = 4_096.0;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScrollDelta {
Pixels(Vector),
Lines(Vector),
}
impl Default for ScrollDelta {
fn default() -> Self {
Self::Lines(Vector::ZERO)
}
}
impl ScrollDelta {
pub fn precise(&self) -> bool {
matches!(self, Self::Pixels(_))
}
pub fn pixel_delta(&self, line_height: f32) -> Vector {
match *self {
Self::Pixels(delta) => bounded_scroll_vector(delta, MAX_SCROLL_PIXELS_PER_EVENT),
Self::Lines(delta) => {
let line_height = if line_height.is_finite() {
line_height.clamp(0.0, MAX_SCROLL_PIXELS_PER_EVENT)
} else {
0.0
};
bounded_scroll_vector(
Vector::new(delta.x * line_height, delta.y * line_height),
MAX_SCROLL_PIXELS_PER_EVENT,
)
}
}
}
pub(crate) fn bounded(self) -> Self {
match self {
Self::Pixels(delta) => {
Self::Pixels(bounded_scroll_vector(delta, MAX_SCROLL_PIXELS_PER_EVENT))
}
Self::Lines(delta) => {
Self::Lines(bounded_scroll_vector(delta, MAX_SCROLL_LINES_PER_EVENT))
}
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ScrollWheelEvent {
pub position: Point,
pub delta: ScrollDelta,
pub phase: GesturePhase,
pub modifiers: Modifiers,
}
impl ScrollWheelEvent {
pub(crate) fn bounded(mut self) -> Self {
self.delta = self.delta.bounded();
self
}
}
fn bounded_scroll_vector(delta: Vector, limit: f32) -> Vector {
let component = |value: f32| {
if value.is_finite() {
value.clamp(-limit, limit)
} else {
0.0
}
};
Vector::new(component(delta.x), component(delta.y))
}
pub const MAX_PINCH_DELTA_PER_EVENT: f32 = 8.0;
pub const MAX_ROTATION_DEGREES_PER_EVENT: f32 = 360.0;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum GesturePhase {
Started,
#[default]
Moved,
Ended,
Cancelled,
}
pub const MAX_ACTIVE_TOUCHES_PER_WINDOW: usize = 32;
pub const MAX_TOUCH_COORDINATE: f32 = 16_777_216.0;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TouchId(pub u64);
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum TouchPhase {
Started,
#[default]
Moved,
Ended,
Cancelled,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TouchEvent {
pub id: TouchId,
pub phase: TouchPhase,
pub position: Point,
pub force: Option<f32>,
}
impl TouchEvent {
pub(crate) fn bounded(mut self) -> Self {
let coordinate = |value: f32| {
if value.is_finite() {
value.clamp(-MAX_TOUCH_COORDINATE, MAX_TOUCH_COORDINATE)
} else {
0.0
}
};
self.position = Point::new(coordinate(self.position.x), coordinate(self.position.y));
self.force = self.force.map(|force| {
if force.is_finite() {
force.clamp(0.0, 1.0)
} else {
0.0
}
});
self
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum PressureStage {
#[default]
Zero,
Normal,
Force,
Other(i64),
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct MousePressureEvent {
pub position: Point,
pub pressure: f32,
pub stage: PressureStage,
pub modifiers: Modifiers,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct PinchEvent {
pub position: Point,
pub delta: f32,
pub phase: GesturePhase,
pub modifiers: Modifiers,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RotationEvent {
pub position: Point,
pub delta: f32,
pub phase: GesturePhase,
pub modifiers: Modifiers,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SmartMagnifyEvent {
pub position: Point,
pub modifiers: Modifiers,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ContextMenuEvent {
pub target: ElementId,
pub position: Point,
pub modifiers: Modifiers,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Key {
Character(String),
ArrowUp,
ArrowDown,
ArrowLeft,
ArrowRight,
PageUp,
PageDown,
Home,
End,
Enter,
Escape,
Space,
Tab,
Backspace,
Delete,
Insert,
Function(u8),
Other,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyDownEvent {
pub key: Key,
pub key_char: Option<Key>,
pub modifiers: Modifiers,
pub repeat: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyUpEvent {
pub key: Key,
pub key_char: Option<Key>,
pub modifiers: Modifiers,
}
bitflags! {
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct Modifiers: u8 {
const SHIFT = 1 << 0;
const CONTROL = 1 << 1;
const ALT = 1 << 2;
const SUPER = 1 << 3;
}
}
#[derive(Debug, Default)]
pub struct EventContext {
pub(crate) globals: GlobalStore,
pub(crate) foreground_tasks: Option<ForegroundTaskSpawner>,
pub(crate) clipboard: Option<ClipboardService>,
pub(crate) displays: Displays,
pub(crate) keyboard_layout: KeyboardLayout,
pub(crate) assets: Assets,
pub(crate) window: Option<WindowHandle>,
pub(crate) parent_window: Option<WindowHandle>,
pub(crate) popup_owner_window: Option<WindowHandle>,
pub(crate) popup_root_window: Option<WindowHandle>,
pub(crate) pointer_position: Option<Point>,
pub(crate) invalidate: bool,
pub(crate) exit: bool,
pub(crate) focus: Option<Option<ElementId>>,
pub(crate) actions: Vec<AnyAction>,
pub(crate) targeted_actions: Vec<(WindowHandle, AnyAction)>,
pub(crate) menus: Option<Vec<Menu>>,
pub(crate) propagate_action: bool,
pub(crate) stop_event_propagation: bool,
pub(crate) prevent_default: bool,
pub(crate) open_windows: Vec<WindowRequest>,
pub(crate) close_current_window: bool,
pub(crate) close_windows: Vec<WindowHandle>,
pub(crate) focus_windows: Vec<WindowHandle>,
pub(crate) invalidate_windows: Vec<WindowHandle>,
pub(crate) window_commands: Vec<WindowCommand>,
pub(crate) platform_requests: Vec<PlatformRequest>,
pub(crate) prevent_close: bool,
pub(crate) form_submissions: Vec<ElementId>,
pub(crate) entity_notifications: Vec<EntityId>,
pub(crate) notify_all_entities: bool,
pub(crate) entity_events: Vec<EntityEvent>,
pub(crate) global_notifications: Vec<TypeId>,
pub(crate) notify_all_globals: bool,
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct EventWindowContext {
pub(crate) window: Option<WindowHandle>,
pub(crate) parent: Option<WindowHandle>,
pub(crate) popup_owner: Option<WindowHandle>,
pub(crate) popup_root: Option<WindowHandle>,
pub(crate) pointer_position: Option<Point>,
}
impl EventContext {
pub(crate) fn with_runtime(
globals: GlobalStore,
foreground_tasks: ForegroundTaskSpawner,
clipboard: ClipboardService,
displays: Displays,
keyboard_layout: KeyboardLayout,
assets: Assets,
window_context: EventWindowContext,
) -> Self {
Self {
globals,
foreground_tasks: Some(foreground_tasks),
clipboard: Some(clipboard),
displays,
keyboard_layout,
assets,
window: window_context.window,
parent_window: window_context.parent,
popup_owner_window: window_context.popup_owner,
popup_root_window: window_context.popup_root,
pointer_position: window_context.pointer_position,
invalidate: false,
exit: false,
focus: None,
actions: Vec::new(),
targeted_actions: Vec::new(),
menus: None,
propagate_action: false,
stop_event_propagation: false,
prevent_default: false,
open_windows: Vec::new(),
close_current_window: false,
close_windows: Vec::new(),
focus_windows: Vec::new(),
invalidate_windows: Vec::new(),
window_commands: Vec::new(),
platform_requests: Vec::new(),
prevent_close: false,
form_submissions: Vec::new(),
entity_notifications: Vec::new(),
notify_all_entities: false,
entity_events: Vec::new(),
global_notifications: Vec::new(),
notify_all_globals: false,
}
}
pub fn displays(&self) -> &[Display] {
self.displays.all()
}
pub fn primary_display(&self) -> Option<&Display> {
self.displays.primary()
}
pub fn find_display(&self, id: DisplayId) -> Option<&Display> {
self.displays.find(id)
}
pub fn keyboard_layout(&self) -> &KeyboardLayout {
&self.keyboard_layout
}
pub fn assets(&self) -> &Assets {
&self.assets
}
pub fn asset_source(&self) -> &Assets {
self.assets()
}
pub fn read_from_clipboard(&self) -> Result<Option<ClipboardItem>, ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.read(ClipboardTarget::General)
}
pub fn write_to_clipboard(&self, item: ClipboardItem) -> Result<(), ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.write(ClipboardTarget::General, item)
}
#[cfg(target_os = "macos")]
pub fn read_from_find_pasteboard(&self) -> Result<Option<ClipboardItem>, ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.read(ClipboardTarget::Find)
}
#[cfg(target_os = "macos")]
pub fn write_to_find_pasteboard(&self, item: ClipboardItem) -> Result<(), ClipboardError> {
self.clipboard
.as_ref()
.ok_or(ClipboardError::Unavailable)?
.write(ClipboardTarget::Find, item)
}
pub fn spawn<V, Build, Fut, R>(&self, build: Build) -> Result<Task<R>, ForegroundTaskSpawnError>
where
V: 'static,
Build: FnOnce(AsyncViewContext<V>) -> Fut,
Fut: Future<Output = R> + 'static,
R: 'static,
{
let foreground_tasks = self
.foreground_tasks
.as_ref()
.ok_or(ForegroundTaskSpawnError::Unavailable)?;
let window = self.window.ok_or(ForegroundTaskSpawnError::Unavailable)?;
foreground_tasks.spawn::<V, _, _, _>(window, build)
}
pub fn invalidate(&mut self) {
self.invalidate = true;
}
pub fn notify<T>(&mut self, entity: &Entity<T>) {
if self.notify_all_entities || self.entity_notifications.contains(&entity.id()) {
return;
}
if self.entity_notifications.len() == MAX_ENTITY_NOTIFICATIONS_PER_EVENT {
self.entity_notifications.clear();
self.notify_all_entities = true;
return;
}
self.entity_notifications.push(entity.id());
}
pub fn emit<T, E>(&mut self, entity: &Entity<T>, event: E) -> bool
where
T: EventEmitter<E>,
E: Any,
{
if self.entity_events.len() == MAX_ENTITY_EVENTS_PER_CALLBACK {
return false;
}
self.entity_events.push(EntityEvent::new(entity, event));
true
}
pub fn has_global<G: Global>(&self) -> bool {
self.globals.has::<G>()
}
pub fn global<G: Global>(&self) -> Ref<'_, G> {
self.globals.get::<G>()
}
pub fn try_global<G: Global>(&self) -> Option<Ref<'_, G>> {
self.globals.try_get::<G>()
}
pub fn global_mut<G: Global>(&mut self) -> RefMut<'_, G> {
self.note_global_changed(TypeId::of::<G>());
self.globals.get_mut::<G>()
}
pub fn default_global<G: Global + Default>(&mut self) -> RefMut<'_, G> {
self.note_global_changed(TypeId::of::<G>());
self.globals.default_mut::<G>()
}
pub fn set_global<G: Global>(&mut self, global: G) {
self.globals.set(global);
self.note_global_changed(TypeId::of::<G>());
}
pub fn update_global<G: Global, R>(&mut self, update: impl FnOnce(&mut G) -> R) -> R {
self.note_global_changed(TypeId::of::<G>());
let mut global = self.globals.get_mut::<G>();
update(&mut global)
}
pub fn remove_global<G: Global>(&mut self) -> G {
let global = self.globals.remove::<G>();
self.note_global_changed(TypeId::of::<G>());
global
}
fn note_global_changed(&mut self, global_type: TypeId) {
if self.notify_all_globals || self.global_notifications.contains(&global_type) {
return;
}
if self.global_notifications.len() == MAX_GLOBAL_NOTIFICATIONS_PER_EVENT {
self.global_notifications.clear();
self.notify_all_globals = true;
return;
}
self.global_notifications.push(global_type);
}
pub fn exit(&mut self) {
self.exit = true;
}
pub fn open_window<V: View>(&mut self, view: V, options: WindowOptions) -> WindowHandle {
let request = WindowRequest::with_parent(view, options, self.window);
let handle = request.handle;
self.open_windows.push(request);
handle
}
pub fn open_anchored_popup<V: View>(
&mut self,
anchor: impl Into<ElementId>,
view: V,
options: WindowOptions,
) -> Result<WindowHandle, WindowCommandError> {
if self.window.is_none() {
return Err(WindowCommandError::Unavailable);
}
if options.kind != crate::WindowKind::AnchoredPopup || options.popup.is_none() {
return Err(WindowCommandError::InvalidPopupConfiguration);
}
let mut request = WindowRequest::with_parent(view, options, self.window);
request.popup_anchor_element = Some(anchor.into());
let handle = request.handle;
self.open_windows.push(request);
Ok(handle)
}
pub fn window_handle(&self) -> Option<WindowHandle> {
self.window
}
pub const fn pointer_position(&self) -> Option<Point> {
self.pointer_position
}
fn current_window_handle(&self) -> Result<WindowHandle, WindowCommandError> {
self.window.ok_or(WindowCommandError::Unavailable)
}
fn push_window_command(&mut self, command: WindowCommand) -> Result<(), WindowCommandError> {
if self.window_commands.len() == crate::MAX_WINDOW_COMMANDS_PER_EVENT {
return Err(WindowCommandError::QueueFull);
}
self.window_commands.push(command);
Ok(())
}
fn push_platform_request(&mut self, request: PlatformRequest) -> Result<(), PlatformError> {
if self.platform_requests.len() == crate::MAX_PLATFORM_REQUESTS_PER_EVENT {
return Err(PlatformError::QueueFull);
}
self.platform_requests.push(request);
Ok(())
}
fn ensure_platform_capacity(&self) -> Result<(), PlatformError> {
if self.platform_requests.len() == crate::MAX_PLATFORM_REQUESTS_PER_EVENT {
Err(PlatformError::QueueFull)
} else {
Ok(())
}
}
pub fn prompt(
&mut self,
level: PromptLevel,
message: impl Into<Arc<str>>,
detail: Option<&str>,
buttons: &[PromptButton],
) -> Result<PlatformResponse<usize>, PlatformError> {
self.ensure_platform_capacity()?;
let window = self.window.ok_or(PlatformError::Unavailable)?;
let (request, response) =
PlatformRequest::prompt(window, level, message, detail.map(Arc::from), buttons)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn prompt_for_paths(
&mut self,
options: PathPromptOptions,
) -> Result<PathPromptResponse, PlatformError> {
self.ensure_platform_capacity()?;
let window = self.window.ok_or(PlatformError::Unavailable)?;
let (request, response) = PlatformRequest::open_paths(window, options)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn prompt_for_new_path(
&mut self,
options: SavePathOptions,
) -> Result<SavePathResponse, PlatformError> {
self.ensure_platform_capacity()?;
let window = self.window.ok_or(PlatformError::Unavailable)?;
let (request, response) = PlatformRequest::save_path(window, options)?;
self.push_platform_request(request)?;
Ok(response)
}
pub fn show_system_notification(
&mut self,
notification: SystemNotification,
) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::show_system_notification(notification)?;
#[cfg(target_os = "macos")]
{
self.push_platform_request(request)
}
#[cfg(not(target_os = "macos"))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn dismiss_system_notification(
&mut self,
tag: impl Into<Arc<str>>,
) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::dismiss_system_notification(tag)?;
#[cfg(target_os = "macos")]
{
self.push_platform_request(request)
}
#[cfg(not(target_os = "macos"))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn open_url(&mut self, url: impl Into<Arc<str>>) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::open_url(url)?;
#[cfg(target_os = "macos")]
{
self.push_platform_request(request)
}
#[cfg(not(target_os = "macos"))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn open_path(&mut self, path: impl Into<PathBuf>) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::open_path(path)?;
#[cfg(target_os = "macos")]
{
self.push_platform_request(request)
}
#[cfg(not(target_os = "macos"))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn reveal_path(&mut self, path: impl Into<PathBuf>) -> Result<(), PlatformError> {
self.ensure_platform_capacity()?;
let request = PlatformRequest::reveal_path(path)?;
#[cfg(target_os = "macos")]
{
self.push_platform_request(request)
}
#[cfg(not(target_os = "macos"))]
{
let _ = request;
Err(PlatformError::Unsupported)
}
}
pub fn set_window_title(&mut self, title: impl Into<String>) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_title_handle(handle, title)
}
pub fn set_window_title_handle(
&mut self,
handle: WindowHandle,
title: impl Into<String>,
) -> Result<(), WindowCommandError> {
let title = title.into();
validate_window_title(&title)?;
self.push_window_command(WindowCommand::SetTitle(handle, title))
}
pub fn set_represented_file(
&mut self,
path: impl Into<PathBuf>,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_represented_file_handle(handle, path)
}
pub fn set_represented_file_handle(
&mut self,
handle: WindowHandle,
path: impl Into<PathBuf>,
) -> Result<(), WindowCommandError> {
let path = path.into();
validate_window_document_path(&path)?;
self.push_window_command(WindowCommand::SetRepresentedFile(handle, Some(path)))
}
pub fn set_document_path(&mut self, path: impl AsRef<Path>) -> Result<(), WindowCommandError> {
self.set_represented_file(path.as_ref().to_path_buf())
}
pub fn set_document_path_handle(
&mut self,
handle: WindowHandle,
path: impl AsRef<Path>,
) -> Result<(), WindowCommandError> {
self.set_represented_file_handle(handle, path.as_ref().to_path_buf())
}
pub fn clear_represented_file(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_represented_file_handle(handle)
}
pub fn clear_represented_file_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetRepresentedFile(handle, None))
}
pub fn set_window_edited(&mut self, edited: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_edited_handle(handle, edited)
}
pub fn set_window_edited_handle(
&mut self,
handle: WindowHandle,
edited: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetDocumentEdited(handle, edited))
}
pub fn set_document_edited(&mut self, edited: bool) -> Result<(), WindowCommandError> {
self.set_window_edited(edited)
}
pub fn show_character_palette(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.show_character_palette_handle(handle)
}
pub fn show_character_palette_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ShowCharacterPalette(handle))
}
pub fn set_tabbing_identifier(
&mut self,
identifier: impl Into<String>,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_tabbing_identifier_handle(handle, identifier)
}
pub fn set_tabbing_identifier_handle(
&mut self,
handle: WindowHandle,
identifier: impl Into<String>,
) -> Result<(), WindowCommandError> {
let identifier = identifier.into();
validate_window_tabbing_identifier(&identifier)?;
self.push_window_command(WindowCommand::SetTabbingIdentifier(
handle,
Some(identifier),
))
}
pub fn clear_tabbing_identifier(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_tabbing_identifier_handle(handle)
}
pub fn clear_tabbing_identifier_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetTabbingIdentifier(handle, None))
}
pub fn select_next_tab(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.select_next_tab_handle(handle)
}
pub fn select_next_tab_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SelectNextTab(handle))
}
pub fn select_previous_tab(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.select_previous_tab_handle(handle)
}
pub fn select_previous_tab_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SelectPreviousTab(handle))
}
pub fn select_tab(&mut self, index: usize) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.select_tab_handle(handle, index)
}
pub fn select_tab_handle(
&mut self,
handle: WindowHandle,
index: usize,
) -> Result<(), WindowCommandError> {
if index >= MAX_SYSTEM_WINDOW_TABS {
return Err(WindowCommandError::InvalidTabIndex);
}
self.push_window_command(WindowCommand::SelectTab(handle, index))
}
pub fn merge_all_windows(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.merge_all_windows_handle(handle)
}
pub fn merge_all_windows_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::MergeAllWindows(handle))
}
pub fn move_tab_to_new_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.move_tab_to_new_window_handle(handle)
}
pub fn move_tab_to_new_window_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::MoveTabToNewWindow(handle))
}
pub fn toggle_tab_bar(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_tab_bar_handle(handle)
}
pub fn toggle_tab_bar_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleTabBar(handle))
}
pub fn toggle_tab_overview(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_tab_overview_handle(handle)
}
pub fn toggle_tab_overview_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleTabOverview(handle))
}
pub fn set_window_bounds(
&mut self,
bounds: crate::WindowBounds,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_bounds_handle(handle, bounds)
}
pub fn set_window_bounds_handle(
&mut self,
handle: WindowHandle,
bounds: crate::WindowBounds,
) -> Result<(), WindowCommandError> {
validate_window_bounds(bounds)?;
self.push_window_command(WindowCommand::SetBounds(handle, bounds))
}
pub fn move_window(&mut self, position: Point) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.move_window_handle(handle, position)
}
pub fn move_window_handle(
&mut self,
handle: WindowHandle,
position: Point,
) -> Result<(), WindowCommandError> {
validate_window_position(position)?;
self.push_window_command(WindowCommand::Move(handle, position))
}
pub fn resize_window(&mut self, size: Size) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.resize_window_handle(handle, size)
}
pub fn resize_window_handle(
&mut self,
handle: WindowHandle,
size: Size,
) -> Result<(), WindowCommandError> {
validate_window_size(size)?;
self.push_window_command(WindowCommand::Resize(handle, size))
}
pub fn minimize_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.minimize_window_handle(handle)
}
pub fn minimize_window_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::Minimize(handle))
}
pub fn restore_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.restore_window_handle(handle)
}
pub fn restore_window_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::Restore(handle))
}
pub fn zoom_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.zoom_window_handle(handle)
}
pub fn zoom_window_handle(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::Zoom(handle))
}
pub fn toggle_fullscreen(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_fullscreen_handle(handle)
}
pub fn toggle_fullscreen_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleFullscreen(handle))
}
pub fn set_fullscreen(&mut self, fullscreen: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_fullscreen_handle(handle, fullscreen)
}
pub fn set_fullscreen_handle(
&mut self,
handle: WindowHandle,
fullscreen: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetFullscreen(handle, fullscreen))
}
pub fn show_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.show_window_handle(handle)
}
pub fn show_window_handle(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetVisible(handle, true))
}
pub fn hide_window(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.hide_window_handle(handle)
}
pub fn hide_window_handle(&mut self, handle: WindowHandle) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetVisible(handle, false))
}
pub fn set_window_movable(&mut self, movable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_movable_handle(handle, movable)
}
pub fn set_window_movable_handle(
&mut self,
handle: WindowHandle,
movable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMovable(handle, movable))
}
pub fn set_window_resizable(&mut self, resizable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_resizable_handle(handle, resizable)
}
pub fn set_window_resizable_handle(
&mut self,
handle: WindowHandle,
resizable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetResizable(handle, resizable))
}
pub fn set_window_minimum_size(&mut self, size: Size) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_minimum_size_handle(handle, size)
}
pub fn set_window_minimum_size_handle(
&mut self,
handle: WindowHandle,
size: Size,
) -> Result<(), WindowCommandError> {
validate_window_size(size)?;
self.push_window_command(WindowCommand::SetMinimumSize(handle, Some(size)))
}
pub fn clear_window_minimum_size(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.clear_window_minimum_size_handle(handle)
}
pub fn clear_window_minimum_size_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMinimumSize(handle, None))
}
pub fn set_window_minimizable(&mut self, minimizable: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_minimizable_handle(handle, minimizable)
}
pub fn set_window_minimizable_handle(
&mut self,
handle: WindowHandle,
minimizable: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetMinimizable(handle, minimizable))
}
pub fn set_window_appearance(
&mut self,
appearance: WindowAppearance,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_appearance_handle(handle, appearance)
}
pub fn set_window_appearance_handle(
&mut self,
handle: WindowHandle,
appearance: WindowAppearance,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetAppearance(handle, Some(appearance)))
}
pub fn follow_system_window_appearance(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.follow_system_window_appearance_handle(handle)
}
pub fn follow_system_window_appearance_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetAppearance(handle, None))
}
pub fn set_window_background_appearance(
&mut self,
appearance: WindowBackgroundAppearance,
) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_window_background_appearance_handle(handle, appearance)
}
pub fn set_window_background_appearance_handle(
&mut self,
handle: WindowHandle,
appearance: WindowBackgroundAppearance,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetBackgroundAppearance(handle, appearance))
}
#[cfg(feature = "inspector")]
pub fn set_inspector(&mut self, open: bool) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.set_inspector_handle(handle, open)
}
#[cfg(feature = "inspector")]
pub fn set_inspector_handle(
&mut self,
handle: WindowHandle,
open: bool,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::SetInspector(handle, open))
}
#[cfg(feature = "inspector")]
pub fn toggle_inspector(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.toggle_inspector_handle(handle)
}
#[cfg(feature = "inspector")]
pub fn toggle_inspector_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::ToggleInspector(handle))
}
pub fn request_window_attention(&mut self) -> Result<(), WindowCommandError> {
let handle = self.current_window_handle()?;
self.request_window_attention_handle(handle)
}
pub fn request_window_attention_handle(
&mut self,
handle: WindowHandle,
) -> Result<(), WindowCommandError> {
self.push_window_command(WindowCommand::RequestAttention(handle))
}
pub fn close_window(&mut self) {
self.close_current_window = true;
}
pub fn close_popup_chain(&mut self) -> bool {
let Some(root) = self.popup_root_window else {
return false;
};
if let Some(owner) = self.popup_owner_window
&& !self.focus_windows.contains(&owner)
{
self.focus_windows.push(owner);
}
if Some(root) == self.window {
self.close_current_window = true;
} else {
self.close_windows.push(root);
}
true
}
pub fn close_window_handle(&mut self, handle: WindowHandle) {
self.close_windows.push(handle);
}
pub fn focus_window(&mut self, handle: WindowHandle) {
self.focus_windows.push(handle);
}
pub fn invalidate_window(&mut self, handle: WindowHandle) {
self.invalidate_windows.push(handle);
}
pub fn prevent_close(&mut self) {
self.prevent_close = true;
}
pub fn focus(&mut self, handle: FocusHandle) {
self.focus = Some(Some(handle.id()));
}
pub fn blur(&mut self) {
self.focus = Some(None);
}
pub fn submit_form(&mut self, form: impl Into<ElementId>) -> bool {
let form = form.into();
if self.form_submissions.contains(&form) {
return true;
}
if self.form_submissions.len() == MAX_FORM_SUBMISSIONS_PER_EVENT {
return false;
}
self.form_submissions.push(form);
true
}
pub fn dispatch_action<A: Action>(&mut self, action: A) {
self.actions.push(AnyAction::new(action));
}
pub fn dispatch_any_action(&mut self, action: AnyAction) {
self.actions.push(action);
}
pub fn dispatch_action_to_window<A: Action>(
&mut self,
window: WindowHandle,
action: A,
) -> bool {
self.dispatch_any_action_to_window(window, AnyAction::new(action))
}
pub fn dispatch_any_action_to_window(
&mut self,
window: WindowHandle,
action: AnyAction,
) -> bool {
if self.targeted_actions.len() == MAX_TARGETED_ACTIONS_PER_EVENT {
return false;
}
self.targeted_actions.push((window, action));
true
}
pub fn dispatch_action_to_parent<A: Action>(&mut self, action: A) -> bool {
let Some(parent) = self.parent_window else {
return false;
};
self.dispatch_action_to_window(parent, action)
}
pub fn dispatch_any_action_to_parent(&mut self, action: AnyAction) -> bool {
let Some(parent) = self.parent_window else {
return false;
};
self.dispatch_any_action_to_window(parent, action)
}
pub const fn parent_window_handle(&self) -> Option<WindowHandle> {
self.parent_window
}
pub fn dispatch_action_to_popup_owner<A: Action>(&mut self, action: A) -> bool {
let Some(owner) = self.popup_owner_window else {
return false;
};
self.dispatch_action_to_window(owner, action)
}
pub fn dispatch_any_action_to_popup_owner(&mut self, action: AnyAction) -> bool {
let Some(owner) = self.popup_owner_window else {
return false;
};
self.dispatch_any_action_to_window(owner, action)
}
pub const fn popup_owner_window_handle(&self) -> Option<WindowHandle> {
self.popup_owner_window
}
pub const fn popup_root_window_handle(&self) -> Option<WindowHandle> {
self.popup_root_window
}
pub fn set_menus(&mut self, menus: impl IntoIterator<Item = Menu>) {
self.menus = Some(menus.into_iter().collect());
}
pub fn propagate(&mut self) {
self.propagate_action = true;
self.stop_event_propagation = false;
}
pub fn stop_propagation(&mut self) {
self.stop_event_propagation = true;
}
pub fn prevent_default(&mut self) {
self.prevent_default = true;
}
}
#[cfg(test)]
mod tests {
use crate::{Global, IntoElement, ViewContext, div};
use super::*;
struct SecondaryView;
#[derive(Default)]
struct TestGlobal(u32);
impl Global for TestGlobal {}
impl View for SecondaryView {
fn render(&mut self, _cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
div()
}
}
#[test]
fn window_commands_keep_stable_handles_and_options() {
let parent = WindowHandle::next();
let mut cx = EventContext {
window: Some(parent),
..EventContext::default()
};
let first = cx.open_window(
SecondaryView,
WindowOptions::new("First").size(480.0, 320.0),
);
let second = cx.open_window(SecondaryView, WindowOptions::new("Second"));
assert_ne!(first, second);
assert_eq!(cx.open_windows[0].handle, first);
assert_eq!(cx.open_windows[0].options.title, "First");
assert_eq!(cx.open_windows[0].options.size, Size::new(480.0, 320.0));
assert_eq!(cx.open_windows[0].parent, Some(parent));
assert_eq!(cx.open_windows[1].parent, Some(parent));
cx.focus_window(first);
cx.invalidate_window(second);
cx.close_window_handle(first);
cx.close_window();
cx.prevent_close();
assert_eq!(cx.focus_windows, [first]);
assert_eq!(cx.invalidate_windows, [second]);
assert_eq!(cx.close_windows, [first]);
assert!(cx.close_current_window);
assert!(cx.prevent_close);
}
#[test]
fn closing_popup_chain_restores_the_non_popup_owner_focus_once() {
let owner = WindowHandle::next();
let root = WindowHandle::next();
let child = WindowHandle::next();
let mut cx = EventContext {
window: Some(child),
popup_owner_window: Some(owner),
popup_root_window: Some(root),
..EventContext::default()
};
assert!(cx.close_popup_chain());
assert!(cx.close_popup_chain());
assert_eq!(cx.focus_windows, [owner]);
assert_eq!(cx.close_windows, [root, root]);
assert!(!cx.close_current_window);
let mut root_context = EventContext {
window: Some(root),
popup_owner_window: Some(owner),
popup_root_window: Some(root),
..EventContext::default()
};
assert!(root_context.close_popup_chain());
assert_eq!(root_context.focus_windows, [owner]);
assert!(root_context.close_current_window);
let mut ordinary_window = EventContext::default();
assert!(!ordinary_window.close_popup_chain());
assert!(ordinary_window.focus_windows.is_empty());
}
#[test]
fn window_mutation_queue_is_bounded_and_validates_before_retaining() {
let window = WindowHandle::next();
let mut cx = EventContext {
window: Some(window),
..EventContext::default()
};
assert_eq!(
cx.set_window_title("x".repeat(crate::MAX_WINDOW_TITLE_BYTES + 1)),
Err(WindowCommandError::TitleTooLong)
);
assert!(cx.window_commands.is_empty());
for _ in 0..crate::MAX_WINDOW_COMMANDS_PER_EVENT {
assert!(cx.request_window_attention().is_ok());
}
assert_eq!(
cx.request_window_attention(),
Err(WindowCommandError::QueueFull)
);
assert_eq!(
cx.window_commands.len(),
crate::MAX_WINDOW_COMMANDS_PER_EVENT
);
assert!(
cx.window_commands
.iter()
.all(|command| command.handle() == window)
);
}
#[test]
fn document_window_commands_validate_and_retain_exact_native_intent() {
let window = WindowHandle::next();
let mut cx = EventContext {
window: Some(window),
..EventContext::default()
};
assert_eq!(
cx.set_document_path(PathBuf::new()),
Err(WindowCommandError::InvalidDocumentPath)
);
assert_eq!(
cx.set_tabbing_identifier(""),
Err(WindowCommandError::InvalidTabbingIdentifier)
);
assert_eq!(
cx.select_tab(MAX_SYSTEM_WINDOW_TABS),
Err(WindowCommandError::InvalidTabIndex)
);
assert!(cx.window_commands.is_empty());
cx.set_document_path("Cargo.toml").unwrap();
cx.set_window_edited(true).unwrap();
cx.show_character_palette().unwrap();
cx.set_tabbing_identifier("dev.quickgui.workspace").unwrap();
cx.select_next_tab().unwrap();
cx.select_previous_tab().unwrap();
cx.select_tab(7).unwrap();
cx.merge_all_windows().unwrap();
cx.move_tab_to_new_window().unwrap();
cx.toggle_tab_bar().unwrap();
cx.toggle_tab_overview().unwrap();
cx.clear_represented_file().unwrap();
cx.clear_tabbing_identifier().unwrap();
assert_eq!(cx.window_commands.len(), 13);
assert!(matches!(
&cx.window_commands[0],
WindowCommand::SetRepresentedFile(handle, Some(path))
if *handle == window && path == &PathBuf::from("Cargo.toml")
));
assert!(matches!(
&cx.window_commands[1],
WindowCommand::SetDocumentEdited(handle, true) if *handle == window
));
assert!(matches!(
&cx.window_commands[3],
WindowCommand::SetTabbingIdentifier(handle, Some(identifier))
if *handle == window && identifier == "dev.quickgui.workspace"
));
assert!(matches!(
&cx.window_commands[11],
WindowCommand::SetRepresentedFile(handle, None) if *handle == window
));
assert!(matches!(
&cx.window_commands[12],
WindowCommand::SetTabbingIdentifier(handle, None) if *handle == window
));
}
#[test]
fn current_window_commands_fail_without_a_native_owner() {
let mut cx = EventContext::default();
assert_eq!(
cx.resize_window(Size::new(640.0, 480.0)),
Err(WindowCommandError::Unavailable)
);
assert!(cx.window_commands.is_empty());
}
#[test]
fn platform_requests_are_bounded_and_validate_before_retaining() {
let window = WindowHandle::next();
let mut cx = EventContext {
window: Some(window),
..EventContext::default()
};
assert_eq!(cx.open_url(""), Err(PlatformError::InvalidUrl));
assert!(cx.platform_requests.is_empty());
for index in 0..crate::MAX_PLATFORM_REQUESTS_PER_EVENT {
assert!(cx.open_url(format!("https://example.com/{index}")).is_ok());
}
assert_eq!(
cx.open_url("https://example.com/overflow"),
Err(PlatformError::QueueFull)
);
assert_eq!(
cx.platform_requests.len(),
crate::MAX_PLATFORM_REQUESTS_PER_EVENT
);
}
#[test]
fn native_dialogs_require_a_window_owner() {
let mut cx = EventContext::default();
assert!(matches!(
cx.prompt(
PromptLevel::Info,
"Message",
None,
&[PromptButton::ok("OK")],
),
Err(PlatformError::Unavailable)
));
assert!(matches!(
cx.prompt_for_paths(PathPromptOptions::new()),
Err(PlatformError::Unavailable)
));
assert!(cx.platform_requests.is_empty());
}
#[cfg(target_os = "macos")]
#[test]
fn system_notifications_are_app_wide_but_still_bounded() {
let mut cx = EventContext::default();
assert_eq!(
cx.show_system_notification(SystemNotification::new("", "Title", "Body")),
Err(PlatformError::InvalidNotificationTag)
);
assert!(cx.platform_requests.is_empty());
assert!(
cx.show_system_notification(SystemNotification::new(
"background-job",
"Finished",
"The export is ready",
))
.is_ok()
);
assert!(cx.dismiss_system_notification("background-job").is_ok());
assert_eq!(cx.platform_requests.len(), 2);
}
#[test]
fn event_context_globals_are_typed_and_changes_coalesce() {
let mut cx = EventContext::default();
assert!(!cx.has_global::<TestGlobal>());
cx.set_global(TestGlobal(2));
cx.update_global::<TestGlobal, _>(|global| global.0 += 3);
cx.global_mut::<TestGlobal>().0 += 5;
assert_eq!(cx.global::<TestGlobal>().0, 10);
assert_eq!(cx.global_notifications, [TypeId::of::<TestGlobal>()]);
assert!(!cx.notify_all_globals);
assert_eq!(cx.remove_global::<TestGlobal>().0, 10);
assert!(!cx.has_global::<TestGlobal>());
assert_eq!(cx.global_notifications, [TypeId::of::<TestGlobal>()]);
}
#[test]
fn too_many_global_changes_fall_back_to_notify_all() {
let mut cx = EventContext {
global_notifications: vec![
TypeId::of::<TestGlobal>();
MAX_GLOBAL_NOTIFICATIONS_PER_EVENT
],
..EventContext::default()
};
cx.note_global_changed(TypeId::of::<SecondaryView>());
assert!(cx.global_notifications.is_empty());
assert!(cx.notify_all_globals);
}
#[test]
fn native_file_payloads_have_a_hard_path_count_bound() {
let files = DroppedFiles::new(
(0..=MAX_DROPPED_FILES).map(|index| PathBuf::from(format!("file-{index}"))),
);
assert_eq!(files.paths().len(), MAX_DROPPED_FILES);
assert_eq!(files.paths().first(), Some(&PathBuf::from("file-0")));
assert_eq!(
files.paths().last(),
Some(&PathBuf::from(format!("file-{}", MAX_DROPPED_FILES - 1)))
);
assert!(files.is_truncated());
}
#[test]
fn outbound_file_payloads_bound_count_and_path_storage() {
let files = FileDragPaths::new(
(0..=MAX_EXTERNAL_DRAG_FILES)
.map(|index| (PathBuf::from(format!("file-{index}")), index % 2 == 0)),
);
assert_eq!(files.entries().len(), MAX_EXTERNAL_DRAG_FILES);
assert!(files.is_truncated());
let oversized = PathBuf::from("x".repeat(MAX_EXTERNAL_DRAG_PATH_BYTES + 1));
let files = FileDragPaths::new([
(PathBuf::new(), false),
(oversized, false),
(PathBuf::from("kept.txt"), false),
]);
assert_eq!(files.entries(), [(PathBuf::from("kept.txt"), false)]);
assert!(files.is_truncated());
assert_eq!(
FileDragPaths::files([PathBuf::from("file.txt")]).entries(),
[(PathBuf::from("file.txt"), false)]
);
assert_eq!(
FileDragPaths::directories([PathBuf::from("folder")]).entries(),
[(PathBuf::from("folder"), true)]
);
}
#[test]
fn outbound_text_and_url_payloads_are_utf8_safe_and_bounded() {
let shared: Arc<str> = Arc::from("shared native drag text");
let shared_text = ExternalDragText::new(Arc::clone(&shared));
assert!(Arc::ptr_eq(&shared_text.text, &shared));
let text =
ExternalDragText::new(format!("{}é", "x".repeat(MAX_EXTERNAL_DRAG_TEXT_BYTES - 1)));
assert_eq!(text.as_str().len(), MAX_EXTERNAL_DRAG_TEXT_BYTES - 1);
assert!(text.is_truncated());
assert!(text.as_str().is_char_boundary(text.as_str().len()));
let shared_url: Arc<str> = Arc::from("https://example.com/路径?q=quickgui");
let url = ExternalDragUrl::new(Arc::clone(&shared_url)).unwrap();
assert!(Arc::ptr_eq(&url.0, &shared_url));
assert_eq!(url.as_str(), "https://example.com/路径?q=quickgui");
assert_eq!(
ExternalDragUrl::new("relative/path").unwrap_err(),
ExternalDragUrlError::InvalidScheme
);
assert_eq!(
ExternalDragUrl::new("https://example.com/a b").unwrap_err(),
ExternalDragUrlError::InvalidCharacter
);
assert!(matches!(
ExternalDragUrl::new(format!(
"https://example.com/{}",
"x".repeat(MAX_EXTERNAL_DRAG_URL_BYTES)
)),
Err(ExternalDragUrlError::TooLarge { .. })
));
}
#[test]
fn scroll_deltas_preserve_precision_and_bound_platform_values() {
let pixels = ScrollDelta::Pixels(Vector::new(12.5, -24.0)).bounded();
assert!(pixels.precise());
assert_eq!(pixels.pixel_delta(40.0), Vector::new(12.5, -24.0));
let lines = ScrollDelta::Lines(Vector::new(2.0, -3.0)).bounded();
assert!(!lines.precise());
assert_eq!(lines.pixel_delta(32.0), Vector::new(64.0, -96.0));
assert_eq!(
ScrollDelta::Pixels(Vector::new(f32::NAN, f32::INFINITY))
.bounded()
.pixel_delta(40.0),
Vector::ZERO
);
assert_eq!(
ScrollDelta::Lines(Vector::new(MAX_SCROLL_LINES_PER_EVENT * 2.0, -1.0))
.bounded()
.pixel_delta(f32::INFINITY),
Vector::ZERO
);
assert_eq!(
ScrollDelta::Pixels(Vector::new(MAX_SCROLL_PIXELS_PER_EVENT * 2.0, 0.0))
.bounded()
.pixel_delta(40.0),
Vector::new(MAX_SCROLL_PIXELS_PER_EVENT, 0.0)
);
}
#[test]
fn input_propagation_and_default_prevention_are_independent() {
let mut cx = EventContext::default();
cx.stop_propagation();
assert!(cx.stop_event_propagation);
assert!(!cx.prevent_default);
cx.prevent_default();
assert!(cx.stop_event_propagation);
assert!(cx.prevent_default);
cx.propagate();
assert!(!cx.stop_event_propagation);
assert!(cx.prevent_default);
assert!(cx.propagate_action);
}
#[test]
fn raw_touch_samples_are_finite_and_pressure_bounded() {
let event = TouchEvent {
id: TouchId(42),
phase: TouchPhase::Moved,
position: Point::new(f32::INFINITY, -MAX_TOUCH_COORDINATE * 2.0),
force: Some(1.5),
}
.bounded();
assert_eq!(event.id, TouchId(42));
assert_eq!(event.position, Point::new(0.0, -MAX_TOUCH_COORDINATE));
assert_eq!(event.force, Some(1.0));
assert_eq!(
TouchEvent {
force: Some(f32::NAN),
..TouchEvent::default()
}
.bounded()
.force,
Some(0.0)
);
}
#[test]
fn erased_command_registry_actions_keep_their_original_payload() {
#[derive(Clone, Debug, Eq, PartialEq)]
struct OpenLine(usize);
let mut cx = EventContext::default();
cx.dispatch_any_action(AnyAction::new(OpenLine(42)));
assert_eq!(cx.actions.len(), 1);
assert_eq!(
cx.actions[0].downcast_ref::<OpenLine>(),
Some(&OpenLine(42))
);
}
#[test]
fn cross_window_actions_are_parent_aware_and_hard_bounded() {
#[derive(Clone, Debug, Eq, PartialEq)]
struct OpenLine(usize);
let parent = WindowHandle::next();
let sibling = WindowHandle::next();
let mut cx = EventContext {
window: Some(WindowHandle::next()),
parent_window: Some(parent),
..EventContext::default()
};
assert_eq!(cx.parent_window_handle(), Some(parent));
assert!(cx.dispatch_action_to_parent(OpenLine(7)));
assert!(cx.dispatch_action_to_window(sibling, OpenLine(9)));
assert_eq!(cx.targeted_actions.len(), 2);
assert_eq!(cx.targeted_actions[0].0, parent);
assert_eq!(
cx.targeted_actions[0].1.downcast_ref::<OpenLine>(),
Some(&OpenLine(7))
);
assert_eq!(cx.targeted_actions[1].0, sibling);
cx.targeted_actions.clear();
for line in 0..MAX_TARGETED_ACTIONS_PER_EVENT {
assert!(cx.dispatch_action_to_parent(OpenLine(line)));
}
assert!(!cx.dispatch_action_to_parent(OpenLine(usize::MAX)));
assert_eq!(cx.targeted_actions.len(), MAX_TARGETED_ACTIONS_PER_EVENT);
let mut root = EventContext::default();
assert!(!root.dispatch_action_to_parent(OpenLine(1)));
assert!(root.targeted_actions.is_empty());
}
#[test]
fn programmatic_form_submissions_coalesce_and_stay_bounded() {
let mut cx = EventContext::default();
assert!(cx.submit_form("profile"));
assert!(cx.submit_form("profile"));
assert_eq!(cx.form_submissions, [ElementId::named("profile")]);
for index in 1..MAX_FORM_SUBMISSIONS_PER_EVENT {
assert!(cx.submit_form(index));
}
assert_eq!(cx.form_submissions.len(), MAX_FORM_SUBMISSIONS_PER_EVENT);
assert!(!cx.submit_form(usize::MAX));
}
}