deLyrium/src/app.rs

106 lines
2.1 KiB
Rust

use crate::editor::view_editor;
use crate::file_select::view_fileselector;
use crate::model::Model;
use crate::controls::ControlsEvent;
use std::path::PathBuf;
use core::time::Duration;
use iced::Subscription;
use iced::Command;
use iced::Application;
use iced::Element;
use iced::executor;
use iced::time;
use iced_native::subscription;
use iced_native::keyboard;
use iced_native::window;
use iced_native::event::Event;
use crate::model::editing::LyricEvent;
pub struct DelyriumApp(Model);
#[derive(Clone, Debug)]
pub enum Message {
LyricEvent {
line_no: usize,
kind: LyricEvent,
},
PasteSent,
PasteRead(String),
Tick,
PromptForFile,
FileOpened(PathBuf),
ControlsEvent(ControlsEvent),
Null,
}
impl Application for DelyriumApp {
type Message = Message;
type Executor = executor::Default;
type Flags = ();
fn new(_: Self::Flags) -> (Self, Command<Message>) {
(
Self(Model::DEFAULT),
Command::none(),
)
}
fn title(&self) -> String {
String::from("Delyrium")
}
fn update(&mut self, message: Message) -> Command<Message>{
self.0.update(message)
}
fn view(&mut self) -> Element<Message> {
match &mut self.0 {
Model::Editing(editing) => {
view_editor(editing)
},
Model::FilePicker { tick } => {
view_fileselector(*tick)
}
}
}
fn subscription(&self) -> Subscription<Message> {
let runtime_events = subscription::events_with(|event, _| {
match event {
Event::Keyboard(keyboard::Event::KeyPressed {key_code, modifiers}) => {
match (key_code, modifiers) {
(
keyboard::KeyCode::V,
modifiers
) if modifiers.control() => {
Some(Message::PasteSent)
}
_ => { None }
}
},
Event::Window(window::Event::FileDropped(path)) => {
Some(Message::FileOpened(path))
},
_ => { None }
}
});
let is_animating = if let Model::Editing(e) = &self.0 {
e.is_animating()
} else {
true
};
let fps30 = if is_animating {
time::every(Duration::from_millis(1000 / 30)).map(|_| Message::Tick)
} else {
Subscription::none()
};
Subscription::batch([
runtime_events,
fps30
])
}
}