RE ADD CONTEXT
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
use crate::prelude::*;
|
||||
use arboard::Clipboard;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use winit::event::{Ime, WindowEvent};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoopProxy};
|
||||
use winit::window::{Window, WindowAttributes};
|
||||
use std::{marker::Sized, sync::Arc, time::Instant};
|
||||
use winit::{
|
||||
event::{Ime, WindowEvent},
|
||||
event_loop::{ActiveEventLoop, EventLoopProxy},
|
||||
window::Window,
|
||||
};
|
||||
|
||||
mod app;
|
||||
mod attr;
|
||||
@@ -21,49 +22,21 @@ pub use render::*;
|
||||
pub use sense::*;
|
||||
|
||||
pub type Proxy<Event> = EventLoopProxy<Event>;
|
||||
pub type DefaultApp<Data> = App<DefaultState<Data>>;
|
||||
|
||||
pub struct DefaultState<AppState> {
|
||||
ui: Ui,
|
||||
ui_state: UiState,
|
||||
app_state: AppState,
|
||||
}
|
||||
|
||||
pub struct UiState {
|
||||
pub struct UiState<State> {
|
||||
pub renderer: UiRenderer,
|
||||
pub input: Input,
|
||||
pub focus: Option<WidgetRef<TextEdit>>,
|
||||
pub focus: Option<WidgetRef<State, TextEdit<State>>>,
|
||||
pub clipboard: Clipboard,
|
||||
pub window: Arc<Window>,
|
||||
pub ime: usize,
|
||||
pub last_click: Instant,
|
||||
}
|
||||
|
||||
pub trait DefaultAppState: 'static {
|
||||
type Event: 'static = ();
|
||||
fn new(ui: &mut Ui, state: &UiState, proxy: Proxy<Self::Event>) -> Self;
|
||||
#[allow(unused_variables)]
|
||||
fn event(&mut self, event: Self::Event, ui: &mut Ui, state: &UiState) {}
|
||||
#[allow(unused_variables)]
|
||||
fn exit(&mut self, ui: &mut Ui, state: &UiState) {}
|
||||
#[allow(unused_variables)]
|
||||
fn window_event(&mut self, event: WindowEvent, ui: &mut Ui, state: &UiState) {}
|
||||
fn window_attrs() -> WindowAttributes {
|
||||
WindowAttributes::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
type Event = State::Event;
|
||||
|
||||
fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<Self::Event>) -> Self {
|
||||
let window = Arc::new(
|
||||
event_loop
|
||||
.create_window(State::window_attrs())
|
||||
.expect("failed to create window "),
|
||||
);
|
||||
let mut ui = Ui::new();
|
||||
let ui_state = UiState {
|
||||
impl<State> UiState<State> {
|
||||
pub fn new(window: impl Into<Arc<Window>>) -> Self {
|
||||
let window = window.into();
|
||||
Self {
|
||||
renderer: UiRenderer::new(window.clone()),
|
||||
window,
|
||||
input: Input::default(),
|
||||
@@ -71,26 +44,42 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
ime: 0,
|
||||
last_click: Instant::now(),
|
||||
focus: None,
|
||||
};
|
||||
let app_state = State::new(&mut ui, &ui_state, proxy);
|
||||
Self {
|
||||
ui,
|
||||
ui_state,
|
||||
app_state,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasUiState: Sized + 'static + HasUi {
|
||||
fn ui_state(&mut self) -> &mut UiState<Self>;
|
||||
fn ui_with_ui_state(&mut self) -> (&mut Ui<Self>, &mut UiState<Self>) {
|
||||
// as long as you're not doing anything actually unhinged this should always work safely
|
||||
(unsafe { std::mem::transmute(self.ui()) }, self.ui_state())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait DefaultAppState: Sized + 'static + HasUi + HasUiState {
|
||||
type Event: 'static = ();
|
||||
fn new(event_loop: &ActiveEventLoop, proxy: Proxy<Self::Event>) -> Self;
|
||||
#[allow(unused_variables)]
|
||||
fn event(&mut self, event: Self::Event) {}
|
||||
#[allow(unused_variables)]
|
||||
fn exit(&mut self) {}
|
||||
#[allow(unused_variables)]
|
||||
fn window_event(&mut self, event: WindowEvent) {}
|
||||
}
|
||||
|
||||
impl<State: DefaultAppState> AppState for State {
|
||||
type Event = State::Event;
|
||||
|
||||
fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<Self::Event>) -> Self {
|
||||
Self::new(event_loop, proxy)
|
||||
}
|
||||
|
||||
fn event(&mut self, event: Self::Event, _: &ActiveEventLoop) {
|
||||
self.app_state.event(event, &mut self.ui, &self.ui_state);
|
||||
self.event(event);
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
||||
let Self {
|
||||
ui,
|
||||
ui_state,
|
||||
app_state,
|
||||
} = self;
|
||||
|
||||
let ui_state = self.ui_state();
|
||||
let input_changed = ui_state.input.event(&event);
|
||||
let cursor_state = ui_state.cursor_state().clone();
|
||||
let old = ui_state.focus;
|
||||
@@ -99,13 +88,9 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
}
|
||||
if input_changed {
|
||||
let window_size = ui_state.window_size();
|
||||
// call sensors with all 3 important contexts
|
||||
// TODO: allow user to specify custom contexts?
|
||||
// and give them both states in case they need both
|
||||
ui.run_sensors(&mut (), &cursor_state, window_size);
|
||||
ui.run_sensors(ui_state, &cursor_state, window_size);
|
||||
ui.run_sensors(app_state, &cursor_state, window_size);
|
||||
self.run_sensors(&cursor_state, window_size);
|
||||
}
|
||||
let (mut ui, mut ui_state) = self.ui_with_ui_state();
|
||||
if old != ui_state.focus
|
||||
&& let Some(old) = old
|
||||
{
|
||||
@@ -133,13 +118,13 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
ui_state.window.set_ime_allowed(false);
|
||||
}
|
||||
TextInputResult::Submit => {
|
||||
ui.run_event::<Submit, _>(app_state, sel, &mut ());
|
||||
self.run_event::<Submit>(sel, &mut ());
|
||||
}
|
||||
TextInputResult::Paste => {
|
||||
if let Ok(t) = ui_state.clipboard.get_text() {
|
||||
text.insert(&t);
|
||||
}
|
||||
ui.run_event::<Edited, _>(app_state, sel, &mut ());
|
||||
self.run_event::<Edited>(sel, &mut ());
|
||||
}
|
||||
TextInputResult::Copy(text) => {
|
||||
if let Err(err) = ui_state.clipboard.set_text(text) {
|
||||
@@ -147,14 +132,14 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
}
|
||||
}
|
||||
TextInputResult::Used => {
|
||||
ui.run_event::<Edited, _>(app_state, sel, &mut ());
|
||||
self.run_event::<Edited>(sel, &mut ());
|
||||
}
|
||||
TextInputResult::Unused => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowEvent::Ime(ime) => {
|
||||
if let Some(sel) = &ui_state.focus {
|
||||
if let Some(sel) = ui_state.focus {
|
||||
let mut text = sel.edit(ui);
|
||||
match ime {
|
||||
Ime::Enabled | Ime::Disabled => (),
|
||||
@@ -171,7 +156,8 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
app_state.window_event(event, ui, ui_state);
|
||||
self.window_event(event);
|
||||
(ui, ui_state) = self.ui_with_ui_state();
|
||||
if ui.needs_redraw() {
|
||||
ui_state.renderer.window().request_redraw();
|
||||
}
|
||||
@@ -179,6 +165,6 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
}
|
||||
|
||||
fn exit(&mut self) {
|
||||
self.app_state.exit(&mut self.ui, &self.ui_state);
|
||||
self.exit();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user