From 170d7d6790cbbd22d6862be0f454dc8d1b077694 Mon Sep 17 00:00:00 2001 From: Lains Date: Wed, 3 Feb 2021 01:46:08 -0300 Subject: [PATCH] =?UTF-8?q?SideBar.vala=20-=20Align=20titles.=20/=20Status?= =?UTF-8?q?Bar.vala=20-=20lines=20=E2=86=92=20sentences.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In SideBar.vala, I've fixed clicking on entries to jump to them, and made the section titles align with the start of what they're heading of. In Statusbar.vala, I've changed the algorithm to count sentences instead of lines. Also chjanged the algorithm of Reading Time to be more natural. --- src/Widgets/SideBar.vala | 6 ++---- src/Widgets/StatusBar.vala | 34 +++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/Widgets/SideBar.vala b/src/Widgets/SideBar.vala index e6018531..4943db4b 100644 --- a/src/Widgets/SideBar.vala +++ b/src/Widgets/SideBar.vala @@ -111,7 +111,6 @@ namespace Quilter.Widgets { var title = new Gtk.Label (_("Files")); title.get_style_context ().add_class ("heading"); title.xalign = 0; - title.margin_start = 6; title.margin_bottom = 6; files_grid = new Gtk.Box (Gtk.Orientation.VERTICAL, 0); @@ -164,7 +163,6 @@ namespace Quilter.Widgets { var title = new Gtk.Label (_("Outline")); title.get_style_context ().add_class ("heading"); title.xalign = 0; - title.margin_start = 6; title.margin_bottom = 6; var sep = new Gtk.Separator (Gtk.Orientation.HORIZONTAL); @@ -220,10 +218,10 @@ namespace Quilter.Widgets { get_selected_row ().title = match.fetch_named ("header") + " " + match.fetch_named ("text"); } else if (match.fetch_named ("header") == "##") { store.insert (out subheader, root, -1); - store.set (subheader, 0, " " + match.fetch_named ("header") + " " + match.fetch_named ("text"), -1); + store.set (subheader, 0, match.fetch_named ("header") + " " + match.fetch_named ("text"), -1); } else if (match.fetch_named ("header") == "###") { store.insert (out section, subheader, -1); - store.set (section, 0, " " + match.fetch_named ("header") + " " + match.fetch_named ("text"), -1); + store.set (section, 0, match.fetch_named ("header") + " " + match.fetch_named ("text"), -1); } } while (match.next ()); } diff --git a/src/Widgets/StatusBar.vala b/src/Widgets/StatusBar.vala index 6e8751f1..278059df 100644 --- a/src/Widgets/StatusBar.vala +++ b/src/Widgets/StatusBar.vala @@ -29,8 +29,8 @@ namespace Quilter { public Gtk.RadioButton track_rtc; public MainWindow window; - /* Averaged normal reading speed is 200 WPM */ - int WPM = 200; + /* Averaged normal reading speed is 265 WPM */ + int WPM = 265; public StatusBar (Gtk.SourceBuffer buf) { this.buf = buf; @@ -43,7 +43,7 @@ namespace Quilter { update_wordcount (); }); - track_lines = new Gtk.RadioButton.with_label_from_widget (track_words, _("Lines")); + track_lines = new Gtk.RadioButton.with_label_from_widget (track_words, _("Sentences")); track_lines.toggled.connect (() => { Quilter.Application.gsettings.set_string("track-type", "lines"); update_linecount (); @@ -96,10 +96,13 @@ namespace Quilter { if (Quilter.Application.gsettings.get_string("track-type") == "words") { update_wordcount (); + track_words.set_active (true); } else if (Quilter.Application.gsettings.get_string("track-type") == "lines") { update_linecount (); + track_lines.set_active (true); } else if (Quilter.Application.gsettings.get_string("track-type") == "rtc") { update_readtimecount (); + track_rtc.set_active (true); } this.transition_type = Gtk.RevealerTransitionType.SLIDE_UP; @@ -114,23 +117,36 @@ namespace Quilter { public void update_linecount () { var lc = get_count(); - track_type_menu.set_label ((_("Lines: ")) + lc.lines.to_string()); + track_type_menu.set_label ((_("Sentences: ")) + lc.lines.to_string()); } public void update_readtimecount () { var rtc = get_count(); - float rt = (rtc.words / WPM); - print ("%f", rt); + double rt = Math.round((rtc.words / WPM)); track_type_menu.set_label ((_("Reading Time: ")) + rt.to_string() + "m"); } public WordCount get_count() { - Gtk.TextIter start, end; + Gtk.TextIter start, end; buf.get_bounds (out start, out end); - var lines = buf.get_line_count (); + var buffer = buf.get_text (start, end, false); + int i = 0; + try { + GLib.MatchInfo match; + var reg = new Regex("(?m)(?
\\.)"); + if (reg.match (buffer, 0, out match)) { + do { + i++; + } while (match.next ()); + } + } catch (Error e) { + warning (e.message); + } + + var lines = i; var words = buf.get_text (start, end, false).split(" ").length; - return new WordCount(words, lines); + return new WordCount(words, lines); } }