Revision: 625 http://trac.macosforge.org/projects/ruby/changeset/625 Author: psychs@limechat.net Date: 2008-09-26 17:25:29 -0700 (Fri, 26 Sep 2008) Log Message: ----------- added growl sample using hotcocoa Added Paths: ----------- MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_growl.rb MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_sample.rb Added: MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_growl.rb =================================================================== --- MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_growl.rb (rev 0) +++ MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_growl.rb 2008-09-27 00:25:29 UTC (rev 625) @@ -0,0 +1,73 @@ +require 'hotcocoa' + +class Growl < NSObject + include HotCocoa + + GROWL_IS_READY = "Lend Me Some Sugar; I Am Your Neighbor!" + GROWL_NOTIFICATION_CLICKED = "GrowlClicked!" + GROWL_NOTIFICATION_TIMED_OUT = "GrowlTimedOut!" + GROWL_KEY_CLICKED_CONTEXT = "ClickedContext" + + PRIORITIES = { + :emergency => 2, + :high => 1, + :normal => 0, + :moderate => -1, + :very_low => -2, + } + + def register(app_name, notifications, default_notifications=nil, icon=nil) + @app_name = app_name + @app_icon = icon || NSApplication.sharedApplication.applicationIconImage + @notifications = notifications + @default_notifications = default_notifications || notifications + register_to_growl! + end + + def notify(name, title, desc, options={}) + dic = { + :NotificationName => name, + :NotificationTitle => title, + :NotificationDescription => desc, + :NotificationPriority => PRIORITIES[options[:priority]] || options[:priority] || 0, + :ApplicationName => @app_name, + :ApplicationPID => self.class.pid, + } + dic[:NotificationIcon] = options[:icon].TIFFRepresentation if options[:icon] + dic[:NotificationSticky] = 1 if options[:sticky] + + context = {} + context[:user_click_context] = options[:click_context] if options[:click_context] + dic[:NotificationClickContext] = context unless context.empty? + + notification :distributed => true, :name => :GrowlNotification, :info => dic + end + + private + + def self.pid + NSProcessInfo.processInfo.processIdentifier.to_i + end + + def register_to_growl! + on_notification(:distributed => true, :named => GROWL_IS_READY) do |n| + register_to_growl! + end + + on_notification(:distributed => true, :named => "#{@app_name}-#{pid}-#{GROWL_NOTIFICATION_CLICKED}") do |n| + puts '@@@ on clicked' + end + + on_notification(:distributed => true, :named => "#{@app_name}-#{pid}-#{GROWL_NOTIFICATION_TIMED_OUT}") do |n| + puts '@@@ on timed out' + end + + dic = { + :ApplicationName => @app_name, + :ApplicationIcon => @app_icon.TIFFRepresentation, + :AllNotifications => @notifications, + :DefaultNotifications => @default_notifications + } + notification :distributed => true, :name => :GrowlApplicationRegistrationNotification, :info => dic + end +end Property changes on: MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_growl.rb ___________________________________________________________________ Added: svn:eol-style + native Added: MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_sample.rb =================================================================== --- MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_sample.rb (rev 0) +++ MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_sample.rb 2008-09-27 00:25:29 UTC (rev 625) @@ -0,0 +1,34 @@ +require 'hotcocoa_growl' + +class GrowlController < NSObject + HELLO_TYPE = 'Hello message received' + + def init + if super + @g = Growl.alloc.init + @g.register('GrowlSample', [HELLO_TYPE]) + @g.notify(HELLO_TYPE, 'Sticky', 'Hello world', :sticky => true, :click_context => Time.now.to_s) + @g.notify(HELLO_TYPE, 'Timed out', 'Hello world', :click_context => Time.now.to_s) + @count = 2 + self + end + end + + def growlNotifierClicked_context(sender, context) + puts "Clicked: #{context}" + checkCount + end + + def growlNotifierTimedOut_context(sender, context) + puts "Timed out: #{context}" + checkCount + end + + def checkCount + @count -= 1 + NSApp.terminate(nil) if @count == 0 + end +end + +g = GrowlController.alloc.init +NSApp.run Property changes on: MacRuby/trunk/sample-macruby/GrowlNotifier/hotcocoa_sample.rb ___________________________________________________________________ Added: svn:eol-style + native
participants (1)
-
source_changes@macosforge.org