changeset 14:ba26532e98c7

Added more comments, updated format functions and added page position display
author unexist
date Fri, 22 Jan 2010 02:27:39 +0100
parents 13c2eeefadca
children f7c6a07fa061
files subtly/browser.rb
diffstat 1 files changed, 110 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/subtly/browser.rb	Mon Dec 14 00:46:32 2009 +0100
+++ b/subtly/browser.rb	Fri Jan 22 02:27:39 2010 +0100
@@ -18,6 +18,14 @@
     attr_accessor :mode, :win, :scrolled, :info, :status, :input, :vbox, :hbox, :webkit
     attr_accessor :keys, :keychain
 
+    LIMIT = 80 #< Limit url length in bar
+
+    ## initialize {{{
+    # Create a new browser
+    #
+    # @return [Object] New browser
+    ##
+
     def initialize
       super
 
@@ -31,8 +39,11 @@
       @scrolled = Subtly::Gtk::Scrolled.new
       @info     = Subtly::Gtk::Label.new(true, true)
       @status   = Subtly::Gtk::Label.new(true, false)
+      @page   = Subtly::Gtk::Label.new(true, false)
       @input    = Subtly::Gtk::Entry.new(false)
-      @vbox     = Subtly::Gtk::Box.new(:vbox, false, 0)
+      #@list     = Subtly::Gtk::List.new
+      @vbox1    = Subtly::Gtk::Box.new(:vbox, false, 0)
+      @vbox2    = Subtly::Gtk::Box.new(:vbox, false, 0)
       @hbox     = Subtly::Gtk::Box.new(:hbox, false, 5)
       @webkit   = Subtly::Webkit.new
 
@@ -46,22 +57,28 @@
       @status.foreground = "#ffffff"
       @input.foreground  = "#000000"
       @input.background  = "#ffffff"
+      @page.foreground = "#ffffff"
+      @page.background = "#0000ff"
       @webkit.focusable  = false
       @input.editable    = false
       @input.focusable   = false
-      @info.alignment(0, 0)
-      @status.alignment(1, 0)
+      @input.length      = LIMIT
+      @info.alignment    = [ 0.0, 0.0 ]
+      @status.alignment  = [ 1.0, 0.0 ]
 
       # Containers
-      @win.add(@vbox)
+      @win.add(@vbox1)
       @scrolled.add(@webkit)
 
       # Boxes
-      @vbox.add(@scrolled, true,  true, 0)
-      @vbox.add(@hbox,     false, true, 0)
-      @hbox.add(@info,     true,  true, 5)
-      @hbox.add(@status,   true,  true, 5)
-      @vbox.add(@input,    false, true, 0)
+      @vbox1.add(@scrolled, true,  true,  0)
+      @vbox1.add(@hbox,     false, true,  0)
+      @hbox.add(@info,      true,  true,  5)
+      @hbox.add(@status,    true,  true,  5)
+      @hbox.add(@page,    false, true,  5)
+      #@vbox2.add(@list,     false, true, 0)
+      @vbox2.add(@input,    false, true,  0)
+      @vbox1.add(@vbox2,    false, true,  0)
 
       # Signals
       @win.connect("destroy",                  method(:win_destroy).to_proc,     nil)
@@ -75,35 +92,76 @@
       @webkit.connect("hovering-over-link",    method(:webkit_hover).to_proc,    nil)
       
       @win.show_all
-    end
+      #@list.hide
+    end # }}}
+
+    ## uri= {{{
+    # Set browser uri
+    #
+    # @param  [String, #read]  uri  Uri to load
+    ##
 
     def uri=(uri)
-      @webkit.uri = uri
-      @info.text  = status(uri)
-    end
+      @webkit.uri  = uri
+      @info.text   = format_status(uri[0..LIMIT])
+      @page.text = "Top"
+    end # }}}
+
+    ## add_keys {{{
+    # Add keys hash
+    #
+    # @param  [Hash, #read]  hash  Keys hash
+    ##
 
     def add_keys(hash)
       hash.each do |v, k|
         # Just make a symbol from keys an store it
         keys[v.to_sym] = k
       end
-    end
+    end # }}}
 
-    def status(str)
+    # Customization
+
+    ## format_status {{{
+    # Set status text
+    #
+    # @param  [String, #read]  str  Status text
+    ##
+
+    def format_status(str)
       "%s" % [ str ]
-    end
+    end # }}}
 
-    def meter(progress, width = 10)
+    ## format_meter {{{
+    # Draw status meter
+    #
+    # @param  [Fixnum, #read]  progress  Load progress in percent
+    # @param  [Fixnum, #read]  width     Width of the meter
+    ##
+
+    def format_meter(progress, width = 14)
       times = (progress * width) / 100
 
       "[%s>%s]" % [ "=" * times, " " * (width - times) ]
-    end
+    end # }}}
 
-    def page()
-    end
+    ## format_page {{{
+    # Draw page meter
+    #
+    # @param  [String, #read]  page  Page position in percent
+    ##
+
+    def format_page(page)
+      case page
+        when 0..2    then "Top"
+        when 98..100 then "Bot"
+        else "%d%%" % [ page ]
+      end
+    end # }}}
     
     private
 
+    # commands {{{
     def commands(sym)
       case sym
         when :SubtlyUriOpen then
@@ -126,16 +184,25 @@
           @webkit.back
         when :SubtlyBrowserForward then
           @webkit.forward
+        when :SubtlyScrollTop
+          @scrolled.vadjust = 0.0
+          @page.text        = format_page(0)
+        when :SubtlyScrollBottom
+          @scrolled.vadjust = 1.0
+          @page.text        = format_page(100)
         when :SubtlyQuit then
           Subtly::Gtk::Runner.ffi_quit 
       end
-    end
+    end # }}}
 
     # Callbacks
+
+    # win_destroy # {{{
     def win_destroy(widget, arg)
       Subtly::Gtk::Runner.ffi_quit 
-    end
+    end # }}}
 
+    # win_keypress {{{
     def win_keypress(widget, arg)
       event = Subtly::Gdk::EventKey.new(arg)
 
@@ -162,14 +229,14 @@
               uri             = @input.text.gsub(":open ", "")
               uri             = "http://" + uri unless(uri.include?("http://"))
               @webkit.uri     = uri
-              @info.text      = uri
+              @info.text      = uri[0..LIMIT]
               @input.editable = false
               @input.text     = ""
           end
         when Subtly::Gdk::Keys[:backspace] then
           case @mode
             when :uri
-             if(@input.text != ":open ")
+             if(":open " != @input.text)
               @input.text = @input.text.chop
             end
           end
@@ -190,40 +257,45 @@
       puts "Key: %s=%d, state=%d, modifier=%d" % [ 
         event[:string], event[:keyval], event[:state], event[:is_modifier]
       ]
-    end
+    end # }}}
 
+    # webkit_started {{{
     def webkit_started(widget, arg)
-      @status.text = meter(0)
-    end
+      @status.text = format_meter(0)
+    end # }}}
 
+    # webkit_finished {{{
     def webkit_finished(widget, arg)
       @status.text = ""
-    end
+    end # }}}
 
+    # webkit_progress {{{
     def webkit_progress(widget, arg, data)
-      @status.text = meter(arg.to_i)
-    end
+      @status.text = format_meter(arg.to_i)
+    end # }}}
 
+    # webkit_title {{{
     def webkit_title(widget, arg1, arg2, data)
       title = arg2.read_string
 
       @win.title = "Subtly: %s" % [ title ]
-    end
+    end # }}}
 
+    # webkit_scroll {{{
     def webkit_scroll(widget, arg1, data)
-      adjust = @scrolled.vadjustment
+      page = (@scrolled.vadjust * 100).to_i
 
-      puts "%f %f %f %f %f" % [ adjust[:lower], adjust[:upper], adjust[:step_increment],
-        adjust[:page_increment], adjust[:page_size]]
-    end
+      @page.text = format_page(page)
+    end # }}}
 
+    # webkit_hover {{{
     def webkit_hover(widget, arg1, arg2, data)
       begin #< Hover start
         uri        = arg2.read_string
-        @info.text = status("Link: %s" % [ uri ])
+        @info.text = format_status("Link: %s" % [ uri[0..LIMIT] ])
       rescue #< Hover end
-        @info.text = status(@webkit.uri)
+        @info.text = format_status(@webkit.uri[0..LIMIT])
       end
-    end
+    end # }}}
   end
 end