smartdict-gtk 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -1,6 +1,8 @@
1
1
  module Smartdict::Gui
2
2
  class Controller
3
3
  extend ActiveSupport::Autoload
4
+ extend Forwardable
5
+
4
6
  autoload :Config
5
7
 
6
8
  HISTORY_SIZE_ON_START = 50
@@ -8,6 +10,8 @@ module Smartdict::Gui
8
10
  attr_reader :main_window, :word_entry, :translate_button, :menu_bar, :text_view, :status_icon,
9
11
  :from_lang_combo_box, :to_lang_combo_box, :interchange_button, :word_list
10
12
 
13
+ def_delegators :@main_window, :toggle_visibility, :hide_visibility, :show_visibility
14
+
11
15
  def initialize
12
16
  @translator = Smartdict::Translator.new(
13
17
  :from_lang => config.from_lang,
@@ -30,6 +34,8 @@ module Smartdict::Gui
30
34
  @interchange_button = InterchangeButton.new(self)
31
35
  @word_list = WordList.new(self)
32
36
 
37
+ @db_mutex = Mutex.new
38
+
33
39
  load_history
34
40
  end
35
41
 
@@ -44,18 +50,21 @@ module Smartdict::Gui
44
50
  end
45
51
 
46
52
  def translate
47
- word = @word_entry.text.strip.downcase
48
- from_lang = @translator.default_opts[:from_lang]
49
- to_lang = @translator.default_opts[:to_lang]
50
-
51
- if add_to_history?(word, from_lang, to_lang)
52
- translation = @translator.translate(word)
53
- add_to_history(translation)
54
- else
55
- translation = @translator.translate(word, :log => false)
53
+ safe_concurrent_run do
54
+ word = @word_entry.text.strip.downcase
55
+ from_lang = @translator.default_opts[:from_lang]
56
+ to_lang = @translator.default_opts[:to_lang]
57
+
58
+ if add_to_history?(word, from_lang, to_lang)
59
+ translation = @translator.translate(word)
60
+ add_to_history(translation)
61
+ else
62
+ translation = @translator.translate(word, :log => false)
63
+ end
64
+
65
+ @text_view.show_translation(translation)
66
+ @word_list.scroll_up
56
67
  end
57
- @text_view.show_translation(translation)
58
- @word_list.scroll_up
59
68
  rescue Smartdict::TranslationNotFound => error
60
69
  @text_view.buffer.text = error.message
61
70
  rescue Exception => error
@@ -70,10 +79,6 @@ module Smartdict::Gui
70
79
  @text_view.show_translation(translation)
71
80
  end
72
81
 
73
- def toggle_main_window
74
- @main_window.toggle_visibility
75
- end
76
-
77
82
  def open_about_window
78
83
  AboutWindow.new
79
84
  end
@@ -93,6 +98,10 @@ module Smartdict::Gui
93
98
  @to_lang_combo_box.active, @from_lang_combo_box.active = @from_lang_combo_box.active, @to_lang_combo_box.active
94
99
  end
95
100
 
101
+ def focus_word_entry
102
+ @word_entry.grab_focus
103
+ end
104
+
96
105
 
97
106
  private
98
107
 
@@ -115,5 +124,13 @@ module Smartdict::Gui
115
124
  end
116
125
  end
117
126
 
127
+ def safe_concurrent_run
128
+ Thread.new do
129
+ @db_mutex.synchronize do
130
+ yield
131
+ end
132
+ end
133
+ end
134
+
118
135
  end
119
136
  end
@@ -10,9 +10,11 @@ class Smartdict::Gui::MainWindow < ::Gtk::Window
10
10
 
11
11
  @controller = controller
12
12
 
13
- signal_connect("delete_event") { @controller.toggle_main_window }
13
+ signal_connect("delete_event") { @controller.hide_visibility }
14
14
  signal_connect("destroy") { @controller.quit }
15
15
 
16
+ signal_connect("focus_in_event") { @controller.focus_word_entry }
17
+
16
18
  set_default_size(720, 450)
17
19
  set_icon(Smartdict::Icons.logo)
18
20
  end
@@ -63,16 +65,23 @@ class Smartdict::Gui::MainWindow < ::Gtk::Window
63
65
 
64
66
 
65
67
  def toggle_visibility
66
- if self.visible? and self.active?
67
- @prev_position = self.position
68
- self.hide_all
69
- elsif self.visible? and !self.active?
70
- self.present
68
+ if visible? and active?
69
+ hide_visibility
70
+ elsif visible? and !active?
71
+ present
71
72
  else
72
- self.show_all
73
- self.move(*@prev_position)
74
- self.present
73
+ show_visibility
75
74
  end
76
75
  end
77
76
 
77
+ def show_visibility
78
+ show_all
79
+ move(*@prev_position) if @prev_position
80
+ present
81
+ end
82
+
83
+ def hide_visibility
84
+ @prev_position = position
85
+ hide_all
86
+ end
78
87
  end
@@ -8,7 +8,7 @@ module Smartdict::Gui
8
8
  build_menu
9
9
  self.pixbuf = Gdk::Pixbuf.new(Smartdict::Icons.logo)
10
10
 
11
- signal_connect('activate'){@controller.toggle_main_window}
11
+ signal_connect('activate'){@controller.toggle_visibility}
12
12
 
13
13
  ##Show menu on rigth click
14
14
  self.signal_connect('popup-menu'){|tray, button, time| @menu.popup(nil, nil, button, time)}
data/lib/smartdict/gui.rb CHANGED
@@ -26,10 +26,40 @@ module Smartdict::Gui
26
26
  LEFT_BOX_BUTTON_HEIGHT = 36
27
27
 
28
28
  def run
29
- Smartdict::Gui::Controller.new.run
29
+ if running?
30
+ puts "smartdict-gtk is already running. PID=#{running_pid}"
31
+ Process.kill("USR1", running_pid)
32
+ else
33
+ File.write(pid_file, Process.pid.to_s)
34
+ controller = Smartdict::Gui::Controller.new
35
+ Signal.trap("USR1") do
36
+ controller.show_visibility
37
+ end
38
+ controller.run
39
+ end
30
40
  end
31
41
 
32
42
  def root_dir
33
43
  File.join(File.dirname(__FILE__), '../..')
34
44
  end
45
+
46
+
47
+
48
+ private
49
+
50
+ def running_pid
51
+ pid = File.read(pid_file).strip.to_i
52
+ Process.getpgid(pid)
53
+ pid
54
+ rescue Errno::ENOENT, Errno::ESRCH
55
+ nil
56
+ end
57
+
58
+ def running?
59
+ !!running_pid
60
+ end
61
+
62
+ def pid_file
63
+ File.join(Smartdict.user_dir, 'smartdict-gtk.pid')
64
+ end
35
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartdict-gtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-11 00:00:00.000000000 Z
12
+ date: 2012-07-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: smartdict-core
@@ -225,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
225
  version: '0'
226
226
  requirements: []
227
227
  rubyforge_project:
228
- rubygems_version: 1.8.21
228
+ rubygems_version: 1.8.24
229
229
  signing_key:
230
230
  specification_version: 3
231
231
  summary: GTK GUI for Smartdict dictionary