audio_processor_iced_design_system/
router.rs

1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23use std::collections::HashMap;
24
25use iced::{widget::Text, Element};
26
27use crate::updatable::Updatable;
28
29pub type RouteIdRef<'a> = &'a str;
30pub type RouteId = String;
31
32#[derive(Debug, Clone)]
33pub enum Message<InnerMessage> {
34    RouteChanged(RouteId),
35    Inner(InnerMessage),
36}
37
38pub fn route_changed_message<InnerMessage>(route_id: RouteIdRef) -> Message<InnerMessage> {
39    Message::RouteChanged(String::from(route_id))
40}
41
42pub struct RouterState<RouteState: Updatable> {
43    current_route: RouteId,
44    routes: HashMap<RouteId, RouteState>,
45}
46
47impl<RouteState: Updatable> RouterState<RouteState> {
48    pub fn new(initial_route: RouteId, initial_routes: HashMap<RouteId, RouteState>) -> Self {
49        RouterState {
50            current_route: initial_route,
51            routes: initial_routes,
52        }
53    }
54
55    pub fn update(&mut self, message: Message<RouteState::Message>) {
56        match message {
57            Message::RouteChanged(route_id) => {
58                self.set_current_route(route_id);
59            }
60            Message::Inner(_) => {}
61        }
62    }
63
64    fn set_current_route(&mut self, route_id: String) {
65        self.current_route = route_id;
66    }
67
68    pub fn add_route(&mut self, route_id: RouteId, state: RouteState) {
69        self.routes.insert(route_id, state);
70    }
71
72    pub fn route<Message, F>(&mut self, route_id: RouteIdRef, renderer: F) -> Element<Message>
73    where
74        F: Fn(&mut RouteState) -> Element<Message>,
75    {
76        if self.current_route != route_id {
77            return empty();
78        }
79
80        if let Some(route) = self.routes.get_mut(route_id) {
81            return renderer(route);
82        }
83
84        empty()
85    }
86}
87
88fn empty<'a, Message>() -> Element<'a, Message> {
89    Text::new("").into()
90}