Skip to content

Commit

Permalink
feat: PluginHandle (#793)
Browse files Browse the repository at this point in the history
* feat: `PluginHandle`

* gamepad_focus

* fix ci

* install `libudev-sys` in CI

* actually install `libudev-dev` in CI

* chore: Clean up

* chore: Doc comment

* chore: fmt
  • Loading branch information
marc2332 authored Aug 30, 2024
1 parent 450815a commit f8a0844
Show file tree
Hide file tree
Showing 27 changed files with 434 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Install linux dependencies
if: runner.os == 'Linux'
run: |
sudo apt update && sudo apt install build-essential libssl-dev pkg-config libglib2.0-dev libgtk-3-dev
sudo apt update && sudo apt install build-essential libssl-dev pkg-config libglib2.0-dev libgtk-3-dev libudev-dev
- name: Check examples
run: cargo check --examples
- name: Lint
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ dioxus-router = { workspace = true }
itertools = "0.13.0"
home = "0.5.9"
dioxus-query = "0.5.1"
gilrs = "0.10.8"
gl = { workspace = true }

[profile.release]
Expand Down
1 change: 0 additions & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ torin = { workspace = true }
dioxus-core = { workspace = true }

accesskit = { workspace = true }
accesskit_winit = { workspace = true }
winit = { workspace = true }
freya-engine = { workspace = true }
freya-native-core = { workspace = true }
Expand Down
2 changes: 0 additions & 2 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod event_messages;
mod layers;
mod layout;
mod paragraphs;

pub use event_messages::*;
pub use layers::*;
pub use layout::*;
pub use paragraphs::*;
2 changes: 1 addition & 1 deletion crates/components/src/native_container.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dioxus::prelude::*;
use freya_common::EventMessage;
use freya_core::prelude::EventMessage;
use freya_elements::{
elements as dioxus_elements,
events::KeyboardEvent,
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dioxus-core = { workspace = true }
tokio = { workspace = true }
winit = { workspace = true }
accesskit = { workspace = true }
accesskit_winit = { workspace = true }

rustc-hash = { workspace = true }
tracing = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions crates/core/src/dom/doms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use dioxus_core::VirtualDom;
use freya_common::{
Layers,
ParagraphElements,
TextGroupMeasurement,
};
use freya_native_core::{
prelude::{
Expand Down Expand Up @@ -38,7 +37,10 @@ use torin::prelude::*;
use tracing::info;

use super::mutations_writer::MutationsWriter;
use crate::prelude::measure_paragraph;
use crate::prelude::{
measure_paragraph,
TextGroupMeasurement,
};

pub type DioxusDOM = RealDom<CustomAttributeValues>;
pub type DioxusNode<'a> = NodeRef<'a, CustomAttributeValues>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use winit::window::{
Window,
};

use crate::prelude::PlatformEvent;

pub struct TextGroupMeasurement {
pub text_id: Uuid,
pub cursor_id: usize,
Expand Down Expand Up @@ -39,6 +41,8 @@ pub enum EventMessage {
ExitApp,
/// Callback to access the Window.
WithWindow(Box<dyn FnOnce(&Window) + Send + Sync>),
/// Raw platform event, this are low level events.
PlatformEvent(PlatformEvent),
}

impl From<accesskit_winit::Event> for EventMessage {
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod accessibility;
pub mod dom;
pub mod elements;
pub mod event_messages;
pub mod events;
pub mod layout;
pub mod node;
Expand All @@ -16,6 +17,7 @@ pub mod prelude {
accessibility::*,
dom::*,
elements::*,
event_messages::*,
events::*,
layout::*,
node::*,
Expand Down
44 changes: 39 additions & 5 deletions crates/core/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,43 @@ use freya_engine::prelude::{
};
use freya_native_core::NodeId;
use torin::torin::Torin;
use winit::window::Window;
use winit::{
event_loop::EventLoopProxy,
window::Window,
};

use crate::{
dom::FreyaDOM,
prelude::{
EventMessage,
PlatformEvent,
},
};

use crate::dom::FreyaDOM;
#[derive(Clone)]
pub struct PluginHandle {
pub proxy: EventLoopProxy<EventMessage>,
}

impl PluginHandle {
pub fn new(proxy: &EventLoopProxy<EventMessage>) -> Self {
Self {
proxy: proxy.clone(),
}
}

/// Emit a [PlatformEvent]. Useful to simulate certain events.
pub fn send_platform_event(&self, event: PlatformEvent) {
self.proxy
.send_event(EventMessage::PlatformEvent(event))
.ok();
}

/// Emit a [EventMessage].
pub fn send_event_loop_event(&self, event: EventMessage) {
self.proxy.send_event(event).ok();
}
}

/// Manages all loaded plugins.
#[derive(Default)]
Expand All @@ -19,9 +53,9 @@ impl PluginsManager {
self.plugins.push(Box::new(plugin))
}

pub fn send(&mut self, event: PluginEvent) {
pub fn send(&mut self, event: PluginEvent, handle: PluginHandle) {
for plugin in &mut self.plugins {
plugin.on_event(&event)
plugin.on_event(&event, handle.clone())
}
}
}
Expand Down Expand Up @@ -59,5 +93,5 @@ pub enum PluginEvent<'a> {
/// Skeleton for Freya plugins.
pub trait FreyaPlugin {
/// React on events emitted by Freya.
fn on_event(&mut self, event: &PluginEvent);
fn on_event(&mut self, event: &PluginEvent, handle: PluginHandle);
}
2 changes: 1 addition & 1 deletion crates/core/src/skia/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::ops::Mul;
use freya_common::{
CachedParagraph,
CursorLayoutResponse,
TextGroupMeasurement,
};
use freya_native_core::prelude::NodeImmutable;
use freya_node_state::CursorState;
Expand All @@ -15,6 +14,7 @@ use torin::prelude::{
use crate::prelude::{
align_main_align_paragraph,
DioxusNode,
TextGroupMeasurement,
};

/// Merasure the cursor positio and text selection and notify the subscribed component of the element.
Expand Down
5 changes: 5 additions & 0 deletions crates/freya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ pub mod common {
pub use freya_common::*;
}

/// Core APIs.
pub mod core {
pub use freya_core::*;
}

/// Elements, attributes and events definitions.
pub use freya_elements::elements;
/// Events data.
Expand Down
11 changes: 7 additions & 4 deletions crates/freya/src/plugins/performance_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use std::time::{
Instant,
};

use freya_core::plugins::{
FreyaPlugin,
PluginEvent,
use freya_core::{
plugins::{
FreyaPlugin,
PluginEvent,
},
prelude::PluginHandle,
};
use freya_engine::prelude::{
Color,
Expand Down Expand Up @@ -35,7 +38,7 @@ pub struct PerformanceOverlayPlugin {
}

impl FreyaPlugin for PerformanceOverlayPlugin {
fn on_event(&mut self, event: &PluginEvent) {
fn on_event(&mut self, event: &PluginEvent, _handle: PluginHandle) {
match event {
PluginEvent::StartedLayout(_) => self.started_layout = Some(Instant::now()),
PluginEvent::FinishedLayout(_) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/hooks/src/use_editable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use dioxus_signals::{
Signal,
Writable,
};
use freya_common::{
CursorLayoutResponse,
use freya_common::CursorLayoutResponse;
use freya_core::prelude::{
EventMessage,
TextGroupMeasurement,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/hooks/src/use_focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use dioxus_signals::{
Signal,
Writable,
};
use freya_common::EventMessage;
use freya_core::{
accessibility::ACCESSIBILITY_ROOT_ID,
platform_state::NavigationMode,
prelude::EventMessage,
types::AccessibilityId,
};
use freya_elements::events::{
Expand Down
2 changes: 1 addition & 1 deletion crates/hooks/src/use_platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use dioxus_signals::{
Readable,
Signal,
};
use freya_common::EventMessage;
use freya_core::prelude::EventMessage;
use tokio::sync::{
broadcast,
mpsc::UnboundedSender,
Expand Down
2 changes: 1 addition & 1 deletion crates/renderer/src/accessibility.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use accesskit_winit::Adapter;
use freya_common::EventMessage;
use freya_core::{
prelude::{
AccessibilityFocusDirection,
AccessibilityManager,
EventMessage,
SharedAccessibilityManager,
ACCESSIBILITY_ROOT_ID,
},
Expand Down
Loading

0 comments on commit f8a0844

Please sign in to comment.