Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic winit port #176

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
817 changes: 704 additions & 113 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ tokio = { version = "1.35", features = ["full"] }
futures-task = "0.3"
bitflags = "2"
tracing = "0.1.37"
accesskit = "0.12"
fnv = "1.0.7"
instant = { version = "0.1.6", features = ["wasm-bindgen"] }
winit = { version = "0.29", features = ["rwh_05"] }

[dependencies.glazier]
git = "https://github.com/linebender/glazier"
rev = "8d2a4b2cafd5e6be49bfb4ac8d0bd26fe02f036e"
default-features = false
features = ["accesskit"]

[dev-dependencies]
env_logger = "0.10.0"
Expand Down
81 changes: 5 additions & 76 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
// limitations under the License.

use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use accesskit::TreeUpdate;
use glazier::{IdleHandle, IdleToken, WindowHandle};
use parley::FontContext;
use tokio::runtime::Runtime;
use vello::{
Expand All @@ -27,8 +24,8 @@ use vello::{
use xilem_core::{AsyncWake, MessageResult};

use crate::widget::{
AccessCx, BoxConstraints, CxState, EventCx, LayoutCx, LifeCycle, LifeCycleCx, PaintCx, Pod,
PodFlags, UpdateCx, ViewContext, WidgetState,
BoxConstraints, CxState, EventCx, LayoutCx, LifeCycle, LifeCycleCx, PaintCx, Pod, PodFlags,
UpdateCx, ViewContext, WidgetState,
};
use crate::{
view::{Cx, Id, View},
Expand All @@ -46,7 +43,6 @@ pub struct App<T, V: View<T>> {
return_chan: tokio::sync::mpsc::Sender<(V, V::State, HashSet<Id>)>,
id: Option<Id>,
events: Vec<Message>,
window_handle: WindowHandle,
root_state: WidgetState,
root_pod: Option<Pod>,
size: Size,
Expand All @@ -55,11 +51,6 @@ pub struct App<T, V: View<T>> {
cx: Cx,
font_cx: FontContext,
pub(crate) rt: Runtime,
// This is allocated an id for AccessKit, but as we get multi-window,
// there should be a real window object with id.
window_id: crate::id::Id,
pub(crate) accesskit_connected: bool,
node_classes: accesskit::NodeClassSet,
}

/// The standard delay for waiting for async futures.
Expand All @@ -79,14 +70,12 @@ struct AppTask<T, V: View<T>, F: FnMut(&mut T) -> V> {
app_logic: F,
view: Option<V>,
state: Option<V::State>,
idle_handle: Option<IdleHandle>,
pending_async: HashSet<Id>,
ui_state: UiState,
}

/// A message sent from the main UI thread ([`App`]) to the [`AppTask`].
pub(crate) enum AppReq {
SetIdleHandle(IdleHandle),
Events(Vec<Message>),
Wake(IdPath),
// Parameter indicates whether it should be delayed for async
Expand Down Expand Up @@ -115,9 +104,6 @@ enum UiState {
WokeUI,
}

#[derive(Clone, Default)]
pub struct WakeQueue(Arc<Mutex<Vec<IdPath>>>);

impl<T: Send + 'static, V: View<T> + 'static> App<T, V>
where
V::State: 'static,
Expand Down Expand Up @@ -159,7 +145,6 @@ where
app_logic,
view: None,
state: None,
idle_handle: None,
pending_async: HashSet::new(),
ui_state: UiState::Start,
};
Expand All @@ -172,64 +157,20 @@ where
id: None,
root_pod: None,
events: Vec::new(),
window_handle: Default::default(),
root_state: WidgetState::new(),
size: Default::default(),
new_size: Default::default(),
cursor_pos: None,
cx,
font_cx: FontContext::new(),
rt,
window_id: crate::id::Id::next(),
accesskit_connected: false,
node_classes: accesskit::NodeClassSet::new(),
}
}

pub fn connect(&mut self, window_handle: WindowHandle) {
self.window_handle = window_handle.clone();
if let Some(idle_handle) = window_handle.get_idle_handle() {
let _ = self
.req_chan
.blocking_send(AppReq::SetIdleHandle(idle_handle));
}
}

pub fn size(&mut self, size: Size) {
self.new_size = size;
}

pub fn accessibility(&mut self) -> TreeUpdate {
let mut update = TreeUpdate {
nodes: vec![],
tree: None,
focus: accesskit::NodeId(0),
};
self.ensure_root();
let root_pod = self.root_pod.as_mut().unwrap();
let mut window_node_builder = accesskit::NodeBuilder::new(accesskit::Role::Window);
window_node_builder.set_name("xilem window");
window_node_builder.set_children(vec![root_pod.id().into()]);
if let Ok(scale) = self.window_handle.get_scale() {
window_node_builder.set_transform(Box::new(accesskit::Affine::scale_non_uniform(
scale.x(),
scale.y(),
)));
}
let window_node = window_node_builder.build(&mut self.node_classes);
update.nodes.push((self.window_id.into(), window_node));
update.tree = Some(accesskit::Tree::new(self.window_id.into()));
let mut cx_state = CxState::new(&self.window_handle, &mut self.font_cx, &mut self.events);
let mut access_cx = AccessCx {
cx_state: &mut cx_state,
widget_state: &mut self.root_state,
update: &mut update,
node_classes: &mut self.node_classes,
};
root_pod.accessibility(&mut access_cx);
update
}

/// Run a paint cycle for the application.
///
/// This is not just painting, but involves processing events, doing layout
Expand All @@ -240,8 +181,7 @@ where
// TODO: be more lazy re-rendering
self.render();
let root_pod = self.root_pod.as_mut().unwrap();
let mut cx_state =
CxState::new(&self.window_handle, &mut self.font_cx, &mut self.events);
let mut cx_state = CxState::new(&mut self.font_cx, &mut self.events);

let mut lifecycle_cx = LifeCycleCx::new(&mut cx_state, &mut self.root_state);
root_pod.lifecycle(&mut lifecycle_cx, &LifeCycle::TreeUpdate);
Expand Down Expand Up @@ -282,16 +222,10 @@ where
continue;
}

if self.accesskit_connected {
let update = self.accessibility();
// TODO: it would be cleaner to not use a closure here.
self.window_handle.update_accesskit_if_active(|| update);
}
// Borrow again to avoid multiple borrows.
// TODO: maybe make accessibility a method on CxState?
let root_pod = self.root_pod.as_mut().unwrap();
let mut cx_state =
CxState::new(&self.window_handle, &mut self.font_cx, &mut self.events);
let mut cx_state = CxState::new(&mut self.font_cx, &mut self.events);
let mut paint_cx = PaintCx::new(&mut cx_state, &mut self.root_state);
root_pod.paint_impl(&mut paint_cx);
break;
Expand All @@ -309,12 +243,11 @@ where
Event::MouseLeft() => {
self.cursor_pos = None;
}
_ => {}
}

self.ensure_root();
let root_pod = self.root_pod.as_mut().unwrap();
let mut cx_state = CxState::new(&self.window_handle, &mut self.font_cx, &mut self.events);
let mut cx_state = CxState::new(&mut self.font_cx, &mut self.events);
let mut event_cx = EventCx::new(&mut cx_state, &mut self.root_state);
root_pod.event(&mut event_cx, &event);
self.send_events();
Expand Down Expand Up @@ -399,7 +332,6 @@ impl<T, V: View<T>, F: FnMut(&mut T) -> V> AppTask<T, V, F> {
};
match req {
Ok(Some(req)) => match req {
AppReq::SetIdleHandle(handle) => self.idle_handle = Some(handle),
AppReq::Events(events) => {
for event in events {
let id_path = &event.id_path[1..];
Expand All @@ -426,9 +358,6 @@ impl<T, V: View<T>, F: FnMut(&mut T) -> V> AppTask<T, V, F> {
if needs_rebuild {
// request re-render from UI thread
if self.ui_state == UiState::Start {
if let Some(handle) = self.idle_handle.as_mut() {
handle.schedule_idle(IdleToken::new(42));
}
self.ui_state = UiState::WokeUI;
}
let id = id_path.last().unwrap();
Expand Down
Loading