Skip to content

Commit

Permalink
chore: expose app api
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Oct 16, 2024
1 parent 156a3f7 commit a3fc86b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
21 changes: 15 additions & 6 deletions violet-core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use glam::{Vec2, Vec3Swizzles};
/// NOTE: maybe redefine these types ourselves
pub use winit::{event, keyboard};
use winit::{
event::{ElementState, KeyEvent, MouseButton},
keyboard::ModifiersState,
event::{ElementState, MouseButton},
keyboard::{Key, ModifiersState, SmolStr},
};

use crate::{
Expand Down Expand Up @@ -161,15 +161,23 @@ impl InputState {
self.modifiers = modifiers;
}

pub fn on_keyboard_input(&mut self, frame: &mut Frame, event: KeyEvent) {
pub fn on_keyboard_input(
&mut self,
frame: &mut Frame,
key: Key,
state: ElementState,
text: Option<SmolStr>,
) {
if let Some(entity) = &self.focused(frame.world()) {
if let Ok(mut on_input) = entity.get_mut(on_keyboard_input()) {
let s = ScopeRef::new(frame, *entity);
on_input(
&s,
KeyboardInput {
modifiers: self.modifiers,
event,
state,
key,
text,
},
);
}
Expand Down Expand Up @@ -235,8 +243,9 @@ pub struct Scroll {

pub struct KeyboardInput {
pub modifiers: ModifiersState,

pub event: KeyEvent,
pub state: ElementState,
pub key: Key,
pub text: Option<SmolStr>,
}

pub type InputEventHandler<T> = Box<dyn Send + Sync + FnMut(&ScopeRef<'_>, T)>;
Expand Down
8 changes: 4 additions & 4 deletions violet-core/src/widget/interactive/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl Widget for TextInput {
.on_event(on_keyboard_input(), {
to_owned![tx];
move |_, input| {
if input.event.state == ElementState::Pressed {
if input.state == ElementState::Pressed {
handle_input(input, |v| {
tx.send(v).ok();
})
Expand Down Expand Up @@ -442,7 +442,7 @@ fn handle_cursor_move(key: NamedKey, mods: ModifiersState) -> Option<CursorMove>

fn handle_input(input: KeyboardInput, send: impl Fn(Action)) {
let ctrl = input.modifiers.control_key();
if let Key::Named(key) = input.event.logical_key {
if let Key::Named(key) = input.key {
if let Some(m) = handle_cursor_move(key, input.modifiers) {
if input.modifiers.shift_key() {
send(Action::Editor(EditorAction::SelectionStart));
Expand All @@ -469,7 +469,7 @@ fn handle_input(input: KeyboardInput, send: impl Fn(Action)) {
}
_ => {}
}
} else if let Key::Character(c) = input.event.logical_key {
} else if let Key::Character(c) = input.key {
match &*c {
"c" if ctrl => return send(Action::Copy),
"v" if ctrl => return send(Action::Paste),
Expand All @@ -478,7 +478,7 @@ fn handle_input(input: KeyboardInput, send: impl Fn(Action)) {
}
}

if let Some(text) = input.event.text {
if let Some(text) = input.text {
send(Action::Editor(EditorAction::Edit(EditAction::InsertText(
text.into(),
))));
Expand Down
17 changes: 10 additions & 7 deletions violet-wgpu/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use crate::{
};

pub struct Canvas<W> {
stylesheet: Entity,
root: W,
pub stylesheet: Entity,
pub root: W,
}

impl<W: Widget> Widget for Canvas<W> {
Expand Down Expand Up @@ -166,15 +166,15 @@ impl AppBuilder {

/// A running application instance of violet
pub struct AppInstance {
frame: Frame,
pub frame: Frame,
root: Entity,
scale_factor: f64,
current_time: Instant,
start_time: Instant,
executor: Executor,
schedule: Schedule,
window_size: PhysicalSize<u32>,
input_state: InputState,
pub input_state: InputState,
text_system: Arc<Mutex<TextSystem>>,
layout_changes_rx: flume::Receiver<(Entity, LayoutUpdateEvent)>,
}
Expand Down Expand Up @@ -382,9 +382,12 @@ impl ApplicationHandler for WindowEventHandler {
}
WindowEvent::KeyboardInput { event, .. } => {
puffin::profile_scope!("KeyboardInput", format!("{event:?}"));
instance
.input_state
.on_keyboard_input(&mut instance.frame, event)
instance.input_state.on_keyboard_input(
&mut instance.frame,
event.logical_key,
event.state,
event.text,
)
}
WindowEvent::CursorMoved { position, .. } => {
puffin::profile_scope!("CursorMoved");
Expand Down

0 comments on commit a3fc86b

Please sign in to comment.