Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ pub enum Move {
Forwards(String),
}

#[derive(Debug, Clone)]
pub enum Editable<T> {
Create(T),
Delete(T),
Update { old: T, new: T },
}

/// The message type that iced uses for actions that can do something
#[derive(Debug, Clone)]
pub enum Message {
Expand All @@ -95,7 +102,7 @@ pub enum Message {
UpdateApps,
SetSender(ExtSender),
SwitchToPage(Page),
ClipboardHistory(ClipBoardContentType),
EditClipboardHistory(Editable<ClipBoardContentType>),
ChangeFocus(ArrowKey, u32),
FileSearchResult(Vec<App>),
FileSearchClear,
Expand Down
164 changes: 116 additions & 48 deletions src/app/pages/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use iced::{
Scrollable,
image::{Handle, Viewer},
scrollable::{Direction, Scrollbar},
text::Wrapping,
text_input,
},
};

use crate::{
app::{ToApp, pages::prelude::*},
app::{Editable, ToApp, pages::prelude::*},
clipboard::ClipBoardContentType,
styles::{delete_button_style, settings_text_input_item_style},
};

/// The clipboard view
Expand All @@ -30,60 +33,36 @@ pub fn clipboard_view(
) -> Element<'static, Message> {
let theme_clone = theme.clone();
let theme_clone_2 = theme.clone();
let viewport_content: Element<'static, Message> = match clipboard_content
.get(focussed_id as usize)
{
Some(content) => match content {
ClipBoardContentType::Text(txt) => Scrollable::with_direction(
Text::new(txt.to_owned())
.height(Length::Fill)
.width(Length::Fill)
.align_x(Alignment::Start)
.font(theme.font())
.size(16),
Direction::Both {
vertical: Scrollbar::new().scroller_width(0.).width(0.),
horizontal: Scrollbar::new().scroller_width(0.).width(0.),
},
)
.into(),

ClipBoardContentType::Image(data) => {
let bytes = data.to_owned_img().into_owned_bytes();
container(
Viewer::new(
Handle::from_rgba(data.width as u32, data.height as u32, bytes.to_vec())
.clone(),
)
.content_fit(ContentFit::ScaleDown)
.scale_step(0.)
.max_scale(1.)
.min_scale(1.),
)
.padding(10)
.style(|_| container::Style {
border: iced::Border {
color: iced::Color::WHITE,
width: 1.,
radius: Radius::new(0.),
},
..Default::default()
})
.width(Length::Fill)
.into()
}
},
None => Text::new("").into(),
};
container(Row::from_vec(vec![
if clipboard_content.is_empty() {
return container(
Text::new("Copy something to use the clipboard history")
.font(theme.font())
.size(30)
.center()
.wrapping(Wrapping::WordOrGlyph),
)
.height(Length::Fill)
.width(Length::Fill)
.style(move |_| result_row_container_style(&theme_clone, false))
.align_x(Alignment::Center)
.align_y(Alignment::Center)
.into();
}
let viewport_content: Element<'static, Message> =
match clipboard_content.get(focussed_id as usize) {
Some(content) => viewport_content(content, &theme),
None => Text::new("").into(),
};
container(Row::from_iter([
container(
iced::widget::scrollable(
Scrollable::with_direction(
Column::from_iter(clipboard_content.iter().enumerate().map(|(i, content)| {
content
.to_app()
.render(theme.clone(), i as u32, focussed_id)
}))
.width(WINDOW_WIDTH / 3.),
Direction::Vertical(Scrollbar::hidden()),
)
.id("results"),
)
Expand All @@ -100,3 +79,92 @@ pub fn clipboard_view(
.height(280)
.into()
}

fn viewport_content(content: &ClipBoardContentType, theme: &Theme) -> Element<'static, Message> {
let viewer: Element<'static, Message> = match content {
ClipBoardContentType::Text(txt) => Scrollable::with_direction(
container(
Text::new(txt.to_owned())
.height(Length::Fill)
.width(Length::Fill)
.align_x(Alignment::Start)
.font(theme.font())
.size(16),
)
.width(Length::Fill)
.height(Length::Fill),
Direction::Both {
vertical: Scrollbar::hidden(),
horizontal: Scrollbar::hidden(),
},
)
.height(Length::Fill)
.width(Length::Fill)
.into(),

ClipBoardContentType::Image(data) => {
let bytes = data.to_owned_img().into_owned_bytes();
container(
Viewer::new(
Handle::from_rgba(data.width as u32, data.height as u32, bytes.to_vec())
.clone(),
)
.content_fit(ContentFit::ScaleDown)
.scale_step(0.)
.max_scale(1.)
.min_scale(1.),
)
.padding(10)
.style(|_| container::Style {
border: iced::Border {
color: iced::Color::WHITE,
width: 1.,
radius: Radius::new(0.),
},
..Default::default()
})
.width(Length::Fill)
.into()
}
};

let theme_clone = theme.clone();
Column::from_iter([
viewer,
container(
Button::new("Delete")
.on_press(Message::EditClipboardHistory(Editable::Delete(
content.to_owned(),
)))
.style(move |_, _| delete_button_style(&theme_clone)),
)
.width(Length::Fill)
.align_x(Alignment::Center)
.padding(10)
.into(),
])
.into()
}

#[allow(unused)]
fn editable_text(text: &str, theme: &Theme) -> Element<'static, Message> {
let text_string = text.to_string();
let theme_clone = theme.clone();
container(
text_input("Edit clipboard history text", text)
.on_input(move |input| {
Message::EditClipboardHistory(Editable::Update {
old: ClipBoardContentType::Text(text_string.clone()),
new: ClipBoardContentType::Text(input),
})
})
.align_x(Alignment::Start)
.size(16)
.width(Length::Fill)
.style(move |_, _| settings_text_input_item_style(&theme_clone))
.font(theme.font()),
)
.height(Length::Fill)
.width(Length::Fill)
.into()
}
4 changes: 3 additions & 1 deletion src/app/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ fn handle_clipboard_history() -> impl futures::Stream<Item = Message> {
{
info!("Adding item to cbhist");
output
.send(Message::ClipboardHistory(content.to_owned()))
.send(Message::EditClipboardHistory(crate::app::Editable::Create(
content.to_owned(),
)))
.await
.ok();
prev_byte_rep = byte_rep;
Expand Down
71 changes: 51 additions & 20 deletions src/app/tile/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use rayon::slice::ParallelSliceMut;

use crate::app::Editable;
use crate::app::SetConfigBufferFields;
use crate::app::SetConfigFields;
use crate::app::SetConfigThemeFields;
Expand Down Expand Up @@ -87,6 +88,13 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
}

Message::EscKeyPressed(id) => {
if !tile.query_lc.is_empty() {
return Task::batch([
Task::done(Message::ClearSearchQuery),
Task::done(Message::ClearSearchResults),
]);
}

match tile.page {
Page::Main => {}
Page::Settings => {
Expand Down Expand Up @@ -168,8 +176,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
};

let quantity = match tile.page {
Page::Main | Page::FileSearch => 66.5,
Page::ClipboardHistory => 50.,
Page::Main | Page::FileSearch | Page::ClipboardHistory => 66.5,
Page::EmojiSearch => 5.,
Page::Settings => 0.,
};
Expand Down Expand Up @@ -435,26 +442,50 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
}
}

Message::ClipboardHistory(content) => {
if !tile.clipboard_content.contains(&content) {
tile.clipboard_content.insert(0, content);
return Task::none();
}

let new_content_vec = tile
.clipboard_content
.par_iter()
.filter_map(|x| {
if *x == content {
None
} else {
Some(x.to_owned())
Message::EditClipboardHistory(action) => {
match action {
Editable::Create(content) => {
if !tile.clipboard_content.contains(&content) {
tile.clipboard_content.insert(0, content);
return Task::none();
}
})
.collect();

tile.clipboard_content = new_content_vec;
tile.clipboard_content.insert(0, content);
let new_content_vec = tile
.clipboard_content
.par_iter()
.filter_map(|x| {
if *x == content {
None
} else {
Some(x.to_owned())
}
})
.collect();

tile.clipboard_content = new_content_vec;
tile.clipboard_content.insert(0, content);
}
Editable::Delete(content) => {
tile.clipboard_content = tile
.clipboard_content
.iter()
.filter_map(|x| {
if *x == content {
None
} else {
Some(x.to_owned())
}
})
.collect();
}
Editable::Update { old, new } => {
tile.clipboard_content = tile
.clipboard_content
.iter()
.map(|x| if x == &old { new.clone() } else { x.to_owned() })
.collect();
}
}
Task::none()
}

Expand Down
2 changes: 1 addition & 1 deletion src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl ToApp for ClipBoardContentType {
fn to_app(&self) -> App {
let mut display_name = match self {
ClipBoardContentType::Image(_) => "Image".to_string(),
ClipBoardContentType::Text(a) => a.to_owned(),
ClipBoardContentType::Text(a) => a.get(0..25).unwrap_or(a).to_string(),
};

let self_clone = self.clone();
Expand Down
14 changes: 14 additions & 0 deletions src/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ pub fn contents_style(theme: &ConfigTheme) -> container::Style {
}
}

pub fn delete_button_style(theme: &ConfigTheme) -> button::Style {
let red_clr = Color::from_rgb(1.0, 0.2, 0.2);
button::Style {
text_color: red_clr,
background: Some(Background::Color(theme.bg_color())),
border: Border {
color: with_alpha(red_clr, 0.3),
width: 0.5,
radius: Radius::new(15),
},
..Default::default()
}
}

/// Styling for each of the buttons that are what the "results" of rustcast are
pub fn result_button_style(theme: &ConfigTheme) -> button::Style {
button::Style {
Expand Down
Loading