2025-03-01 09:57:01 +01:00
|
|
|
use std::{cell::RefCell, collections::HashMap};
|
2024-04-01 16:57:34 +02:00
|
|
|
|
|
|
|
|
use adw::{prelude::*, subclass::prelude::*};
|
|
|
|
|
use gtk::glib;
|
|
|
|
|
|
2025-03-01 09:57:01 +01:00
|
|
|
use crate::{db::TranslatedString, editor::translation_entry::TranslationEntry, util};
|
2024-04-01 16:57:34 +02:00
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default, gtk::CompositeTemplate)]
|
2025-03-01 09:57:01 +01:00
|
|
|
#[template(file = "data/ui/editor/translation.blp")]
|
|
|
|
|
pub struct TranslationEditor {
|
2024-05-31 13:57:14 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub list_box: TemplateChild<gtk::ListBox>,
|
2024-04-01 16:57:34 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub entry_row: TemplateChild<adw::EntryRow>,
|
|
|
|
|
|
2025-03-01 09:57:01 +01:00
|
|
|
pub translation_entries: RefCell<Vec<TranslationEntry>>,
|
2024-04-01 16:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2025-03-01 09:57:01 +01:00
|
|
|
impl ObjectSubclass for TranslationEditor {
|
2024-05-31 13:57:14 +02:00
|
|
|
const NAME: &'static str = "MusicusTranslationEditor";
|
2025-03-01 09:57:01 +01:00
|
|
|
type Type = super::TranslationEditor;
|
2024-05-31 13:57:14 +02:00
|
|
|
type ParentType = adw::Bin;
|
2024-04-01 16:57:34 +02:00
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
|
|
|
|
klass.bind_template();
|
|
|
|
|
klass.bind_template_instance_callbacks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
|
|
|
|
obj.init_template();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-01 09:57:01 +01:00
|
|
|
impl ObjectImpl for TranslationEditor {
|
2024-04-01 16:57:34 +02:00
|
|
|
fn constructed(&self) {
|
|
|
|
|
self.parent_constructed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-01 09:57:01 +01:00
|
|
|
impl WidgetImpl for TranslationEditor {}
|
|
|
|
|
impl BinImpl for TranslationEditor {}
|
2024-04-01 16:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
2025-03-01 09:57:01 +01:00
|
|
|
pub struct TranslationEditor(ObjectSubclass<imp::TranslationEditor>)
|
2024-04-01 16:57:34 +02:00
|
|
|
@extends gtk::Widget, adw::PreferencesGroup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[gtk::template_callbacks]
|
2025-03-01 09:57:01 +01:00
|
|
|
impl TranslationEditor {
|
2024-06-05 19:03:04 +02:00
|
|
|
pub fn new() -> Self {
|
|
|
|
|
glib::Object::new()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_translation(&self, translation: &TranslatedString) {
|
|
|
|
|
let mut translation = translation.0.clone();
|
2024-04-01 16:57:34 +02:00
|
|
|
|
2024-06-05 19:03:04 +02:00
|
|
|
self.imp()
|
2024-04-01 16:57:34 +02:00
|
|
|
.entry_row
|
|
|
|
|
.set_text(&translation.remove("generic").unwrap_or_default());
|
|
|
|
|
|
|
|
|
|
for (lang, translation) in translation {
|
2024-06-05 19:03:04 +02:00
|
|
|
self.add_entry(&lang, &translation);
|
2024-04-01 16:57:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[template_callback]
|
2025-03-01 08:55:04 +01:00
|
|
|
fn add_translation(&self) {
|
2024-04-01 16:57:34 +02:00
|
|
|
self.add_entry(&util::LANG, &self.imp().entry_row.text());
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 15:17:56 +02:00
|
|
|
pub fn translation(&self) -> TranslatedString {
|
2024-04-01 16:57:34 +02:00
|
|
|
let imp = self.imp();
|
|
|
|
|
let mut translation = HashMap::<String, String>::new();
|
|
|
|
|
|
|
|
|
|
translation.insert(String::from("generic"), imp.entry_row.text().into());
|
|
|
|
|
for entry in &*imp.translation_entries.borrow() {
|
|
|
|
|
translation.insert(entry.lang(), entry.translation());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TranslatedString(translation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn add_entry(&self, lang: &str, translation: &str) {
|
2025-03-01 09:57:01 +01:00
|
|
|
let entry = TranslationEntry::new(lang, translation);
|
2024-04-01 16:57:34 +02:00
|
|
|
|
|
|
|
|
let obj = self.clone();
|
|
|
|
|
entry.connect_remove(move |entry| {
|
2024-06-05 19:03:04 +02:00
|
|
|
obj.imp()
|
|
|
|
|
.translation_entries
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.retain(|e| e != entry);
|
2024-05-31 13:57:14 +02:00
|
|
|
obj.imp().list_box.remove(entry);
|
2024-04-01 16:57:34 +02:00
|
|
|
});
|
|
|
|
|
|
2024-06-05 19:03:04 +02:00
|
|
|
self.imp().list_box.insert(
|
|
|
|
|
&entry,
|
|
|
|
|
self.imp().translation_entries.borrow().len() as i32 + 1,
|
|
|
|
|
);
|
2024-05-31 13:57:14 +02:00
|
|
|
entry.grab_focus();
|
|
|
|
|
|
2024-04-01 16:57:34 +02:00
|
|
|
self.imp().translation_entries.borrow_mut().push(entry);
|
|
|
|
|
}
|
|
|
|
|
}
|