From 7f31a2001102ceb8cf7514d7cde1978a836337b2 Mon Sep 17 00:00:00 2001 From: Cichol Date: Tue, 17 Mar 2015 23:58:44 +0800 Subject: [PATCH] add multi form --- example/app.rb | 7 ++- lib/shenmegui.rb | 1 + lib/shenmegui/controls.rb | 3 +- lib/shenmegui/core.rb | 129 ++++++++++++++------------------------ lib/shenmegui/utils.rb | 40 ++++++++++++ static/style.css | 23 ++++--- static/style.scss | 22 ++++--- 7 files changed, 122 insertions(+), 103 deletions(-) create mode 100644 lib/shenmegui/utils.rb diff --git a/example/app.rb b/example/app.rb index 65a804c..d88e981 100644 --- a/example/app.rb +++ b/example/app.rb @@ -2,7 +2,7 @@ ShenmeGUI.app do form title: 'test' do - button('open file').onclick { @text.text = ShenmeGUI::FileDialog.get_open_file_name; @i.src="file:///#{@text.text}" } + button('open file').onclick { @text.text = get_open_file_name; @i.src=@text.text} @sel = select %w{1 2 3} radio %w{option1 option2 option3}, arrange: 'horizontal' @@ -14,7 +14,7 @@ @b = button 'button1' @b.onclick do @b.text = "clicked" - @text << " ok" + @text << "ok" @t.text[0] = '1' @sel.options.pop end @@ -48,8 +48,9 @@ checkbox ['check me', 'and me'], checked:['check me'] end + end -ShenmeGUI.debug! +ShenmeGUI.enable_debug ShenmeGUI.start! diff --git a/lib/shenmegui.rb b/lib/shenmegui.rb index 49e488a..6d91f69 100644 --- a/lib/shenmegui.rb +++ b/lib/shenmegui.rb @@ -5,3 +5,4 @@ require_relative 'shenmegui/core' require_relative 'shenmegui/controls' require_relative 'shenmegui/file_dialog' +require_relative 'shenmegui/utils' diff --git a/lib/shenmegui/controls.rb b/lib/shenmegui/controls.rb index c628425..d9c263c 100644 --- a/lib/shenmegui/controls.rb +++ b/lib/shenmegui/controls.rb @@ -111,7 +111,7 @@ class Form < Base end class Button < Base - property :text, :state + property :text shortcut :text end @@ -128,7 +128,6 @@ class Textarea < Base def <<(t) text << "\n#{t}" - sync end end diff --git a/lib/shenmegui/core.rb b/lib/shenmegui/core.rb index 66d5312..5572e70 100644 --- a/lib/shenmegui/core.rb +++ b/lib/shenmegui/core.rb @@ -1,43 +1,7 @@ module ShenmeGUI - class HookedArray < Array - - @unhook_methods = %i{<< []= clear collect! compact! concat delete delete_at delete_if fill flatten! replace insert keep_if map map! pop push reject! replace rotate! select! shift shuffle! slice! sort! sort_by! uniq! unshift} - @unhook_methods = Hash[@unhook_methods.collect{|x| [x, Array.instance_method(x)]}] - - def initialize(arr, owner) - @owner = owner - super(arr) - end - - @unhook_methods.each do |k, v| - define_method(k) do |*arr, &block| - result = v.bind(self).call(*arr, &block) - @owner.sync - result - end - end - - end - - class HookedString < String - @unhook_methods = %i{<< []= capitalize! chomp! chop! clear concat delete! downcase! encode! force_encoding gsub! insert lstrip! succ! next! prepend replace reverse! rstrip! slice! squeeze! strip! sub! swapcase! tr! tr_s! upcase!} - @unhook_methods = Hash[@unhook_methods.collect{|x| [x, String.instance_method(x)]}] - - def initialize(str, owner) - @owner = owner - super(str) - end - - @unhook_methods.each do |k, v| - define_method(k) do |*arr, &block| - result = v.bind(self).call(*arr, &block) - @owner.sync - result - end - end - - end + @elements = [] + @temp_stack = [] class << self attr_accessor :socket @@ -73,61 +37,60 @@ def alert(msg) @socket.send("alert:0->#{data.to_json}") end -# def data_url(path) -# extension = path.match(/\.(.+)$/)[1] -# file = File.open(path, 'r'){|f| f.read} -# "data:image/#{extension.downcase};base64,#{Base64.encode64(file)}" -# end - end - - @elements = [] - @temp_stack = [] + def get_open_file_name(params={}) + FileDialog.get_open_file_name(params) + end - def self.debug! - Thread.new do - ShenmeGUI.instance_eval do - bind = binding - while true - begin - command = $stdin.gets.chomp - result = eval command, bind - puts "=> #{result}" - rescue - puts "#{$!}" + def get_save_file_name(params={}) + FileDialog.get_save_file_name(params) + end + + def enable_debug + Thread.new do + ShenmeGUI.instance_eval do + bind = binding + while true + begin + command = $stdin.gets.chomp + result = eval command, bind + puts "=> #{result}" + rescue + puts "#{$!}" + end end end - end + end end - end - - def self.start! - ws_thread = Thread.new do - EM.run do - EM::WebSocket.run(:host => "0.0.0.0", :port => 80) do |ws| - ws.onopen do - puts "WebSocket connection open" - @elements.each { |e| e.add_events; e.sync } - end - - ws.onclose { puts "Connection closed" } - ws.onmessage do |msg| - puts "Recieved message: #{msg}" - handle msg + def start! + ws_thread = Thread.new do + EM.run do + EM::WebSocket.run(:host => "0.0.0.0", :port => 80) do |ws| + ws.onopen do + puts "WebSocket connection open" + @elements.each { |e| e.add_events; e.sync } + end + + ws.onclose { puts "Connection closed" } + + ws.onmessage do |msg| + puts "Recieved message: #{msg}" + handle msg + end + + @socket = ws end - - @socket = ws end end - end - index_path = "#{Dir.pwd}/index.html" - `start file:///#{index_path}` + index_path = "#{Dir.pwd}/index.html" + `start file:///#{index_path}` - ws_thread.join - rescue Interrupt - puts 'bye~' - end + ws_thread.join + rescue Interrupt + puts 'bye~' + end + end end \ No newline at end of file diff --git a/lib/shenmegui/utils.rb b/lib/shenmegui/utils.rb new file mode 100644 index 0000000..aca1ebc --- /dev/null +++ b/lib/shenmegui/utils.rb @@ -0,0 +1,40 @@ +module ShenmeGUI + class HookedArray < Array + + @unhook_methods = %i{<< []= clear collect! compact! concat delete delete_at delete_if fill flatten! replace insert keep_if map map! pop push reject! replace rotate! select! shift shuffle! slice! sort! sort_by! uniq! unshift} + @unhook_methods = Hash[@unhook_methods.collect{|x| [x, Array.instance_method(x)]}] + + def initialize(arr, owner) + @owner = owner + super(arr) + end + + @unhook_methods.each do |k, v| + define_method(k) do |*arr, &block| + result = v.bind(self).call(*arr, &block) + @owner.sync + result + end + end + + end + + class HookedString < String + @unhook_methods = %i{<< []= capitalize! chomp! chop! clear concat delete! downcase! encode! force_encoding gsub! insert lstrip! succ! next! prepend replace reverse! rstrip! slice! squeeze! strip! sub! swapcase! tr! tr_s! upcase!} + @unhook_methods = Hash[@unhook_methods.collect{|x| [x, String.instance_method(x)]}] + + def initialize(str, owner) + @owner = owner + super(str) + end + + @unhook_methods.each do |k, v| + define_method(k) do |*arr, &block| + result = v.bind(self).call(*arr, &block) + @owner.sync + result + end + end + + end +end \ No newline at end of file diff --git a/static/style.css b/static/style.css index d9957c2..3ab783e 100644 --- a/static/style.css +++ b/static/style.css @@ -16,7 +16,10 @@ body { border-bottom-color: #808080; box-shadow: 1px 1px 0 0 #000; width: 400px; - cursor: default; } + cursor: default; + float: left; + margin-right: 30px; + margin-bottom: 20px; } .window .content { padding: 7px 10px; } .window .title { @@ -34,13 +37,21 @@ body { float: left; margin-right: 2px; } +.button, .checkbox, .image, .progress, .radio, .select, .textarea, .textline, .label { + display: inline-block; + margin-bottom: 0.5em; + margin-right: 0.2em; + max-width: 100%; } + .stack > * { - display: block !important; } + display: block !important; + max-width: 100%; } .flow > * { - display: inline !important; } + display: inline-block !important; + max-width: 100%; } -.image, .progress { +.progress { width: 100%; } button { @@ -124,10 +135,6 @@ input[type="radio"]:checked + label:before { .option { margin-right: 1em; } -.button, .checkbox, .image, .progress, .radio, .select, .textarea, .textline, .label { - margin-bottom: 0.5em; - margin-right: 0.2em; } - .label { font-weight: bolder; } diff --git a/static/style.scss b/static/style.scss index 1efbfd9..6cabdc9 100644 --- a/static/style.scss +++ b/static/style.scss @@ -18,6 +18,10 @@ body { box-shadow: 1px 1px 0 0 #000; width: 400px; cursor: default; + //multi-form + float: left; + margin-right: 30px; + margin-bottom: 20px; .content { padding: 7px 10px; @@ -42,15 +46,24 @@ body { } } +.button, .checkbox, .image, .progress, .radio, .select, .textarea, .textline, .label { + display: inline-block; + margin-bottom: 0.5em; + margin-right: 0.2em; + max-width: 100%; +} + .stack>* { display: block !important; + max-width: 100%; } .flow>* { - display: inline !important; + display: inline-block !important; + max-width: 100%; } -.image, .progress { +.progress { width: 100%; } @@ -148,11 +161,6 @@ input[type="radio"]:checked + label:before { margin-right: 1em; } -.button, .checkbox, .image, .progress, .radio, .select, .textarea, .textline, .label { - margin-bottom: 0.5em; - margin-right: 0.2em; -} - .label { font-weight: bolder; } \ No newline at end of file