Skip to content
Merged
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 po/POTFILES
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ src/Widgets/PopoverWidgets/ApiLevel.vala
src/Widgets/Buttons/OrientationBox.vala
src/Widgets/Popovers/SettingsPopover.vala
src/Widgets/Popovers/OptionsPopover.vala
src/Widgets/LanguageSelectionBox.vala
src/Widgets/Panes/Pane.vala
src/Widgets/Panes/SourcePane.vala
src/Widgets/Panes/TargetPane.vala
Expand Down
47 changes: 24 additions & 23 deletions src/Views/TranslationView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/
public class Inscriptions.TranslationView : Gtk.Box {

Gtk.Paned paned {get; set;}
Gtk.CenterBox paned {get; set;}
public Inscriptions.SourcePane source_pane;
public Inscriptions.TargetPane target_pane;
private Inscriptions.LanguageSelectionBox language_selection;

// Add a debounce so we aren't requesting the API constantly
public uint debounce_timer_id = 0;
Expand All @@ -37,7 +38,7 @@ public class Inscriptions.TranslationView : Gtk.Box {
};

construct {
orientation = HORIZONTAL;
orientation = VERTICAL;
spacing = 0;

actions = new SimpleActionGroup ();
Expand All @@ -58,26 +59,26 @@ public class Inscriptions.TranslationView : Gtk.Box {

/* ---------------- UI ---------------- */
source_pane = new Inscriptions.SourcePane ();
var selected_source_language = Application.settings.get_string ("source-language");

source_pane.language = selected_source_language;

target_pane = new Inscriptions.TargetPane ();
var selected_target_language = Application.settings.get_string ("target-language");
target_pane.language = selected_target_language;

paned = new Gtk.Paned (HORIZONTAL) {
start_child = source_pane,
end_child = target_pane,
shrink_start_child = shrink_end_child = false
paned = new Gtk.CenterBox () {
vexpand = true
};
paned.start_widget = source_pane;
paned.center_widget = new Gtk.Separator (VERTICAL);
paned.end_widget = target_pane;

append (paned);
// paned.start_ (source_pane);
// paned.append (source_pane);
// paned.append (target_pane);

language_selection = new Inscriptions.LanguageSelectionBox ();
append (language_selection);
append (paned);



/* ---------------- CONNECTS ---------------- */
/* ---------------- CONNECTS AND BINDS ---------------- */
// Logic for toggling the panes/layout
on_orientation_toggled ();
Application.settings.changed["vertical-layout"].connect (on_orientation_toggled);
Expand All @@ -100,16 +101,16 @@ public class Inscriptions.TranslationView : Gtk.Box {
if (if_connect) {
// translate when text is entered or user changes any language or option
source_pane.textview.buffer.changed.connect (on_text_to_translate);
source_pane.language_changed.connect (on_text_to_translate);
target_pane.language_changed.connect (on_text_to_translate);
language_selection.source_changed.connect (on_text_to_translate);
language_selection.target_changed.connect (on_text_to_translate);
Application.settings.changed["context"].connect (on_text_to_translate);
Application.settings.changed["formality"].connect (on_text_to_translate);

} else {
// no
source_pane.textview.buffer.changed.disconnect (on_text_to_translate);
source_pane.language_changed.disconnect (on_text_to_translate);
target_pane.language_changed.disconnect (on_text_to_translate);
language_selection.source_changed.disconnect (on_text_to_translate);
language_selection.target_changed.disconnect (on_text_to_translate);
Application.settings.changed["context"].disconnect (on_text_to_translate);
Application.settings.changed["formality"].disconnect (on_text_to_translate);
}
Expand All @@ -124,17 +125,17 @@ public class Inscriptions.TranslationView : Gtk.Box {
connect_all (false);

// Temp variables
var newtarget = source_pane.language;
var newtarget = language_selection.selected_source;
var newtarget_text = source_pane.text;

var newsource = target_pane.language;
var newsource = language_selection.selected_target;
var newsource_text = target_pane.text;

// Letsgo
source_pane.language = newsource;
language_selection.selected_source = newsource;
source_pane.text = newsource_text;

target_pane.language = newtarget;
language_selection.selected_target = newtarget;
target_pane.text = newtarget_text;

source_pane.textview.refresh ();
Expand Down Expand Up @@ -166,7 +167,7 @@ public class Inscriptions.TranslationView : Gtk.Box {
* Filter not-requests, set or reset debounce_timer
*/
public void on_text_to_translate () {
if (source_pane.language == target_pane.language) {
if (language_selection.selected_source == language_selection.selected_target) {
source_pane.message (_("Target language is the same as source"));
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Widgets/HeaderBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public class Inscriptions.HeaderBar : Granite.Bin {


//TRANSLATORS: This is for a button that switches source and target language
switchlang_button = new Gtk.Button.from_icon_name ("media-playlist-repeat") {
tooltip_markup = Granite.markup_accel_tooltip ({"<Ctrl>I"}, _("Switch languages"))
};
switchlang_button.action_name = TranslationView.ACTION_PREFIX + TranslationView.ACTION_SWITCH_LANG;
// switchlang_button = new Gtk.Button.from_icon_name ("media-playlist-repeat") {
// tooltip_markup = Granite.markup_accel_tooltip ({"<Ctrl>I"}, _("Switch languages"))
// };
// switchlang_button.action_name = TranslationView.ACTION_PREFIX + TranslationView.ACTION_SWITCH_LANG;

var toggle_highlight = new Gtk.ToggleButton () {
icon_name = "format-text-highlight",
Expand All @@ -118,7 +118,7 @@ public class Inscriptions.HeaderBar : Granite.Bin {
};

var toolbar = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 5);
toolbar.append (switchlang_button);
//toolbar.append (switchlang_button);
//toolbar.append (toggle_highlight);

toolbar_revealer = new Gtk.Revealer () {
Expand Down
60 changes: 60 additions & 0 deletions src/Widgets/LanguageDropDown.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025 Stella & Charlie (teamcons.carrd.co)
*/

/**
* A convenience wrapper allowing to build the DropDown we want with a list of languages, and access selection and changes easily
*/
public class Inscriptions.LanguageDropDown : Granite.Bin {

Inscriptions.DDModel model;
Gtk.DropDown dropdown;

public string selected {
owned get { return get_selected_language ();}
set { set_selected_language (value);}
}

public signal void language_changed (string code = "");

public LanguageDropDown (Lang[] languages) {

hexpand = true;
model = new Inscriptions.DDModel ();

foreach (var language in languages) {
model.model_append (language);
}

var expression = new Gtk.PropertyExpression (typeof (Inscriptions.Lang), null, "both");
dropdown = new Gtk.DropDown (model.model, expression) {
factory = model.factory_header,
list_factory = model.factory_list,
enable_search = true,
search_match_mode= Gtk.StringFilterMatchMode.SUBSTRING,
show_arrow = false,
hexpand = true
};

child = dropdown;

/* ---------------- CONNECTS AND BINDS ---------------- */
dropdown.notify["selected-item"].connect (on_selected_language);
}

private void on_selected_language () {
var selected_lang = dropdown.get_selected_item () as Lang;
language_changed (selected_lang.code);
}

private void set_selected_language (string code) {
var position = model.model_where_code (code);
dropdown.set_selected (position);
}

private string get_selected_language () {
var selected_lang = dropdown.get_selected_item () as Lang;
return selected_lang.code;
}
}
97 changes: 97 additions & 0 deletions src/Widgets/LanguageSelectionBox.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025 Stella & Charlie (teamcons.carrd.co)
*/

/**
* Small horizontal box containing Source and Target dropdowns and a switch languages button
*/
public class Inscriptions.LanguageSelectionBox : Gtk.Box {

public LanguageDropDown dropdown_source {get; set;}
public LanguageDropDown dropdown_target {get; set;}

public string selected_source {
owned get { return get_selected_language (true);}
set { set_selected_language (value, true);}
}

public string selected_target {
owned get { return get_selected_language (false);}
set { set_selected_language (value, false);}
}

public signal void source_changed (string code = "");
public signal void target_changed (string code = "");

construct {
orientation = Gtk.Orientation.HORIZONTAL;
spacing = 0;
hexpand = true;

/* ---------------- SOURCE ---------------- */
dropdown_source = new LanguageDropDown (Inscriptions.SourceLang ()) {
tooltip_text = _("Set the language to translate from")
};

//TRANSLATORS: This is for a button that switches source and target language
var switchlang_button = new Gtk.Button.from_icon_name ("media-playlist-repeat-symbolic") {
tooltip_markup = Granite.markup_accel_tooltip ({"<Ctrl>I"}, _("Switch languages"))
};
switchlang_button.action_name = TranslationView.ACTION_PREFIX + TranslationView.ACTION_SWITCH_LANG;

dropdown_target = new LanguageDropDown (Inscriptions.SourceLang ()) {
tooltip_text = _("Set the language to translate to")
};



var cb = new Gtk.CenterBox () {
hexpand = true
};

cb.start_widget = dropdown_source;
cb.center_widget = switchlang_button;
cb.end_widget = dropdown_target;

append (cb);

/* ---------------- CONNECTS AND BINDS ---------------- */

Application.settings.bind (KEY_SOURCE_LANGUAGE,
this, "selected_source",
GLib.SettingsBindFlags.DEFAULT
);

Application.settings.bind (KEY_TARGET_LANGUAGE,
this, "selected_target",
GLib.SettingsBindFlags.DEFAULT
);

dropdown_source.language_changed.connect ( (language_code) => {source_changed (language_code);});
dropdown_target.language_changed.connect ( (language_code) => {target_changed (language_code);});
}


private void set_selected_language (string code, bool is_source) {
if (is_source) {
dropdown_source.selected = code;

} else {
dropdown_target.selected = code;
}

print ("Set " + code + " Source?" + is_source.to_string () + "\n");
}

private string get_selected_language (bool is_source) {
if (is_source) {
//print ("is selected " + selected.code + selected.name + "\n");
return dropdown_source.selected;

} else {
//print ("is selected " + selected.code + selected.name + "\n");
return dropdown_target.selected;
}
}
}
51 changes: 0 additions & 51 deletions src/Widgets/Panes/Pane.vala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class Inscriptions.Pane : Gtk.Box {

public Inscriptions.DDModel model {get; construct;}

public Gtk.Revealer dropdown_revealer;
public Gtk.DropDown dropdown;
public Inscriptions.Lang selected;
public Inscriptions.TextView textview;
public Gtk.ScrolledWindow scrolledwindow;
public Gtk.ActionBar actionbar;
Expand All @@ -28,33 +25,10 @@ public class Inscriptions.Pane : Gtk.Box {
set { textview.buffer.text = value;}
}

public string language {
owned get { return get_selected_language ();}
set { set_selected_language (value);}
}

public signal void language_changed (string code = "");

public Pane (DDModel model) {
Object (model: model);
}

construct {
orientation = Gtk.Orientation.VERTICAL;
spacing = 0;

var expression = new Gtk.PropertyExpression (typeof(Inscriptions.Lang), null, "both");

/* ---------------- DROPDOWN ---------------- */
dropdown = new Gtk.DropDown (model.model, expression) {
factory = model.factory_header,
list_factory = model.factory_list,
enable_search = true,
search_match_mode= Gtk.StringFilterMatchMode.SUBSTRING,
show_arrow = false
};
dropdown.notify["selected-item"].connect(on_selected_language);

/* ---------------- VIEW ---------------- */
textview = new Inscriptions.TextView ();
textview.set_wrap_mode (Gtk.WrapMode.WORD_CHAR);
Expand Down Expand Up @@ -97,37 +71,12 @@ public class Inscriptions.Pane : Gtk.Box {
};
stack.add_child (main_view);

append (dropdown);
append (stack);

toast.default_action.connect (() => {
textview.buffer.undo ();
});
}

public void on_selected_language () {
selected = dropdown.get_selected_item () as Lang;
language_changed (selected.code);
//print ("\nS selected %s:%s", selected.code, selected.name);
}

private void set_selected_language (string code) {
//print ("got " + code + "\n");
var position = model.model_where_code (code);
dropdown.set_selected (position);
}

private string get_selected_language () {
selected = dropdown.get_selected_item () as Lang;
//print ("is selected " + selected.code + selected.name + "\n");
return selected.code;
}

public string language_localized_name () {
selected = dropdown.get_selected_item () as Lang;
return selected.name;
}

// Respectful of Undo
public void replace_text (string new_text) {

Expand Down
Loading
Loading