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
13 changes: 7 additions & 6 deletions examples/flip_cards/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rust_search::SearchBuilder;
use serde_json::{Map, Value};
use std::collections::{HashSet, VecDeque};
use std::fs;
use std::sync::Arc;

// Function to find and extract flip cards from JSON data
fn find_flip_cards(value: &Value) -> Vec<Value> {
Expand Down Expand Up @@ -74,8 +75,8 @@ fn find_flip_cards(value: &Value) -> Vec<Value> {
#[derive(Clone)]
struct FlipCardState {
show_answer: bool,
question: String,
answer: String,
question: Arc<str>,
answer: Arc<str>,
}

// Struct to represent the state of all flip cards
Expand Down Expand Up @@ -114,8 +115,8 @@ fn main() {
.iter()
.map(|card| FlipCardState {
show_answer: false,
question: card["q"].as_str().unwrap().to_string(),
answer: card["a"].as_str().unwrap().to_string(),
question: card["q"].to_string().into(),
answer: card["a"].to_string().into(),
})
.collect(),
current_index: 0, // Start at the first card
Expand All @@ -128,9 +129,9 @@ fn main() {
// Render the current flip card
vstack((
text(if current_card.show_answer {
current_card.answer.as_str()
&current_card.answer
} else {
current_card.question.as_str()
&current_card.question
})
.font_size(12)
.padding(Auto),
Expand Down
5 changes: 3 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::any::TypeId;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use std::ops;
use std::sync::Arc;

pub type LocalSpace = vger::defs::LocalSpace;
pub type WorldSpace = vger::defs::WorldSpace;
Expand All @@ -19,7 +20,7 @@ pub type WorldToLocal = Transform2D<f32, WorldSpace, LocalSpace>;

#[derive(Clone, Eq, PartialEq)]
pub struct CommandInfo {
pub path: String,
pub path: Arc<str>,
pub key: Option<HotKey>,
}

Expand Down Expand Up @@ -81,7 +82,7 @@ pub struct Context {
pub(crate) focused_id: Option<ViewId>,

/// The current title of the window
pub window_title: String,
pub window_title: Arc<str>,

/// Are we fullscreen?
pub fullscreen: bool,
Expand Down
4 changes: 3 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use crate::*;

/// User interface event.
Expand Down Expand Up @@ -29,7 +31,7 @@ pub enum Event {
MouseLeftWindow,

/// Menu command.
Command(String),
Command(Arc<str>),

/// Key press.
Key(Key),
Expand Down
16 changes: 8 additions & 8 deletions src/views/command.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::*;
use std::any::Any;
use std::{any::Any, sync::Arc};

#[derive(Clone)]
pub struct Command<V, F> {
child: V,
name: String,
name: Arc<str>,
key: Option<HotKey>,
func: F,
}
Expand All @@ -14,7 +14,7 @@ where
V: View,
F: Fn(&mut Context) + Clone + 'static,
{
pub fn new(v: V, name: String, key: Option<HotKey>, f: F) -> Self {
pub fn new(v: V, name: Arc<str>, key: Option<HotKey>, f: F) -> Self {
Self {
child: v,
name,
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<V, F> private::Sealed for Command<V, F> {}

pub trait DynCommandBase {
fn exec(&self);
fn name(&self) -> String;
fn name(&self) -> Arc<str>;
fn key(&self) -> Option<HotKey>;
}

Expand Down Expand Up @@ -337,7 +337,7 @@ impl<V, C> private::Sealed for CommandGroup<V, C> {}

#[derive(Clone)]
pub struct NullCommand {
name: String,
name: Arc<str>,
key: Option<HotKey>,
}

Expand All @@ -351,7 +351,7 @@ pub fn command(name: &str) -> NullCommand {

impl DynCommandBase for NullCommand {
fn exec(&self) {}
fn name(&self) -> String {
fn name(&self) -> Arc<str> {
self.name.clone()
}
fn key(&self) -> Option<HotKey> {
Expand Down Expand Up @@ -379,7 +379,7 @@ impl NullCommand {

#[derive(Clone)]
pub struct Command2<F> {
name: String,
name: Arc<str>,
key: Option<HotKey>,
func: F,
}
Expand All @@ -391,7 +391,7 @@ where
fn exec(&self) {
(self.func)();
}
fn name(&self) -> String {
fn name(&self) -> Arc<str> {
self.name.clone()
}
fn key(&self) -> Option<HotKey> {
Expand Down
13 changes: 5 additions & 8 deletions src/views/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ macro_rules! impl_text {
let txt = &format!("{}", self);
let vger = &mut args.vger;
let origin = vger.text_bounds(txt, Text::DEFAULT_SIZE, None).origin;

vger.save();
vger.translate([-origin.x, -origin.y]);
vger.text(txt, Text::DEFAULT_SIZE, TEXT_COLOR, None);
Expand All @@ -116,7 +116,7 @@ macro_rules! impl_text {
let txt = &format!("{}", self);
(args.text_bounds)(txt, Text::DEFAULT_SIZE, None).size
}

fn access(
&self,
path: &mut IdPath,
Expand All @@ -131,8 +131,7 @@ macro_rules! impl_text {
}
}

impl TextModifiers for $ty
{
impl TextModifiers for $ty {
fn font_size(self, size: u32) -> Text {
Text {
text: format!("{}", self),
Expand All @@ -159,8 +158,7 @@ macro_rules! impl_text {
}
}
}

}
};
}

// XXX: this used to be generic for any Display but
Expand Down Expand Up @@ -205,8 +203,7 @@ impl DynView for &'static str {
}
}

impl TextModifiers for &'static str
{
impl TextModifiers for &'static str {
fn font_size(self, size: u32) -> Text {
Text {
text: format!("{}", self),
Expand Down
7 changes: 7 additions & 0 deletions src/views/touch.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
use crate::*;
use std::any::Any;

#[derive(Clone, PartialEq)]
pub enum TouchState {
Begin,
End,
}

#[derive(Clone)]
pub struct TouchInfo {
/// The position of the touch in local space of the view that received the touch.
pub pt: LocalPoint,

/// The mouse button that was used for the touch if a mouse was used.
pub button: Option<MouseButton>,

/// The state of the touch. IE: Begin or End.
pub state: TouchState,
}

Expand Down
4 changes: 2 additions & 2 deletions src/views/window.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::*;
use std::any::Any;
use std::{any::Any, sync::Arc};

/// Struct for the `window_title` modifier.
#[derive(Clone)]
pub struct TitleView<V> {
child: V,
title: String,
title: Arc<str>,
}

impl<V> TitleView<V>
Expand Down
6 changes: 3 additions & 3 deletions src/winit_event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ struct EventHandler<T>
where
T: View,
{
title: String,
title: Arc<str>,
running: bool,
// The GPU resources, if running.
context: Option<DrawContext>,
Expand Down Expand Up @@ -189,7 +189,7 @@ where
self.running = true;

// Create the main window.
let window_attributes = Window::default_attributes().with_title(&self.title);
let window_attributes = Window::default_attributes().with_title(self.title.to_string());
self.window = match event_loop.create_window(window_attributes) {
Err(e) => {
log::error!("Error creating window: {:?}", e);
Expand Down Expand Up @@ -500,7 +500,7 @@ pub fn rui(view: impl View) {

let window_title = String::from("rui");
let mut app = EventHandler {
title: window_title,
title: window_title.into(),
running: false,
context: None,
window: None,
Expand Down