[macruby-changes] [446] MacRuby/trunk/lib/hotcocoa/mappings

source_changes at macosforge.org source_changes at macosforge.org
Thu Aug 14 21:43:29 PDT 2008


Revision: 446
          http://trac.macosforge.org/projects/ruby/changeset/446
Author:   rich at infoether.com
Date:     2008-08-14 21:43:28 -0700 (Thu, 14 Aug 2008)
Log Message:
-----------
map the on_<menu> stuff for app, enable better menu nav, add on_action to menu item

Modified Paths:
--------------
    MacRuby/trunk/lib/hotcocoa/mappings/application.rb
    MacRuby/trunk/lib/hotcocoa/mappings/menu.rb
    MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb

Modified: MacRuby/trunk/lib/hotcocoa/mappings/application.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/application.rb	2008-08-15 04:42:06 UTC (rev 445)
+++ MacRuby/trunk/lib/hotcocoa/mappings/application.rb	2008-08-15 04:43:28 UTC (rev 446)
@@ -5,15 +5,12 @@
   end
   
   def handle_block(application, &block)
+    begin
+      require 'lib/menu'
+      application.menu = application_menu
+    rescue LoadError => e
+    end
     block.call(application)
-    unless application.menu
-      begin
-        require 'lib/menu'
-        application.menu = application_menu(application)
-      rescue LoadError => e
-        puts "No menu specified"
-      end
-    end
     application.run
   end
   
@@ -31,10 +28,47 @@
       setMainMenu(menu)
     end
     
-    def menu
-      mainMenu
+    def menu(path=nil)
+      if path
+        find_menu(mainMenu, path)
+      else
+        mainMenu
+      end
     end
     
+    def on_hide(menu)
+      hide(menu)
+    end
+    
+    def on_about(menu)
+      orderFrontStandardAboutPanel(menu)
+    end
+    
+    def on_hide_others(menu)
+      hideOtherApplications(menu)
+    end
+    
+    def on_show_all(menu)
+      unhideAllApplications(menu)
+    end
+    
+    def on_quit(menu)
+      terminate(menu)
+    end
+    
+    private
+    
+      def find_menu(menu, path)
+        key = path.keys.first
+        value = path.values.first
+        menu = menu[key]
+        if value.kind_of?(Array)
+          find_menu(menu, value.first)
+        else
+          menu[value]
+        end
+      end
+    
   end
   
 end
\ No newline at end of file

Modified: MacRuby/trunk/lib/hotcocoa/mappings/menu.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/menu.rb	2008-08-15 04:42:06 UTC (rev 445)
+++ MacRuby/trunk/lib/hotcocoa/mappings/menu.rb	2008-08-15 04:43:28 UTC (rev 446)
@@ -27,6 +27,7 @@
     
     def item(symbol, options={})
       options[:title] ||= titleize(symbol)
+      options[:action] ||= "on_#{symbol}:"
       item = builder.menu_item(options)
       item_map[symbol] = item
       addItem item
@@ -37,8 +38,9 @@
       addItem NSMenuItem.separatorItem
     end
     
-    def [](symbol)
-      item_map[symbol]
+    def [](*symbols)
+      symbol = symbols.shift
+      symbols.empty? ? item_map[symbol] : item_map[symbol][*symbols]
     end
     
     private

Modified: MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb	2008-08-15 04:42:06 UTC (rev 445)
+++ MacRuby/trunk/lib/hotcocoa/mappings/menu_item.rb	2008-08-15 04:43:28 UTC (rev 446)
@@ -14,7 +14,22 @@
   end
   
   custom_methods do
+
+    def on_action=(behavior)
+      object = Object.new
+      object.instance_variable_set("@behavior", behavior)
+      def object.perform_action(sender)
+        @behavior.call(sender)
+      end
+      setTarget(object)
+      setAction("perform_action:")
+    end
     
+    def on_action(&behavior)
+      self.on_action = behavior
+      self
+    end
+        
     def key
       keyEquivalent
     end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080814/7d4cbd7a/attachment.html 


More information about the macruby-changes mailing list