Add support for multi-line pasting

This commit is contained in:
Emi Simpson 2021-12-31 15:32:23 -05:00
parent 5a4992c419
commit ba0069ddbc
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
5 changed files with 113 additions and 23 deletions

1
Cargo.lock generated
View file

@ -489,6 +489,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"exoquant", "exoquant",
"iced", "iced",
"iced_native",
"image", "image",
] ]

View file

@ -15,3 +15,4 @@ image = "0.23.14"
# Display windows & graphics # Display windows & graphics
iced = "0.3.0" iced = "0.3.0"
iced_native = "0.4.0"

View file

@ -23,25 +23,67 @@ impl Lyrics {
} }
} }
pub fn insert_text(&mut self, text: String) {
let mut pieces = text.trim_end()
.split('\n')
.map(str::trim);
let (line_no, current_line) = self.current_line_mut();
current_line.deselect();
current_line.value.push_str(pieces.next().unwrap());
let pieces = pieces
.collect::<Vec<_>>()
.into_iter()
.map(str::to_owned)
.map(Lyric::new_with_value);
let n_pieces = pieces.size_hint().0;
self.lines.splice((line_no + 1)..(line_no + 1), pieces);
self.lines[line_no + n_pieces].select();
}
pub fn update_line(&mut self, new_content: String, line_no: usize) { pub fn update_line(&mut self, new_content: String, line_no: usize) {
self.lines[line_no].set_content(new_content); self.lines[line_no].value = new_content;
} }
pub fn advance_line(&mut self, current_line: usize) { pub fn advance_line(&mut self, current_line: usize) {
let new_line = current_line + 1; let new_line = current_line + 1;
if new_line == self.lines.len() { let line = if new_line == self.lines.len() {
self.lines.push(Lyric::new()) self.insert_line(new_line, None)
} } else {
self.lines.get_mut(new_line) self.lines.get_mut(new_line)
.expect("Unexpected .advance_line with index beyond # of lines") .expect("Unexpected .advance_line with index beyond # of lines")
.select(); };
line.select();
self.lines.get_mut(current_line) self.lines.get_mut(current_line)
.unwrap() .unwrap()
.deselect(); .deselect();
} }
pub fn insert_line(&mut self, index: usize, content: Option<String>) -> &mut Lyric {
self.lines.insert(index, match content {
Some(content) => Lyric::new_with_value(content),
None => Lyric::new(),
});
self.lines.get_mut(index).unwrap()
}
pub fn current_line_mut(&mut self) -> (usize, &mut Lyric) {
self.lines
.iter_mut()
.enumerate()
.filter(|(_, l)| l.is_selected())
.next()
.expect("no line currently selected")
}
pub fn view(&mut self, theme: Theme) -> Element<Message> { pub fn view(&mut self, theme: Theme) -> Element<Message> {
let is_sole_line = self.lines.len() == 1; let is_sole_line = self.lines.len() == 1;
self.lines.iter_mut() self.lines.iter_mut()
@ -57,14 +99,18 @@ impl Lyrics {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Lyric { pub struct Lyric {
state: text_input::State, state: text_input::State,
value: String, pub value: String,
} }
impl Lyric { impl Lyric {
pub fn new() -> Self { pub fn new() -> Self {
Self::new_with_value(String::with_capacity(70))
}
pub fn new_with_value(val: String) -> Self {
Lyric { Lyric {
state: text_input::State::new(), state: text_input::State::new(),
value: String::with_capacity(70), value: val,
} }
} }
@ -86,20 +132,20 @@ impl Lyric {
) )
.style(theme) .style(theme)
.size(size) .size(size)
.width(Length::Units(300)) .width(Length::Units(350))
.on_submit(Message::LineAdvanced(line_no)) .on_submit(Message::LineAdvanced(line_no))
.into() .into()
} }
pub fn set_content(&mut self, content: String) {
self.value = content;
}
pub fn select(&mut self) { pub fn select(&mut self) {
self.state.focus(); self.state.focus();
self.state.move_cursor_to_end(); self.state.move_cursor_to_end();
} }
pub fn is_selected(&self) -> bool {
self.state.is_focused()
}
pub fn deselect(&mut self) { pub fn deselect(&mut self) {
self.state.unfocus(); self.state.unfocus();
} }

View file

@ -1,9 +1,16 @@
use iced::Subscription;
use iced::Clipboard;
use iced::Command;
use iced::Application;
use iced::Container; use iced::Container;
use iced::Row; use iced::Row;
use iced::Element; use iced::Element;
use iced::Sandbox;
use iced::Length; use iced::Length;
use iced::Align; use iced::Align;
use iced::executor;
use iced_native::subscription;
use iced_native::keyboard;
use iced_native::event::Event;
mod lyrics; mod lyrics;
mod styles; mod styles;
@ -23,31 +30,47 @@ pub enum Message {
new_value: String, new_value: String,
}, },
LineAdvanced(usize), LineAdvanced(usize),
PasteSent,
} }
impl Sandbox for DelyriumApp { impl Application for DelyriumApp {
type Message = Message; type Message = Message;
type Executor = executor::Default;
type Flags = ();
fn new() -> Self { fn new(_: Self::Flags) -> (Self, Command<Message>) {
(
DelyriumApp { DelyriumApp {
lyrics_component: lyrics::Lyrics::new(), lyrics_component: lyrics::Lyrics::new(),
theme: Theme::default(), theme: Theme::default(),
} },
Command::none(),
)
} }
fn title(&self) -> String { fn title(&self) -> String {
String::from("Delyrium") String::from("Delyrium")
} }
fn update(&mut self, message: Message) { fn update(&mut self, message: Message, clipboard: &mut Clipboard) -> Command<Message>{
match message { match message {
Message::LyricChanged { line_no, new_value } => { Message::LyricChanged { line_no, new_value } => {
self.lyrics_component.update_line(new_value, line_no); self.lyrics_component.update_line(new_value, line_no);
}, },
Message::LineAdvanced(current_line) => { Message::LineAdvanced(current_line) => {
self.lyrics_component.advance_line(current_line); self.lyrics_component.advance_line(current_line);
},
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);
} }
} }
Command::none()
} }
fn view(&mut self) -> Element<Message> { fn view(&mut self) -> Element<Message> {
@ -60,4 +83,23 @@ impl Sandbox for DelyriumApp {
.height(Length::Fill) .height(Length::Fill)
.into() .into()
} }
fn subscription(&self) -> Subscription<Message> {
subscription::events_with(|event, _| {
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 }
}
}
_ => { None }
}
})
}
} }

View file

@ -1,4 +1,4 @@
use iced::Sandbox; use iced::Application;
use iced::settings::Settings; use iced::settings::Settings;
mod palette; mod palette;