cowmic/cowmic/src/main.rs

218 lines
5.8 KiB
Rust

use iced::{Sandbox, Settings};
pub struct Cowmic {
view: View,
templates: Vec<Template>,
dirty: bool,
saving: bool,
}
impl Sandbox for Cowmic {
type Message = Message;
fn new() -> Self {
// TODO: load templates from XDG Directory
// this is temporary to show menu
let templates = vec![
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
Template::new(
Some(Box::new(
include_bytes!("../../cowgen/tests/assets/mariah.jpg").to_vec(),
)),
vec![],
"cow".to_string(),
),
];
Self {
view: View::Library,
templates,
dirty: false,
saving: false,
}
}
fn title(&self) -> String {
"Cowmic".to_string()
}
fn update(&mut self, message: Self::Message) {
match message {
Message::ChangeView(c) => self.view = c,
}
}
fn view(&self) -> iced::Element<'_, Self::Message> {
match self.view {
View::Library => Library::view(&self),
_ => panic!(),
// View::Create => Create::view(),
// View::Edit => Edit::view(),
}
}
}
#[derive(Debug, Clone)]
pub enum Message {
ChangeView(View),
}
#[derive(Debug, Clone)]
pub enum View {
Library,
Create,
Edit,
}
mod Library {
use super::Cowmic;
use super::Message;
use iced::alignment::Horizontal;
use iced::widget::image::Handle;
use iced::widget::Row;
use iced::Length;
use iced::{
widget::{Column, Image, Scrollable, Text},
Element,
};
pub fn view(state: &Cowmic) -> Element<Message> {
Scrollable::new(iced_lazy::responsive(|size| {
let mut rows = Column::new();
let mut row = Row::new();
let mut row_width = 0.0;
for template in &state.templates {
if row_width + 256.0 < size.width {
let image_data = Handle::from_memory(*template.base.clone().unwrap());
let thumbnail = Image::new(image_data).height(Length::FillPortion(5));
let label = Text::new(template.name.clone())
.height(Length::FillPortion(1))
.horizontal_alignment(Horizontal::Center);
let item = Column::new()
.push(thumbnail)
.push(label)
.padding(16)
.height(Length::Units(256))
.width(Length::Units(256));
row = row.push(item).height(Length::Shrink);
row_width += 256.0;
} else {
rows = rows.push(row);
row = Row::new();
row_width = 0.0;
}
}
rows = rows.align_items(iced::Alignment::Center);
rows.into()
}))
.into()
}
}
mod Create {}
mod Edit {}
struct Template {
base: Option<Box<Vec<u8>>>,
elements: Vec<Element>,
name: String,
}
impl Template {
fn new(base: Option<Box<Vec<u8>>>, elements: Vec<Element>, name: String) -> Self {
Self {
base,
elements,
name,
}
}
}
struct Element {
position: (i32, i32),
dimensions: (i32, i32),
kind: MediaKind,
}
enum MediaKind {
Text,
Image,
}
fn main() -> iced::Result {
Cowmic::run(Settings {
antialiasing: true,
..Settings::default()
})
}