Skip to content

Commit

Permalink
Merge pull request #6 from sdaqo/5-add-deeplx
Browse files Browse the repository at this point in the history
Add deeplx (#5)
  • Loading branch information
sdaqo authored May 30, 2024
2 parents 638ef75 + 8a92924 commit 37a11bd
Show file tree
Hide file tree
Showing 22 changed files with 1,052 additions and 781 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ cargo build --release
```

## Features
- Full custom translator widget
- Full custom translator widget (Google, DeeplX)
- Auto translation in seperate line
- Always works: Open mpv after the Popout or before, it detects mpv and attaches itself
- Custom Font and Font size
Expand All @@ -141,7 +141,8 @@ cargo build --release
- Custom text color
- Linux & Windows Support

**Note that some features like docked or always on top do not work under wayland!**
**Note 1: Some features like docked or always on top do not work under wayland!**
**Note 2: When setting the api key for the /v2/translate endpoint for DeeplX do it like this (with space) `[yourAccessToken] [yourAuthKey]`**

### Planned
- Dictionary for looking up words
Expand Down
53 changes: 50 additions & 3 deletions resources/translator.ui
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<property name="stock">gtk-go-forward</property>
<property name="icon_size">5</property>
</object>
<object class="GtkImage" id="image2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-redo</property>
</object>
<object class="GtkImage" id="image3">
<property name="visible">True</property>
<property name="can-focus">False</property>
Expand Down Expand Up @@ -145,10 +150,11 @@
</child>
<child>
<object class="GtkLinkButton" id="api_key_hint_label">
<property name="label" translatable="yes">Get Api Key Here</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="receives-default">False</property>
<property name="margin-top">5</property>
<property name="label" translatable="yes">Get Api Key Here</property>
</object>
<packing>
<property name="expand">True</property>
Expand All @@ -170,6 +176,47 @@
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="spacing">5</property>
<child>
<object class="GtkEntry" id="url_field">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-top">10</property>
<property name="placeholder-text" translatable="yes">Translator URL</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="reset_url_button">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="image">image2</property>
<property name="always-show-image">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
Expand All @@ -184,7 +231,7 @@
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
<property name="position">4</property>
</packing>
</child>
<child>
Expand Down Expand Up @@ -294,7 +341,7 @@
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">4</property>
<property name="position">5</property>
</packing>
</child>
</object>
Expand Down
95 changes: 49 additions & 46 deletions src/app/channel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use glib::{MainContext, Sender, clone};
use gtk::{glib, subclass::prelude::ObjectSubclassIsExt, prelude::*};
use std::thread;
use glib::{clone, MainContext, Sender};
use gtk::{glib, prelude::*, subclass::prelude::ObjectSubclassIsExt};
use std::panic;
use std::thread;

use crate::app::MpvSubsWindow;
use crate::mpv::mpv_subs_update;
Expand All @@ -11,62 +11,65 @@ pub enum Message {
UpdateTlLabel(String),
SetTlLabelVisibilty(bool),
SpawnThread,
Quit
Quit,
}

pub fn setup_channel(window: &MpvSubsWindow) -> Sender<Message> {
let (sender, receiver) = MainContext::channel::<Message>(glib::PRIORITY_DEFAULT);

receiver.attach(None, clone!(@weak window, @strong sender => @default-return glib::Continue(true), move |msg| {
match msg {
Message::UpdateLabel(text) => {
window.imp()
.sub_label
.get()
.unwrap()
.set_text(text.as_str());
},
receiver.attach(
None,
clone!(@weak window, @strong sender => @default-return glib::Continue(true), move |msg| {
match msg {
Message::UpdateLabel(text) => {
window.imp()
.sub_label
.get()
.unwrap()
.set_text(text.as_str());
},

Message::UpdateTlLabel(text) => {
window.imp()
.tl_label
.get()
.unwrap()
.set_text(text.as_str())
},
Message::UpdateTlLabel(text) => {
window.imp()
.tl_label
.get()
.unwrap()
.set_text(text.as_str())
},

Message::SetTlLabelVisibilty(visible) => {
let label_box = window.imp().label_box.get().unwrap();
let contains_tl_label = label_box
.children()
.len() > 1;
Message::SetTlLabelVisibilty(visible) => {
let label_box = window.imp().label_box.get().unwrap();
let contains_tl_label = label_box
.children()
.len() > 1;


if visible {
if !contains_tl_label {
label_box.add(window.imp().tl_label.get().unwrap());
label_box.show_all();
if visible {
if !contains_tl_label {
label_box.add(window.imp().tl_label.get().unwrap());
label_box.show_all();
}
} else if contains_tl_label {
label_box.remove(window.imp().tl_label.get().unwrap());
label_box.show_all();
}
} else if contains_tl_label {
label_box.remove(window.imp().tl_label.get().unwrap());
label_box.show_all();
}
},
},

Message::SpawnThread => {
thread::spawn(clone!(@strong sender => move || {
panic::catch_unwind(panic::AssertUnwindSafe(|| {
mpv_subs_update(sender.clone());
})).ok();
Message::SpawnThread => {
thread::spawn(clone!(@strong sender => move || {
panic::catch_unwind(panic::AssertUnwindSafe(|| {
mpv_subs_update(sender.clone());
})).ok();

sender.send(Message::SpawnThread).ok();
sender.send(Message::SpawnThread).ok();

}));
},
Message::Quit => { window.quit() }
}
glib::Continue(true)
}));
}));
},
Message::Quit => { window.quit() }
}
glib::Continue(true)
}),
);

sender
}
20 changes: 8 additions & 12 deletions src/app/ctxmenu/imp.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

use gtk::prelude::*;
use gtk::gdk;
use gtk::prelude::*;

pub struct ContextMenu {
menu: gtk::Menu
menu: gtk::Menu,
}


impl ContextMenu {
pub fn new() -> Self {
let menu = gtk::Menu::new();
Expand All @@ -15,9 +13,10 @@ impl ContextMenu {
}

pub fn add_item<W: IsA<gtk::Widget>>(
&self, widget: &W,
&self,
widget: &W,
callback: Box<dyn Fn(&W, &gdk::EventButton) -> Inhibit>,
callback_should_show: Option<Box<dyn Fn(&W) -> bool>>
callback_should_show: Option<Box<dyn Fn(&W) -> bool>>,
) {
let item = gtk::MenuItem::new();
item.add(widget);
Expand All @@ -34,19 +33,17 @@ impl ContextMenu {
}
});
}

let widget_clone = widget.clone();
item.connect_button_press_event(move |_wg, ev| {
callback(&widget_clone, ev)
});
item.connect_button_press_event(move |_wg, ev| callback(&widget_clone, ev));

self.menu.append(&item);
item.show_all();
}

pub fn attach_to_widget<W: IsA<gtk::Widget>>(&self, widget: &W) {
let cloned_menu = self.menu.clone();

widget.connect_button_press_event(move |_, event| {
if event.button() == gdk::BUTTON_SECONDARY {
cloned_menu.popup_easy(event.button(), event.time());
Expand All @@ -59,6 +56,5 @@ impl ContextMenu {
Inhibit(false)
}
});

}
}
5 changes: 3 additions & 2 deletions src/app/ctxmenu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn build_ctxmenu(window: &MpvSubsWindow) -> ContextMenu {

let sizelock_btn = CheckButton::builder()
.label("Lock Size")
.active(config.size_lock != None)
.active(config.size_lock.is_some())
.build();

ctxmenu.add_item(
Expand Down Expand Up @@ -192,6 +192,7 @@ pub fn build_ctxmenu(window: &MpvSubsWindow) -> ContextMenu {
return Inhibit(true);
}


if let Some(font_desc) = font_chooser.font_desc() {
let family = font_desc.family().unwrap_or_default().to_string();
let size = font_desc.size() / gtk::pango::SCALE;
Expand Down Expand Up @@ -334,7 +335,7 @@ pub fn build_ctxmenu(window: &MpvSubsWindow) -> ContextMenu {
}
};

reference_dialog.run_async();
reference_dialog.run();
Inhibit(true)
}),
),
Expand Down
29 changes: 10 additions & 19 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
pub mod window;
pub mod ctxmenu;
pub mod utils;
pub mod channel;
pub mod ctxmenu;
pub mod reference_dialog;
pub mod utils;
pub mod window;

use gtk::{prelude::*, subclass::prelude::ObjectSubclassIsExt, Label};
use gtk::gdk;
use gtk::{prelude::*, subclass::prelude::ObjectSubclassIsExt, Label};

use window::MpvSubsWindow;
use ctxmenu::build_ctxmenu;
use channel::setup_channel;
use crate::config::AppConfig;

use channel::setup_channel;
use ctxmenu::build_ctxmenu;
use window::MpvSubsWindow;

pub fn build_window(app: &gtk::Application) -> MpvSubsWindow {
let window = MpvSubsWindow::new(app);
Expand All @@ -36,17 +35,11 @@ pub fn build_window(app: &gtk::Application) -> MpvSubsWindow {
label_box.set_homogeneous(true);
label_box.set_margin(9);

let sub_label = Label::builder()
.name("sub_label")
.selectable(true)
.build();
let sub_label = Label::builder().name("sub_label").selectable(true).build();
sub_label.style_context().add_class("sub_label");
ctx_menu.attach_to_widget(&sub_label);


let tl_label = Label::builder()
.name("tl_label")
.build();
let tl_label = Label::builder().name("tl_label").build();
tl_label.style_context().add_class("sub_label");
ctx_menu.attach_to_widget(&tl_label);

Expand All @@ -64,12 +57,10 @@ pub fn build_window(app: &gtk::Application) -> MpvSubsWindow {
label_box.add(window.imp().tl_label.get().unwrap());
}


window.add(&label_box);
window.imp().label_box.set(label_box).ok();

window.imp().channel_sender.set(setup_channel(&window)).ok();

window
}

Loading

0 comments on commit 37a11bd

Please sign in to comment.