2

我正在尝试使用 JRuby 来显示带有 SWT 的托盘项目。我在 Windows 7、Ubuntu Unity/gnome 3/ 和 gnome classic 上试过这个。

我发现托盘项目没有出现。菜单也显示在鼠标光标位置。

我在这里做错了什么?

=begin
# references
* usage of swt gem -- https://github.com/danlucraft/swt/blob/master/examples/menu_and_toolbar.rb
* SWT tray example -- http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java
=end

require 'java'
require_relative '../dependencies/swt/lib/swt'

class Doro
  include Swt::Widgets

  IMAGE = File.expand_path('../../assets/doro.jpg', __FILE__)

  def initialize
    ui_start
    setup_tray
    ui_end
  end

  def ui_start
    display = Display.get_current
    @shell = Shell.new
    #@shell.text = self.class.to_s
  end

  def ui_end
    #@shell.set_bounds(50, 50, 300, 200)
    #@shell.visible = false
    @shell.pack
    @shell.open
  end

  def setup_tray
    display = @shell.display
    tray = display.get_system_tray
    tray_item = TrayItem.new(tray, Swt::SWT::NONE)
    tray_item.tool_tip_text = 'Doro'
    tray_item.image = Swt::Graphics::Image.new(display, IMAGE)

    tray_item.add_selection_listener { }

=begin
    tray_item.add_show_listener { |event| puts 'show' }
    tray_item.add_listener(Swt::SWT::Show, Swt::SWT::Listener.new { |event| puts 'show' })
    tray_item.add_hide_listener { }
    tray_item.add_default_selection_listener { }
=end

    setup_menu tray_item

    tray_item.visible = true
  end

  def setup_menu(parent)
    menu = Swt::Widgets::Menu.new(@shell, Swt::SWT::POP_UP)

    fileItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
    fileItem.setText("File")

    editItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
    editItem.setText("Edit")

    menu.visible = true

    parent.add_menu_detect_listener do
      menu.each { |m| m.visible = true }
    end
  end

  def start
    display = @shell.display

    while !@shell.isDisposed
      display.sleep unless display.read_and_dispatch
    end

    display.dispose
  end
end

app = Doro.new
Swt::Widgets::Display.set_app_name app.class.to_s
app.start

更新:

我在工作中看到此代码在我的 Windows 7 m/c 上没有任何更改。我正确地看到了图标。我想知道为什么它在我家的窗户上不起作用。(当然永远不会在 ubuntu 上)。我使用了相同版本的 SWT jar 和 swt gem。

4

1 回答 1

1

我发现您引用 TrayItem 类的方式存在问题:

def setup_tray
    ...     
    # tray_item = TrayItem.new(tray, Swt::SWT::NONE) # here is the problem, replace with above
    tray_item = org.eclipse.swt.widgets.TrayItem.new(tray, Swt::SWT::NONE)

此更改使图标出现在托盘中。

要在右键单击图标时显示菜单,请修复 setup_menu:

def setup_menu(parent)
    menu = Swt::Widgets::Menu.new(@shell, Swt::SWT::POP_UP)

   fileItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
   fileItem.setText("File")

   editItem = Swt::Widgets::MenuItem.new(menu, Swt::SWT::PUSH)
   editItem.setText("Edit")

   menu.visible = false # FIXED   
   parent.add_menu_detect_listener do
=begin # fixed debug code     
     items = menu.getItems
     puts "this code block run every time you rightclick the icon in the systray,"
     puts "your menu consists of #{items.count} items"
     items.each { |m| p m.text }
=end      
     # items.each { |m| m.visible = true } #original code
     menu.visible = true # FIXED
   end
end

关于引用 swt 类的方法,我参考了这篇文章:Sébastien Le Callonnecusing swt with jruby

否则,按照 Martin Sadler 的文章:An Introduction to Desktop Apps with Ruby,我找到了一个基于 AWT(不是 SWT)的解决方案

有一些编辑:

1) 在您的 jruby 类中包含您需要的 Java 和导入 awt 类

class Doro
  include Java
  import java.awt.TrayIcon
  import java.awt.Toolkit
  ...

2) 设置菜单并在 setup_tray 中设置托盘图标

 def setup_tray
    # Setup our menu items
    file_item = java.awt.MenuItem.new("File")
    edit_item = java.awt.MenuItem.new("Edit")
    # Add the items to the popup menu itself
    menu = java.awt.PopupMenu.new
    menu.add(file_item)
    menu.add(edit_item)
    # Give the tray an icon and attach the popup menu to it
    image    = java.awt.Toolkit::default_toolkit.get_image(IMAGE)
    tray_icon = TrayIcon.new(image, "Screenshot!",  menu)
    tray_icon.image_auto_size = true
    # Finally add the tray icon to the tray
    tray = java.awt.SystemTray::system_tray
    tray.add(tray_icon)
  end

测试:

操作系统:Microsoft Windows [版本 6.1.7601]

ruby 解释器:jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (IBM J9 VM 1.6.0) [Windows Vista-amd64-java]

宝石:swt(0.13)

于 2012-10-06T18:37:13.737 回答