Skip to content

Commit

Permalink
1.3.0 - Preview Mode now updates as typing occurs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Galardi committed Sep 28, 2017
1 parent 0068398 commit fb1133e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
48 changes: 19 additions & 29 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace Quilter {
private Gtk.ScrolledWindow edit_view;
private int edit_view_id;
private int preview_view_id;

public signal void updated ();
private bool timer_scheduled = false;
private const int TIME_TO_REFRESH = 300;

private bool _is_fullscreen;
public bool is_fullscreen {
Expand All @@ -56,10 +56,6 @@ namespace Quilter {
unfullscreen ();
}
}

// current state for webview
private bool timer_scheduled = false;
private uint timer_id = 0;

public MainWindow (Gtk.Application application) {
Object (application: application,
Expand All @@ -69,6 +65,8 @@ namespace Quilter {
width_request: 920);

view_mode.notify["selected"].connect (on_view_mode_changed);
schedule_timer ();
edit_view_content.changed.connect (schedule_timer);
}

construct {
Expand Down Expand Up @@ -192,8 +190,6 @@ namespace Quilter {
preview_view_content = new Widgets.WebView (this);
preview_view.add (preview_view_content);

schedule_timer ();

stack = new Gtk.Stack ();
stack.transition_type = Gtk.StackTransitionType.SLIDE_LEFT_RIGHT;
stack.add (edit_view);
Expand Down Expand Up @@ -242,23 +238,6 @@ namespace Quilter {
});
}

private void schedule_timer () {
timer_id = Timeout.add (300, render_func);
timer_scheduled = true;
}

private void remove_timer () {
if (timer_scheduled) {
Source.remove(timer_id);
}
}

private bool render_func () {
preview_view_content.update_html_view ();
timer_scheduled = false;
return false;
}

protected bool match_keycode (int keyval, uint code) {
Gdk.KeymapKey [] keys;
Gdk.Keymap keymap = Gdk.Keymap.get_default ();
Expand Down Expand Up @@ -290,8 +269,19 @@ namespace Quilter {
settings.last_file = @"$cache/temp";
Services.FileUtils.save_tmp_file ();
}
return false;
}

remove_timer ();
private void schedule_timer () {
if (!timer_scheduled) {
Timeout.add (TIME_TO_REFRESH, render_func);
timer_scheduled = true;
}
}

private bool render_func () {
preview_view_content.update_html_view ();
timer_scheduled = false;
return false;
}

Expand Down Expand Up @@ -322,7 +312,7 @@ namespace Quilter {
debug ("New button pressed.");
var settings = AppSettings.get_default ();

if (!Widgets.SourceView.is_modified) {
if (Widgets.SourceView.is_modified) {
try {
debug ("Making new file...");
Services.FileUtils.new_document ();
Expand All @@ -332,9 +322,9 @@ namespace Quilter {
} catch (Error e) {
warning ("Unexpected error: " + e.message);
}
Widgets.SourceView.is_modified = true;
} else {
Widgets.SourceView.is_modified = false;
} else {
Widgets.SourceView.is_modified = true;
}

file = null;
Expand Down
9 changes: 7 additions & 2 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ namespace Quilter.Widgets {
public static new Gtk.SourceBuffer buffer;
public static bool is_modified;
public File file;

public WebView webview;
private string font;

public signal void changed ();

public SourceView () {
update_settings ();
var settings = AppSettings.get_default ();
Expand All @@ -37,9 +39,9 @@ namespace Quilter.Widgets {
var manager = Gtk.SourceLanguageManager.get_default ();
var language = manager.guess_language (null, "text/x-markdown");
buffer = new Gtk.SourceBuffer.with_language (language);
buffer.changed.connect (on_text_modified);
buffer.highlight_syntax = true;
buffer.set_max_undo_levels (20);
buffer.changed.connect (on_text_modified);

is_modified = false;
Timeout.add_seconds (20, () => {
Expand All @@ -63,6 +65,7 @@ namespace Quilter.Widgets {
if (!is_modified) {
is_modified = true;
} else {
changed ();
is_modified = false;
}
}
Expand All @@ -81,12 +84,14 @@ namespace Quilter.Widgets {
public void set_text (string text, bool opening = true) {
if (opening) {
buffer.begin_not_undoable_action ();
buffer.changed.disconnect (on_text_modified);
}

buffer.text = text;

if (opening) {
buffer.end_not_undoable_action ();
buffer.changed.connect (on_text_modified);
}

Gtk.TextIter? start = null;
Expand Down
7 changes: 4 additions & 3 deletions src/Widgets/WebView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ using WebKit;

namespace Quilter {
public class Widgets.WebView : WebKit.WebView {
public signal void updated ();
public MainWindow parent_window;

public WebView (MainWindow window) {
Object(user_content_manager: new UserContentManager());
parent_window = window;
visible = true;
vexpand = true;
hexpand = true;
var settingsweb = get_settings();
settingsweb.enable_plugins = false;
settingsweb.enable_page_cache = false;
settingsweb.enable_page_cache = true;
settingsweb.enable_developer_extras = false;
web_context.set_cache_model(WebKit.CacheModel.DOCUMENT_VIEWER);

update_html_view ();
Expand Down Expand Up @@ -144,7 +146,6 @@ namespace Quilter {
html += process (text);
html += "</div></body></html>";
this.load_html (html, "file://");
updated ();
}
}
}

0 comments on commit fb1133e

Please sign in to comment.