Skip to content
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
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/craft_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ default = ["clipboard", "accesskit"]
[dependencies]
craft_logging = { path = "../craft_logger", version = "0.1.0" }

smol_str = "0.3.2"

[dependencies.cfg-if]
workspace = true

Expand Down
5 changes: 3 additions & 2 deletions crates/craft_core/src/animations/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use smallvec::SmallVec;
use std::collections::HashMap;
use std::iter::zip;
use std::time::Duration;
use smol_str::SmolStr;

#[derive(Clone, Debug)]
pub struct KeyFrame {
Expand Down Expand Up @@ -80,7 +81,7 @@ pub enum TimingFunction {

#[derive(Clone, Debug)]
pub struct Animation {
pub name: String,
pub name: SmolStr,
pub key_frames: SmallVec<[KeyFrame; 2]>,
pub duration: Duration,
pub timing_function: TimingFunction,
Expand All @@ -96,7 +97,7 @@ pub enum LoopAmount {
impl Animation {
pub fn new(name: &str, duration: Duration, timing_function: TimingFunction) -> Self {
Self {
name: name.to_string(),
name: name.into(),
key_frames: SmallVec::new(),
duration,
timing_function,
Expand Down
11 changes: 6 additions & 5 deletions crates/craft_core/src/components/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::elements::{Container, Element};
use crate::window_context::WindowContext;
use std::any::{Any, TypeId};
use std::ops::Deref;
use smol_str::SmolStr;

/// A Component's view function.
pub type ViewFn = fn(
Expand Down Expand Up @@ -42,7 +43,7 @@ pub struct ComponentData {
pub view_fn: ViewFn,
pub update_fn: UpdateFn,
/// A unique identifier for view_fn.
pub tag: String,
pub tag: SmolStr,
/// The type id of the view function. This is currently not used.
pub type_id: TypeId,
}
Expand All @@ -59,7 +60,7 @@ pub enum ComponentOrElement {
pub struct ComponentSpecification {
pub component: ComponentOrElement,
/// Specify a key when the component position or type may change, but state should be retained.
pub key: Option<String>,
pub key: Option<SmolStr>,
/// A read only reference to the props of the component.
pub props: Option<Props>,
/// The children of the component.
Expand All @@ -79,8 +80,8 @@ impl ComponentSpecification {
}
}

pub fn key(mut self, key: &str) -> Self {
self.key = Some(key.to_owned());
pub fn key<T: Into<SmolStr>>(mut self, key: T) -> Self {
self.key = Some(key.into());
self
}

Expand Down Expand Up @@ -416,7 +417,7 @@ where
default_props: Self::default_props,
view_fn: Self::generic_view_internal,
update_fn: Self::update_internal,
tag: std::any::type_name_of_val(&Self::generic_view_internal).to_string(),
tag: std::any::type_name_of_val(&Self::generic_view_internal).into(),
type_id: Self::generic_view_internal.type_id(),
};

Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/devtools/dev_tools_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::sync::Arc;
use kurbo::Affine;
use taffy::{NodeId, TaffyTree};
use winit::window::Window;
use smol_str::SmolStr;

#[derive(Clone, Default)]
pub struct DevTools {
Expand Down
3 changes: 2 additions & 1 deletion crates/craft_core/src/devtools/layout_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ impl Component for LayoutWindow {
}

fn update(context: &mut Context<Self>) {
if let Some(id) = context.target().and_then(|e| e.get_id().clone()) {
let id = context.target().and_then(|e| e.get_id().map(|id| id.to_string()));
if let Some(id) = id {
if context.message().clicked() {
if id == "tab_styles" {
context.state_mut().layout_tab = LayoutTab::Styles
Expand Down
2 changes: 1 addition & 1 deletion crates/craft_core/src/devtools/tree_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn tree_window(
row = row.push(
Container::new()
.push(
Text::new(custom_id.as_str())
Text::new(custom_id)
.color(Color::WHITE)
.margin("2.5px", "10px", "2.5px", "10px")
)
Expand Down
3 changes: 2 additions & 1 deletion crates/craft_core/src/elements/base_element_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::elements::element_states::ElementState;
use crate::style::Style;
use std::collections::HashMap;
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use crate::animations::animation::ActiveAnimation;

#[derive(Debug, Default, Clone)]
Expand All @@ -15,7 +16,7 @@ pub struct BaseElementState {
/// Useful for scroll thumbs.
pub(crate) pointer_capture: HashMap<i64, bool>,
pub(crate) focused: bool,
pub(crate) animations: Option<FxHashMap<String, ActiveAnimation>>,
pub(crate) animations: Option<FxHashMap<SmolStr, ActiveAnimation>>,
}

impl<'a> BaseElementState {
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use kurbo::Affine;
use taffy::{NodeId, TaffyTree};
use winit::window::Window;
use crate::elements::StatefulElement;
use smol_str::SmolStr;

#[derive(Clone, Default)]
pub struct Canvas {
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use kurbo::Affine;
use taffy::{NodeId, TaffyTree};
use winit::window::Window;
use crate::elements::StatefulElement;
use smol_str::SmolStr;

/// An element for storing related elements.
#[derive(Clone, Default)]
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/dropdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::sync::Arc;
use kurbo::Affine;
use taffy::{NodeId, Position, TaffyTree, TraversePartialTree};
use winit::window::Window;
use smol_str::SmolStr;

/// The index of the dropdown list in the layout tree.
const DROPDOWN_LIST_INDEX: usize = 1;
Expand Down
14 changes: 8 additions & 6 deletions crates/craft_core/src/elements/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::mem;
use std::sync::Arc;
use std::time::Duration;
use rustc_hash::FxHashMap;
use smol_str::SmolStr;
use taffy::{NodeId, Overflow, TaffyTree};
use winit::window::Window;

Expand Down Expand Up @@ -72,8 +73,8 @@ pub trait Element: Any + StandardElementClone + Send + Sync {
}
}

fn get_id(&self) -> &Option<String> {
&self.element_data().id
fn get_id(&self) -> Option<&str> {
self.element_data().id.as_deref()
}

fn component_id(&self) -> ComponentId {
Expand Down Expand Up @@ -552,8 +553,8 @@ macro_rules! generate_component_methods_no_children {
}

#[allow(dead_code)]
pub fn key(mut self, key: &str) -> Self {
self.element_data.key = Some(key.to_string());
pub fn key<T: Into<SmolStr>>(mut self, key: T) -> Self {
self.element_data.key = Some(key.into());
self
}

Expand All @@ -564,8 +565,8 @@ macro_rules! generate_component_methods_no_children {
}

#[allow(dead_code)]
pub fn id(mut self, id: &str) -> Self {
self.element_data.id = Some(id.to_string());
pub fn id<T: Into<SmolStr>>(mut self, id: T) -> Self {
self.element_data.id = Some(id.into());
self
}

Expand Down Expand Up @@ -640,6 +641,7 @@ macro_rules! generate_component_methods_private_push {

#[macro_export]
macro_rules! generate_component_methods {

() => {
$crate::generate_component_methods_no_children!();

Expand Down
5 changes: 3 additions & 2 deletions crates/craft_core/src/elements/element_data.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use smol_str::SmolStr;
use crate::components::{ComponentId, ComponentSpecification};
use crate::components::Props;
use crate::elements::element::ElementBoxed;
Expand Down Expand Up @@ -31,14 +32,14 @@ pub struct ElementData {
pub children: Vec<ElementBoxed>,

/// A user-defined id for the element.
pub id: Option<String>,
pub id: Option<SmolStr>,

/// The id of the component that this element belongs to.
pub component_id: ComponentId,

// Used for converting the element to a component specification.
pub child_specs: Vec<ComponentSpecification>,
pub(crate) key: Option<String>,
pub(crate) key: Option<SmolStr>,
pub(crate) props: Option<Props>,
pub(crate) event_handlers: EventHandlers,
}
Expand Down
12 changes: 6 additions & 6 deletions crates/craft_core/src/elements/element_pre_order_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ mod tests {
initial_tree.component_tree.print_tree();

let mut iter = initial_tree.element_tree.internal.pre_order_iter();
assert_eq!(iter.next().unwrap().get_id().clone(), Some("0".to_string()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("1".to_string()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("2".to_string()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("3".to_string()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("4".to_string()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("5".to_string()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("0".into()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("1".into()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("2".into()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("3".into()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("4".into()));
assert_eq!(iter.next().unwrap().get_id().clone(), Some("5".into()));
assert!(iter.next().is_none());
}
}
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::sync::Arc;
use kurbo::Affine;
use taffy::{NodeId, TaffyTree};
use winit::window::Window;
use smol_str::SmolStr;

#[derive(Clone)]
pub struct Font {
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::sync::Arc;
use kurbo::Affine;
use taffy::{NodeId, TaffyTree};
use winit::window::Window;
use smol_str::SmolStr;

#[derive(Clone)]
pub struct Image {
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::sync::Arc;
use kurbo::Affine;
use taffy::{NodeId, TaffyTree};
use winit::window::Window;
use smol_str::SmolStr;

/// An element for storing related elements.
#[derive(Clone, Default)]
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use ui_events::keyboard::{Code, KeyState};
use ui_events::keyboard::Code::{ArrowDown, ArrowLeft, ArrowRight, ArrowUp};
use winit::window::Window;
use crate::elements::StatefulElement;
use smol_str::SmolStr;

#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
pub enum SliderDirection {
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use taffy::{NodeId, TaffyTree};
use ui_events::keyboard::{Code, KeyState};
use winit::window::Window;
use crate::elements::StatefulElement;
use smol_str::SmolStr;

/// An element that represents an on or off state.
#[derive(Clone)]
Expand Down
9 changes: 5 additions & 4 deletions crates/craft_core/src/elements/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ use winit::dpi;
use web_time as time;
use winit::window::Window;
use craft_primitives::ColorBrush;
use smol_str::SmolStr;

// A stateful element that shows text.
#[derive(Clone, Default)]
pub struct Text {
text: Option<String>,
text: Option<SmolStr>,
element_data: ElementData,
selectable: bool,
}

pub struct TextState {
scale_factor: f32,
selection: Selection,
text: Option<String>,
text: Option<SmolStr>,
text_hash: Option<u64>,
text_render: Option<TextRender>,
last_text_style: Style,
Expand All @@ -74,7 +75,7 @@ impl StatefulElement<TextState> for Text {}
impl Text {
pub fn new(text: &str) -> Text {
Text {
text: Some(text.to_string()),
text: Some(text.into()),
element_data: Default::default(),
selectable: true,
}
Expand Down Expand Up @@ -320,7 +321,7 @@ impl Element for Text {

let mut current_node = accesskit::Node::new(Role::Label);
let padding_box = self.element_data().layout_item.computed_box_transformed.padding_rectangle().scale(scale_factor);
current_node.set_value(*Box::new(state.text.clone().unwrap()));
current_node.set_value(state.text.as_deref().unwrap());
current_node.add_action(Action::SetTextSelection);

current_node.set_bounds(accesskit::Rect {
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::elements::base_element_state::BaseElementState;
use crate::reactive::element_id::create_unique_element_id;
use crate::text::parley_editor::{PlainEditor, PlainEditorDriver};
use crate::utils::cloneable_any::CloneableAny;
use smol_str::SmolStr;

// A stateful element that shows text.
#[derive(Clone, Default)]
Expand Down
1 change: 1 addition & 0 deletions crates/craft_core/src/elements/tinyvg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::Arc;
use kurbo::Affine;
use taffy::{NodeId, TaffyTree};
use winit::window::Window;
use smol_str::SmolStr;

#[derive(Clone)]
pub struct TinyVg {
Expand Down
Loading