首页 > 解决方案 > Rust - Iced 应用程序上的多个页面

问题描述

Iced GUI 问题(针对多页结构):我有一个应用程序,可以说可能有 10 页。我想要松耦合。

mod login;
pub use login::Login;

use iced::{
    button, executor, text_input, Application, Clipboard, Column, Command, Container, Element,
    Length, Settings,
};
struct MyWindow {
    page: Page,
}
#[derive(Debug)]
pub enum Page {
    Login(Login),
}

impl Application for MyWindow {
    type Executor = executor::Default;
    type Flags = ();
    type Message = Page;

    fn new(_flags: ()) -> (MyWindow, Command<Page>) {
        (
            MyWindow {
                page: Page::Login(Login {
                    username: String::from(""),
                    password: String::from(""),
                    login_button: button::State::new(),
                    register_button: button::State::new(),
                    forgotpass_button: button::State::new(),
                }),
            },
            Command::none(),
        )
    }

    fn title(&self) -> String {
        String::from("Iced App")
    }

    fn update(&mut self, message: Page, clipboard: &mut Clipboard) -> Command<Self::Message> {
        match self.page {
            Page::Login(_) => Command::none(),
        }
    }

    fn view(&mut self) -> Element<'_, Self::Message> {
        match self.page {
            Page::Login(_) => {
                //adds login view logic
            }
        }
    }
}
fn main() -> iced::Result {
    MyWindow::run(Settings::default())
}


我的login.rs样子:

use iced::button;
#[derive(Debug)]
pub struct Login {
    //The username and password values
    pub(crate) username: String,
    pub(crate) password: String,
    pub(crate) login_button: button::State,
    pub(crate) register_button: button::State,
    pub(crate) forgotpass_button: button::State,
}

pub enum LoginMessage {
    LoginPressed,
    RegisterPressed,
    ForgotPassPresed,
}

我会构造它,以便在每个相应的页面上添加页面逻辑吗?在main.rsunderview()的匹配项中,我只想调用一个公共函数(例如,init_login()初始化该页面)。

谢谢

标签: rust

解决方案


推荐阅读