Skip to content

Print #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Print #336

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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ paste = "1.0.15"
patch = "0.7.0"
regex = "1.11"
serde = { version = "1", features = ["serde_derive"] }
tempfile = "3"
tokio = { version = "1", features = ["process", "time"] }
# Extra syntax highlighting
syntect = "5.2.0"
Expand Down
6 changes: 6 additions & 0 deletions i18n/en/cosmic_edit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ staged-changes = Staged changes
unstage = Unstage
unstaged-changes = Unstaged changes

## Print dialog
print-title = Send to printer
print-message = Do you really want to print this document?
print-confirm = Print
print-cancel = Cancel

## Project search
project-search = Project search

Expand Down
6 changes: 6 additions & 0 deletions i18n/fr/cosmic_edit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ staged-changes = Modifications indexées
unstage = Désindexer
unstaged-changes = Modifications non indexées

## Print dialog
print-title = Envoyer à l'imprimante
print-message = Voulez-vous vraiment imprimer ce document ?
print-confirm = Imprimer
print-cancel= Annuler

## Project search
project-search = Recherche de projets

Expand Down
63 changes: 63 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ mod tab;
use self::text_box::text_box;
mod text_box;

mod print;

static ICON_CACHE: OnceLock<Mutex<IconCache>> = OnceLock::new();
static LINE_NUMBER_CACHE: OnceLock<Mutex<LineNumberCache>> = OnceLock::new();
static SWASH_CACHE: OnceLock<Mutex<SwashCache>> = OnceLock::new();
Expand Down Expand Up @@ -199,6 +201,9 @@ pub enum Action {
OpenRecentFile(usize),
OpenRecentProject(usize),
Paste,
Print,
PrintCancelled,
PrintConfirmed,
Quit,
Redo,
RevertAllChanges,
Expand Down Expand Up @@ -249,6 +254,9 @@ impl Action {
Self::OpenRecentFile(index) => Message::OpenRecentFile(*index),
Self::OpenRecentProject(index) => Message::OpenRecentProject(*index),
Self::Paste => Message::Paste,
Self::Print => Message::Print,
Self::PrintCancelled => Message::PrintCancelled,
Self::PrintConfirmed => Message::PrintConfirmed,
Self::Quit => Message::Quit,
Self::Redo => Message::Redo,
Self::RevertAllChanges => Message::RevertAllChanges,
Expand Down Expand Up @@ -379,6 +387,10 @@ pub enum Message {
ProjectSearchSubmit,
ProjectSearchValue(String),
PromptSaveChanges(segmented_button::Entity),
Print,
PrintCancelled,
PrintConfirmed,
PrintDialog(String),
Quit,
QuitForce,
Redo,
Expand Down Expand Up @@ -425,6 +437,7 @@ pub enum ContextPage {

#[derive(Clone, Debug, Eq, PartialEq)]
enum DialogPage {
PrintDialog(String),
PromptSaveClose(segmented_button::Entity),
PromptSaveQuit(Vec<segmented_button::Entity>),
}
Expand Down Expand Up @@ -755,6 +768,8 @@ impl App {
self.dialog_page_opt = Some(DialogPage::PromptSaveQuit(unsaved));
}
}
Some(DialogPage::PrintDialog(_)) => {
}
None => {}
}
Task::none()
Expand Down Expand Up @@ -1554,6 +1569,24 @@ impl Application for App {
let cosmic_theme::Spacing { space_xxs, .. } = self.core().system_theme().cosmic().spacing;

match dialog {
DialogPage::PrintDialog(text) => {
let print_label = fl!("print-title");
let message_label = fl!("print-message");
let confirm_label = fl!("print-confirm");
let cancel_label = fl!("print-cancel");

let confirm_button = widget::button::suggested(confirm_label)
.on_press(Message::PrintConfirmed);
let cancel_button = widget::button::destructive(cancel_label)
.on_press(Message::PrintCancelled);
let dialog = widget::dialog()
.title(print_label)
.body(format!("{}: {}", message_label, text))
.icon(icon::from_name("dialog-warning-symbolic").size(64))
.primary_action(confirm_button)
.secondary_action(cancel_button);
Some(dialog.into())
}
DialogPage::PromptSaveClose(entity) => {
let save_button =
widget::button::suggested(fl!("save")).on_press(Message::Save(Some(*entity)));
Expand Down Expand Up @@ -2277,6 +2310,36 @@ impl Application for App {
|x| x,
);
}
Message::Print => {
self.dialog_page_opt = Some(DialogPage::PrintDialog(String::new()));
return Task::none();
}
Message::PrintCancelled => {
self.dialog_page_opt = None;
return Task::none();
}
Message::PrintConfirmed => {
if let Some(DialogPage::PrintDialog(_)) = self.dialog_page_opt.take() {
if let Some(tab) = self.active_tab() {
if let Tab::Editor(editor_tab) = tab {
let mut text = String::new();
let editor = editor_tab.editor.lock().unwrap();
editor.with_buffer(|buffer| {
for line in &buffer.lines {
text.push_str(line.text());
text.push_str(line.ending().as_str());
}
});
let _ = crate::print::print_text(&text);
}
}
}
return Task::none();
}
Message::PrintDialog(text) => {
self.dialog_page_opt = Some(DialogPage::PrintDialog(text));
return Task::none();
}
Message::ProjectSearchResult(project_search_result) => {
self.project_search_result = Some(project_search_result);

Expand Down
3 changes: 2 additions & 1 deletion src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ pub fn menu_bar<'a>(
None,
Action::ToggleGitManagement,
),
//TODO MenuItem::Button(fl!("print"), Action::Todo),
MenuItem::Divider,
MenuItem::Button(fl!("print"), None, Action::Print),
MenuItem::Divider,
MenuItem::Button(fl!("quit"), None, Action::Quit),
],
Expand Down
23 changes: 23 additions & 0 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::process::Command;
use std::io::{self, Write};
use tempfile::NamedTempFile;

pub fn print_text(text: &str) -> io::Result<()> {
let mut file = NamedTempFile::new()?;
write!(file, "{}", text)?;
file.flush()?;

let path = file.path().to_str().unwrap();

let status = Command::new("lp")
.arg(path)
.status()?;

if !status.success() {
eprintln!("lp failed with status: {:?}", status);
}

Ok(())
}


12 changes: 12 additions & 0 deletions src/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ impl EditorTab {
//TODO: dynamically discover light/dark changes
editor.update_theme(config.syntax_theme());
}

pub fn get_buffer_text(&self) -> String {
let editor = self.editor.lock().unwrap();
let mut text = String::new();
editor.with_buffer(|buffer| {
for line in buffer.lines.iter() {
text.push_str(line.text());
text.push_str(line.ending().as_str());
}
});
text
}

pub fn open(&mut self, path: PathBuf) {
let mut editor = self.editor.lock().unwrap();
Expand Down
Empty file added {
Empty file.