Skip to content

Commit

Permalink
Add option to change window width
Browse files Browse the repository at this point in the history
  • Loading branch information
abb128 committed Dec 20, 2022
1 parent 00241a1 commit 13bb871
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 27 deletions.
4 changes: 4 additions & 0 deletions data/net.sapples.LiveCaptions.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<summary>Make window transparent</summary>
</key>

<key name="line-width" type="i">
<default>50</default>
</key>

<key name="benchmark" type="d">
<default>-1.0</default>
</key>
Expand Down
24 changes: 15 additions & 9 deletions src/line-gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,21 @@ void line_generator_update(struct line_generator *lg, size_t num_tokens, const A
}

// break line if too long
curr->len += line_generator_get_text_width(lg, token);
if(curr->len >= lg->max_text_width) {
// find previous word boundary
while((tokens[j].token[0] != ' ') && (j > 0)) j--;

// line break
lg->current_line = REL_LINE_IDX(lg->current_line, 1);
lg->active_start_of_lines[lg->current_line] = j;
return line_generator_update(lg, num_tokens, tokens);
if(i == lg->current_line){
curr->len += line_generator_get_text_width(lg, token);
if(curr->len >= lg->max_text_width) {
size_t tgt_brk = j;
// find previous word boundary
while((tokens[tgt_brk].token[0] != ' ') && (tgt_brk > start_of_line)) tgt_brk--;

// if we backtracked all the way to the start of line, just give up and break here
if(tgt_brk == start_of_line) tgt_brk = j;

// line break
lg->current_line = REL_LINE_IDX(lg->current_line, 1);
lg->active_start_of_lines[lg->current_line] = tgt_brk;
return line_generator_update(lg, num_tokens, tokens);
}
}

// write the actual line
Expand Down
4 changes: 3 additions & 1 deletion src/livecaptions-settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ static void livecaptions_settings_class_init(LiveCaptionsSettingsClass *klass) {
gtk_widget_class_bind_template_child (widget_class, LiveCaptionsSettings, transparent_window_switch);
gtk_widget_class_bind_template_child (widget_class, LiveCaptionsSettings, transparent_window_switch_ar);

gtk_widget_class_bind_template_child (widget_class, LiveCaptionsSettings, line_width_adjustment);

gtk_widget_class_bind_template_child (widget_class, LiveCaptionsSettings, benchmark_label);

Expand All @@ -147,8 +148,9 @@ static void livecaptions_settings_init(LiveCaptionsSettings *self) {
g_settings_bind(self->settings, "text-uppercase", self->text_upper_switch, "state", G_SETTINGS_BIND_DEFAULT);
g_settings_bind(self->settings, "fade-text", self->fade_text_switch, "state", G_SETTINGS_BIND_DEFAULT);
g_settings_bind(self->settings, "filter-profanity", self->filter_profanity_switch, "state", G_SETTINGS_BIND_DEFAULT);
g_settings_bind(self->settings, "transparent-window", self->transparent_window_switch, "state", G_SETTINGS_BIND_DEFAULT);
//g_settings_bind(self->settings, "filter-slurs", self->filter_slurs_switch, "state", G_SETTINGS_BIND_DEFAULT);
g_settings_bind(self->settings, "transparent-window", self->transparent_window_switch, "state", G_SETTINGS_BIND_DEFAULT);
g_settings_bind(self->settings, "line-width", self->line_width_adjustment, "value", G_SETTINGS_BIND_DEFAULT);

g_settings_bind(self->settings, "font-name", self->font_button, "font", G_SETTINGS_BIND_DEFAULT);

Expand Down
2 changes: 2 additions & 0 deletions src/livecaptions-settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct _LiveCaptionsSettings {
AdwActionRow *filter_profanity_switch_ar;
//GtkSwitch *filter_slurs_switch;

GtkAdjustment *line_width_adjustment;

GtkLabel *benchmark_label;
};

Expand Down
22 changes: 22 additions & 0 deletions src/livecaptions-settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@
</child>


<child>
<object class="AdwActionRow">
<property name="title" translatable="yes">Window Width</property>

<child type="suffix">
<object class="GtkSpinButton">
<property name="visible">True</property>
<property name="valign">center</property>
<property name="adjustment">
<object class="GtkAdjustment" id="line_width_adjustment">
<property name="lower">20</property>
<property name="upper">300</property>
<property name="step-increment">1</property>
<property name="page-increment">1</property>
</object>
</property>
</object>
</child>
</object>
</child>


<child>
<object class="AdwActionRow" id="text_upper_switch_ar">
<property name="title" translatable="yes">Show Text in Uppercase</property>
Expand Down
44 changes: 27 additions & 17 deletions src/livecaptions-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,20 @@ static void change_orientable_orientation(LiveCaptionsWindow *self, gint text_he
}
}

const char LINE_WIDTH_TEXT_TEMPLATE[] = "This program is free software, you can redistribute or modify";
static void update_font(LiveCaptionsWindow *self) {
PangoFontDescription *desc = pango_font_description_from_string(g_settings_get_string(self->settings, "font-name"));

PangoAttribute *attr_font = pango_attr_font_desc_new(desc);

PangoAttrList *attr = gtk_label_get_attributes(self->label);
if(attr == NULL){
attr = pango_attr_list_new();
}
pango_attr_list_change(attr, attr_font);

gtk_label_set_attributes(self->label, attr);

pango_font_description_free(desc);

const char LINE_WIDTH_TEXT_TEMPLATE[] = "This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.";
static void update_line_width(LiveCaptionsWindow *self){
if(self->font_layout != NULL) g_object_unref(self->font_layout);

int preferred_width = g_settings_get_int(self->settings, "line-width");
size_t text_len = sizeof(LINE_WIDTH_TEXT_TEMPLATE);
if(preferred_width < text_len) text_len = preferred_width;

int width, height;
PangoLayout *layout = gtk_label_get_layout(self->label);
layout = pango_layout_copy(layout);

pango_layout_set_width(layout, -1);
pango_layout_set_text(layout, LINE_WIDTH_TEXT_TEMPLATE, sizeof(LINE_WIDTH_TEXT_TEMPLATE));
pango_layout_set_text(layout, LINE_WIDTH_TEXT_TEMPLATE, text_len);
pango_layout_get_size(layout, &width, &height);

height = (height / PANGO_SCALE) * 2 + 2;
Expand All @@ -82,6 +72,24 @@ static void update_font(LiveCaptionsWindow *self) {
self->font_layout_counter++;
}

static void update_font(LiveCaptionsWindow *self) {
PangoFontDescription *desc = pango_font_description_from_string(g_settings_get_string(self->settings, "font-name"));

PangoAttribute *attr_font = pango_attr_font_desc_new(desc);

PangoAttrList *attr = gtk_label_get_attributes(self->label);
if(attr == NULL){
attr = pango_attr_list_new();
}
pango_attr_list_change(attr, attr_font);

gtk_label_set_attributes(self->label, attr);

pango_font_description_free(desc);

update_line_width(self);
}

static void update_window_transparency(LiveCaptionsWindow *self) {
bool use_transparency = g_settings_get_boolean(self->settings, "transparent-window");

Expand All @@ -99,6 +107,8 @@ static void on_settings_change(G_GNUC_UNUSED GSettings *settings,
LiveCaptionsWindow *self = user_data;
if(g_str_equal(key, "font-name")) {
update_font(self);
}else if(g_str_equal(key, "line-width")) {
update_line_width(self);
}else if(g_str_equal(key, "transparent-window")) {
update_window_transparency(self);
}
Expand Down

0 comments on commit 13bb871

Please sign in to comment.