deLyrium/src/app.rs

106 lines
2.1 KiB
Rust
Raw Normal View History

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