lib/mdv/markdown_viewer.rb in mdv-0.3.1 vs lib/mdv/markdown_viewer.rb in mdv-0.4.0
- old
+ new
@@ -1,14 +1,15 @@
+# frozen_string_literal: true
+
require 'webkit2-gtk'
+require 'mdv/document'
module MDV
# Markdown viewer window class
class MarkdownViewer
- attr_reader :file
-
def initialize(file)
- @file = file
+ @document = Document.new(file)
setup_gui
reload
connect_signals
@win.show_all
end
@@ -16,10 +17,11 @@
private
def connect_signals
connect_key_press_event_signal
connect_destroy_signal
+ connect_web_view_signals
end
def connect_destroy_signal
@win.signal_connect('destroy') { Gtk.main_quit }
end
@@ -29,48 +31,56 @@
handle_key(evt) if evt.state.control_mask?
false
end
end
+ def connect_web_view_signals
+ web_view.signal_connect('context-menu') { true }
+ web_view.signal_connect('decide-policy') do |_wv, decision, decision_type|
+ handle_decide_policy(decision, decision_type)
+ end
+ end
+
def handle_key(evt)
case evt.keyval
when 'q'.ord
@win.destroy
when 'r'.ord
reload
end
end
+ def handle_decide_policy(decision, decision_type)
+ case decision_type.nick
+ when 'navigation-action'
+ action = decision.navigation_action
+ if action.user_gesture?
+ Gtk.show_uri_on_window(@win, action.request.uri, 0)
+ true
+ end
+ when 'new-window-action'
+ true
+ end
+ end
+
def setup_gui
@win = Gtk::Window.new :toplevel
@win.set_default_geometry 700, 500
@win.add scrolled_window
end
def scrolled_window
- @scr ||= Gtk::ScrolledWindow.new(nil, nil).tap do |it|
+ @scrolled_window ||= Gtk::ScrolledWindow.new(nil, nil).tap do |it|
it.add web_view
end
end
def web_view
- @wv ||= WebKit2Gtk::WebView.new
+ @web_view ||= WebKit2Gtk::WebView.new
end
- def fullpath
- @fullpath ||= File.expand_path(file, Dir.pwd)
- end
-
- def base_uri
- @base_uri ||= "file://#{fullpath}"
- end
-
- def html
- GitHub::Markup.render(fullpath)
- end
-
def reload
- web_view.load_html html, base_uri
+ web_view.load_html @document.html, @document.base_uri
end
end
end