2022-01-01 16:20:25 +00:00
|
|
|
use crate::lyrics::Lyrics;
|
|
|
|
use crate::file_select::FileSelector;
|
|
|
|
use crate::styles::Theme;
|
|
|
|
use rfd::AsyncFileDialog;
|
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::Clipboard;
|
|
|
|
use iced::Command;
|
|
|
|
use iced::Application;
|
2021-12-30 22:32:52 +00:00
|
|
|
use iced::Container;
|
|
|
|
use iced::Row;
|
|
|
|
use iced::Element;
|
2021-12-30 22:58:56 +00:00
|
|
|
use iced::Length;
|
2021-12-31 05:09:01 +00:00
|
|
|
use iced::Align;
|
2021-12-31 20:32:23 +00:00
|
|
|
use iced::executor;
|
2022-01-01 04:41:12 +00:00
|
|
|
use iced_futures::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;
|
2021-12-30 22:32:52 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct DelyriumApp {
|
2022-01-01 16:20:25 +00:00
|
|
|
lyrics_component: Lyrics,
|
2021-12-30 22:58:56 +00:00
|
|
|
theme: Theme,
|
2022-01-01 04:41:12 +00:00
|
|
|
mode: AppMode,
|
2022-01-01 16:20:25 +00:00
|
|
|
file_selector: FileSelector,
|
2022-01-01 04:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
|
|
enum AppMode {
|
|
|
|
FileSelect, Main
|
2021-12-30 22:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Message {
|
|
|
|
LyricChanged {
|
|
|
|
line_no: usize,
|
|
|
|
new_value: String,
|
|
|
|
},
|
2021-12-31 04:22:59 +00:00
|
|
|
LineAdvanced(usize),
|
2021-12-31 20:32:23 +00:00
|
|
|
PasteSent,
|
2022-01-01 04:41:12 +00:00
|
|
|
Tick,
|
2022-01-01 06:02:54 +00:00
|
|
|
PromptForFile,
|
|
|
|
FileOpened(PathBuf),
|
|
|
|
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>) {
|
|
|
|
(
|
|
|
|
DelyriumApp {
|
2022-01-01 16:20:25 +00:00
|
|
|
lyrics_component: Lyrics::new(),
|
2021-12-31 20:32:23 +00:00
|
|
|
theme: Theme::default(),
|
2022-01-01 04:41:12 +00:00
|
|
|
mode: AppMode::FileSelect,
|
|
|
|
file_selector: FileSelector::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")
|
|
|
|
}
|
|
|
|
|
2021-12-31 20:32:23 +00:00
|
|
|
fn update(&mut self, message: Message, clipboard: &mut Clipboard) -> Command<Message>{
|
2022-01-01 06:02:54 +00:00
|
|
|
let mut command = None;
|
2021-12-31 04:01:08 +00:00
|
|
|
match message {
|
|
|
|
Message::LyricChanged { line_no, new_value } => {
|
|
|
|
self.lyrics_component.update_line(new_value, line_no);
|
|
|
|
},
|
2021-12-31 04:22:59 +00:00
|
|
|
Message::LineAdvanced(current_line) => {
|
|
|
|
self.lyrics_component.advance_line(current_line);
|
2021-12-31 20:32:23 +00:00
|
|
|
},
|
|
|
|
Message::PasteSent => {
|
|
|
|
let clip_text = clipboard.read().unwrap_or(String::new());
|
|
|
|
let clip_pasted_len = clip_text.chars()
|
|
|
|
.filter(|c| *c != '\r' && *c != '\n')
|
|
|
|
.count();
|
|
|
|
let line = self.lyrics_component.current_line_mut().1;
|
|
|
|
line.value.truncate(line.value.len() - clip_pasted_len);
|
|
|
|
self.lyrics_component.insert_text(clip_text);
|
2022-01-01 04:41:12 +00:00
|
|
|
},
|
|
|
|
Message::Tick => {
|
|
|
|
match self.mode {
|
|
|
|
AppMode::FileSelect => {
|
|
|
|
self.file_selector.tick();
|
|
|
|
},
|
|
|
|
_ => { },
|
|
|
|
}
|
2022-01-01 06:02:54 +00:00
|
|
|
},
|
|
|
|
Message::FileOpened(path) => {
|
|
|
|
println!("File opened! {}", path.display());
|
|
|
|
},
|
|
|
|
Message::PromptForFile => {
|
|
|
|
let task = async {
|
|
|
|
let handle = AsyncFileDialog::new()
|
|
|
|
.add_filter("Song Files", &["mp3", "flac", "ogg", "opus", "wav"])
|
|
|
|
.set_title("Select a song")
|
|
|
|
.pick_file()
|
|
|
|
.await;
|
|
|
|
if let Some(h) = handle {
|
|
|
|
Message::FileOpened(h.path().to_owned())
|
|
|
|
} else {
|
|
|
|
Message::Null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
command = Some(task.into());
|
|
|
|
},
|
|
|
|
Message::Null => { },
|
2021-12-31 04:01:08 +00:00
|
|
|
}
|
2022-01-01 06:02:54 +00:00
|
|
|
|
|
|
|
command.unwrap_or_else(Command::none)
|
2021-12-30 22:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&mut self) -> Element<Message> {
|
2022-01-01 04:41:12 +00:00
|
|
|
match self.mode {
|
|
|
|
AppMode::Main => {
|
|
|
|
Container::new(
|
|
|
|
Row::new()
|
|
|
|
.push(self.lyrics_component.view(self.theme))
|
|
|
|
)
|
|
|
|
.align_y(Align::Center)
|
|
|
|
.style(self.theme)
|
|
|
|
.height(Length::Fill)
|
|
|
|
.into()
|
|
|
|
},
|
|
|
|
AppMode::FileSelect => {
|
|
|
|
self.file_selector.view()
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
keyboard::Modifiers { control, .. }
|
|
|
|
) if control == true => {
|
|
|
|
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 fps30 = time::every(Duration::from_millis(1000 / 30)).map(|_| Message::Tick);
|
|
|
|
|
|
|
|
match self.mode {
|
|
|
|
AppMode::FileSelect => {
|
|
|
|
Subscription::batch([
|
|
|
|
runtime_events,
|
|
|
|
fps30
|
|
|
|
])
|
|
|
|
},
|
|
|
|
AppMode::Main => runtime_events,
|
|
|
|
}
|
2021-12-31 20:32:23 +00:00
|
|
|
}
|
2021-12-30 22:32:52 +00:00
|
|
|
}
|