Skip to content

Commit

Permalink
add translate button
Browse files Browse the repository at this point in the history
  • Loading branch information
zu1k committed Oct 29, 2021
1 parent f09d8e0 commit 19e2da9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 49 deletions.
36 changes: 19 additions & 17 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 @@ -17,6 +17,7 @@ deepl = { path = "./deepl", default-features = false } # features must be specif
eframe = { git = "https://github.com/zu1k/egui.git", branch = "copy-translator" }
egui = { git = "https://github.com/zu1k/egui.git", branch = "copy-translator" }
egui_glium = { git = "https://github.com/zu1k/egui.git", branch = "copy-translator" }
epaint = { git = "https://github.com/zu1k/egui.git", branch = "copy-translator" }
epi = { git = "https://github.com/zu1k/egui.git", branch = "copy-translator" }
enigo = { git = "https://github.com/enigo-rs/enigo" }
glium = "0.30"
Expand Down
18 changes: 8 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::env;

fn main() {
if env::var("PROFILE").unwrap() == "release" {
if let Ok(_) = env::var("CARGO_CFG_WINDOWS") {
let mut res = winres::WindowsResource::new();
if let Ok(host) = env::var("HOST") {
if host.contains("linux") {
res.set_toolkit_path("/usr/bin")
.set_windres_path("x86_64-w64-mingw32-windres");
}
if env::var("PROFILE").unwrap() == "release" && env::var("CARGO_CFG_WINDOWS").is_ok() {
let mut res = winres::WindowsResource::new();
if let Ok(host) = env::var("HOST") {
if host.contains("linux") {
res.set_toolkit_path("/usr/bin")
.set_windres_path("x86_64-w64-mingw32-windres");
}
res.set_icon("res/copy-translator.ico").set_language(0x04);
res.compile().unwrap();
}
res.set_icon("res/copy-translator.ico").set_language(0x04);
res.compile().unwrap();
}
}
24 changes: 12 additions & 12 deletions deepl/Cargo.lock

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

37 changes: 27 additions & 10 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{ctrl_c, font};
use deepl;
use eframe::{egui, epi};
use epaint::Color32;
use std::{fmt::Debug, sync::mpsc};

#[cfg(target_os = "windows")]
Expand All @@ -13,6 +14,9 @@ pub struct MouseState {
last_event: u8,
}

const LINK_COLOR_DOING: Color32 = Color32::GREEN;
const LINK_COLOR_COMMON: Color32 = Color32::GRAY;

impl MouseState {
fn new() -> Self {
Self { last_event: 0 }
Expand Down Expand Up @@ -60,6 +64,7 @@ pub struct MyApp {

event_rx: mpsc::Receiver<Event>,
clipboard_last: String,
link_color: Color32,

#[cfg(target_os = "windows")]
hk_setting: HotkeySetting,
Expand Down Expand Up @@ -96,6 +101,7 @@ impl MyApp {
mouse_state: MouseState::new(),
event_rx,
clipboard_last: String::new(),
link_color: Color32::GRAY,

#[cfg(target_os = "windows")]
hk_setting,
Expand Down Expand Up @@ -125,7 +131,7 @@ impl epi::App for MyApp {
self.task_chan
.send((self.text.clone(), self.target_lang, Some(self.source_lang)));
self.clipboard_last = self.text.clone();
self.text = "正在翻译中...\r\n\r\n".to_string() + &self.text;
self.link_color = LINK_COLOR_DOING;
}
}

Expand All @@ -141,6 +147,7 @@ impl epi::App for MyApp {
mouse_state,
event_rx,
clipboard_last,
link_color,
#[cfg(target_os = "windows")]
hk_setting,
#[cfg(target_os = "windows")]
Expand All @@ -158,6 +165,7 @@ impl epi::App for MyApp {
while let Ok(event) = event_rx.try_recv() {
match event {
Event::TextSet(text_new) => {
*link_color = LINK_COLOR_COMMON;
*text = text_new;
}
Event::MouseEvent(mouse_event) => match mouse_event {
Expand All @@ -181,15 +189,17 @@ impl epi::App for MyApp {
if let Some(text_new) = ctrl_c() {
if text_new.ne(clipboard_last) {
*clipboard_last = text_new.clone();
*text = "正在翻译中...\r\n\r\n".to_string() + &text_new;
*text = text_new.clone();
*link_color = LINK_COLOR_DOING;
let _ = task_chan.send((text_new, *target_lang, Some(*source_lang)));
}
}
}

#[cfg(target_os = "windows")]
if let Ok(text_new) = rx_this.try_recv() {
*text = "正在翻译中...\r\n\r\n".to_string() + &text_new;
*text = text_new.clone();
*link_color = LINK_COLOR_DOING;
let _ = task_chan.send((text_new, *target_lang, Some(*source_lang)));
}

Expand Down Expand Up @@ -226,26 +236,34 @@ impl epi::App for MyApp {
ui.selectable_value(target_lang, i, i.description());
}
});
if ui.add(egui::Button::new("翻译")).clicked() {
let _ = task_chan.send((text.clone(), *target_lang, Some(*source_lang)));
*link_color = LINK_COLOR_DOING;
};

ui.vertical_centered_justified(|ui| {
ui.with_layout(egui::Layout::right_to_left(), |ui| {
ui.visuals_mut().hyperlink_color = *link_color;
ui.hyperlink_to(
format!("{} GitHub", egui::special_emojis::GITHUB),
egui::special_emojis::GITHUB,
"https://github.com/zu1k/copy-translator",
);

let drag_button = ui.add(egui::Button::new("○").frame(false));
if drag_button.clicked() {
if ui.add(egui::Button::new("□").frame(false)).clicked() {
*show_box = !*show_box;
} else if ctx.input().key_pressed(egui::Key::D)
&& drag_button.is_pointer_button_down_on()
frame.set_decorations(*show_box);
};
if ui
.add(egui::Button::new("○").frame(false))
.is_pointer_button_down_on()
{
frame.drag_window();
}
};
});
});

if *source_lang != old_source_lang || *target_lang != old_target_lang {
*link_color = LINK_COLOR_DOING;
let _ = task_chan.send((text.clone(), *target_lang, Some(*source_lang)));
};
});
Expand All @@ -266,7 +284,6 @@ impl epi::App for MyApp {
});
});

frame.set_decorations(*show_box);
ctx.request_repaint();
}
}

0 comments on commit 19e2da9

Please sign in to comment.