[macruby-changes] [607] MacRuby/trunk/lib

source_changes at macosforge.org source_changes at macosforge.org
Fri Sep 19 06:04:38 PDT 2008


Revision: 607
          http://trac.macosforge.org/projects/ruby/changeset/607
Author:   rich at infoether.com
Date:     2008-09-19 06:04:38 -0700 (Fri, 19 Sep 2008)
Log Message:
-----------
add notifications support

Modified Paths:
--------------
    MacRuby/trunk/lib/hotcocoa/mappings/image.rb
    MacRuby/trunk/lib/hotcocoa/mappings/text_view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/view.rb
    MacRuby/trunk/lib/hotcocoa/mappings/window.rb
    MacRuby/trunk/lib/hotcocoa.rb

Added Paths:
-----------
    MacRuby/trunk/lib/hotcocoa/mappings/notification.rb
    MacRuby/trunk/lib/hotcocoa/notification_listener.rb

Modified: MacRuby/trunk/lib/hotcocoa/mappings/image.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/image.rb	2008-09-19 01:39:37 UTC (rev 606)
+++ MacRuby/trunk/lib/hotcocoa/mappings/image.rb	2008-09-19 13:04:38 UTC (rev 607)
@@ -11,5 +11,5 @@
       NSImage.alloc.init
     end
   end
-  
+
 end

Added: MacRuby/trunk/lib/hotcocoa/mappings/notification.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/notification.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/mappings/notification.rb	2008-09-19 13:04:38 UTC (rev 607)
@@ -0,0 +1,17 @@
+HotCocoa::Mappings.map :notification => :NSNotification do
+
+  defaults :post => true, :distributed => false
+  
+  def alloc_with_options(options)
+    if options.delete(:post)
+      if options.delete(:distributed)
+        NSDistributedNotificationCenter.defaultCenter.postNotificationWithName options.delete(:name), object:options.delete(:object), userInfo:options.delete(:info), deliverImmediately:(options.delete(:immediately) == true)
+      else
+        NSNotificationCenter.defaultCenter.postNotificationWithName options.delete(:name), object:options.delete(:object), userInfo:options.delete(:info)
+      end
+    else
+      NSNotification.notificationWithName options.delete(:name), object:options.delete(:object), userInfo:options.delete(:info)
+    end
+  end
+  
+end

Modified: MacRuby/trunk/lib/hotcocoa/mappings/text_view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/text_view.rb	2008-09-19 01:39:37 UTC (rev 606)
+++ MacRuby/trunk/lib/hotcocoa/mappings/text_view.rb	2008-09-19 13:04:38 UTC (rev 607)
@@ -1,9 +1,13 @@
 HotCocoa::Mappings.map :text_view => :NSTextView do
 
-  defaults :frame => [0,0,0,0]
-
+  defaults :layout => {}, :frame => DefaultEmptyRect
+  
   def init_with_options(text_view, options)
-    text_view.initWithFrame options.delete(:frame)
+    if options[:container]
+      text_view.initWithFrame options.delete(:frame), :textContainer => options.delete(:container)
+    else
+      text_view.initWithFrame options.delete(:frame)
+    end
   end
-
+  
 end

Modified: MacRuby/trunk/lib/hotcocoa/mappings/view.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/view.rb	2008-09-19 01:39:37 UTC (rev 606)
+++ MacRuby/trunk/lib/hotcocoa/mappings/view.rb	2008-09-19 13:04:38 UTC (rev 607)
@@ -44,6 +44,11 @@
       @layout
     end
 
+    def on_notification(options={}, &block)
+      options[:sent_by] = self
+      NotificationListener.new(options, &block)
+    end
+
     def remove(subview, options = {})
       raise ArgumentError, "#{subview} is not a subview of #{self} and cannot be removed." unless subview.superview == self
       options[:needs_display] == false ? subview.removeFromSuperviewWithoutNeedingDisplay : subview.removeFromSuperview

Modified: MacRuby/trunk/lib/hotcocoa/mappings/window.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/window.rb	2008-09-19 01:39:37 UTC (rev 606)
+++ MacRuby/trunk/lib/hotcocoa/mappings/window.rb	2008-09-19 13:04:38 UTC (rev 607)
@@ -56,6 +56,11 @@
     def view=(view)
       setContentView(view)
     end
+
+    def on_notification(options={}, &block)
+      options[:sent_by] = self
+      NotificationListener.new(options, &block)
+    end
     
     def show
       display

Added: MacRuby/trunk/lib/hotcocoa/notification_listener.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/notification_listener.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/notification_listener.rb	2008-09-19 13:04:38 UTC (rev 607)
@@ -0,0 +1,57 @@
+module HotCocoa
+  
+  class NotificationListener
+    
+    DistributedBehaviors = {
+      :drop => NSNotificationSuspensionBehaviorDrop,
+      :coalese => NSNotificationSuspensionBehaviorCoalesce,
+      :hold => NSNotificationSuspensionBehaviorHold,
+      :deliver_immediately => NSNotificationSuspensionBehaviorDeliverImmediately
+    }
+    
+    attr_reader :callback, :name, :sender, :suspension_behavior
+    
+    def initialize(options={}, &block)
+      @callback = block
+      @distributed = (options[:distributed] == true)
+      @suspension_behavior = DistributedBehaviors[options[:when_suspended] || :coalese]
+      @name = options[:named]
+      @sender = options[:sent_by]
+      observe
+    end
+    
+    def distributed?
+      @distributed
+    end
+    
+    def receive(notification)
+      callback.call(notification)
+    end
+    
+    def stop_notifications(options={})
+      if options.has_key?(:named) || options.has_key?(:sent_by)
+        notification_center.removeObserver(self, name:options[:named], object:options[:sender])
+      else
+        notification_center.removeObserver(self)
+      end
+    end
+    
+    private
+    
+      def observe
+        if distributed?
+          notification_center.addObserver self, selector:'receive:', name:name, object:sender, suspensionBehavior:suspension_behavior
+        else
+          notification_center.addObserver self, selector:'receive:', name:name, object:sender
+        end
+      end
+      
+      def notification_center
+        @notification_center ||= (distributed? ? NSDistributedNotificationCenter.defaultCenter : NSNotificationCenter.defaultCenter)
+      end
+  end
+  
+  def on_notification(options={}, &block)
+    NotificationListener.new(options, &block)
+  end
+end
\ No newline at end of file

Modified: MacRuby/trunk/lib/hotcocoa.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa.rb	2008-09-19 01:39:37 UTC (rev 606)
+++ MacRuby/trunk/lib/hotcocoa.rb	2008-09-19 13:04:38 UTC (rev 607)
@@ -12,6 +12,7 @@
 require 'hotcocoa/mapper'
 require 'hotcocoa/layout_view'
 require 'hotcocoa/delegate_builder'
+require 'hotcocoa/notification_listener'
 require 'hotcocoa/data_sources/table_data_source'
 require 'hotcocoa/data_sources/combo_box_data_source'
 require 'hotcocoa/kernel_ext'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080919/ea13596f/attachment-0001.html 


More information about the macruby-changes mailing list