Skip to content

Commit

Permalink
Merge pull request #339 from scarpe-team/clear_append_destroy
Browse files Browse the repository at this point in the history
Clear append destroy
  • Loading branch information
noahgibbs authored Jul 27, 2023
2 parents 24f9c5f + 578e95f commit 46424be
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
24 changes: 24 additions & 0 deletions examples/clear_and_append.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Shoes.app do
stack do
button("More Stuff!") do
@slot.append do
ins "I'm enthused."
title "Enthused!"
banner "ENTHUSED!"
end
end

button("No Stuff!") do
@slot.clear
end

button("Different Stuff!") do
@slot.clear do
para "I'm so different!"
title "I'm like a totally different but emo person! Or possibly chicken."
end
end

@slot = stack(width: "100%") { para 'Good Morning' }
end
end
4 changes: 2 additions & 2 deletions examples/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
def collapsed
@para = para(
"'Scarpe' means shoes in Italian. 'Scarpe' also means Shoes in...",
link("(show more)") { @para.destroy_self; @para = expanded }
link("(show more)") { @para.destroy; @para = expanded }
)
end

Expand All @@ -12,7 +12,7 @@ def expanded
"Scarpe isn't feature complete with any version of Shoes (yet?). We're initially targeting Shoes Classic. ",
link("Learn more", click: "http://github.com/schwad/scarpe"),
" ",
link("(show less)") { @para.destroy_self; @para = collapsed }
link("(show less)") { @para.destroy; @para = collapsed }
)
end

Expand Down
8 changes: 4 additions & 4 deletions lacci/lib/shoes/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def initialize(
resizable: true,
&app_code_body
)
log_init("Shoes::App")

if Shoes::App.instance
@log.error("Trying to create a second Shoes::App in the same process! Fail!")
raise "Cannot create multiple Shoes::App objects!"
else
Shoes::App.instance = self
end

log_init("Shoes::App")

@do_shutdown = false

super
Expand Down Expand Up @@ -144,7 +144,7 @@ def find_widgets_by(*specs)
specs.each do |spec|
if spec.is_a?(Class)
widgets.select! { |w| spec === w }
elsif spec.is_a?(Symbol)
elsif spec.is_a?(Symbol) || spec.is_a?(String)
s = spec.to_s
case s[0]
when "$"
Expand All @@ -156,7 +156,7 @@ def find_widgets_by(*specs)
raise "Error getting global variable: #{spec.inspect}"
end
when "@"
if app.instance_variables.include?(spec)
if Shoes::App.instance.instance_variables.include?(spec.to_sym)
widgets &= [self.instance_variable_get(spec)]
else
raise "Can't find top-level instance variable: #{spec.inspect}!"
Expand Down
14 changes: 13 additions & 1 deletion lacci/lib/shoes/widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,19 @@ def destroy
send_shoes_event(event_name: "destroy", target: linkable_id)
end

alias_method :destroy_self, :destroy
alias_method :remove, :destroy

def clear(&block)
@children.dup.each(&:destroy)
append(&block) if block_given?
end

def append(&block)
raise("append requires a block!") unless block_given?
raise("Don't append to something that isn't a slot!") unless self.is_a?(Shoes::Slot)

Shoes::App.instance.with_slot(self, &block)
end

# We use method_missing for widget-creating methods like "button",
# and also to auto-create display-property getters and setters.
Expand Down
37 changes: 37 additions & 0 deletions test/test_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,40 @@ def test_it_accepts_margin_hash
# assert(stack.to_html.include?("background:linear-gradient(45deg, #AAA, #DDD);"))
#end
end

class TestStackMethods < LoggedScarpeTest
def test_stack_clear_append
run_test_scarpe_code(<<-'SCARPE_APP', app_test_code: <<-'TEST_CODE')
Shoes.app do
@slot = stack do
para "Hello World"
end
@b_clear = button("Clear") { @slot.clear }
@b_add = button("Add") { @slot.append { para "Woot!" } }
end
SCARPE_APP
on_heartbeat do
main = stack("@slot")
assert_equal 1, main.children.size
b_clear = button("@b_clear")
b_add = button("@b_add")
b_clear_js = b_clear.display.handler_js_code("click")
b_add_js = b_add.display.handler_js_code("click")
query_js_value(b_add_js)
query_js_value(b_add_js)
query_js_value(b_add_js)
wait fully_updated
assert_equal 4, main.children.size
query_js_value(b_clear_js) # Run the click-event code
wait fully_updated
assert_equal 0, main.children.size
test_finished
end
TEST_CODE
end
end

0 comments on commit 46424be

Please sign in to comment.