Untitled

mail@pastecode.io avatar
unknown
rust
7 months ago
3.7 kB
1
Indexable
Never
use iced::widget::{column, container, text, vertical_slider};
use iced::window::icon::from_rgba;
use iced::window::{Icon, Settings as Window};
use iced::{Application, Command, Length, Settings};
use image::GenericImageView;

use self::theme::Theme;
use self::widget::Element;

fn main() -> iced::Result {
    let settings: Settings<()> = Settings {
        window: Window {
            size: (1080, 720),
            icon: Some(icon()),
            ..iced::window::Settings::default()
        },
        ..iced::Settings::default()
    };

    Editor::run(settings)
}

struct Editor {
    slider_value: u8,
}

#[derive(Debug, Clone)]
pub enum Message {
    SliderChanged(u8),
}

impl Application for Editor {
    type Executor = tokio::runtime::Runtime;
    type Message = Message;
    type Flags = ();
    type Theme = Theme;

    fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
        (Editor { slider_value: 100 }, Command::none())
    }

    fn title(&self) -> String {
        "Style Test".into()
    }

    fn update(&mut self, message: Message) -> Command<Message> {
        match message {
            Message::SliderChanged(value) => {
                self.slider_value = value;
            }
        }
        Command::none()
    }

    fn view(&self) -> Element<Message> {
        let value: u8 = self.slider_value;

        let v_slider: container::Container<'_, Message> =
            container(vertical_slider(0..=200, value, Message::SliderChanged)).height(200);

        let text = text(format!("{value}"));

        container(
            column![
                container(v_slider).width(Length::Fill).center_x(),
                container(text).width(Length::Fill).center_x(),
            ]
            .spacing(25),
        )
        .height(Length::Fill)
        .width(Length::Fill)
        .center_x()
        .center_y()
        .style(theme::Container::Bordered)
        .into()
    }
}

mod widget {
    #![allow(dead_code)]
    use crate::theme::Theme;

    pub type Renderer = iced::Renderer<Theme>;
    pub type Element<'a, Message> = iced::Element<'a, Message, Renderer>;
    pub type Container<'a, Message> = iced::widget::Container<'a, Message, Renderer>;
}

mod theme {
    use iced::widget::container;
    use iced::{application, Color};

    #[derive(Debug, Clone, Copy, Default)]
    pub struct Theme;

    impl application::StyleSheet for Theme {
        type Style = ();

        fn appearance(&self, _style: &Self::Style) -> application::Appearance {
            application::Appearance {
                background_color: Color::TRANSPARENT,
                text_color: Color::BLACK,
            }
        }
    }

    #[derive(Debug, Clone, Copy, Default)]
    pub enum Container {
        #[default]
        Default,
        Bordered,
    }

    impl container::StyleSheet for Theme {
        type Style = Container;

        fn appearance(&self, style: &Self::Style) -> container::Appearance {
            match style {
                Container::Default => container::Appearance::default(),
                Container::Bordered => container::Appearance {
                    border_color: Color::BLACK,
                    border_width: 2.0,
                    ..Default::default()
                },
            }
        }
    }
}

fn icon() -> Icon {
    let image: image::DynamicImage =
        image::load_from_memory(include_bytes!("../icon.ico")).unwrap();
    let (width, height) = image.dimensions();

    from_rgba(image.as_bytes().to_vec(), width, height).unwrap()
}
Leave a Comment