Skip to content

Commit

Permalink
chore: fix clippy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CalliEve committed Dec 20, 2024
1 parent a8c507c commit 36f7c9f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/components/atoms/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn Button(
let hover_class =
"hidden group-hover:block rounded text-xs absolute z-10 width-fit p-1.5 bg-neutral-800 text-center bottom-[110%] right-0 whitespace-pre";

let text_for_children = text.clone();
let text_for_children = text;
let children_signal = move || {
children
.clone()
Expand All @@ -178,7 +178,7 @@ pub fn Button(
{children_signal}
<Show when=move || has_children>
<span class=hover_class>
{text.clone()}
{text}
</span>
</Show>
</>
Expand Down
14 changes: 6 additions & 8 deletions src/components/atoms/canvas_info_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
use leptos::{
html::Div,
prelude::*,
text_prop::TextProp,
};

use crate::MapState;

/// A generic canvas info box that others can be based upon.
#[allow(clippy::needless_pass_by_value)] // cannot be a reference because of the `Fn` trait
#[component]
pub fn CanvasInfoBox<S>(
pub fn CanvasInfoBox(
/// The title of the info box,
title: S,
#[prop(into)]
title: TextProp,
/// If the info box should be shown.
click_position: Signal<Option<(f64, f64)>>,
/// The body of the info box if applicable.
#[prop(optional)]
children: Option<Children>,
) -> impl IntoView
where
S: ToString + 'static,
{
) -> impl IntoView {
let info_box_ref: NodeRef<Div> = NodeRef::new();
let map_state =
use_context::<RwSignal<MapState>>().expect("to have found the global map state");
Expand Down Expand Up @@ -78,7 +76,7 @@ where
class="absolute w-80 max-w-2xl max-h-full">
// title
<div class=move || String::from("text-lg px-2 pb-0.5 font-semibold bg-white shadow dark:bg-gray-700") + if has_children { " rounded-t-lg" } else {" rounded-lg"}>
<h2>{title.to_string()}</h2>
<h2>{move || title.get()}</h2>
</div>
<div
aria-hidden={move || if has_children {"false"} else {"true"}}
Expand Down
10 changes: 5 additions & 5 deletions src/components/atoms/text_with_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
)
});
// Clone to satisfy lifetimes and moves.
let button_label = edit_label.clone();
let button_label = edit_label;

let text_for_effect = text.clone();
Effect::new(move |_| {
Expand All @@ -99,7 +99,7 @@ where
fallback=move || view!{
<span class="flex justify-between max-h-5">
{text.get()}
<Button text={button_label.clone()} smaller=true on_click=Box::new(on_click)>
<Button text={button_label} smaller=true on_click=Box::new(on_click)>
"edit"
</Button>
</span>
Expand All @@ -109,14 +109,14 @@ where
type="text"
maxlength="100"
class="grow peer block min-h-[auto] w-full rounded border-b-2 rounded-md border-solid border-blue-400 bg-transparent px-3 py-[0.32rem] leading-[1.6] outline-none transition-all duration-200 ease-linear peer-focus:text-primary motion-reduce:transition-none dark:text-white dark:placeholder:text-neutral-300 dark:autofill:shadow-autofill dark:peer-focus:text-primary dark:border-blue-600 focus:border-blue-600 dark:focus:border-blue-800"
id={id.clone()}
id={id}
on:input=move |ev| set_text_input(event_target_value(&ev))
prop:value=move || text_input.get()
on:keydown=on_submit />
<label
for={id.clone()}
for={id}
class="pointer-events-none absolute left-3 top-0 mb-0 max-w-[90%] origin-[0_0] truncate pt-[0.37rem] leading-[1.6] text-neutral-500 peer-focus:text-primary -translate-y-[0.9rem] scale-[0.8] dark:text-neutral-400 dark:peer-focus:text-primary"
>{edit_label.clone()}
>{edit_label}
</label>
<Button text="finish editing" smaller=true on_click=Box::new(on_done)>
"done"
Expand Down
10 changes: 3 additions & 7 deletions src/components/canvas/keydown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ use crate::{
/// Listener for the [keydown] event on the canvas.
///
/// [keydown]: https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event
pub fn on_keydown(
map_state_signal: &RwSignal<MapState>,
history_signal: HistoryState,
ev: &KeyboardEvent,
) {
pub fn on_keydown(map_state_signal: &RwSignal<MapState>, ev: &KeyboardEvent) {
if ev.key() == "Escape" {
map_state_signal.update(|map_state| {
map_state.clear_all_selections();
Expand All @@ -30,7 +26,7 @@ pub fn on_keydown(

if ev.key() == "z" && ev.ctrl_key() {
map_state_signal.update(|map_state| {
if let Some(map) = history_signal.undo(
if let Some(map) = HistoryState::undo(
map_state
.get_map()
.clone(),
Expand All @@ -42,7 +38,7 @@ pub fn on_keydown(

if ev.key() == "Z" && ev.ctrl_key() {
map_state_signal.update(|map_state| {
if let Some(map) = history_signal.redo(
if let Some(map) = HistoryState::redo(
map_state
.get_map()
.clone(),
Expand Down
9 changes: 1 addition & 8 deletions src/components/canvas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use wasm_bindgen::{

use crate::components::{
ErrorState,
HistoryState,
MapState,
};

Expand Down Expand Up @@ -55,8 +54,6 @@ pub fn Canvas() -> impl IntoView {
use_context::<RwSignal<MapState>>().expect("to have found the global map state");
let error_state =
use_context::<RwSignal<ErrorState>>().expect("to have found the global error state");
let history_state =
use_context::<HistoryState>().expect("to have found the global history state");

// ensures we know the size of the canvas and that one page resizing, the canvas
// is also resized.
Expand All @@ -67,11 +64,7 @@ pub fn Canvas() -> impl IntoView {
DOCUMENT_LOADED.store(true, Ordering::Release);
let on_resize = Closure::<dyn Fn()>::new(move || update_canvas_size(&map_state));
let on_keydown = Closure::<dyn Fn(JsValue)>::new(move |ev: JsValue| {
on_keydown(
&map_state,
history_state,
ev.unchecked_ref(),
);
on_keydown(&map_state, ev.unchecked_ref());
});
window().set_onresize(Some(
on_resize
Expand Down
2 changes: 1 addition & 1 deletion src/components/molecules/edge_info_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn LineInfo(
if i > 0 {view!{
<hr class="my-0.5"/>
}.into_any()} else {
view!{}.into_any()}
().into_any()}
}
<p class="text-md font-semibold"><b>"Name:\n"</b>
<TextWithEdit
Expand Down
10 changes: 4 additions & 6 deletions src/components/organisms/canvas_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ pub fn CanvasControls() -> impl IntoView {
use_context::<RwSignal<MapState>>().expect("to have found the global map state");
let error_state =
use_context::<RwSignal<ErrorState>>().expect("to have found the global error state");
let history_state =
use_context::<HistoryState>().expect("to have found the global history state");
let (executor, _) = signal_local(
PoolExecutor::<AlgorithmWorker>::new(1).expect("failed to start web-worker pool"),
);
Expand Down Expand Up @@ -228,20 +226,20 @@ pub fn CanvasControls() -> impl IntoView {
let current_map = map_state
.get_map()
.clone();
if let Some(map) = history_state.undo(current_map) {
if let Some(map) = HistoryState::undo(current_map) {
map_state.set_map_no_history(map);
}
})
});
};
let redo = move |_| {
map_state.update(|map_state| {
let current_map = map_state
.get_map()
.clone();
if let Some(map) = history_state.redo(current_map) {
if let Some(map) = HistoryState::redo(current_map) {
map_state.set_map_no_history(map);
}
})
});
};

// Run the algorithm on the entire map.
Expand Down
22 changes: 16 additions & 6 deletions src/components/state/history.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Contains everything for being able to redo and undo map changes.
use std::{
collections::VecDeque,
sync::{
Expand All @@ -8,22 +10,28 @@ use std::{

use crate::models::Map;

/// The stack that contains the past maps.
static PAST_STACK: LazyLock<Mutex<BoundedStack<5, Map>>> =
LazyLock::new(|| Mutex::new(BoundedStack::new()));
/// The stack that contains maps with changes that were undone by the user.
static FUTURE_STACK: LazyLock<Mutex<BoundedStack<5, Map>>> =
LazyLock::new(|| Mutex::new(BoundedStack::new()));

/// A stack that is bounded to a certain size.
struct BoundedStack<const N: usize, T> {
/// The stack itself.
stack: VecDeque<T>,
}

impl<const N: usize, T> BoundedStack<N, T> {
/// Create a new bounded stack.
fn new() -> Self {
Self {
stack: VecDeque::new(),
}
}

/// Push an item onto the stack.
fn push(&mut self, item: T) {
self.stack
.push_back(item);
Expand All @@ -37,26 +45,26 @@ impl<const N: usize, T> BoundedStack<N, T> {
}
}

/// Pop an item off the stack.
fn pop(&mut self) -> Option<T> {
self.stack
.pop_back()
}

/// Clear the stack.
fn clear(&mut self) {
self.stack
.clear();
}
}

/// Contains everything for being able to redo and undo map changes.
#[derive(Debug, Copy, Clone)]
pub struct HistoryState {}

impl HistoryState {
pub fn new() -> Self {
Self {}
}

pub fn undo(&self, current: Map) -> Option<Map> {
/// Returns the last map that was stored.
pub fn undo(current: Map) -> Option<Map> {
let map = PAST_STACK
.lock()
.unwrap()
Expand All @@ -68,7 +76,8 @@ impl HistoryState {
Some(map)
}

pub fn redo(&self, current: Map) -> Option<Map> {
/// Returns the last map that was undone.
pub fn redo(current: Map) -> Option<Map> {
let map = FUTURE_STACK
.lock()
.unwrap()
Expand All @@ -81,6 +90,7 @@ impl HistoryState {
}
}

/// Pushes the current map onto the past stack and clears the future stack.
pub(super) fn push_past_map(map: Map) {
PAST_STACK
.lock()
Expand Down
2 changes: 0 additions & 2 deletions src/components/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ pub fn StateProvider(
/// The contents of the page that will have access to the global state.
children: Children,
) -> impl IntoView {
let history_state = history::HistoryState::new();
let map_state = RwSignal::new(MapState::new(Map::new()));
let error_state = RwSignal::new(error::ErrorState::new());

provide_context(history_state);
provide_context(map_state);
provide_context(error_state);

Expand Down

0 comments on commit 36f7c9f

Please sign in to comment.