use std::{
borrow::Cow,
fmt,
rc::Rc,
time::Instant,
};
#[cfg(feature = "accessibility")]
use accesskit_winit::WindowEvent as AccessibilityWindowEvent;
use freya_components::cache::AssetCacher;
use freya_core::{
integration::*,
metrics::Metrics,
prelude::GlobalTasks,
};
use freya_engine::prelude::{
FontCollection,
FontMgr,
SkData,
TypefaceFontProvider,
register_font_typeface,
};
use rustc_hash::FxHashMap;
use torin::prelude::CursorPoint;
#[cfg(feature = "tray")]
use tray_icon::TrayIcon;
#[cfg(feature = "accessibility")]
use winit::dpi::{
LogicalPosition,
LogicalSize,
};
use winit::{
application::ApplicationHandler,
event::{
ElementState,
Ime,
MouseScrollDelta,
Touch,
TouchPhase,
WindowEvent,
},
event_loop::{
ActiveEventLoop,
ControlFlow,
EventLoopProxy,
},
window::{
Theme,
WindowId,
},
};
#[cfg(feature = "accessibility")]
use crate::integration::is_ime_role;
use crate::{
accessibility::AccessibilityTask,
config::{
CloseDecision,
WindowConfig,
},
drivers::{
GraphicsContext,
GraphicsDriver,
},
plugins::{
PluginEvent,
PluginHandle,
PluginsManager,
},
window::{
AppWindow,
frame_interval,
},
winit_mappings::{
self,
map_winit_mouse_button,
map_winit_touch_force,
map_winit_touch_phase,
},
};
pub struct WinitRenderer {
pub windows_configs: Vec<WindowConfig>,
#[cfg(feature = "tray")]
pub(crate) tray: (
Option<crate::config::TrayIconGetter>,
Option<crate::config::TrayHandler>,
),
#[cfg(feature = "tray")]
pub(crate) tray_icon: Option<TrayIcon>,
pub resumed: bool,
pub windows: FxHashMap<WindowId, AppWindow>,
pub global_contexts: GlobalContexts,
pub proxy: EventLoopProxy<NativeEvent>,
pub plugins: PluginsManager,
pub fallback_fonts: Vec<Cow<'static, str>>,
pub font_provider: TypefaceFontProvider,
pub font_manager: FontMgr,
pub font_collection: FontCollection,
pub exit_on_close: bool,
pub gpu_resource_cache_limit: usize,
pub graphics_context: GraphicsContext,
}
impl WinitRenderer {
fn process_pending_events(&mut self) {
let now = Instant::now();
for app in self.windows.values_mut() {
if !app
.event_buffer
.deadline()
.is_some_and(|deadline| deadline <= now)
{
continue;
}
app.event_buffer.interval = frame_interval(&app.window);
if app.process_buffered_events(
&mut self.font_collection,
&self.font_manager,
&self.fallback_fonts,
&mut self.plugins,
PluginHandle::new(&self.proxy),
) {
app.window.request_redraw();
}
}
}
}
impl Drop for WinitRenderer {
fn drop(&mut self) {
let tasks = self.global_contexts.get_context::<GlobalTasks>();
tasks.clear();
for window_id in self.windows.keys() {
Platform::get().unregister_window(*window_id);
}
self.windows.clear();
tasks.clear();
self.global_contexts.unregister();
}
}
pub struct RendererContext<'a> {
pub windows: &'a mut FxHashMap<WindowId, AppWindow>,
pub global_contexts: &'a GlobalContexts,
pub proxy: &'a mut EventLoopProxy<NativeEvent>,
pub plugins: &'a mut PluginsManager,
pub fallback_fonts: &'a mut Vec<Cow<'static, str>>,
pub font_manager: &'a mut FontMgr,
pub font_collection: &'a mut FontCollection,
pub active_event_loop: &'a ActiveEventLoop,
pub gpu_resource_cache_limit: usize,
pub graphics_context: &'a mut GraphicsContext,
}
impl RendererContext<'_> {
pub fn launch_window(&mut self, window_config: WindowConfig) -> WindowId {
let app_window = AppWindow::new(
window_config,
self.active_event_loop,
self.proxy,
self.plugins,
self.font_collection,
self.font_manager,
self.fallback_fonts,
self.gpu_resource_cache_limit,
self.graphics_context,
);
let window_id = app_window.window.id();
self.proxy
.send_event(NativeEvent::Window(NativeWindowEvent {
window_id,
action: NativeWindowEventAction::PollRunner,
}))
.ok();
self.windows.insert(window_id, app_window);
window_id
}
pub fn windows(&self) -> &FxHashMap<WindowId, AppWindow> {
self.windows
}
pub fn windows_mut(&mut self) -> &mut FxHashMap<WindowId, AppWindow> {
self.windows
}
pub fn exit(&mut self) {
self.active_event_loop.exit();
}
}
#[derive(Debug)]
pub enum NativeWindowEventAction {
PollRunner,
#[cfg(feature = "accessibility")]
Accessibility(AccessibilityWindowEvent),
PlatformEvent(PlatformEvent),
User(UserEvent),
}
#[derive(Clone)]
pub struct LaunchProxy(pub EventLoopProxy<NativeEvent>);
impl LaunchProxy {
pub fn post_callback<F, T: 'static>(&self, f: F) -> futures_channel::oneshot::Receiver<T>
where
F: FnOnce(&mut RendererContext) -> T + 'static,
{
let (tx, rx) = futures_channel::oneshot::channel::<T>();
let cb = Box::new(move |ctx: &mut RendererContext| {
let res = (f)(ctx);
let _ = tx.send(res);
});
let _ = self
.0
.send_event(NativeEvent::Generic(NativeGenericEvent::User(
GlobalUserEvent::Erased(SingleThreadErasedEvent(Box::new(
NativePlatformErasedEventAction::RendererCallback(cb),
))),
)));
rx
}
}
pub type RendererCallback = Box<dyn FnOnce(&mut RendererContext) + 'static>;
pub enum NativePlatformErasedEventAction {
LaunchWindow {
window_config: Box<WindowConfig>,
ack: futures_channel::oneshot::Sender<WindowId>,
},
CloseWindow(WindowId),
RendererCallback(RendererCallback),
}
#[derive(Debug)]
pub struct NativeWindowEvent {
pub window_id: WindowId,
pub action: NativeWindowEventAction,
}
pub enum NativeGenericEvent {
PollFutures,
User(GlobalUserEvent),
}
impl fmt::Debug for NativeGenericEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NativeGenericEvent::PollFutures => f.write_str("PollFutures"),
NativeGenericEvent::User(event) => f.debug_tuple("User").field(event).finish(),
}
}
}
unsafe impl Send for NativeGenericEvent {}
unsafe impl Sync for NativeGenericEvent {}
#[derive(Debug)]
pub enum NativeEvent {
Window(NativeWindowEvent),
#[cfg(feature = "tray")]
Tray(crate::tray_icon::TrayEvent),
Generic(NativeGenericEvent),
Preferences(mundy::Preferences),
}
#[cfg(feature = "accessibility")]
impl From<accesskit_winit::Event> for NativeEvent {
fn from(event: accesskit_winit::Event) -> Self {
NativeEvent::Window(NativeWindowEvent {
window_id: event.window_id,
action: NativeWindowEventAction::Accessibility(event.window_event),
})
}
}
impl ApplicationHandler<NativeEvent> for WinitRenderer {
fn resumed(&mut self, active_event_loop: &winit::event_loop::ActiveEventLoop) {
if !self.resumed {
#[cfg(feature = "tray")]
{
if let Some(tray_icon) = self.tray.0.take() {
self.tray_icon = Some((tray_icon)());
}
#[cfg(target_os = "macos")]
{
use objc2_core_foundation::CFRunLoop;
let rl = CFRunLoop::main().expect("Failed to run CFRunLoop");
CFRunLoop::wake_up(&rl);
}
}
for window_config in self.windows_configs.drain(..) {
let app_window = AppWindow::new(
window_config,
active_event_loop,
&self.proxy,
&mut self.plugins,
&mut self.font_collection,
&self.font_manager,
&self.fallback_fonts,
self.gpu_resource_cache_limit,
&mut self.graphics_context,
);
self.proxy
.send_event(NativeEvent::Window(NativeWindowEvent {
window_id: app_window.window.id(),
action: NativeWindowEventAction::PollRunner,
}))
.ok();
self.windows.insert(app_window.window.id(), app_window);
}
self.resumed = true;
subscribe_preferences(self.proxy.clone());
let _ = self
.proxy
.send_event(NativeEvent::Generic(NativeGenericEvent::PollFutures));
} else {
let old_windows: Vec<_> = self.windows.drain().collect();
for (old_id, mut app_window) in old_windows {
let (new_driver, new_window) = GraphicsDriver::new(
active_event_loop,
app_window.window_attributes.clone(),
self.gpu_resource_cache_limit,
app_window.renderer,
&mut self.graphics_context,
);
let new_id = new_window.id();
Platform::get().unregister_window(old_id);
app_window.platform.id = new_id.into();
app_window.platform.sender = Rc::new({
let proxy = self.proxy.clone();
move |event| {
let _ = proxy.send_event(NativeEvent::Window(NativeWindowEvent {
window_id: new_id,
action: NativeWindowEventAction::User(event),
}));
}
});
app_window
.runner
.provide_root_context(|| CurrentWindowId(app_window.platform.id));
Platform::get().register_window(app_window.platform.clone());
app_window.driver = new_driver;
app_window.window = new_window;
app_window.process_layout_on_next_render = true;
app_window.tree.layout.reset();
self.windows.insert(new_id, app_window);
self.proxy
.send_event(NativeEvent::Window(NativeWindowEvent {
window_id: new_id,
action: NativeWindowEventAction::PollRunner,
}))
.ok();
}
}
}
fn user_event(
&mut self,
active_event_loop: &winit::event_loop::ActiveEventLoop,
event: NativeEvent,
) {
match event {
NativeEvent::Generic(NativeGenericEvent::PollFutures) => {
GlobalTasks::get().poll();
}
NativeEvent::Preferences(prefs) => {
for app in self.windows.values_mut() {
app.platform
.accent_color
.set_if_modified(prefs.accent_color);
}
}
#[cfg(feature = "tray")]
NativeEvent::Tray(event) => {
let renderer_context = RendererContext {
fallback_fonts: &mut self.fallback_fonts,
active_event_loop,
windows: &mut self.windows,
global_contexts: &self.global_contexts,
proxy: &mut self.proxy,
plugins: &mut self.plugins,
font_manager: &mut self.font_manager,
font_collection: &mut self.font_collection,
gpu_resource_cache_limit: self.gpu_resource_cache_limit,
graphics_context: &mut self.graphics_context,
};
if let Some(tray_handler) = &mut self.tray.1 {
(tray_handler)(event, renderer_context);
}
}
NativeEvent::Generic(NativeGenericEvent::User(GlobalUserEvent::LoadFont {
font_name,
font_data,
})) => {
let Some(typeface) = FontMgr::custom_empty()
.unwrap_or_default()
.new_from_data(SkData::new_copy(&font_data), None)
else {
tracing::error!("Failed to load font {font_name}.");
return;
};
register_font_typeface(&mut self.font_provider, font_name.as_ref(), typeface);
self.font_collection.clear_caches();
for app in self.windows.values_mut() {
app.process_layout_on_next_render = true;
app.tree.layout.reset();
app.tree.text_cache.reset();
app.window.request_redraw();
}
}
NativeEvent::Generic(NativeGenericEvent::User(event)) => match event {
GlobalUserEvent::OpenUrl(url) => {
if let Err(error) = open::that(&url) {
tracing::error!(%error, %url, "Failed to open URL");
}
}
GlobalUserEvent::Exit => active_event_loop.exit(),
GlobalUserEvent::LoadFont { .. } => unreachable!(),
GlobalUserEvent::Erased(data) => {
let action = data
.0
.downcast::<NativePlatformErasedEventAction>()
.expect("Expected NativePlatformErasedEventAction");
match *action {
NativePlatformErasedEventAction::LaunchWindow { window_config, ack } => {
let app_window = AppWindow::new(
*window_config,
active_event_loop,
&self.proxy,
&mut self.plugins,
&mut self.font_collection,
&self.font_manager,
&self.fallback_fonts,
self.gpu_resource_cache_limit,
&mut self.graphics_context,
);
let window_id = app_window.window.id();
let _ = self
.proxy
.send_event(NativeEvent::Window(NativeWindowEvent {
window_id,
action: NativeWindowEventAction::PollRunner,
}));
self.windows.insert(window_id, app_window);
let _ = ack.send(window_id);
}
NativePlatformErasedEventAction::CloseWindow(window_id) => {
Platform::get().unregister_window(window_id);
if let Some(app) = self.windows.remove(&window_id) {
self.plugins.send(
PluginEvent::WindowClosed {
window: &app.window,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
}
#[cfg(feature = "tray")]
let has_tray = self.tray.1.is_some();
#[cfg(not(feature = "tray"))]
let has_tray = false;
if self.windows.is_empty() && !has_tray && self.exit_on_close {
active_event_loop.exit();
}
}
NativePlatformErasedEventAction::RendererCallback(callback) => {
let mut renderer_context = RendererContext {
fallback_fonts: &mut self.fallback_fonts,
active_event_loop,
windows: &mut self.windows,
proxy: &mut self.proxy,
plugins: &mut self.plugins,
font_manager: &mut self.font_manager,
font_collection: &mut self.font_collection,
gpu_resource_cache_limit: self.gpu_resource_cache_limit,
graphics_context: &mut self.graphics_context,
global_contexts: &self.global_contexts,
};
callback(&mut renderer_context);
}
}
}
},
NativeEvent::Window(NativeWindowEvent { action, window_id }) => {
if let Some(app) = &mut self.windows.get_mut(&window_id) {
match action {
NativeWindowEventAction::PollRunner => {
if app.poll_runner(&mut self.plugins, PluginHandle::new(&self.proxy)) {
app.window.request_redraw();
}
}
#[cfg(feature = "accessibility")]
NativeWindowEventAction::Accessibility(
accesskit_winit::WindowEvent::AccessibilityDeactivated,
) => {
app.screen_reader.set(false);
}
#[cfg(feature = "accessibility")]
NativeWindowEventAction::Accessibility(
accesskit_winit::WindowEvent::ActionRequested(request),
) => {
if app.accessibility.handle_action(
request,
&mut app.tree,
&app.events_sender,
) {
app.accessibility_tasks_for_next_render |=
AccessibilityTask::ProcessUpdate { mode: None };
app.window.request_redraw();
}
}
#[cfg(feature = "accessibility")]
NativeWindowEventAction::Accessibility(
accesskit_winit::WindowEvent::InitialTreeRequested,
) => {
app.accessibility_tasks_for_next_render = AccessibilityTask::Init;
app.window.request_redraw();
}
NativeWindowEventAction::User(user_event) => match user_event {
UserEvent::RequestRedraw => {
app.window.request_redraw();
}
UserEvent::FocusAccessibilityNode(strategy) => {
let task = match strategy {
AccessibilityFocusStrategy::Backward(_)
| AccessibilityFocusStrategy::Forward(_) => {
AccessibilityTask::ProcessUpdate {
mode: Some(NavigationMode::Keyboard),
}
}
_ => AccessibilityTask::ProcessUpdate { mode: None },
};
if let AccessibilityFocusStrategy::Node(id) = &strategy {
app.platform.focused_accessibility_id.set_if_modified(*id);
}
app.tree.accessibility_diff.request_focus(strategy);
app.accessibility_tasks_for_next_render |= task;
app.window.request_redraw();
}
UserEvent::SetCustomScaleFactor(custom_scale_factor) => {
app.set_custom_scale_factor(custom_scale_factor);
}
},
NativeWindowEventAction::PlatformEvent(platform_event) => {
app.queue_platform_events(vec![platform_event]);
}
}
}
}
}
}
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
self.process_pending_events();
if !matches!(event_loop.control_flow(), ControlFlow::Poll) {
let deadline = self
.windows
.values()
.filter_map(|app| app.event_buffer.deadline())
.min();
event_loop.set_control_flow(match deadline {
Some(deadline) => ControlFlow::WaitUntil(deadline),
None => ControlFlow::Wait,
});
}
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
let mut needs_recovery = false;
if let Some(app) = &mut self.windows.get_mut(&window_id) {
#[cfg(feature = "accessibility")]
app.accessibility_adapter.process_event(&app.window, &event);
match event {
WindowEvent::ThemeChanged(theme) => {
app.platform.preferred_theme.set(match theme {
Theme::Light => PreferredTheme::Light,
Theme::Dark => PreferredTheme::Dark,
});
}
WindowEvent::ScaleFactorChanged { .. } => {
app.scale_factor_changed();
}
WindowEvent::CloseRequested => {
let mut on_close_hook = self
.windows
.get_mut(&window_id)
.and_then(|app| app.on_close.take());
let decision = if let Some(ref mut on_close) = on_close_hook {
let renderer_context = RendererContext {
fallback_fonts: &mut self.fallback_fonts,
active_event_loop: event_loop,
windows: &mut self.windows,
global_contexts: &self.global_contexts,
proxy: &mut self.proxy,
plugins: &mut self.plugins,
font_manager: &mut self.font_manager,
font_collection: &mut self.font_collection,
gpu_resource_cache_limit: self.gpu_resource_cache_limit,
graphics_context: &mut self.graphics_context,
};
on_close(renderer_context, window_id)
} else {
CloseDecision::Close
};
if matches!(decision, CloseDecision::KeepOpen)
&& let Some(app) = self.windows.get_mut(&window_id)
{
app.on_close = on_close_hook;
}
if matches!(decision, CloseDecision::Close) {
Platform::get().unregister_window(window_id);
if let Some(app) = self.windows.remove(&window_id) {
self.plugins.send(
PluginEvent::WindowClosed {
window: &app.window,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
}
let has_windows = !self.windows.is_empty();
let has_tray = {
#[cfg(feature = "tray")]
{
self.tray.1.is_some()
}
#[cfg(not(feature = "tray"))]
{
false
}
};
if !has_windows && !has_tray && self.exit_on_close {
event_loop.exit();
}
}
}
WindowEvent::ModifiersChanged(modifiers) => {
app.modifiers_state = modifiers.state();
}
WindowEvent::Focused(is_focused) => {
app.platform.is_app_focused.set_if_modified(is_focused);
if !is_focused && !app.pressed_keys.is_empty() {
let modifiers = winit_mappings::map_winit_modifiers(app.modifiers_state);
let platform_events: Vec<_> = app
.pressed_keys
.drain(..)
.map(|(key, code)| PlatformEvent::Keyboard {
name: KeyboardEventName::KeyUp,
key,
code,
modifiers,
})
.collect();
app.queue_platform_events(platform_events);
}
}
WindowEvent::RedrawRequested => {
let scale_factor = app.effective_scale_factor();
hotpath::measure_block!("RedrawRequested", {
app.measure_layout(
&mut self.font_collection,
&self.font_manager,
&self.fallback_fonts,
&mut self.plugins,
PluginHandle::new(&self.proxy),
);
let resource_cache = app.driver.resource_cache_usage();
let present_result = app.driver.present(
app.window.inner_size().cast(),
&app.window,
|surface| {
self.plugins.send(
PluginEvent::BeforeRender {
window: &app.window,
canvas: surface.canvas(),
font_collection: &self.font_collection,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
let render_pipeline = RenderPipeline {
font_collection: &mut self.font_collection,
font_manager: &self.font_manager,
tree: &app.tree,
canvas: surface.canvas(),
scale_factor,
background: app.background,
};
render_pipeline.render();
let cached_assets = app.runner.with_root_context(|| {
AssetCacher::try_get()
.map(|asset_cacher| asset_cacher.cached_size())
.unwrap_or_default()
});
let metrics = Metrics::new(
&app.runner,
&app.tree,
cached_assets,
resource_cache,
);
self.plugins.send(
PluginEvent::AfterRender {
window: &app.window,
canvas: surface.canvas(),
font_collection: &self.font_collection,
tree: &app.tree,
animation_clock: &app.animation_clock,
metrics,
},
PluginHandle::new(&self.proxy),
);
for render_callback in app.render_callbacks.drain(..) {
render_callback(&mut *surface);
}
self.plugins.send(
PluginEvent::BeforePresenting {
window: &app.window,
font_collection: &self.font_collection,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
},
);
if let Err(error) = present_result {
tracing::warn!(
"Graphics driver lost ({error:?}), rebuilding on the same window"
);
needs_recovery = true;
}
self.plugins.send(
PluginEvent::AfterPresenting {
window: &app.window,
font_collection: &self.font_collection,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
self.plugins.send(
PluginEvent::BeforeAccessibility {
window: &app.window,
font_collection: &self.font_collection,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
match app.accessibility_tasks_for_next_render.take() {
AccessibilityTask::ProcessUpdate { mode } => {
app.process_accessibility_update(mode);
}
#[cfg(feature = "accessibility")]
AccessibilityTask::Init => {
let title = app.window.title();
let update = app.accessibility.init(&mut app.tree, &title);
app.platform
.focused_accessibility_id
.set_if_modified(update.focus);
let node_id = app.accessibility.focused_node_id().unwrap();
let layout_node = app.tree.layout.get(&node_id).unwrap();
let focused_node = AccessibilityTree::create_node(
node_id,
layout_node,
&app.tree,
&title,
);
app.window.set_ime_allowed(is_ime_role(focused_node.role()));
app.platform
.focused_accessibility_node
.set_if_modified(focused_node);
let area = layout_node.visible_area();
app.window.set_ime_cursor_area(
LogicalPosition::new(area.min_x(), area.min_y()),
LogicalSize::new(area.width(), area.height()),
);
#[cfg(feature = "accessibility")]
{
app.screen_reader.set(true);
app.accessibility_adapter.update_if_active(|| update);
}
}
AccessibilityTask::None => {}
}
self.plugins.send(
PluginEvent::AfterAccessibility {
window: &app.window,
font_collection: &self.font_collection,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
app.ticker_sender.notify();
self.plugins.send(
PluginEvent::AfterRedraw {
window: &app.window,
font_collection: &self.font_collection,
tree: &app.tree,
},
PluginHandle::new(&self.proxy),
);
});
}
WindowEvent::Resized(size) => {
if let Err(error) = app.driver.resize(size) {
tracing::warn!(
"Graphics driver lost while resizing ({error:?}), rebuilding on the same window"
);
needs_recovery = true;
}
app.window.request_redraw();
app.process_layout_on_next_render = true;
app.tree.layout.clear_dirty();
app.tree.layout.invalidate(NodeId::ROOT);
}
WindowEvent::MouseInput { state, button, .. } => {
app.mouse_state = state;
app.platform
.navigation_mode
.set(NavigationMode::NotKeyboard);
let name = if state == ElementState::Pressed {
MouseEventName::MouseDown
} else {
MouseEventName::MouseUp
};
app.queue_platform_events(vec![PlatformEvent::Mouse {
name,
cursor: (app.position.x, app.position.y).into(),
button: Some(map_winit_mouse_button(button)),
}]);
}
WindowEvent::KeyboardInput {
event,
is_synthetic,
..
} => {
if is_synthetic && event.state == ElementState::Pressed {
return;
}
let name = match event.state {
ElementState::Pressed => KeyboardEventName::KeyDown,
ElementState::Released => KeyboardEventName::KeyUp,
};
let key = winit_mappings::map_winit_key(&event.logical_key);
let code = winit_mappings::map_winit_physical_key(&event.physical_key);
let modifiers = winit_mappings::map_winit_modifiers(app.modifiers_state);
app.pressed_keys
.retain(|(_, pressed_code)| *pressed_code != code);
if event.state.is_pressed() {
app.pressed_keys.push((key.clone(), code));
}
self.plugins.send(
PluginEvent::KeyboardInput {
window: &app.window,
key: key.clone(),
code,
modifiers,
is_pressed: event.state.is_pressed(),
},
PluginHandle::new(&self.proxy),
);
app.queue_platform_events(vec![PlatformEvent::Keyboard {
name,
key,
code,
modifiers,
}]);
}
WindowEvent::MouseWheel { delta, phase, .. } => {
const WHEEL_SPEED_MODIFIER: f64 = 53.0;
const TOUCHPAD_SPEED_MODIFIER: f64 = 2.0;
let (delta_x, delta_y, source) = match delta {
MouseScrollDelta::LineDelta(x, y) => (
x as f64 * WHEEL_SPEED_MODIFIER,
y as f64 * WHEEL_SPEED_MODIFIER,
WheelSource::Line,
),
MouseScrollDelta::PixelDelta(position) => (
position.x * TOUCHPAD_SPEED_MODIFIER,
position.y * TOUCHPAD_SPEED_MODIFIER,
WheelSource::Pixel,
),
};
app.queue_platform_events(vec![PlatformEvent::Wheel {
name: WheelEventName::Wheel,
scroll: (delta_x, delta_y).into(),
cursor: app.position,
source,
phase: map_winit_touch_phase(phase),
}]);
}
WindowEvent::CursorLeft { .. } => {
if std::mem::replace(&mut app.mouse_state, ElementState::Released)
== ElementState::Released
{
app.cursor_in_window = false;
app.queue_platform_events(vec![PlatformEvent::PointerExit {
cursor: app.position,
}]);
}
}
WindowEvent::CursorMoved { position, .. } => {
app.position = CursorPoint::from((position.x, position.y));
app.cursor_in_window = true;
let mut platform_events = vec![PlatformEvent::Mouse {
name: MouseEventName::MouseMove,
cursor: app.position,
button: None,
}];
if !app.dropped_file_paths.is_empty() {
platform_events.push(PlatformEvent::File {
name: FileEventName::FileDrop,
file_paths: std::mem::take(&mut app.dropped_file_paths),
cursor: app.position,
});
}
app.queue_platform_events(platform_events);
}
WindowEvent::Touch(Touch {
location,
phase,
id,
force,
..
}) => {
app.position = CursorPoint::from((location.x, location.y));
let name = match phase {
TouchPhase::Cancelled => TouchEventName::TouchCancel,
TouchPhase::Ended => TouchEventName::TouchEnd,
TouchPhase::Moved => TouchEventName::TouchMove,
TouchPhase::Started => TouchEventName::TouchStart,
};
app.queue_platform_events(vec![PlatformEvent::Touch {
name,
location: app.position,
finger_id: id,
phase: map_winit_touch_phase(phase),
force: force.map(map_winit_touch_force),
}]);
}
WindowEvent::Ime(Ime::Commit(text)) => {
app.queue_platform_events(vec![PlatformEvent::Keyboard {
name: KeyboardEventName::KeyDown,
key: keyboard_types::Key::Character(text),
code: keyboard_types::Code::Unidentified,
modifiers: winit_mappings::map_winit_modifiers(app.modifiers_state),
}]);
}
WindowEvent::Ime(Ime::Preedit(text, pos)) => {
app.queue_platform_events(vec![PlatformEvent::ImePreedit {
name: ImeEventName::Preedit,
text,
cursor: pos,
}]);
}
WindowEvent::DroppedFile(file_path) => {
app.dropped_file_paths.push(file_path);
}
WindowEvent::HoveredFile(file_path) => {
app.queue_platform_events(vec![PlatformEvent::File {
name: FileEventName::FileHover,
file_paths: vec![file_path],
cursor: app.position,
}]);
}
WindowEvent::HoveredFileCancelled => {
app.queue_platform_events(vec![PlatformEvent::File {
name: FileEventName::FileHoverCancelled,
file_paths: Vec::new(),
cursor: app.position,
}]);
}
_ => {}
}
}
if needs_recovery && let Some(mut app) = self.windows.remove(&window_id) {
drop(app.driver);
app.driver = GraphicsDriver::recover_reusing_window(
event_loop,
&app.window,
self.gpu_resource_cache_limit,
app.window_attributes.transparent,
);
tracing::info!("Recovered onto the {} driver", app.driver.name());
self.plugins.send(
PluginEvent::GraphicsDriverChanged {
window: &app.window,
graphics_driver: app.driver.name(),
gpu_name: app.driver.gpu_name(),
},
PluginHandle::new(&self.proxy),
);
app.window.request_redraw();
self.windows.insert(window_id, app);
}
}
}
fn subscribe_preferences(proxy: EventLoopProxy<NativeEvent>) {
let subscription = mundy::Preferences::subscribe(mundy::Interest::AccentColor, move |prefs| {
let _ = proxy.send_event(NativeEvent::Preferences(prefs));
});
std::mem::forget(subscription);
}