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
126 changes: 93 additions & 33 deletions src/app/pages/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ use iced::{
widget::{
Scrollable,
image::{Handle, Viewer},
rule,
scrollable::{Direction, Scrollbar},
text::Wrapping,
text_input,
},
};

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

/// The clipboard view
Expand All @@ -27,6 +31,7 @@ use crate::{
/// Returns:
/// - the iced Element to render
pub fn clipboard_view(
query: String,
clipboard_content: Vec<ClipBoardContentType>,
focussed_id: u32,
theme: Theme,
Expand Down Expand Up @@ -54,27 +59,47 @@ pub fn clipboard_view(
Some(content) => viewport_content(content, &theme),
None => Text::new("").into(),
};

let row_render_theme = theme.clone();
let query = query.clone();
container(Row::from_iter([
container(
Scrollable::with_direction(
Column::from_iter(clipboard_content.iter().enumerate().map(|(i, content)| {
content
.to_app()
.render(theme.clone(), i as u32, focussed_id, None)
}))
.width(WINDOW_WIDTH / 3.),
Column::from_iter(
clipboard_content
.iter()
.filter(|x| match x {
ClipBoardContentType::Text(data) | ClipBoardContentType::Url(data) => {
data.to_lowercase().contains(&query)
}
ClipBoardContentType::Image(_) => query == "image",
} || query.trim().is_empty())
.enumerate()
.map(|(i, content)| {
content.render_row(i == focussed_id as usize, &row_render_theme)
}),
)
.width((WINDOW_WIDTH + 50.) / 3.),
Direction::Vertical(Scrollbar::hidden()),
)
.id("results"),
)
.height(10000)
.style(move |_| result_row_container_style(&theme_clone_2, false))
.into(),
rule::vertical(0.3)
.style(|iced_theme: &iced::Theme| rule::Style {
color: with_alpha(iced_theme.palette().text, 0.7),
radius: Radius::new(0),
fill_mode: rule::FillMode::Full,
snap: true,
})
.into(),
container(viewport_content)
.height(10000)
.padding(10)
.style(move |_| result_row_container_style(&theme_clone, false))
.width((WINDOW_WIDTH / 3.) * 2.)
.width(((WINDOW_WIDTH + 50.) / 3.) * 2.)
.into(),
]))
.height(280)
Expand All @@ -83,25 +108,27 @@ pub fn clipboard_view(

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),
ClipBoardContentType::Text(txt) | ClipBoardContentType::Url(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)
.height(Length::Fill),
Direction::Both {
vertical: Scrollbar::hidden(),
horizontal: Scrollbar::hidden(),
},
)
.height(Length::Fill)
.width(Length::Fill)
.into(),
.into()
}

ClipBoardContentType::Image(data) => {
let bytes = data.to_owned_img().into_owned_bytes();
Expand All @@ -124,23 +151,56 @@ fn viewport_content(content: &ClipBoardContentType, theme: &Theme) -> Element<'s
},
..Default::default()
})
.height(Length::Fill)
.width(Length::Fill)
.into()
}
};

let theme_clone = theme.clone();
let theme_clone_2 = theme.clone();
let horizontal_line = rule::horizontal(0.3).style(|them: &iced::Theme| rule::Style {
color: with_alpha(them.palette().text, 0.7),
radius: Radius::new(0),
fill_mode: rule::FillMode::Full,
snap: true,
});
let open_url_option = match content {
ClipBoardContentType::Url(url) => Button::new(
open_icon()
.width(Length::Fill)
.height(Length::Fill)
.center(),
)
.on_press(Message::RunFunction(
crate::commands::Function::OpenWebsite(url.clone()),
))
.height(30)
.width(30)
.style(open_button_style)
.into(),
_ => iced::widget::space().width(0).into(),
};
Column::from_iter([
viewer,
horizontal_line.into(),
container(
Row::from_iter([
Button::new("Delete")
.on_press(Message::EditClipboardHistory(Editable::Delete(
content.to_owned(),
)))
.style(move |_, _| delete_button_style(&theme_clone))
.into(),
open_url_option,
iced::widget::space().width(Length::Fill).into(),
Button::new(
trash_icon()
.width(Length::Fill)
.height(Length::Fill)
.center(),
)
.width(30)
.height(30)
.on_press(Message::EditClipboardHistory(Editable::Delete(
content.to_owned(),
)))
.style(move |_, _| delete_button_style(&theme_clone))
.into(),
Button::new("Clear")
.on_press(Message::ClearClipboardHistory)
.style(move |_, _| delete_button_style(&theme_clone_2))
Expand All @@ -149,7 +209,7 @@ fn viewport_content(content: &ClipBoardContentType, theme: &Theme) -> Element<'s
.spacing(10),
)
.width(Length::Fill)
.align_x(Alignment::Center)
.align_x(Alignment::End)
.padding(10)
.into(),
])
Expand Down
6 changes: 6 additions & 0 deletions src/app/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rayon::slice::ParallelSliceMut;
use tokio::io::{AsyncBufReadExt, AsyncRead};
use tray_icon::TrayIcon;
use url::Url;

use std::collections::HashMap;
use std::fmt::Debug;
Expand Down Expand Up @@ -386,6 +387,11 @@ fn handle_clipboard_history() -> impl futures::Stream<Item = Message> {
loop {
let byte_rep = if let Ok(a) = clipboard.get_image() {
Some(ClipBoardContentType::Image(a))
} else if let Ok(a) = clipboard.get_text()
&& !a.trim().is_empty()
&& Url::parse(&a).is_ok()
{
Some(ClipBoardContentType::Url(a))
} else if let Ok(a) = clipboard.get_text()
&& !a.trim().is_empty()
{
Expand Down
5 changes: 3 additions & 2 deletions src/app/tile/elm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::app::tile::{AppIndex, Hotkeys};
use crate::app::{DEFAULT_WINDOW_HEIGHT, SettingsTab, ToApp, ToApps};
use crate::config::Theme;
use crate::debounce::Debouncer;
use crate::platform;
use crate::platform::macos::events::Event;
use crate::styles::{
contents_style, glass_border, glass_surface, results_scrollbar_style, rustcast_text_input_style,
};
use crate::{app::WINDOW_WIDTH, platform};
use crate::{app::pages::clipboard::clipboard_view, platform::get_installed_apps};
use crate::{
app::{Message, Page, apps::App, default_settings, tile::Tile},
Expand Down Expand Up @@ -142,6 +142,7 @@ pub fn view(tile: &Tile, wid: window::Id) -> Element<'_, Message> {

let results = match tile.page {
Page::ClipboardHistory => clipboard_view(
tile.query_lc.clone(),
tile.clipboard_content.clone(),
tile.focus_id,
tile.config.theme.clone(),
Expand Down Expand Up @@ -269,7 +270,7 @@ fn footer(theme: Theme, current_mode: String, text: String) -> Element<'static,
)
.align_y(Alignment::Center)
.center(Length::Fill)
.width(WINDOW_WIDTH)
.width(Length::Fill)
.padding(5)
.height(30)
.style(move |_| container::Style {
Expand Down
32 changes: 15 additions & 17 deletions src/app/tile/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,11 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
Message::ResizeWindow(id, height) => {
info!("Resizing rustcast window");
tile.height = height;
window::resize(
id,
iced::Size {
width: WINDOW_WIDTH,
height,
},
)
let width = match tile.page {
Page::ClipboardHistory => WINDOW_WIDTH + 50.,
_ => WINDOW_WIDTH,
};
window::resize(id, iced::Size { width, height })
}
Message::LoadRanking => {
for (name, rank) in &tile.ranking {
Expand Down Expand Up @@ -489,7 +487,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> {
let id = x.unwrap();
Message::ResizeWindow(
id,
((7 * 30) + 35 + DEFAULT_WINDOW_HEIGHT as usize) as f32,
((7 * 55) + 35 + DEFAULT_WINDOW_HEIGHT as usize) as f32,
)
})
}
Expand Down Expand Up @@ -1144,16 +1142,16 @@ fn resize_for_results_count(id: Id, count: usize) -> Task<Message> {
}

fn open_result(tile: &mut Tile, id: usize) -> Task<Message> {
let results = if tile.page == Page::ClipboardHistory {
tile.clipboard_content
.iter()
.map(|x| x.to_app().to_owned())
.collect()
} else {
tile.results.clone()
};
if tile.page == Page::ClipboardHistory {
let Some(content) = tile.clipboard_content.get(id) else {
return Task::none();
};
return Task::done(message_for_open_command(&AppCommand::Function(
Function::CopyToClipboard(content.clone()),
)));
}

let Some(app) = results.get(id).cloned() else {
let Some(app) = tile.results.get(id).cloned() else {
return Task::none();
};

Expand Down
Loading
Loading