Skip to content

Commit 97fbdef

Browse files
authored
Merge pull request #77 from Leif-Rydenfalk/development
2 parents 5401b21 + 5ed711c commit 97fbdef

File tree

8 files changed

+38
-30
lines changed

8 files changed

+38
-30
lines changed

examples/flip_cards/src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rust_search::SearchBuilder;
33
use serde_json::{Map, Value};
44
use std::collections::{HashSet, VecDeque};
55
use std::fs;
6+
use std::sync::Arc;
67

78
// Function to find and extract flip cards from JSON data
89
fn find_flip_cards(value: &Value) -> Vec<Value> {
@@ -74,8 +75,8 @@ fn find_flip_cards(value: &Value) -> Vec<Value> {
7475
#[derive(Clone)]
7576
struct FlipCardState {
7677
show_answer: bool,
77-
question: String,
78-
answer: String,
78+
question: Arc<str>,
79+
answer: Arc<str>,
7980
}
8081

8182
// Struct to represent the state of all flip cards
@@ -114,8 +115,8 @@ fn main() {
114115
.iter()
115116
.map(|card| FlipCardState {
116117
show_answer: false,
117-
question: card["q"].as_str().unwrap().to_string(),
118-
answer: card["a"].as_str().unwrap().to_string(),
118+
question: card["q"].to_string().into(),
119+
answer: card["a"].to_string().into(),
119120
})
120121
.collect(),
121122
current_index: 0, // Start at the first card
@@ -128,9 +129,9 @@ fn main() {
128129
// Render the current flip card
129130
vstack((
130131
text(if current_card.show_answer {
131-
current_card.answer.as_str()
132+
&current_card.answer
132133
} else {
133-
current_card.question.as_str()
134+
&current_card.question
134135
})
135136
.font_size(12)
136137
.padding(Auto),

src/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::any::TypeId;
55
use std::collections::{HashMap, HashSet};
66
use std::iter::FromIterator;
77
use std::ops;
8+
use std::sync::Arc;
89

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

2021
#[derive(Clone, Eq, PartialEq)]
2122
pub struct CommandInfo {
22-
pub path: String,
23+
pub path: Arc<str>,
2324
pub key: Option<HotKey>,
2425
}
2526

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

8384
/// The current title of the window
84-
pub window_title: String,
85+
pub window_title: Arc<str>,
8586

8687
/// Are we fullscreen?
8788
pub fullscreen: bool,

src/event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use crate::*;
24

35
/// User interface event.
@@ -29,7 +31,7 @@ pub enum Event {
2931
MouseLeftWindow,
3032

3133
/// Menu command.
32-
Command(String),
34+
Command(Arc<str>),
3335

3436
/// Key press.
3537
Key(Key),

src/views/command.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::*;
2-
use std::any::Any;
2+
use std::{any::Any, sync::Arc};
33

44
#[derive(Clone)]
55
pub struct Command<V, F> {
66
child: V,
7-
name: String,
7+
name: Arc<str>,
88
key: Option<HotKey>,
99
func: F,
1010
}
@@ -14,7 +14,7 @@ where
1414
V: View,
1515
F: Fn(&mut Context) + Clone + 'static,
1616
{
17-
pub fn new(v: V, name: String, key: Option<HotKey>, f: F) -> Self {
17+
pub fn new(v: V, name: Arc<str>, key: Option<HotKey>, f: F) -> Self {
1818
Self {
1919
child: v,
2020
name,
@@ -97,7 +97,7 @@ impl<V, F> private::Sealed for Command<V, F> {}
9797

9898
pub trait DynCommandBase {
9999
fn exec(&self);
100-
fn name(&self) -> String;
100+
fn name(&self) -> Arc<str>;
101101
fn key(&self) -> Option<HotKey>;
102102
}
103103

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

338338
#[derive(Clone)]
339339
pub struct NullCommand {
340-
name: String,
340+
name: Arc<str>,
341341
key: Option<HotKey>,
342342
}
343343

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

352352
impl DynCommandBase for NullCommand {
353353
fn exec(&self) {}
354-
fn name(&self) -> String {
354+
fn name(&self) -> Arc<str> {
355355
self.name.clone()
356356
}
357357
fn key(&self) -> Option<HotKey> {
@@ -379,7 +379,7 @@ impl NullCommand {
379379

380380
#[derive(Clone)]
381381
pub struct Command2<F> {
382-
name: String,
382+
name: Arc<str>,
383383
key: Option<HotKey>,
384384
func: F,
385385
}
@@ -391,7 +391,7 @@ where
391391
fn exec(&self) {
392392
(self.func)();
393393
}
394-
fn name(&self) -> String {
394+
fn name(&self) -> Arc<str> {
395395
self.name.clone()
396396
}
397397
fn key(&self) -> Option<HotKey> {

src/views/text.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ macro_rules! impl_text {
106106
let txt = &format!("{}", self);
107107
let vger = &mut args.vger;
108108
let origin = vger.text_bounds(txt, Text::DEFAULT_SIZE, None).origin;
109-
109+
110110
vger.save();
111111
vger.translate([-origin.x, -origin.y]);
112112
vger.text(txt, Text::DEFAULT_SIZE, TEXT_COLOR, None);
@@ -116,7 +116,7 @@ macro_rules! impl_text {
116116
let txt = &format!("{}", self);
117117
(args.text_bounds)(txt, Text::DEFAULT_SIZE, None).size
118118
}
119-
119+
120120
fn access(
121121
&self,
122122
path: &mut IdPath,
@@ -131,8 +131,7 @@ macro_rules! impl_text {
131131
}
132132
}
133133

134-
impl TextModifiers for $ty
135-
{
134+
impl TextModifiers for $ty {
136135
fn font_size(self, size: u32) -> Text {
137136
Text {
138137
text: format!("{}", self),
@@ -159,8 +158,7 @@ macro_rules! impl_text {
159158
}
160159
}
161160
}
162-
163-
}
161+
};
164162
}
165163

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

208-
impl TextModifiers for &'static str
209-
{
206+
impl TextModifiers for &'static str {
210207
fn font_size(self, size: u32) -> Text {
211208
Text {
212209
text: format!("{}", self),

src/views/touch.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
use crate::*;
22
use std::any::Any;
33

4+
#[derive(Clone, PartialEq)]
45
pub enum TouchState {
56
Begin,
67
End,
78
}
89

10+
#[derive(Clone)]
911
pub struct TouchInfo {
12+
/// The position of the touch in local space of the view that received the touch.
1013
pub pt: LocalPoint,
14+
15+
/// The mouse button that was used for the touch if a mouse was used.
1116
pub button: Option<MouseButton>,
17+
18+
/// The state of the touch. IE: Begin or End.
1219
pub state: TouchState,
1320
}
1421

src/views/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::*;
2-
use std::any::Any;
2+
use std::{any::Any, sync::Arc};
33

44
/// Struct for the `window_title` modifier.
55
#[derive(Clone)]
66
pub struct TitleView<V> {
77
child: V,
8-
title: String,
8+
title: Arc<str>,
99
}
1010

1111
impl<V> TitleView<V>

src/winit_event_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct EventHandler<T>
157157
where
158158
T: View,
159159
{
160-
title: String,
160+
title: Arc<str>,
161161
running: bool,
162162
// The GPU resources, if running.
163163
context: Option<DrawContext>,
@@ -189,7 +189,7 @@ where
189189
self.running = true;
190190

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

501501
let window_title = String::from("rui");
502502
let mut app = EventHandler {
503-
title: window_title,
503+
title: window_title.into(),
504504
running: false,
505505
context: None,
506506
window: None,

0 commit comments

Comments
 (0)