Merge branch 'master' into get-parsed-values-for-form-values

This commit is contained in:
Jonathan Kelley committed 2024-01-08 09:57:03 -08:00
commit 7f6e2dd64b
314 files changed
+13745 -4840

No files matched your search

+75 -54
View File
@@ -2,6 +2,10 @@ use crossterm::event::{
Event as TermEvent, KeyCode as TermKeyCode, KeyModifiers, ModifierKeyCode, MouseButton,
MouseEventKind,
};
use dioxus_html::{
HasFormData, HasKeyboardData, HasWheelData, SerializedFocusData, SerializedKeyboardData,
SerializedMouseData, SerializedWheelData,
};
use dioxus_native_core::prelude::*;
use dioxus_native_core::real_dom::NodeImmutable;
use rustc_hash::{FxHashMap, FxHashSet};
@@ -11,9 +15,11 @@ use dioxus_html::geometry::{
ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint, WheelDelta,
};
use dioxus_html::input_data::keyboard_types::{Code, Key, Location, Modifiers};
use dioxus_html::input_data::MouseButtonSet as DioxusMouseButtons;
use dioxus_html::input_data::{MouseButton as DioxusMouseButton, MouseButtonSet};
use dioxus_html::{event_bubbles, FocusData, FormValue, KeyboardData, MouseData, WheelData};
use dioxus_html::input_data::{
MouseButton as DioxusMouseButton, MouseButtonSet as DioxusMouseButtons,
};
use dioxus_html::{event_bubbles, prelude::*};
use dioxus_html::{FocusData, FormValue, KeyboardData, MouseData, WheelData};
use std::any::Any;
use std::collections::HashMap;
use std::{
@@ -38,10 +44,10 @@ pub struct Event {
#[derive(Debug, Clone, PartialEq)]
pub enum EventData {
Mouse(MouseData),
Keyboard(KeyboardData),
Focus(FocusData),
Wheel(WheelData),
Mouse(SerializedMouseData),
Keyboard(SerializedKeyboardData),
Focus(SerializedFocusData),
Wheel(SerializedWheelData),
Form(FormData),
}
@@ -52,27 +58,31 @@ impl EventData {
EventData::Keyboard(k) => Rc::new(k),
EventData::Focus(f) => Rc::new(f),
EventData::Wheel(w) => Rc::new(w),
EventData::Form(f) => Rc::new(f.into_html()),
EventData::Form(f) => Rc::new(f),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct FormData {
pub value: String,
pub(crate) value: String,
pub values: HashMap<String, FormValue>,
pub files: Option<Files>,
pub(crate) files: Option<Files>,
}
impl FormData {
fn into_html(self) -> dioxus_html::FormData {
dioxus_html::FormData {
value: self.value,
values: self.values,
files: None,
}
impl HasFormData for FormData {
fn value(&self) -> String {
self.value.clone()
}
fn values(&self) -> HashMap<String, Vec<String>> {
self.values.clone()
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
@@ -89,9 +99,9 @@ type EventCore = (&'static str, EventData);
const MAX_REPEAT_TIME: Duration = Duration::from_millis(100);
pub struct InnerInputState {
mouse: Option<MouseData>,
wheel: Option<WheelData>,
last_key_pressed: Option<(KeyboardData, Instant)>,
mouse: Option<SerializedMouseData>,
wheel: Option<SerializedWheelData>,
last_key_pressed: Option<(SerializedKeyboardData, Instant)>,
pub(crate) focus_state: FocusState,
// subscribers: Vec<Rc<dyn Fn() + 'static>>,
}
@@ -114,7 +124,7 @@ impl InnerInputState {
EventData::Mouse(ref mut m) => {
let mut held_buttons = match &self.mouse {
Some(previous_data) => previous_data.held_buttons(),
None => MouseButtonSet::empty(),
None => DioxusMouseButtons::empty(),
};
match evt.0 {
@@ -133,10 +143,16 @@ impl InnerInputState {
_ => {}
}
let new_mouse_data = MouseData::new(
m.coordinates(),
let coordinates = m.coordinates();
let new_mouse_data = SerializedMouseData::new(
m.trigger_button(),
held_buttons,
Coordinates::new(
m.screen_coordinates(),
m.client_coordinates(),
coordinates.element(),
m.page_coordinates(),
),
m.modifiers(),
);
@@ -155,7 +171,13 @@ impl InnerInputState {
.is_some();
if is_repeating {
*k = KeyboardData::new(k.key(), k.code(), k.location(), true, k.modifiers());
*k = SerializedKeyboardData::new(
k.key(),
k.code(),
k.location(),
is_repeating,
k.modifiers(),
);
}
self.last_key_pressed = Some((k.clone(), Instant::now()));
@@ -199,13 +221,13 @@ impl InnerInputState {
resolved_events.push(Event {
name: "focus",
id,
data: EventData::Focus(FocusData {}),
data: EventData::Focus(SerializedFocusData::default()),
bubbles: event_bubbles("focus"),
});
resolved_events.push(Event {
name: "focusin",
id,
data: EventData::Focus(FocusData {}),
data: EventData::Focus(SerializedFocusData::default()),
bubbles: event_bubbles("focusin"),
});
}
@@ -213,7 +235,7 @@ impl InnerInputState {
resolved_events.push(Event {
name: "focusout",
id,
data: EventData::Focus(FocusData {}),
data: EventData::Focus(SerializedFocusData::default()),
bubbles: event_bubbles("focusout"),
});
}
@@ -226,7 +248,7 @@ impl InnerInputState {
fn resolve_mouse_events(
&mut self,
previous_mouse: Option<MouseData>,
previous_mouse: Option<SerializedMouseData>,
resolved_events: &mut Vec<Event>,
layout: &Taffy,
dom: &mut RealDom,
@@ -273,7 +295,10 @@ impl InnerInputState {
}
}
fn prepare_mouse_data(mouse_data: &MouseData, layout: &Layout) -> MouseData {
fn prepare_mouse_data(
mouse_data: &SerializedMouseData,
layout: &Layout,
) -> SerializedMouseData {
let Point { x, y } = layout.location;
let node_origin = ClientPoint::new(
layout_to_screen_space(x).into(),
@@ -284,17 +309,15 @@ impl InnerInputState {
.to_point()
.cast_unit();
let coordinates = Coordinates::new(
mouse_data.screen_coordinates(),
mouse_data.client_coordinates(),
new_client_coordinates,
mouse_data.page_coordinates(),
);
MouseData::new(
coordinates,
SerializedMouseData::new(
mouse_data.trigger_button(),
mouse_data.held_buttons(),
Coordinates::new(
mouse_data.screen_coordinates(),
mouse_data.client_coordinates(),
new_client_coordinates,
mouse_data.page_coordinates(),
),
mouse_data.modifiers(),
)
}
@@ -305,7 +328,7 @@ impl InnerInputState {
// a mouse button is pressed if a button was not down and is now down
let previous_buttons = previous_mouse
.map_or(MouseButtonSet::empty(), |previous_data| {
.map_or(DioxusMouseButtons::empty(), |previous_data| {
previous_data.held_buttons()
});
let was_pressed = !(mouse_data.held_buttons() - previous_buttons).is_empty();
@@ -689,17 +712,6 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> {
// from https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
// The `page` and `screen` coordinates are inconsistent with the MDN definition, as they are relative to the viewport (client), not the target element/page/screen, respectively.
// todo?
// But then, MDN defines them in terms of pixels, yet crossterm provides only row/column, and it might not be possible to get pixels. So we can't get 100% consistency anyway.
let coordinates = Coordinates::new(
ScreenPoint::new(x, y),
ClientPoint::new(x, y),
// offset x/y are set when the origin of the event is assigned to an element
ElementPoint::new(0., 0.),
PagePoint::new(x, y),
);
let mut modifiers = Modifiers::empty();
if shift {
modifiers.insert(Modifiers::SHIFT);
@@ -715,17 +727,26 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> {
}
// held mouse buttons get set later by maintaining state, as crossterm does not provide them
EventData::Mouse(MouseData::new(
coordinates,
EventData::Mouse(SerializedMouseData::new(
button,
DioxusMouseButtons::empty(),
Coordinates::new(
// The `page` and `screen` coordinates are inconsistent with the MDN definition, as they are relative to the viewport (client), not the target element/page/screen, respectively.
// todo?
// But then, MDN defines them in terms of pixels, yet crossterm provides only row/column, and it might not be possible to get pixels. So we can't get 100% consistency anyway.
ScreenPoint::new(x, y),
ClientPoint::new(x, y),
// offset x/y are set when the origin of the event is assigned to an element
ElementPoint::new(0., 0.),
PagePoint::new(x, y),
),
modifiers,
))
};
let get_wheel_data = |up| {
let y = if up { -1.0 } else { 1.0 };
EventData::Wheel(WheelData::new(WheelDelta::lines(0., y, 0.)))
EventData::Wheel(SerializedWheelData::new(WheelDelta::lines(0., y, 0.)))
};
match m.kind {
@@ -750,7 +771,7 @@ fn translate_key_event(event: crossterm::event::KeyEvent) -> Option<EventData> {
let code = guess_code_from_crossterm_key_code(event.code)?;
let modifiers = modifiers_from_crossterm_modifiers(event.modifiers);
Some(EventData::Keyboard(KeyboardData::new(
Some(EventData::Keyboard(SerializedKeyboardData::new(
key,
code,
Location::Standard,
+2 -2
View File
@@ -92,10 +92,10 @@ impl State for TaffyLayout {
attribute, value, ..
} in attributes
{
if let Some(text) = value.as_text() {
if value.as_custom().is_none() {
apply_layout_attributes_cfg(
&attribute.name,
text,
&value.to_string(),
&mut style,
&LayoutConfigeration {
border_widths: BorderWidths {
+3 -3
View File
@@ -17,6 +17,7 @@ use futures::{channel::mpsc::UnboundedSender, pin_mut, Future, StreamExt};
use futures_channel::mpsc::unbounded;
use layout::TaffyLayout;
use prevent_default::PreventDefault;
use ratatui::{backend::CrosstermBackend, Terminal};
use std::{io, time::Duration};
use std::{
pin::Pin,
@@ -26,7 +27,6 @@ use std::{rc::Rc, sync::RwLock};
use style_attributes::StyleModifier;
pub use taffy::{geometry::Point, prelude::*};
use tokio::select;
use tui::{backend::CrosstermBackend, Terminal};
use widgets::{register_widgets, RinkWidgetResponder, RinkWidgetTraitObject};
mod config;
@@ -180,7 +180,7 @@ pub fn render<R: Driver>(
if !to_rerender.is_empty() || updated {
updated = false;
fn resize(dims: tui::layout::Rect, taffy: &mut Taffy, rdom: &RealDom) {
fn resize(dims: ratatui::layout::Rect, taffy: &mut Taffy, rdom: &RealDom) {
let width = screen_to_layout_space(dims.width);
let height = screen_to_layout_space(dims.height);
let root_node = rdom
@@ -222,7 +222,7 @@ pub fn render<R: Driver>(
} else {
let rdom = rdom.read().unwrap();
resize(
tui::layout::Rect {
ratatui::layout::Rect {
x: 0,
y: 0,
width: 1000,
+3 -4
View File
@@ -1,11 +1,10 @@
use dioxus_native_core::{prelude::*, tree::TreeRef};
use std::io::Stdout;
use ratatui::{layout::Rect, style::Color};
use taffy::{
geometry::Point,
prelude::{Dimension, Layout, Size},
Taffy,
};
use tui::{backend::CrosstermBackend, layout::Rect, style::Color};
use crate::{
focus::Focused,
@@ -20,7 +19,7 @@ use crate::{
const RADIUS_MULTIPLIER: [f32; 2] = [1.0, 0.5];
pub(crate) fn render_vnode(
frame: &mut tui::Frame<CrosstermBackend<Stdout>>,
frame: &mut ratatui::Frame,
layout: &Taffy,
node: NodeRef,
cfg: Config,
@@ -96,7 +95,7 @@ pub(crate) fn render_vnode(
impl RinkWidget for NodeRef<'_> {
fn render(self, area: Rect, mut buf: RinkBuffer<'_>) {
use tui::symbols::line::*;
use ratatui::symbols::line::*;
enum Direction {
Left,
+2 -1
View File
@@ -1,6 +1,6 @@
use std::{num::ParseFloatError, str::FromStr};
use tui::style::{Color, Modifier, Style};
use ratatui::style::{Color, Modifier, Style};
use crate::RenderingMode;
@@ -442,6 +442,7 @@ impl RinkStyle {
impl From<RinkStyle> for Style {
fn from(val: RinkStyle) -> Self {
Style {
underline_color: None,
fg: val.fg.map(|c| c.color),
bg: val.bg.map(|c| c.color),
add_modifier: val.add_modifier,
+4 -4
View File
@@ -187,8 +187,8 @@ pub enum BorderStyle {
}
impl BorderStyle {
pub fn symbol_set(&self) -> Option<tui::symbols::line::Set> {
use tui::symbols::line::*;
pub fn symbol_set(&self) -> Option<ratatui::symbols::line::Set> {
use ratatui::symbols::line::*;
const DASHED: Set = Set {
horizontal: "╌",
vertical: "╎",
@@ -570,7 +570,7 @@ fn apply_animation(name: &str, _value: &str, _style: &mut StyleModifier) {
}
fn apply_font(name: &str, value: &str, style: &mut StyleModifier) {
use tui::style::Modifier;
use ratatui::style::Modifier;
match name {
"font" => (),
"font-family" => (),
@@ -593,7 +593,7 @@ fn apply_font(name: &str, value: &str, style: &mut StyleModifier) {
}
fn apply_text(name: &str, value: &str, style: &mut StyleModifier) {
use tui::style::Modifier;
use ratatui::style::Modifier;
match name {
"text-align" => todo!(),
+1 -1
View File
@@ -1,4 +1,4 @@
use tui::{
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier},
+2 -2
View File
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use dioxus_html::input_data::keyboard_types::Key;
use dioxus_html::{input_data::keyboard_types::Key, HasKeyboardData};
use dioxus_native_core::{
custom_element::CustomElement,
node::OwnedAttributeDiscription,
@@ -11,7 +11,7 @@ use dioxus_native_core::{
};
use shipyard::UniqueView;
use crate::FormData;
use crate::hooks::FormData;
use super::{RinkWidget, WidgetContext};
+2 -2
View File
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use dioxus_html::input_data::keyboard_types::Key;
use dioxus_html::{input_data::keyboard_types::Key, HasKeyboardData};
use dioxus_native_core::{
custom_element::CustomElement,
node::OwnedAttributeDiscription,
@@ -11,7 +11,7 @@ use dioxus_native_core::{
};
use shipyard::UniqueView;
use crate::FormData;
use crate::hooks::FormData;
use super::{RinkWidget, WidgetContext};
+1 -1
View File
@@ -1,4 +1,4 @@
use dioxus_html::input_data::keyboard_types::Key;
use dioxus_html::{input_data::keyboard_types::Key, HasKeyboardData};
use dioxus_native_core::{
custom_element::CustomElement,
real_dom::{NodeImmutable, RealDom},
+8 -4
View File
@@ -1,6 +1,9 @@
use std::collections::HashMap;
use dioxus_html::{input_data::keyboard_types::Key, KeyboardData, MouseData};
use dioxus_html::{
input_data::keyboard_types::Key, prelude::*, HasKeyboardData, SerializedKeyboardData,
SerializedMouseData,
};
use dioxus_native_core::{
custom_element::CustomElement,
node::{OwnedAttributeDiscription, OwnedAttributeValue},
@@ -12,7 +15,8 @@ use dioxus_native_core::{
use shipyard::UniqueView;
use super::{RinkWidget, WidgetContext};
use crate::{query::get_layout, Event, EventData, FormData, Query};
use crate::hooks::FormData;
use crate::{query::get_layout, Event, EventData, Query};
#[derive(Debug)]
pub(crate) struct Slider {
@@ -209,7 +213,7 @@ impl Slider {
}
}
fn handle_keydown(&mut self, mut root: NodeMut, data: &KeyboardData) {
fn handle_keydown(&mut self, mut root: NodeMut, data: &SerializedKeyboardData) {
let key = data.key();
let step = self.step();
@@ -231,7 +235,7 @@ impl Slider {
self.write_value(rdom, id);
}
fn handle_mousemove(&mut self, mut root: NodeMut, data: &MouseData) {
fn handle_mousemove(&mut self, mut root: NodeMut, data: &SerializedMouseData) {
if !data.held_buttons().is_empty() {
let id = root.id();
let rdom = root.real_dom_mut();
+9 -5
View File
@@ -1,7 +1,10 @@
use std::{collections::HashMap, io::stdout};
use crossterm::{cursor::MoveTo, execute};
use dioxus_html::{input_data::keyboard_types::Key, KeyboardData, MouseData};
use dioxus_html::{
input_data::keyboard_types::Key, prelude::*, HasKeyboardData, SerializedKeyboardData,
SerializedMouseData,
};
use dioxus_native_core::{
custom_element::CustomElement,
node::OwnedAttributeDiscription,
@@ -14,7 +17,8 @@ use dioxus_native_core::{
use shipyard::UniqueView;
use taffy::geometry::Point;
use crate::{query::get_layout, Event, EventData, FormData, Query};
use crate::hooks::FormData;
use crate::{query::get_layout, Event, EventData, Query};
use super::{RinkWidget, WidgetContext};
@@ -184,7 +188,7 @@ impl<C: TextLikeController> TextLike<C> {
}
}
fn handle_keydown(&mut self, mut root: NodeMut, data: &KeyboardData) {
fn handle_keydown(&mut self, mut root: NodeMut, data: &SerializedKeyboardData) {
let key = data.key();
let modifiers = data.modifiers();
let code = data.code();
@@ -228,7 +232,7 @@ impl<C: TextLikeController> TextLike<C> {
}
}
fn handle_mousemove(&mut self, mut root: NodeMut, data: &MouseData) {
fn handle_mousemove(&mut self, mut root: NodeMut, data: &SerializedMouseData) {
if self.dragging {
let id = root.id();
let offset = data.element_coordinates();
@@ -245,7 +249,7 @@ impl<C: TextLikeController> TextLike<C> {
}
}
fn handle_mousedown(&mut self, mut root: NodeMut, data: &MouseData) {
fn handle_mousedown(&mut self, mut root: NodeMut, data: &SerializedMouseData) {
let offset = data.element_coordinates();
let mut new = Pos::new(offset.x as usize, offset.y as usize);