Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Nov 12, 2024
1 parent 414d4a6 commit ceea0b5
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 92 deletions.
4 changes: 2 additions & 2 deletions src/animate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
ViewId,
};

use std::any::Any;
use std::rc::Rc;
use std::{any::Any, sync::Arc};

use floem_reactive::{create_updater, RwSignal, SignalGet, Trigger};
use smallvec::{smallvec, SmallVec};
Expand All @@ -24,7 +24,7 @@ use web_time::{Duration, Instant};
#[derive(Clone, Debug)]
pub struct KeyFrameProp {
// the style prop value. This will either come from an animation frameor it will be pulled from the computed style
val: Rc<dyn Any>,
val: Arc<dyn Any>,
// the frame id
id: u16,
/// This easing will be used while animating towards this keyframe. while this prop is the lower one this easing function will not be used.
Expand Down
24 changes: 15 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use raw_window_handle::HasDisplayHandle;
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy},
event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy},
monitor::MonitorHandle,
window::WindowId,
};
Expand Down Expand Up @@ -157,12 +157,14 @@ impl Application {
pub fn new() -> Self {
let event_loop = EventLoop::new().expect("can't start the event loop");
let event_loop_proxy = event_loop.create_proxy();
*EVENT_LOOP_PROXY.lock() = Some(event_loop_proxy.clone());
let (sender, receiver) = crossbeam_channel::unbounded();
*EVENT_LOOP_PROXY.lock() = Some((event_loop_proxy.clone(), sender));
unsafe {
Clipboard::init(event_loop.display_handle().unwrap().as_raw());
}
let handle = ApplicationHandle::new();
Self {
receiver,
handle: Some(handle),
event_listener: None,
event_loop: Some(event_loop),
Expand All @@ -187,8 +189,7 @@ impl Application {
config: Option<WindowConfig>,
) -> Self {
self.handle.as_mut().unwrap().new_window(
&self.event_loop,
self.event_loop.create_proxy(),
self.event_loop.as_ref().unwrap(),
Box::new(|window_id| app_view(window_id).into_any()),
config.unwrap_or_default(),
);
Expand Down Expand Up @@ -237,12 +238,19 @@ impl Application {
}
}

pub(crate) fn send_proxy_event(event: UserEvent) {
if let Some((proxy, sender)) = EVENT_LOOP_PROXY.lock().as_ref() {
let _ = sender.send(event);
proxy.wake_up();
}
}

pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
self.event_loop.available_monitors()
self.event_loop.as_ref().unwrap().available_monitors()
}

pub fn primary_monitor(&self) -> Option<MonitorHandle> {
self.event_loop.primary_monitor()
self.event_loop.as_ref().unwrap().primary_monitor()
}
}

Expand All @@ -251,7 +259,5 @@ impl Application {
/// This function sends a `QuitApp` event to the application's event loop,
/// triggering the application to close gracefully.
pub fn quit_app() {
Application::with_event_loop_proxy(|proxy| {
let _ = proxy.send_event(UserEvent::QuitApp);
});
Application::send_proxy_event(UserEvent::QuitApp);
}
6 changes: 2 additions & 4 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl ApplicationHandle {

pub(crate) fn handle_user_event(
&mut self,
event_loop: &EventLoopWindowTarget<UserEvent>,
event_loop: &ActiveEventLoop,
event_proxy: EventLoopProxy<UserEvent>,
event: UserEvent,
) {
Expand Down Expand Up @@ -274,7 +274,6 @@ impl ApplicationHandle {
pub(crate) fn new_window(
&mut self,
event_loop: &dyn ActiveEventLoop,
event_proxy: EventLoopProxy,
view_fn: Box<dyn FnOnce(WindowId) -> Box<dyn View>>,
#[allow(unused_variables)] WindowConfig {
size,
Expand Down Expand Up @@ -336,7 +335,7 @@ impl ApplicationHandle {
}

if let Some(logical_size) = logical_size {
window_attributes = window_attributes.with_inner_size(logical_size);
window_attributes = window_attributes.with_surface_size(logical_size);
}

#[cfg(not(target_os = "macos"))]
Expand Down Expand Up @@ -420,7 +419,6 @@ impl ApplicationHandle {
let window_id = window.id();
let window_handle = WindowHandle::new(
window,
event_proxy,
view_fn,
transparent,
apply_default_theme,
Expand Down
19 changes: 8 additions & 11 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
rc::Rc,
sync::Arc,
};
use winit::window::Window;

#[cfg(not(target_arch = "wasm32"))]
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -1169,7 +1170,7 @@ impl<'a> PaintCx<'a> {
pub enum PaintState {
/// The renderer is not yet initialized. This state is used to wait for the GPU resources to be acquired.
PendingGpuResources {
window: Arc<dyn wgpu::WindowHandle>,
window: Arc<dyn Window>,
rx: crossbeam::channel::Receiver<Result<GpuResources, GpuResourceError>>,
font_embolden: f32,
/// This field holds an instance of `Renderer::Uninitialized` until the GPU resources are acquired,
Expand All @@ -1178,17 +1179,15 @@ pub enum PaintState {
///
/// Previously, `PaintState::renderer` and `PaintState::renderer_mut` would panic if called when the renderer was uninitialized.
/// However, this turned out to be hard to handle properly and led to panics, especially since the rest of the application code can't control when the renderer is initialized.
renderer: crate::renderer::Renderer<Arc<dyn wgpu::WindowHandle>>,
renderer: crate::renderer::Renderer,
},
/// The renderer is initialized and ready to paint.
Initialized {
renderer: crate::renderer::Renderer<Arc<dyn wgpu::WindowHandle>>,
},
Initialized { renderer: crate::renderer::Renderer },
}

impl PaintState {
pub fn new(
window: Arc<dyn wgpu::WindowHandle>,
window: Arc<dyn Window>,
rx: crossbeam::channel::Receiver<Result<GpuResources, GpuResourceError>>,
scale: f64,
size: Size,
Expand Down Expand Up @@ -1224,16 +1223,14 @@ impl PaintState {
}
}

pub(crate) fn renderer(&self) -> &crate::renderer::Renderer<Arc<dyn wgpu::WindowHandle>> {
pub(crate) fn renderer(&self) -> &crate::renderer::Renderer {
match self {
PaintState::PendingGpuResources { renderer, .. } => renderer,
PaintState::Initialized { renderer } => renderer,
}
}

pub(crate) fn renderer_mut(
&mut self,
) -> &mut crate::renderer::Renderer<Arc<dyn wgpu::WindowHandle>> {
pub(crate) fn renderer_mut(&mut self) -> &mut crate::renderer::Renderer {
match self {
PaintState::PendingGpuResources { renderer, .. } => renderer,
PaintState::Initialized { renderer } => renderer,
Expand Down Expand Up @@ -1264,7 +1261,7 @@ impl<'a> UpdateCx<'a> {
}

impl Deref for PaintCx<'_> {
type Target = crate::renderer::Renderer<Arc<dyn wgpu::WindowHandle>>;
type Target = crate::renderer::Renderer;

fn deref(&self) -> &Self::Target {
self.paint_state.renderer()
Expand Down
8 changes: 4 additions & 4 deletions src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl CapturedView {
children: id
.children()
.into_iter()
.map(|view| Rc::new(CapturedView::capture(view, app_state, clipped)))
.map(|view| Arc::new(CapturedView::capture(view, app_state, clipped)))
.collect(),
}
}
Expand Down Expand Up @@ -1173,7 +1173,7 @@ pub fn capture(window_id: WindowId) {
})
}

fn find_view(name: &str, views: &Rc<CapturedView>) -> Vec<ViewId> {
fn find_view(name: &str, views: &Arc<CapturedView>) -> Vec<ViewId> {
let mut ids = Vec::new();
if name.is_empty() {
return ids;
Expand Down Expand Up @@ -1205,7 +1205,7 @@ fn find_view(name: &str, views: &Rc<CapturedView>) -> Vec<ViewId> {

fn find_relative_view_by_id_without_self(
id: ViewId,
views: &Rc<CapturedView>,
views: &Arc<CapturedView>,
) -> Option<RelativeViewId> {
let mut parent_id = None;
let mut big_brother_id = None;
Expand Down Expand Up @@ -1246,7 +1246,7 @@ fn find_relative_view_by_id_without_self(

fn find_relative_view_by_id_with_self(
id: ViewId,
views: &Rc<CapturedView>,
views: &Arc<CapturedView>,
) -> Option<RelativeViewId> {
if views.id == id {
let first_child_id = views.children.first().map(|x| x.id);
Expand Down
18 changes: 9 additions & 9 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
//! - Only one view can be active at a time.
//! - Only one view can be focused at a time.
//!
use std::sync::Arc;

use crate::text::TextLayout;
use floem_renderer::gpu_resources::GpuResources;
use floem_renderer::Img;
Expand All @@ -57,14 +59,15 @@ use floem_vello_renderer::VelloRenderer;
use floem_vger_renderer::VgerRenderer;
use peniko::kurbo::{self, Affine, Rect, Shape, Size, Stroke};
use peniko::BrushRef;
use winit::window::Window;

#[allow(clippy::large_enum_variant)]
pub enum Renderer<W> {
pub enum Renderer {
#[cfg(feature = "vello")]
Vello(VelloRenderer),
#[cfg(not(feature = "vello"))]
Vger(VgerRenderer),
TinySkia(TinySkiaRenderer<W>),
TinySkia(TinySkiaRenderer<Arc<dyn Window>>),
/// Uninitialized renderer, used to allow the renderer to be created lazily
/// All operations on this renderer are no-ops
Uninitialized {
Expand All @@ -73,17 +76,14 @@ pub enum Renderer<W> {
},
}

impl<W: wgpu::WindowHandle> Renderer<W> {
impl Renderer {
pub fn new(
window: W,
window: Arc<dyn Window>,
gpu_resources: GpuResources,
scale: f64,
size: Size,
font_embolden: f32,
) -> Self
where
W: Clone + 'static,
{
) -> Self {
let size = Size::new(size.width.max(1.0), size.height.max(1.0));

let force_tiny_skia = std::env::var("FLOEM_FORCE_TINY_SKIA")
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<W: wgpu::WindowHandle> Renderer<W> {
}
}

impl<W: wgpu::WindowHandle> floem_renderer::Renderer for Renderer<W> {
impl floem_renderer::Renderer for Renderer {
fn begin(&mut self, capture: bool) {
match self {
#[cfg(feature = "vello")]
Expand Down
11 changes: 8 additions & 3 deletions src/screen_layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Tools for computing screen locations from locations within a View and
//! vice-versa.
use std::sync::Arc;

use crate::ViewId;
use peniko::kurbo::{Point, Rect, Size};
use winit::window::{Window, WindowId};
Expand All @@ -26,7 +28,7 @@ pub fn try_create_screen_layout(view: &ViewId) -> Option<ScreenLayout> {
.outer_position()
.map(|outer_position| {
let monitor_bounds = monitor_bounds_for_monitor(window, &monitor);
let inner_size = window.inner_size();
let inner_size = window.surface_size();
let outer_size = window.outer_size();

let window_bounds = rect_from_physical_bounds_for_window(
Expand Down Expand Up @@ -63,7 +65,10 @@ pub fn try_create_screen_layout(view: &ViewId) -> Option<ScreenLayout> {
.unwrap_or(None)
}

pub fn screen_layout_for_window(window_id: WindowId, window: &Window) -> Option<ScreenLayout> {
pub fn screen_layout_for_window(
window_id: WindowId,
window: &Arc<dyn Window>,
) -> Option<ScreenLayout> {
window
.current_monitor()
.map(|monitor| {
Expand All @@ -74,7 +79,7 @@ pub fn screen_layout_for_window(window_id: WindowId, window: &Window) -> Option<
.outer_position()
.map(|outer_position| {
let monitor_bounds = monitor_bounds_for_monitor(window, &monitor);
let inner_size = window.inner_size();
let inner_size = window.surface_size();
let outer_size = window.outer_size();

let window_bounds = rect_from_physical_bounds_for_window(
Expand Down
Loading

0 comments on commit ceea0b5

Please sign in to comment.