From 025bceb9ea6a9030f9e164ea6c2dd491e25b65c0 Mon Sep 17 00:00:00 2001 From: Nil Gradisnik Date: Fri, 5 Jan 2018 15:34:42 -0800 Subject: [PATCH] Working webkit2gtk preview --- Cargo.lock | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/gtk-ui.glade | 13 +------- src/main.rs | 22 ++++++++++---- src/preview.rs | 4 +-- 5 files changed, 98 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b6d900f..b7401a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -367,6 +367,23 @@ name = "itoa" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "javascriptcore-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "javascriptcore-rs-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "javascriptcore-rs-sys" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -396,6 +413,7 @@ dependencies = [ "gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "horrorshow 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "sourceview 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "webkit2gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -548,6 +566,19 @@ dependencies = [ "serde 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "soup-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "sourceview" version = "0.3.0" @@ -703,6 +734,48 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "webkit2gtk" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gio 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "javascriptcore-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "webkit2gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "webkit2gtk-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "javascriptcore-rs-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "pango-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "soup-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.2.8" @@ -749,6 +822,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum if_chain 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "61bb90bdd39e3af69b0172dfc6130f6cd6332bf040fbb9bdd4401d37adbd48b8" "checksum itertools 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d3f2be4da1690a039e9ae5fd575f706a63ad5a2120f161b1d653c9da3930dd21" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" +"checksum javascriptcore-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "25753bc0f02e8123447441d40ef5516aec678769a58db60fac2e19375439a6a5" +"checksum javascriptcore-rs-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3fae3b8197de3f21279a7b74c2f3a8d951e51e4c1c19f030de1122b39d86a58c" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" @@ -772,6 +847,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)" = "0672de7300b02bac3f3689f8faea813c4a1ea9fe0cb49e80f714231d267518a2" "checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab" "checksum serde_json 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ea28ea0cca944668919bec6af209864a8dfe769fd2b0b723f36b22e20c1bf69f" +"checksum soup-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "11c783e6dec746da651ce72bb9c24f39e8c89f01af2f1e41ecb1d1df13891e31" "checksum sourceview 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "673844d773083b34c3cb3dab036c75eebb7b223343724f167915980a4586912e" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" @@ -792,5 +868,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum webkit2gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f17004b7c76cee4e82041bbebbb1caec1dacaab7838ed7da72e2e46bc05c24d7" +"checksum webkit2gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c42e3815b8d0ca4ca5fa13cb4ba068678b29ec642a7aa6e253d4e3ea862b3ca7" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index 8c4f514..a743a7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ gio = "0.3" sourceview = "0.3" comrak = "0.2" horrorshow = "0.6" +webkit2gtk = "0.3" [dependencies.gtk] version = "0.3" diff --git a/src/gtk-ui.glade b/src/gtk-ui.glade index 28e83a8..c4c0fc5 100644 --- a/src/gtk-ui.glade +++ b/src/gtk-ui.glade @@ -101,18 +101,7 @@ Author: Nil Gradisnik in 200 - - True - True - False - word - 4 - 4 - 4 - 4 - 2 - True - + diff --git a/src/main.rs b/src/main.rs index 180e281..46bdd40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate gtk; #[macro_use] extern crate horrorshow; extern crate sourceview; +extern crate webkit2gtk; mod preview; mod utils; @@ -15,6 +16,8 @@ use gtk::prelude::*; use gtk::Builder; use gio::MenuExt; +use webkit2gtk::*; + use std::env::args; use utils::{buffer_to_string, configure_sourceview, open_file, set_title}; @@ -51,7 +54,11 @@ fn build_system_menu(application: >k::Application) { application.set_app_menu(&menu); } -fn add_actions(application: >k::Application, window: >k::ApplicationWindow, about_dialog: >k::AboutDialog) { +fn add_actions( + application: >k::Application, + window: >k::ApplicationWindow, + about_dialog: >k::AboutDialog, +) { let quit = gio::SimpleAction::new("quit", None); quit.connect_activate(clone!(window => move |_, _| { window.destroy(); @@ -85,7 +92,10 @@ fn build_ui(application: >k::Application) { let text_buffer: sourceview::Buffer = builder.get_object("text_buffer").unwrap(); configure_sourceview(&text_buffer); - let markdown_view: gtk::TextView = builder.get_object("markdown_view").unwrap(); + let context = WebContext::get_default().unwrap(); + let preview = WebView::new_with_context(&context); + let markdown_view: gtk::ScrolledWindow = builder.get_object("scrolled_window_right").unwrap(); + markdown_view.add(&preview); let file_chooser: gtk::FileChooserDialog = builder.get_object("file_chooser").unwrap(); file_chooser.add_buttons(&[ @@ -99,7 +109,7 @@ fn build_ui(application: >k::Application) { about_dialog.set_authors(&[AUTHORS]); about_dialog.set_comments(DESCRIPTION); - open_button.connect_clicked(clone!(header_bar, text_buffer, markdown_view => move |_| { + open_button.connect_clicked(clone!(header_bar, text_buffer, preview => move |_| { file_chooser.show(); if file_chooser.run() == gtk::ResponseType::Ok.into() { @@ -109,15 +119,15 @@ fn build_ui(application: >k::Application) { set_title(&header_bar, &filename); text_buffer.set_text(&contents); - markdown_view.get_buffer().unwrap().set_text(&preview::render(&contents)); + preview.load_html(&preview::render(&contents), None); } file_chooser.hide(); })); - text_view.connect_key_release_event(clone!(text_buffer, markdown_view => move |_, _| { + text_view.connect_key_release_event(clone!(text_buffer, preview => move |_, _| { let markdown = buffer_to_string(&text_buffer).unwrap(); - markdown_view.get_buffer().unwrap().set_text(&preview::render(&markdown)); + preview.load_html(&preview::render(&markdown), None); Inhibit(true) })); diff --git a/src/preview.rs b/src/preview.rs index cce88dc..49e7cfc 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -30,8 +30,8 @@ pub fn render(markdown: &str) -> String { : Raw("hljs.initHighlightingOnLoad()") } style { - : "body { width: 80%; margin: 0 auto }"; - : "img { max-width: 80% }" + : "body { width: 90%; margin: 0 auto }"; + : "img { max-width: 90% }" } } body { -- 2.39.2