Class: TinyWebBrowser::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_web_browser/browser.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Browser



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/tiny_web_browser/browser.rb', line 8

def initialize(uri)
  @window = Gtk::Window.new
  @window.signal_connect('destroy') do
    Gtk.main_quit
  end

  @window_box = Gtk::VBox.new
  @notebook = Gtk::Notebook.new

  @shift_pressed = false
  @ctrl_pressed = false
  @g_pressed_last = false

  @tabbar = Gtk::VBox.new
  @tabbar_up = Gtk::HBox.new
  @tabbar_down = Gtk::HBox.new
  @go_back_button = Gtk::Button.new('戻る')
  @go_forward_button = Gtk::Button.new('進む')
  @uri_entry = Gtk::Entry.new
  @open_uri_button = Gtk::Button.new('開く')
  @add_tab_buttong = Gtk::Button.new('新規')
  @close_tab_button = Gtk::Button.new('閉じる')
  @search_entry = Gtk::Entry.new
  @last_event = nil

  @search_entry.signal_connect('changed') do |widget|
    current_webview.search_text(@search_entry.text, false, true, true)
  end
  @search_entry.signal_connect('activate') do |widget|
    @notebook.grab_focus
  end

  @notebook.signal_connect('key-press-event') do |widget, event|
    puts "notebook #{Gdk::Keyval.to_name(event.keyval)} pressed"
    #puts "notebook #{Gdk::Keyval.to_unicode(event.keyval)} pressed"
    case event.keyval
    when Gdk::Keyval::GDK_KEY_minus
      current_webview.zoom_out
    when Gdk::Keyval::GDK_KEY_plus
      current_webview.zoom_in
    when Gdk::Keyval::GDK_KEY_0
      current_webview.set_zoom_level(1.0)
    when Gdk::Keyval::GDK_KEY_t
      if @last_event != nil
        if @last_event.keyval == Gdk::Keyval::GDK_KEY_g
          next_page
        else
          add_page(uri)
        end
      else
        add_page(uri)
      end
    when Gdk::Keyval::GDK_KEY_T
      if @last_event != nil
        if @last_event.keyval == Gdk::Keyval::GDK_KEY_g
          prev_page
        end
      end
    when Gdk::Keyval::GDK_KEY_g
      if @last_event != nil
        if @last_event.keyval == Gdk::Keyval::GDK_KEY_g
          current_page.dom_element.set_scroll_top(0)
        end
      end
    when Gdk::Keyval::GDK_KEY_G
      current_page.dom_element.set_scroll_top(current_page.dom_element.scroll_height)
    when Gdk::Keyval::GDK_KEY_f
      if @ctrl_pressed
        scroll_top = current_page.dom_element.scroll_top
        current_page.dom_element.set_scroll_top(scroll_top + current_page.dom_element.client_height)
      end
    when Gdk::Keyval::GDK_KEY_b
      if @ctrl_pressed
        scroll_top = current_page.dom_element.scroll_top
        current_page.dom_element.set_scroll_top(scroll_top - current_page.dom_element.client_height)
      end
    when Gdk::Keyval::GDK_KEY_d
      if @ctrl_pressed
        scroll_top = current_page.dom_element.scroll_top
        current_page.dom_element.set_scroll_top(scroll_top + current_page.dom_element.client_height/2)
      else
        close_current_page
      end
    when Gdk::Keyval::GDK_KEY_u
      if @ctrl_pressed
        scroll_top = current_page.dom_element.scroll_top
        current_page.dom_element.set_scroll_top(scroll_top - current_page.dom_element.client_height/2)
      end
    when Gdk::Keyval::GDK_KEY_r
      current_webview.reload
    when Gdk::Keyval::GDK_KEY_i
      @uri_entry.select_region(0, -1)
      @uri_entry.grab_focus
    when Gdk::Keyval::GDK_KEY_h
      scroll_left = current_page.dom_element.scroll_left
      current_page.dom_element.set_scroll_left(scroll_left - 15)
    when Gdk::Keyval::GDK_KEY_l
      scroll_left = current_page.dom_element.scroll_left
      current_page.dom_element.set_scroll_left(scroll_left + 15)
    when Gdk::Keyval::GDK_KEY_j
      scroll_top = current_page.dom_element.scroll_top
      current_page.dom_element.set_scroll_top(scroll_top + 15)
    when Gdk::Keyval::GDK_KEY_k
      scroll_top = current_page.dom_element.scroll_top
      current_page.dom_element.set_scroll_top(scroll_top - 15)
    when Gdk::Keyval::GDK_KEY_slash
      @search_entry.grab_focus
    when Gdk::Keyval::GDK_KEY_n
      if @ctrl_pressed
        next_page
      else
        current_webview.search_text(@search_entry.text, false, true, true)
      end
    when Gdk::Keyval::GDK_KEY_N
      current_webview.search_text(@search_entry.text, false, false, true)
    when Gdk::Keyval::GDK_KEY_H
      current_webview.go_back
    when Gdk::Keyval::GDK_KEY_L
      current_webview.go_forward
    when Gdk::Keyval::GDK_KEY_s
      x, y, width, height, depth = window.window.geometry
      pixbuf = Gdk::Pixbuf.from_drawable(nil,
                                         window.window,
                                         0, 0,
                                         width, height)
      pixbuf.save("screenshot-#{Time.now.strftime("%Y%m%d%H%M%S")}.png", "png")
    when Gdk::Keyval::GDK_KEY_Shift_L, Gdk::Keyval::GDK_KEY_Shift_R
      @shift_pressed = true
    when Gdk::Keyval::GDK_KEY_Control_L, Gdk::Keyval::GDK_KEY_Control_R
      @ctrl_pressed = true
    when Gdk::Keyval::GDK_KEY_p
      if @ctrl_pressed
        prev_page
      end
    when Gdk::Keyval::GDK_KEY_q
      if @ctrl_pressed
        Gtk.main_quit
      end
    end

    if Gdk::Keyval.to_unicode(event.keyval) != 0
      @last_event = event
    end
  end

  @notebook.signal_connect('key-release-event') do |widget, event|
    #puts "notebook #{Gdk::Keyval.to_name(event.keyval)} released"
    case event.keyval
    when Gdk::Keyval::GDK_KEY_Shift_L, Gdk::Keyval::GDK_KEY_Shift_R
      @shift_pressed = false
    when Gdk::Keyval::GDK_KEY_Control_L, Gdk::Keyval::GDK_KEY_Control_R
      @ctrl_pressed = false
    end
  end

  @notebook.signal_connect('switch_page') do |notebook, page, page_num|
    puts_at_uri_entry(page_num)
  end

  @go_back_button.signal_connect('clicked') do
    current_webview.go_back
  end
  @go_forward_button.signal_connect('clicked') do
    current_webview.go_forward
  end
  @open_uri_button.signal_connect('clicked') do
    if @uri_entry.text == ''
      current_webview.load_uri(uri)
    else
      current_webview.load_uri(@uri_entry.text)
    end
  end


  #@uri_entry.signal_connect('grab-focus') do |widget, event|
  #  puts 'grab-focus'
  #  @uri_entry.select_region(0, -1)
  #  #@notebook.grab_focus
  #end

  @uri_entry.signal_connect('activate') do |widget, event|
    if (@uri_entry.text =~ /\./) and !(@uri_entry.text =~ /\:\/\//) and (('http://' + @uri_entry.text) =~ URI::regexp)
      open_page('http://' + @uri_entry.text)
    elsif @uri_entry.text =~ URI::regexp
      open_page(@uri_entry.text)
    else
      search_at_google(@uri_entry.text)
    end
    @notebook.grab_focus
  end
  @add_tab_buttong.signal_connect('clicked') do
    add_page(uri)
  end
  @close_tab_button.signal_connect('clicked') do
    close_current_page
  end
  @tabbar_up.pack_start(@go_back_button, false)
  @tabbar_up.pack_start(@go_forward_button, false)
  @tabbar_up.pack_start(@uri_entry)
  @tabbar_up.pack_start(@open_uri_button, false)
  @tabbar_down.pack_start(@add_tab_buttong, false)
  @tabbar_down.pack_start(@close_tab_button, false)

  @tabbar.pack_start(@tabbar_up)
  @tabbar.pack_start(@tabbar_down)

  @window_box.pack_start(@tabbar, false)
  @window_box.pack_start(@notebook)
  @window_box.pack_start(@search_entry, false)

  @window.add(@window_box)

  @pages = []
  add_page(uri)
end

Instance Method Details

#add_page(uri) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/tiny_web_browser/browser.rb', line 230

def add_page(uri)
  newpage = BrowserPage.new(uri)
  @pages << newpage
  #@notebook.append_page(@pages[@pages.length - 1].get_page_widget, @pages[@pages.length - 1].get_label_widget)
  @notebook.append_page(@pages[@pages.length - 1].get_widget)
  @notebook.show_all
  @notebook.set_page(@notebook.n_pages - 1)
  puts_at_uri_entry

  current_webview.signal_connect('title-changed') do |webview, frame, title|
    puts_at_uri_entry
    if title.length >= 10
      @notebook.set_tab_label_text(webview, title[0, 9] + '')
    else
      @notebook.set_tab_label_text(webview, title)
    end
  end

  current_webview.signal_connect('create-web-view') do |webview, frame, user_data|
    puts 'create'
    add_page(frame.uri)
  end
  #current_webview.signal_connect('new-window-policy-decision-requested') do |webview, frame, request, navigation_icon, policy_decision, user_data|
  #  puts 'new-window'
  #  add_page(request.uri)
  #end
  current_webview
end

#close_current_pageObject



259
260
261
262
# File 'lib/tiny_web_browser/browser.rb', line 259

def close_current_page
  close_page(@notebook.page)
  puts_at_uri_entry
end

#close_page(n) ⇒ Object



264
265
266
267
268
# File 'lib/tiny_web_browser/browser.rb', line 264

def close_page(n)
  if @notebook.n_pages > 1
    @notebook.remove_page(n)
  end
end

#current_pageObject



295
296
297
# File 'lib/tiny_web_browser/browser.rb', line 295

def current_page
  @pages[@notebook.page]
end

#current_webviewObject



291
292
293
# File 'lib/tiny_web_browser/browser.rb', line 291

def current_webview
  @notebook.get_nth_page(@notebook.page)
end

#next_pageObject



283
284
285
286
287
288
289
# File 'lib/tiny_web_browser/browser.rb', line 283

def next_page
  if @notebook.page < @notebook.n_pages - 1
    @notebook.set_page(@notebook.page + 1)
  else
    @notebook.set_page(0)
  end
end

#open_page(uri) ⇒ Object



270
271
272
273
# File 'lib/tiny_web_browser/browser.rb', line 270

def open_page(uri)
  current_webview.load_uri(uri)
  puts_at_uri_entry
end

#prev_pageObject



275
276
277
278
279
280
281
# File 'lib/tiny_web_browser/browser.rb', line 275

def prev_page
  if @notebook.page > 0
    @notebook.set_page(@notebook.page - 1)
  else
    @notebook.set_page(@notebook.n_pages - 1)
  end
end

#puts_at_uri_entry(page_num = -1)) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/tiny_web_browser/browser.rb', line 305

def puts_at_uri_entry(page_num = -1)
  if page_num < 0
    @uri_entry.set_text(current_webview.uri)
  else
    @uri_entry.set_text(@notebook.get_nth_page(page_num).uri)
  end
end

#search_at_google(query) ⇒ Object



299
300
301
302
303
# File 'lib/tiny_web_browser/browser.rb', line 299

def search_at_google(query)
  search_uri = 'https://www.google.com/search'
  uri = search_uri + '?q=' + query
  open_page(uri)
end

#show_allObject



225
226
227
# File 'lib/tiny_web_browser/browser.rb', line 225

def show_all
  @window.show_all
end