Skip to content

Commit

Permalink
add multi form
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxblade committed Mar 17, 2015
1 parent ab1cbb6 commit 7f31a20
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 103 deletions.
7 changes: 4 additions & 3 deletions example/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -48,8 +48,9 @@
checkbox ['check me', 'and me'], checked:['check me']

end

end

ShenmeGUI.debug!
ShenmeGUI.enable_debug

ShenmeGUI.start!
1 change: 1 addition & 0 deletions lib/shenmegui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
require_relative 'shenmegui/core'
require_relative 'shenmegui/controls'
require_relative 'shenmegui/file_dialog'
require_relative 'shenmegui/utils'
3 changes: 1 addition & 2 deletions lib/shenmegui/controls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Form < Base
end

class Button < Base
property :text, :state
property :text
shortcut :text

end
Expand All @@ -128,7 +128,6 @@ class Textarea < Base

def <<(t)
text << "\n#{t}"
sync
end
end

Expand Down
129 changes: 46 additions & 83 deletions lib/shenmegui/core.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
40 changes: 40 additions & 0 deletions lib/shenmegui/utils.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 15 additions & 8 deletions static/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions static/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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%;
}

Expand Down Expand Up @@ -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;
}

0 comments on commit 7f31a20

Please sign in to comment.