From abe725f50c1929280077b515f87cdfd6d58338bb Mon Sep 17 00:00:00 2001 From: Michal Jirku Date: Fri, 26 Jul 2013 22:56:21 +0200 Subject: [PATCH 1/6] Add constraint options, with string/symbol shortcuts --- lib/motion-layout/layout.rb | 41 +++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index d8a2bc1..c64c6be 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -21,16 +21,41 @@ def view(view) @view = view end - def horizontal(horizontal) - @horizontals << horizontal + def horizontal(horizontal, *options) + options = [:centery] if options.empty? + @horizontals << [horizontal, resolve_options(options)] end - def vertical(vertical) - @verticals << vertical + def vertical(vertical, *options) + options = [:centerx] if options.empty? + @verticals << [vertical, resolve_options(options)] end private + def resolve_opts(opt) + opt_hash = { + left: NSLayoutFormatAlignAllLeft, + right: NSLayoutFormatAlignAllRight, + top: NSLayoutFormatAlignAllTop, + bottom: NSLayoutFormatAlignAllBottom, + leading: NSLayoutFormatAlignAllLeading, + trailing: NSLayoutFormatAlignAllTrailing, + centerx: NSLayoutFormatAlignAllCenterX, + centery: NSLayoutFormatAlignAllCenterY, + baseline: NSLayoutFormatAlignAllBaseline, + } + opt.inject(0) do |m,x| + if x.kind_of?(Numeric) + m | x.to_i + elsif o = opt_hash[x.to_s.downcase.to_sym] + m | o + else + raise "invalid opt: #{x.to_s.downcase}" + end + end + end + def strain @subviews.values.each do |subview| subview.translatesAutoresizingMaskIntoConstraints = false @@ -38,11 +63,11 @@ def strain end constraints = [] - constraints += @verticals.map do |vertical| - NSLayoutConstraint.constraintsWithVisualFormat("V:#{vertical}", options:NSLayoutFormatAlignAllCenterX, metrics:@metrics, views:@subviews) + constraints += @verticals.map do |vertical, options| + NSLayoutConstraint.constraintsWithVisualFormat("V:#{vertical}", options:options, metrics:@metrics, views:@subviews) end - constraints += @horizontals.map do |horizontal| - NSLayoutConstraint.constraintsWithVisualFormat("H:#{horizontal}", options:NSLayoutFormatAlignAllCenterY, metrics:@metrics, views:@subviews) + constraints += @horizontals.map do |horizontal, options| + NSLayoutConstraint.constraintsWithVisualFormat("H:#{horizontal}", options:options, metrics:@metrics, views:@subviews) end @view.addConstraints(constraints.flatten) From 8f0fdecab96929af0a424636d58a8a51e441a62d Mon Sep 17 00:00:00 2001 From: Michal Jirku Date: Fri, 26 Jul 2013 23:02:32 +0200 Subject: [PATCH 2/6] Fix typo --- lib/motion-layout/layout.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index c64c6be..b61c2d1 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -33,7 +33,7 @@ def vertical(vertical, *options) private - def resolve_opts(opt) + def resolve_options(opt) opt_hash = { left: NSLayoutFormatAlignAllLeft, right: NSLayoutFormatAlignAllRight, From 5bddcbc938502a4fcaaee30a26cfbf31a56d1651 Mon Sep 17 00:00:00 2001 From: Michal Jirku Date: Sat, 27 Jul 2013 11:07:38 +0200 Subject: [PATCH 3/6] Fix strain when @view == subview (subview centering FTW) --- lib/motion-layout/layout.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index b61c2d1..4e163b7 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -53,11 +53,12 @@ def resolve_options(opt) else raise "invalid opt: #{x.to_s.downcase}" end - end + end end def strain @subviews.values.each do |subview| + next if @view == subview # you wouldn't believe the mess it would create subview.translatesAutoresizingMaskIntoConstraints = false @view.addSubview(subview) end From 777ff914b94b89da28f0f58a6a2e7b5005d2fd74 Mon Sep 17 00:00:00 2001 From: Francis Chong Date: Thu, 12 Dec 2013 23:24:48 +0800 Subject: [PATCH 4/6] Add all options to Layout - add format direction - extract options into constant. --- lib/motion-layout/layout.rb | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index 4e163b7..749c898 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -1,5 +1,19 @@ module Motion class Layout + OPTIONS = { + left: NSLayoutFormatAlignAllLeft, + right: NSLayoutFormatAlignAllRight, + top: NSLayoutFormatAlignAllTop, + bottom: NSLayoutFormatAlignAllBottom, + leading: NSLayoutFormatAlignAllLeading, + trailing: NSLayoutFormatAlignAllTrailing, + centerx: NSLayoutFormatAlignAllCenterX, + centery: NSLayoutFormatAlignAllCenterY, + baseline: NSLayoutFormatAlignAllBaseline, + leading_to_trailing: NSLayoutFormatDirectionLeadingToTrailing, + left_to_right: NSLayoutFormatDirectionLeftToRight, + right_to_left: NSLayoutFormatDirectionRightToLeft + } def initialize(&block) @verticals = [] @horizontals = [] @@ -34,21 +48,10 @@ def vertical(vertical, *options) private def resolve_options(opt) - opt_hash = { - left: NSLayoutFormatAlignAllLeft, - right: NSLayoutFormatAlignAllRight, - top: NSLayoutFormatAlignAllTop, - bottom: NSLayoutFormatAlignAllBottom, - leading: NSLayoutFormatAlignAllLeading, - trailing: NSLayoutFormatAlignAllTrailing, - centerx: NSLayoutFormatAlignAllCenterX, - centery: NSLayoutFormatAlignAllCenterY, - baseline: NSLayoutFormatAlignAllBaseline, - } opt.inject(0) do |m,x| if x.kind_of?(Numeric) m | x.to_i - elsif o = opt_hash[x.to_s.downcase.to_sym] + elsif o = OPTIONS[x.to_s.downcase.to_sym] m | o else raise "invalid opt: #{x.to_s.downcase}" From 535127cb04851887ef6cd6d882c483016f0eeb24 Mon Sep 17 00:00:00 2001 From: Francis Chong Date: Thu, 12 Dec 2013 23:25:14 +0800 Subject: [PATCH 5/6] add document and example, update project to modern RubyMotion. --- README.md | 11 +++++++++++ example/Rakefile | 2 +- example/app/timer_controller.rb | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d824598..26d4d1a 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,17 @@ Motion::Layout.new do |layout| layout.horizontal "|-margin-[action]-margin-|" end ``` +### Layout Options + +Check Motion::Layout::OPTIONS for available options. The default layout options is 0, which is same as [NSLayoutConstraint constraintsWithVisualFormat:options:mertics:views:]. Check [document](https://developer.apple.com/library/ios/DOCUMENTATION/AppKit/Reference/NSLayoutConstraint_Class/NSLayoutConstraint/NSLayoutConstraint.html#//apple_ref/occ/clm/NSLayoutConstraint/constraintsWithVisualFormat:options:metrics:views:) for meaning of each options. + +``` ruby +Motion::Layout.new do |layout| + ... + layout.vertical "[version]-|" + layout.horizontal "[version]-5-[version_number]-|", :baseline +end +``` ## TODO diff --git a/example/Rakefile b/example/Rakefile index 129a145..914dbe6 100644 --- a/example/Rakefile +++ b/example/Rakefile @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- $:.unshift("/Library/RubyMotion/lib") -require 'motion/project' +require 'motion/project/template/ios' require 'bundler' Bundler.setup diff --git a/example/app/timer_controller.rb b/example/app/timer_controller.rb index 3282c14..bf67f34 100644 --- a/example/app/timer_controller.rb +++ b/example/app/timer_controller.rb @@ -37,13 +37,29 @@ def viewDidLoad @action.setTitle('Stop', forState:UIControlStateSelected) @action.addTarget(self, action:'actionTapped', forControlEvents:UIControlEventTouchUpInside) + @version = UILabel.new + @version.font = UIFont.systemFontOfSize(10) + @version.text = 'version' + @version.textAlignment = UITextAlignmentRight + @version.textColor = UIColor.whiteColor + @version.backgroundColor = UIColor.clearColor + + @versionNumber = UILabel.new + @versionNumber.font = UIFont.systemFontOfSize(19) + @versionNumber.text = '0.1.0' + @versionNumber.textAlignment = UITextAlignmentRight + @versionNumber.textColor = UIColor.whiteColor + @versionNumber.backgroundColor = UIColor.clearColor + Motion::Layout.new do |layout| layout.view view - layout.subviews "state" => @state, "action" => @action + layout.subviews "state" => @state, "action" => @action, "version" => @version, "version_number" => @versionNumber layout.metrics "top" => 200, "margin" => 20, "height" => 40 layout.vertical "|-top-[state(==height)]-margin-[action(==height)]" + layout.vertical "[version]|" layout.horizontal "|-margin-[state]-margin-|" layout.horizontal "|-margin-[action]-margin-|" + layout.horizontal "[version]-5-[version_number]|", :baseline end end From 035ae9ba8dab2c0c664611dee37d72e337a8f551 Mon Sep 17 00:00:00 2001 From: Francis Chong Date: Mon, 16 Dec 2013 14:19:33 +0800 Subject: [PATCH 6/6] default options to none --- lib/motion-layout/layout.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/motion-layout/layout.rb b/lib/motion-layout/layout.rb index bfea794..c61f112 100644 --- a/lib/motion-layout/layout.rb +++ b/lib/motion-layout/layout.rb @@ -1,6 +1,7 @@ module Motion class Layout OPTIONS = { + none: 0, left: NSLayoutFormatAlignAllLeft, right: NSLayoutFormatAlignAllRight, top: NSLayoutFormatAlignAllTop, @@ -36,12 +37,12 @@ def view(view) end def horizontal(horizontal, *options) - options = [:centery] if options.empty? + options = [:none] if options.empty? @horizontals << [horizontal, resolve_options(options)] end def vertical(vertical, *options) - options = [:centerx] if options.empty? + options = [:none] if options.empty? @verticals << [vertical, resolve_options(options)] end