changeset 47:878a40d32d22

Added hooks
author unexist
date Sun, 24 Jan 2010 16:11:02 +0100
parents 0f9e756631e1
children 63e0bb8b9300
files bin/subtly subtly/browser.rb
diffstat 2 files changed, 118 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/bin/subtly	Sun Jan 24 14:51:58 2010 +0100
+++ b/bin/subtly	Sun Jan 24 16:11:02 2010 +0100
@@ -48,6 +48,45 @@
 # Set default page
 b.uri = ARGV[0] || "http://www.google.de"
 
+# Hooks
+=begin
+b.on(:link_hover) do |uri|
+  puts "Hover %s" % [ uri ]
+end
+
+b.on(:link_unhover) do
+  puts "Unhover"
+end
+
+b.on(:title_change) do |title|
+  puts "Title %s" % [ title ]
+end
+
+b.on(:scroll) do |percent|
+  puts "Scroll %d" % [ percent ]
+end
+
+b.on(:progress) do |percent|
+  puts "Progress %d" % [ percent ]
+end
+
+b.on(:load_start) do |uri|
+  puts "Load %s" % [ uri ]
+end
+
+b.on(:load_finish) do
+  puts "Finished"
+end
+
+b.on(:exit) do
+  puts "Exit"
+end
+
+b.on(:keypress) do |key|
+  puts "Key %d" % [ key ]
+end
+=end
+
 b.run
 
 # vim:ts=2:bs=2:sw=2:et:fdm=marker
--- a/subtly/browser.rb	Sun Jan 24 14:51:58 2010 +0100
+++ b/subtly/browser.rb	Sun Jan 24 16:11:02 2010 +0100
@@ -31,6 +31,7 @@
       # Init
       @mode     = :command
       @keys     = {}
+      @hooks    = {}
       @keychain = ""
       @buffer   = ""
       @times    = ""
@@ -102,9 +103,14 @@
     # Set browser uri
     #
     # @param [String, #read]  uri  Uri to load
+    #
+    # @raise [String] Uri error
+    # @since 0.0
     ##
 
     def uri=(uri)
+      raise "Unknown value type" unless(uri.is_a?(String))
+
       uri         = "http://" + uri unless(uri.include?("http://"))
       @webkit.uri = uri
       @info.text  = format_status(uri[0..LIMIT])
@@ -115,9 +121,14 @@
     # Set browser proxy
     #
     # @param [String, #read]  uri  Uri to proxy
+    #
+    # @raise [String] Proxy error
+    # @since 0.0
     ##
     
     def proxy=(uri)
+      raise "Unknown value type" unless(uri.is_a?(String))
+
       @webkit.proxy = uri
     end # }}}
 
@@ -125,15 +136,37 @@
     # Add keys hash
     #
     # @param [Hash, #read]  hash  Keys hash
+    #
+    # @raise [String] Key error
+    # @since 0.0
     ##
 
     def add_keys(hash)
+      raise "Unknown value type" unless(hash.is_a?(Hash)) 
+
       hash.each do |v, k|
         # Just make a symbol from keys an store it
         @keys[v.to_sym] = k
       end
     end # }}}
 
+    ## on {{{
+    # Add hooks
+    #
+    # @param [Symbol, #read]  event  Event name
+    #
+    # @raise [String] Hook error
+    # @since 0.0
+    ##
+
+    def on(event, &blk)
+      raise "No block given" unless(block_given?)
+      raise "Unknown value type" unless(event.is_a?(Symbol)) 
+      
+      # Add the hook
+      @hooks[event] = blk
+    end
+
     # Customization
 
     ## format_status {{{
@@ -175,8 +208,17 @@
     
     private
 
-    # commands {{{
-    def commands(sym)
+    # run_hook {{{
+    def run_hook(sym, data = nil)
+      @hooks.each do |k, v|
+        if(k == sym)
+          @hooks[k].call(data)
+        end
+      end
+    end # }}}
+
+    # run_command {{{
+    def run_command(sym)
       case sym
         when :SubtlyUriOpen then
           @input.text     = ":open "
@@ -237,6 +279,7 @@
           @scrolled.vadjust = 1.0
           @page.text        = format_page(100)
         when :SubtlyQuit then
+          run_hook(:exit)
           Subtly::Gtk::Runner.ffi_quit 
       end
     end # }}}
@@ -245,6 +288,8 @@
 
     # win_destroy # {{{
     def win_destroy(widget, arg)
+      run_hook(:exit)
+      
       Subtly::Gtk::Runner.ffi_quit 
     end # }}}
 
@@ -252,6 +297,8 @@
     def win_keypress(widget, arg)
       event = Subtly::Gdk::EventKey.new(arg)
 
+      run_hook(:keypress, event[:keyval])
+
       # Handle keys
       case event[:keyval]
         when Subtly::Gdk::Keys[:escape]
@@ -368,7 +415,7 @@
                     if(command.is_a?(String))
                       @webkit.execute(command)
                     else
-                      commands(command)
+                      run_command(command)
                     end
                   end
 
@@ -387,41 +434,64 @@
 
     # webkit_started {{{
     def webkit_started(widget, arg)
+      run_hook(:load_start, @webkit.uri)
+
       @status.text = format_meter(0)
     end # }}}
 
     # webkit_finished {{{
     def webkit_finished(widget, arg)
       @status.text = ""
+
+      run_hook(:load_finish)
+
       @info.text = format_status(@webkit.uri[0..LIMIT])
     end # }}}
 
     # webkit_progress {{{
     def webkit_progress(widget, arg, data)
+      # Get percent
+      progress = arg.to_i rescue 0
+
+      run_hook(:progress, progress)
+
       @status.text = format_meter(arg.to_i)
     end # }}}
 
     # webkit_title {{{
     def webkit_title(widget, arg1, arg2, data)
-      title = arg2.read_string
+      # Read title
+      title = arg2.read_string_to_null rescue "unknowm"
+
+      run_hook(:title_change, title)
 
       @win.title = "Subtly: %s" % [ title ]
     end # }}}
 
     # webkit_scroll {{{
     def webkit_scroll(widget, arg1, data)
-      page = (@scrolled.vadjust * 100).to_i
+      # Get page
+      page = (@scrolled.vadjust * 100).to_i rescue 0
+
+      run_hook(:scroll, page)
 
       @page.text = format_page(page)
     end # }}}
 
     # webkit_hover {{{
     def webkit_hover(widget, arg1, arg2, data)
-      begin #< Hover start
-        uri        = arg2.read_string
+      # Read uri
+      uri = arg2.read_string_to_null rescue nil
+
+      # Handle hover
+      if(uri.nil?)
+        run_hook(:link_unhover)
+
+        @info.text = format_status(@webkit.uri[0..LIMIT])
+      else
+        run_hook(:link_hover, uri)
+
         @info.text = format_status("Link: %s" % [ uri[0..LIMIT] ])
-      rescue #< Hover end
-        @info.text = format_status(@webkit.uri[0..LIMIT])
       end
     end # }}}
   end