Created
May 26, 2020 00:40
-
-
Save justinmoon/d63ee3c85da224034f29448f5b5623ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use iced::{button, Align, Button, Column, Element, Row, Sandbox, Settings, Text}; | |
// | |
// Node page | |
// | |
struct NodePage {} | |
impl NodePage { | |
fn view(&mut self) -> Element<Message> { | |
Text::new("Node").into() | |
} | |
} | |
// | |
// Accounts page | |
// | |
struct AccountsPage {} | |
impl AccountsPage { | |
fn view(&mut self) -> Element<Message> { | |
Text::new("Accounts").into() | |
} | |
} | |
enum Modal { | |
Send(SendModal), | |
Receive(ReceiveModal), | |
} | |
struct SendModal {} | |
impl SendModal { | |
fn view(&mut self) -> Element<Message> { | |
Text::new("Send").into() | |
} | |
} | |
struct ReceiveModal {} | |
impl ReceiveModal { | |
fn view(&mut self) -> Element<Message> { | |
Text::new("Receive").into() | |
} | |
} | |
// | |
// Nav | |
// | |
#[derive(Default)] | |
struct Nav { | |
accounts_button: button::State, | |
send_button: button::State, | |
receive_button: button::State, | |
node_button: button::State, | |
} | |
impl Nav { | |
fn view(&mut self) -> Element<Message> { | |
Row::new() | |
.align_items(Align::Center) | |
.push( | |
Button::new(&mut self.accounts_button, Text::new("Accounts")) | |
.on_press(Message::VisitAccounts), | |
) | |
.push( | |
Button::new(&mut self.send_button, Text::new("Send")).on_press(Message::SendModal), | |
) | |
.push( | |
Button::new(&mut self.receive_button, Text::new("Receive")) | |
.on_press(Message::ReceiveModal), | |
) | |
.push( | |
Button::new(&mut self.node_button, Text::new("Node")).on_press(Message::VisitNode), | |
) | |
.into() | |
} | |
} | |
enum CurrentPage { | |
Accounts, | |
Node, | |
} | |
struct Demo { | |
// Pages | |
accounts_page: AccountsPage, | |
node_page: NodePage, | |
current_page: CurrentPage, | |
// Modal | |
modal: Option<SendModal>, | |
// Navigation | |
nav: Nav, | |
} | |
#[derive(Debug, Clone, Copy)] | |
enum Message { | |
// Navigation | |
VisitAccounts, | |
VisitNode, | |
SendModal, | |
ReceiveModal, | |
} | |
impl Sandbox for Demo { | |
type Message = Message; | |
fn new() -> Self { | |
Self { | |
accounts_page: AccountsPage {}, | |
node_page: NodePage {}, | |
current_page: CurrentPage::Accounts, | |
modal: None, | |
nav: Nav::default(), | |
} | |
} | |
fn title(&self) -> String { | |
String::from("Router") | |
} | |
fn update(&mut self, message: Message) { | |
match message { | |
Message::VisitAccounts => self.current_page = CurrentPage::Accounts, | |
Message::VisitNode => self.current_page = CurrentPage::Node, | |
Message::SendModal => self.modal = Some(SendModal {}), | |
// FIXME | |
Message::ReceiveModal => self.modal = Some(SendModal {}), | |
} | |
} | |
fn view(&mut self) -> Element<Message> { | |
Column::new() | |
.padding(20) | |
.align_items(Align::Center) | |
.push(self.nav.view()) | |
.push(match &self.modal { | |
Some(modal) => modal.view(), | |
None => match self.current_page { | |
CurrentPage::Accounts => self.accounts_page.view(), | |
CurrentPage::Node => self.node_page.view(), | |
}, | |
}) | |
.into() | |
} | |
} | |
pub fn main() { | |
Demo::run(Settings::default()) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment