Skip to content

Commit

Permalink
Add option to save puzzle as jpg.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianch committed Jan 31, 2024
1 parent 5013739 commit 67c82f9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ lopdf = "0.31.0"
open = "5.0.1"
#rfd = { version = "0.13.0", default-features = false, features = ["xdg-portal", "tokio"] }
rfd = "0.13.0"
image = { version = "0.24.8", default-features = false, features = ["jpeg"] }

[target.'cfg(windows)'.dependencies]
libsqlite3-sys = { version = "0.26.0", features = ["bundled"] }
37 changes: 35 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use std::fs::File as StdFile;
use std::str::FromStr;
use tokio::sync::mpsc::{self, Sender};
use iced::widget::{Svg, Container, Button, row, Row, Column, Text, Radio, responsive};
use iced::{Application, Element, Size, Subscription};
use iced::{Application, Element, Rectangle, Size, Subscription};
use iced::{executor, alignment, Command, Alignment, Length, Settings };
use iced::window;
use iced::window::{self, Screenshot};
use iced::Event;
use std::borrow::Cow;
use image::RgbaImage;
use rfd::AsyncFileDialog;

use iced_aw::{TabLabel, Tabs};
use chess::{Board, BoardStatus, ChessMove, Color, Piece, Rank, Square, File, Game};
Expand Down Expand Up @@ -80,6 +82,8 @@ pub enum Message {
ShowPreviousPuzzle,
GoBackMove,
RedoPuzzle,
ScreenshotCreated(Screenshot),
SaveScreenshot(Option<(Screenshot, String)>),
ExportPDF(Option<String>),
LoadPuzzle(Option<Vec<config::Puzzle>>),
ChangeSettings(Option<config::OfflinePuzzlesConfig>),
Expand Down Expand Up @@ -629,6 +633,26 @@ impl Application for OfflinePuzzles {
self.puzzle_tab.update(message)
} (_, Message::Search(message)) => {
self.search_tab.update(message)
} (_, Message::ScreenshotCreated(screenshot)) => {
Command::perform(screenshot_save_dialog(screenshot), Message::SaveScreenshot)
} (_, Message::SaveScreenshot(img_and_path)) => {
if let Some(img_and_path) = img_and_path {
let screenshot = img_and_path.0;
let path = img_and_path.1;
let crop = screenshot.crop(Rectangle::<u32> {
x: 0,
y: 0,
width: self.settings_tab.window_height - 110,
height: self.settings_tab.window_height - 110}
);
if let Ok (screenshot) = crop {
let img = RgbaImage::from_raw(screenshot.size.width, screenshot.size.height, (&screenshot.bytes).to_vec());
if let Some(image) = img {
let _ = image.save_with_format(path, image::ImageFormat::Jpeg);
}
}
}
Command::none()
} (_, Message::ExportPDF(file_path)) => {
if let Some(file_path) = file_path {
export::to_pdf(&self.puzzle_tab.puzzles, self.settings_tab.export_pgs.parse::<i32>().unwrap(), &self.lang, file_path);
Expand Down Expand Up @@ -814,6 +838,15 @@ impl Application for OfflinePuzzles {
}
}

pub async fn screenshot_save_dialog(img: Screenshot) -> Option<(Screenshot, String)> {
let file_path = AsyncFileDialog::new().add_filter("jpg", &["jpg", "jpeg"]).save_file();
if let Some(file_path) = file_path.await {
Some((img, file_path.path().display().to_string()))
} else {
None
}
}

fn gen_view<'a>(
game_mode: config::GameMode,
current_puzzle_side: Color,
Expand Down
5 changes: 5 additions & 0 deletions src/puzzles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use iced::{alignment, Command, Alignment, Length, Element};
use chess::{Color, Piece};
use iced_aw::TabLabel;
use rfd::AsyncFileDialog;

use crate::{Message, Tab, config, styles, lang};

#[derive(Debug, Clone)]
pub enum PuzzleMessage {
ChangeTextInputs(String),
CopyText(String),
OpenLink(String),
TakeScreenshot,
ExportToPDF,
}

Expand Down Expand Up @@ -51,6 +53,8 @@ impl PuzzleTab {
} PuzzleMessage::OpenLink(link) => {
let _ = open::that_detached(link);
Command::none()
} PuzzleMessage::TakeScreenshot => {
iced::window::screenshot(Message::ScreenshotCreated)
} PuzzleMessage::ExportToPDF => {
Command::perform(PuzzleTab::export(), Message::ExportPDF)
}
Expand Down Expand Up @@ -132,6 +136,7 @@ impl Tab for PuzzleTab {
Button::new(Text::new(lang::tr(&self.lang, "copy"))).on_press(PuzzleMessage::CopyText(self.puzzles[self.current_puzzle].game_url.clone())),
Button::new(Text::new(lang::tr(&self.lang, "open"))).on_press(PuzzleMessage::OpenLink(self.puzzles[self.current_puzzle].game_url.clone())),
],
Button::new(Text::new(lang::tr(&self.lang, "screenshot"))).on_press(PuzzleMessage::TakeScreenshot),
Button::new(Text::new(lang::tr(&self.lang, "export_pdf_btn"))).on_press(PuzzleMessage::ExportToPDF),
].spacing(10).align_items(Alignment::Center))
} else {
Expand Down
1 change: 1 addition & 0 deletions translations/en-US/ocp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ popularity = Popularity (-100 to 100):{" "}
times_played = Times Played (on lichess):{" "}
themes = Themes:
url = Game url:{" "}
screenshot = Save as Image
export_pdf_btn = Export current puzzles to PDF
no_puzzle = No puzzle loaded
Expand Down
1 change: 1 addition & 0 deletions translations/es/ocp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ popularity = Popularidad (-100 to 100):{" "}
times_played = Nº de veces que fue jugado (en lichess):{" "}
themes = Temas:
url = URL del juego:{" "}
screenshot = Guardar como imagen
export_pdf_btn = Exportar ejercícios para PDF
no_puzzle = Ningún ejercício cargado
Expand Down
1 change: 1 addition & 0 deletions translations/fr/ocp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ popularity = Popularité (de -100 à 100):{" "}
times_played = Nombre de fois joué (sur Lichess):{" "}
themes = Thèmes:
url = URL de la partie:{" "}
screenshot = Enregistrer en tant qu'image
export_pdf_btn = Exporter en PDF les puzzles de la recherche
no_puzzle = Aucun puzzle n'est chargé
Expand Down
1 change: 1 addition & 0 deletions translations/pt-BR/ocp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ popularity = Popularidade (-100 to 100):{" "}
times_played = Nº de vezes jogado (no lichess):{" "}
themes = Temas:
url = Url do jogo:{" "}
screenshot = Salvar como Imagem
export_pdf_btn = Exportar puzzles para PDF
no_puzzle = Nenhum puzzle carregado
Expand Down

0 comments on commit 67c82f9

Please sign in to comment.