Skip to content

Commit 08bcbfe

Browse files
committed
Ui improvement + delete items from clipboard history
1 parent c0692eb commit 08bcbfe

File tree

6 files changed

+193
-71
lines changed

6 files changed

+193
-71
lines changed

src/app.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ pub enum Move {
6969
Forwards(String),
7070
}
7171

72+
#[derive(Debug, Clone)]
73+
pub enum Editable<T> {
74+
Create(T),
75+
Delete(T),
76+
Update { old: T, new: T },
77+
}
78+
7279
/// The message type that iced uses for actions that can do something
7380
#[derive(Debug, Clone)]
7481
pub enum Message {
@@ -95,7 +102,7 @@ pub enum Message {
95102
UpdateApps,
96103
SetSender(ExtSender),
97104
SwitchToPage(Page),
98-
ClipboardHistory(ClipBoardContentType),
105+
EditClipboardHistory(Editable<ClipBoardContentType>),
99106
ChangeFocus(ArrowKey, u32),
100107
FileSearchResult(Vec<App>),
101108
FileSearchClear,

src/app/pages/clipboard.rs

Lines changed: 116 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ use iced::{
66
Scrollable,
77
image::{Handle, Viewer},
88
scrollable::{Direction, Scrollbar},
9+
text::Wrapping,
10+
text_input,
911
},
1012
};
1113

1214
use crate::{
13-
app::{ToApp, pages::prelude::*},
15+
app::{Editable, ToApp, pages::prelude::*},
1416
clipboard::ClipBoardContentType,
17+
styles::{delete_button_style, settings_text_input_item_style},
1518
};
1619

1720
/// The clipboard view
@@ -30,60 +33,36 @@ pub fn clipboard_view(
3033
) -> Element<'static, Message> {
3134
let theme_clone = theme.clone();
3235
let theme_clone_2 = theme.clone();
33-
let viewport_content: Element<'static, Message> = match clipboard_content
34-
.get(focussed_id as usize)
35-
{
36-
Some(content) => match content {
37-
ClipBoardContentType::Text(txt) => Scrollable::with_direction(
38-
Text::new(txt.to_owned())
39-
.height(Length::Fill)
40-
.width(Length::Fill)
41-
.align_x(Alignment::Start)
42-
.font(theme.font())
43-
.size(16),
44-
Direction::Both {
45-
vertical: Scrollbar::new().scroller_width(0.).width(0.),
46-
horizontal: Scrollbar::new().scroller_width(0.).width(0.),
47-
},
48-
)
49-
.into(),
50-
51-
ClipBoardContentType::Image(data) => {
52-
let bytes = data.to_owned_img().into_owned_bytes();
53-
container(
54-
Viewer::new(
55-
Handle::from_rgba(data.width as u32, data.height as u32, bytes.to_vec())
56-
.clone(),
57-
)
58-
.content_fit(ContentFit::ScaleDown)
59-
.scale_step(0.)
60-
.max_scale(1.)
61-
.min_scale(1.),
62-
)
63-
.padding(10)
64-
.style(|_| container::Style {
65-
border: iced::Border {
66-
color: iced::Color::WHITE,
67-
width: 1.,
68-
radius: Radius::new(0.),
69-
},
70-
..Default::default()
71-
})
72-
.width(Length::Fill)
73-
.into()
74-
}
75-
},
76-
None => Text::new("").into(),
77-
};
78-
container(Row::from_vec(vec![
36+
if clipboard_content.is_empty() {
37+
return container(
38+
Text::new("Copy something to use the clipboard history")
39+
.font(theme.font())
40+
.size(30)
41+
.center()
42+
.wrapping(Wrapping::WordOrGlyph),
43+
)
44+
.height(Length::Fill)
45+
.width(Length::Fill)
46+
.style(move |_| result_row_container_style(&theme_clone, false))
47+
.align_x(Alignment::Center)
48+
.align_y(Alignment::Center)
49+
.into();
50+
}
51+
let viewport_content: Element<'static, Message> =
52+
match clipboard_content.get(focussed_id as usize) {
53+
Some(content) => viewport_content(content, &theme),
54+
None => Text::new("").into(),
55+
};
56+
container(Row::from_iter([
7957
container(
80-
iced::widget::scrollable(
58+
Scrollable::with_direction(
8159
Column::from_iter(clipboard_content.iter().enumerate().map(|(i, content)| {
8260
content
8361
.to_app()
8462
.render(theme.clone(), i as u32, focussed_id)
8563
}))
8664
.width(WINDOW_WIDTH / 3.),
65+
Direction::Vertical(Scrollbar::hidden()),
8766
)
8867
.id("results"),
8968
)
@@ -100,3 +79,92 @@ pub fn clipboard_view(
10079
.height(280)
10180
.into()
10281
}
82+
83+
fn viewport_content(content: &ClipBoardContentType, theme: &Theme) -> Element<'static, Message> {
84+
let viewer: Element<'static, Message> = match content {
85+
ClipBoardContentType::Text(txt) => Scrollable::with_direction(
86+
container(
87+
Text::new(txt.to_owned())
88+
.height(Length::Fill)
89+
.width(Length::Fill)
90+
.align_x(Alignment::Start)
91+
.font(theme.font())
92+
.size(16),
93+
)
94+
.width(Length::Fill)
95+
.height(Length::Fill),
96+
Direction::Both {
97+
vertical: Scrollbar::hidden(),
98+
horizontal: Scrollbar::hidden(),
99+
},
100+
)
101+
.height(Length::Fill)
102+
.width(Length::Fill)
103+
.into(),
104+
105+
ClipBoardContentType::Image(data) => {
106+
let bytes = data.to_owned_img().into_owned_bytes();
107+
container(
108+
Viewer::new(
109+
Handle::from_rgba(data.width as u32, data.height as u32, bytes.to_vec())
110+
.clone(),
111+
)
112+
.content_fit(ContentFit::ScaleDown)
113+
.scale_step(0.)
114+
.max_scale(1.)
115+
.min_scale(1.),
116+
)
117+
.padding(10)
118+
.style(|_| container::Style {
119+
border: iced::Border {
120+
color: iced::Color::WHITE,
121+
width: 1.,
122+
radius: Radius::new(0.),
123+
},
124+
..Default::default()
125+
})
126+
.width(Length::Fill)
127+
.into()
128+
}
129+
};
130+
131+
let theme_clone = theme.clone();
132+
Column::from_iter([
133+
viewer,
134+
container(
135+
Button::new("Delete")
136+
.on_press(Message::EditClipboardHistory(Editable::Delete(
137+
content.to_owned(),
138+
)))
139+
.style(move |_, _| delete_button_style(&theme_clone)),
140+
)
141+
.width(Length::Fill)
142+
.align_x(Alignment::Center)
143+
.padding(10)
144+
.into(),
145+
])
146+
.into()
147+
}
148+
149+
#[allow(unused)]
150+
fn editable_text(text: &str, theme: &Theme) -> Element<'static, Message> {
151+
let text_string = text.to_string();
152+
let theme_clone = theme.clone();
153+
container(
154+
text_input("Edit clipboard history text", text)
155+
.on_input(move |input| {
156+
Message::EditClipboardHistory(Editable::Update {
157+
old: ClipBoardContentType::Text(text_string.clone()),
158+
new: ClipBoardContentType::Text(input),
159+
})
160+
})
161+
.align_x(Alignment::Start)
162+
.size(16)
163+
.width(Length::Fill)
164+
.style(move |_, _| settings_text_input_item_style(&theme_clone))
165+
.font(theme.font()),
166+
)
167+
.height(Length::Fill)
168+
.width(Length::Fill)
169+
.into()
170+
}

src/app/tile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ fn handle_clipboard_history() -> impl futures::Stream<Item = Message> {
318318
{
319319
info!("Adding item to cbhist");
320320
output
321-
.send(Message::ClipboardHistory(content.to_owned()))
321+
.send(Message::EditClipboardHistory(crate::app::Editable::Create(
322+
content.to_owned(),
323+
)))
322324
.await
323325
.ok();
324326
prev_byte_rep = byte_rep;

src/app/tile/update.rs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rayon::iter::IntoParallelRefIterator;
1515
use rayon::iter::ParallelIterator;
1616
use rayon::slice::ParallelSliceMut;
1717

18+
use crate::app::Editable;
1819
use crate::app::SetConfigBufferFields;
1920
use crate::app::SetConfigFields;
2021
use crate::app::SetConfigThemeFields;
@@ -87,6 +88,13 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
8788
}
8889

8990
Message::EscKeyPressed(id) => {
91+
if !tile.query_lc.is_empty() {
92+
return Task::batch([
93+
Task::done(Message::ClearSearchQuery),
94+
Task::done(Message::ClearSearchResults),
95+
]);
96+
}
97+
9098
match tile.page {
9199
Page::Main => {}
92100
Page::Settings => {
@@ -168,8 +176,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
168176
};
169177

170178
let quantity = match tile.page {
171-
Page::Main | Page::FileSearch => 66.5,
172-
Page::ClipboardHistory => 50.,
179+
Page::Main | Page::FileSearch | Page::ClipboardHistory => 66.5,
173180
Page::EmojiSearch => 5.,
174181
Page::Settings => 0.,
175182
};
@@ -435,26 +442,50 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
435442
}
436443
}
437444

438-
Message::ClipboardHistory(content) => {
439-
if !tile.clipboard_content.contains(&content) {
440-
tile.clipboard_content.insert(0, content);
441-
return Task::none();
442-
}
443-
444-
let new_content_vec = tile
445-
.clipboard_content
446-
.par_iter()
447-
.filter_map(|x| {
448-
if *x == content {
449-
None
450-
} else {
451-
Some(x.to_owned())
445+
Message::EditClipboardHistory(action) => {
446+
match action {
447+
Editable::Create(content) => {
448+
if !tile.clipboard_content.contains(&content) {
449+
tile.clipboard_content.insert(0, content);
450+
return Task::none();
452451
}
453-
})
454-
.collect();
455452

456-
tile.clipboard_content = new_content_vec;
457-
tile.clipboard_content.insert(0, content);
453+
let new_content_vec = tile
454+
.clipboard_content
455+
.par_iter()
456+
.filter_map(|x| {
457+
if *x == content {
458+
None
459+
} else {
460+
Some(x.to_owned())
461+
}
462+
})
463+
.collect();
464+
465+
tile.clipboard_content = new_content_vec;
466+
tile.clipboard_content.insert(0, content);
467+
}
468+
Editable::Delete(content) => {
469+
tile.clipboard_content = tile
470+
.clipboard_content
471+
.iter()
472+
.filter_map(|x| {
473+
if x.to_owned() == content {
474+
None
475+
} else {
476+
Some(x.to_owned())
477+
}
478+
})
479+
.collect();
480+
}
481+
Editable::Update { old, new } => {
482+
tile.clipboard_content = tile
483+
.clipboard_content
484+
.iter()
485+
.map(|x| if x == &old { new.clone() } else { x.to_owned() })
486+
.collect();
487+
}
488+
}
458489
Task::none()
459490
}
460491

src/clipboard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl ToApp for ClipBoardContentType {
1919
fn to_app(&self) -> App {
2020
let mut display_name = match self {
2121
ClipBoardContentType::Image(_) => "Image".to_string(),
22-
ClipBoardContentType::Text(a) => a.to_owned(),
22+
ClipBoardContentType::Text(a) => a.get(0..25).unwrap_or(a).to_string(),
2323
};
2424

2525
let self_clone = self.clone();

src/styles.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ pub fn contents_style(theme: &ConfigTheme) -> container::Style {
5151
}
5252
}
5353

54+
pub fn delete_button_style(theme: &ConfigTheme) -> button::Style {
55+
let red_clr = Color::from_rgb(1.0, 0.2, 0.2);
56+
button::Style {
57+
text_color: red_clr,
58+
background: Some(Background::Color(theme.bg_color())),
59+
border: Border {
60+
color: with_alpha(red_clr, 0.3),
61+
width: 0.5,
62+
radius: Radius::new(15),
63+
},
64+
..Default::default()
65+
}
66+
}
67+
5468
/// Styling for each of the buttons that are what the "results" of rustcast are
5569
pub fn result_button_style(theme: &ConfigTheme) -> button::Style {
5670
button::Style {

0 commit comments

Comments
 (0)