prehnite/app/window/version_info_window/
mod.rs

1use fluent_bundle::FluentArgs;
2use crate::app::resources::app_icon_handle;
3use crate::app::window::{app_default_window_settings, Window, WindowMessage};
4use iced::alignment::Horizontal;
5use iced::widget::text::Wrapping;
6use iced::widget::{button, Container};
7use iced::window::Settings;
8use iced::{window, Element, Length, Size, Task};
9use prehnite_core::i18n::{i18n, i18n_fmt, i18n_w};
10use prehnite_core::widget::font::ftext;
11
12fn app_version_info() -> String {
13    let mut args = FluentArgs::new();
14    args.set("app-name", env!("CARGO_PKG_NAME"));
15    args.set("version", env!("CARGO_PKG_VERSION"));
16    i18n_fmt("version-info-detail", Some(&args))
17}
18
19fn app_build_target() -> &'static str {
20    env!("BUILD_INFO_TARGET")
21}
22
23fn app_build_features() -> &'static str {
24    env!("BUILD_INFO_FEATURE")
25}
26
27fn app_build_profile() -> &'static str {
28    env!("BUILD_PROFILE")
29}
30
31
32#[derive(Clone, Debug)]
33pub enum VersionInfoWindowMessage {
34    Close,
35}
36
37#[derive(Debug)]
38pub struct VersionInfoWindow {
39    window_id: Option<window::Id>,
40}
41
42impl VersionInfoWindow {
43    fn update_impl(&mut self, message: VersionInfoWindowMessage) -> Task<VersionInfoWindowMessage> {
44        match message {
45            VersionInfoWindowMessage::Close => window::close(self.window_id.unwrap()),
46        }
47    }
48
49    fn view_impl(&'_ self) -> Element<'_, VersionInfoWindowMessage> {
50        Container::new(
51            iced::widget::column![
52                iced::widget::image(app_icon_handle()),
53                iced::widget::scrollable(
54                    Container::new(
55                        ftext(format!(
56                            "{}.{}.{}{}",
57                            app_version_info(),
58                            app_build_profile(),
59                            app_build_target(),
60                            if app_build_features().is_empty() {
61                                "".to_string()
62                            } else {
63                                format!(".[{}]", app_build_features())
64                            }
65                        ))
66                        .wrapping(Wrapping::None)
67                    )
68                    .padding(15)
69                )
70                .width(300)
71                .horizontal(),
72                Element::new(iced::widget::space().height(10)),
73                button(i18n_w("close")).on_press(VersionInfoWindowMessage::Close)
74            ]
75            .align_x(Horizontal::Center),
76        )
77        .center(Length::Fill)
78        .into()
79    }
80}
81
82impl Window for VersionInfoWindow {
83    fn new() -> Self
84    where
85        Self: Sized,
86    {
87        VersionInfoWindow { window_id: None }
88    }
89
90    fn update(&mut self, message: WindowMessage) -> Task<WindowMessage> {
91        if let WindowMessage::VersionInfoWindowMessage(msg) = message {
92            self.update_impl(msg).map(WindowMessage::VersionInfoWindowMessage)
93        } else {
94            Task::none()
95        }
96    }
97
98    fn view(&'_ self) -> Element<'_, WindowMessage> {
99        self.view_impl().map(WindowMessage::VersionInfoWindowMessage)
100    }
101
102    fn title(&'_ self) -> String {
103        i18n("version-info")
104    }
105
106    fn set_window_id(&mut self, window_id: window::Id) {
107        self.window_id = Some(window_id)
108    }
109
110    fn window_settings() -> Settings
111    where
112        Self: Sized,
113    {
114        Settings {
115            size: Size::new(400.0f32, 400.0f32),
116            resizable: false,
117            minimizable: false,
118            ..app_default_window_settings()
119        }
120    }
121}