[macruby-changes] [1551] MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib

source_changes at macosforge.org source_changes at macosforge.org
Thu May 7 14:52:44 PDT 2009


Revision: 1551
          http://trac.macosforge.org/projects/ruby/changeset/1551
Author:   rich at infoether.com
Date:     2009-05-07 14:52:44 -0700 (Thu, 07 May 2009)
Log Message:
-----------
got it working!

Modified Paths:
--------------
    MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/application.rb
    MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/mvc.rb

Modified: MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/application.rb
===================================================================
--- MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/application.rb	2009-05-07 20:11:15 UTC (rev 1550)
+++ MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/application.rb	2009-05-07 21:52:44 UTC (rev 1551)
@@ -5,50 +5,56 @@
   
 end
 
-
 class ApplicationController < HotCocoaApplicationController
-  #def switch_views
-  #  main_window.view = my_other_view
-  #end
+  def switch_views(sender)
+    application_window.view.remove(application_view)
+    application_window.view << my_other_view
+  end
 end
 
-class ApplicationWindow < HotCocoaWindow
+class ApplicationView < HotCocoaView
+  def render 
+    self << HotCocoa.button(:title => "View 1!", :on_action => controller.method(:switch_views))
+  end
+end
+
+class MyController < HotCocoaController
+  def switch_back(sender)
+    application_window.view.remove(my_other_view)
+    application_window.view << application_view
+  end
+
+  def open_window(sender)
+    HotCocoa.window(:center => true) << third_view
+  end
   
 end
 
+class MyOtherView < HotCocoaView
 
-class ApplicationView < HotCocoaView
-  
-  controller :application_controller
-  
-  options :layout => {:expand => [:width, :height]}
-  
+  controller :my_controller
+
   def render 
-    self << my_button
+    self << HotCocoa.button(:title => "View 2!", :on_action => controller.method(:switch_back), :frame => [0, 100, 200, 20])
+    self << HotCocoa.button(:title => "open window!", :on_action => controller.method(:open_window))
   end
 
-  def my_button
-    @my_button ||= button(:title => "Switch")#, :on_action => controller.method(:switch_views))
+end
+
+class ThirdController < HotCocoaController
+  def close_window(sender)
+    third_view.window.close
   end
+end
 
+class ThirdView < HotCocoaView
+
+  controller :third_controller
+
+  def render 
+    self << HotCocoa.button(:title => "close window!", :on_action => controller.method(:close_window))
+  end
+
 end
 
-# class MyController < HotCocoaController
-#   def switch_back
-#     window.view = application_view
-#   end
-# end
-# 
-# class MyOtherView < HotCocoaView
-#   controller :my_controller
-# 
-#   def render 
-#     view << my_button
-#   end
-# 
-#   def my_button
-#     @my_button ||= button(:title => "Switch", :on_action => :switch_back)
-#   end
-# end
-
 Application.new.start
\ No newline at end of file

Modified: MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/mvc.rb
===================================================================
--- MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/mvc.rb	2009-05-07 20:11:15 UTC (rev 1550)
+++ MacRuby/trunk/sample-macruby/HotCocoa/mvc/lib/mvc.rb	2009-05-07 21:52:44 UTC (rev 1551)
@@ -29,10 +29,10 @@
   end
   
   def initialize
+    HotCocoaApplication.instance = self
     @controllers = {}
     @shared_application = application
     @application_controller = controller(:application_controller)
-    HotCocoaApplication.instance = self
     shared_application.delegate = application_controller
   end
   
@@ -42,9 +42,18 @@
   
   def controller(controller_name)
     controller_class = Object.const_get(controller_name.to_s.camel_case)
-    @controllers[controller_name] ||= controller_class.new(self)
+    @controllers[controller_name] || create_controller_instance(controller_name, controller_class)
   end
   
+  private
+  
+    def create_controller_instance(controller_name, controller_class)
+      controller_instance = controller_class.new(self)
+      @controllers[controller_name] = controller_instance
+      controller_instance.application_window
+      controller_instance
+    end
+  
 end
 
 class HotCocoaController
@@ -59,8 +68,8 @@
     @application = application
   end
   
-  def main_window
-    @application.application_controller.main_window
+  def application_window
+    @application.application_controller.application_window
   end
 
 end
@@ -69,11 +78,10 @@
   
   def initialize(application)
     super(application)
-    @main_window = ApplicationWindow.new(self)
   end
   
-  def main_window
-    @main_window
+  def application_window
+    @application_window ||= ApplicationWindow.new(self).application_window
   end
   
   # help menu item
@@ -96,17 +104,16 @@
 
 class HotCocoaWindow
   
-  attr_reader :application_controller, :main_window
+  attr_reader :application_controller, :application_window
   
-  include HotCocoa
-  
   def initialize(application_controller)
     @application_controller = application_controller
     render
   end
   
   def render
-    @main_window = window(:title => title, :view => application_controller.application_view)
+    @application_window = HotCocoa.window(:title => title)
+    @application_window.view << application_controller.application_view
   end
   
   def title
@@ -117,6 +124,8 @@
 
 class HotCocoaView < NSView
   
+  DefaultLayoutOptions = {:expand => [:width, :height]}
+  
   module ClassMethods
     def controller(name=nil)
       if name
@@ -137,7 +146,7 @@
     HotCocoaController.class_eval %{
       def #{class_name}
         view = HotCocoaController.view_instances[:#{class_name}] ||= #{klass.name}.alloc.initWithFrame([0,0,0,0])
-        puts view.inspect
+        view.setup_view
         view
       end
     }, __FILE__, __LINE__
@@ -145,11 +154,29 @@
   
   attr_reader :controller
 
-  def initialize
-    @controller = HotCocoaApplication.instance.controller(self.class.controller)
+  def setup_view
+    @controller = class_controller
+    self.layout = layout_options
     render
   end
   
+  private
+  
+    def class_controller
+      HotCocoaApplication.instance.controller(self.class.controller)
+    end
+  
+    def layout_options
+      options = if self.class.options && self.class.options[:layout]
+        self.class.options[:layout]
+      else
+        DefaultLayoutOptions
+      end
+    end
+
 end
 
+class ApplicationWindow < HotCocoaWindow
+  
+end
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090507/4c35423f/attachment-0001.html>


More information about the macruby-changes mailing list