librelist archives

« back to archive

Creating own widgets

Creating own widgets

From:
Artyom Bolshakov
Date:
2011-01-31 @ 23:49
Hello,

I tried to create my own widget, but can’t to use it in some use cases:

class Shoes::Avatar < Shoes::Widget 
  def initialize path
    @path = path
  end
end

Shoes.app do
  button "Open file" do
    return unless FileTest::exists?(source_image_path = ask_open_file)
    avatar source_image_path # exception here
  end
  source_image_path = File.join('images', 'default.jpg') 
  avatar source_image_path # works fine here
end

This is exception:
Error in <unknown> line 0
	undefined method `avatar` for (Shoes::App "SHoes")
	test.rb:10:in `method_missing`
	test.rb:10:in `block (2 levels) in<main>`
	-e:in `call`

How can I get a widget inside button block?

----
        Artyom

Re: [shoes] Creating own widgets

From:
Cecil Coupe
Date:
2011-02-01 @ 00:22
I think 'source_image_path' is in two different scopes. Try using
@source_image_path (it's common practice in Shoes)

On Tue, 2011-02-01 at 02:49 +0300, Artyom Bolshakov wrote:
> Hello,
> 
> I tried to create my own widget, but can’t to use it in some use cases:
> 
> class Shoes::Avatar < Shoes::Widget 
>   def initialize path
>     @path = path
>   end
> end
> 
> Shoes.app do
>   button "Open file" do
>     return unless FileTest::exists?(source_image_path = ask_open_file)
>     avatar source_image_path # exception here
>   end
>   source_image_path = File.join('images', 'default.jpg') 
>   avatar source_image_path # works fine here
> end
> 
> This is exception:
> Error in <unknown> line 0
> 	undefined method `avatar` for (Shoes::App "SHoes")
> 	test.rb:10:in `method_missing`
> 	test.rb:10:in `block (2 levels) in<main>`
> 	-e:in `call`
> 
> How can I get a widget inside button block?
> 
> ----
>         Artyom

Re: [shoes] Creating own widgets

From:
Cecil Coupe
Date:
2011-02-01 @ 03:16
Artyom,

  I answered too quick.

The following version works for me. Be aware that Shoes Widgets are
buggy, incomplete and confusing. 

> class Avatar < Shoes::Widget 
>   def initialize path
>     $stderr.puts path
>     @path = path
>   end
> end
> 
> Shoes.app do
>   button "Open file" do
>     return unless FileTest::exists?(@source_image_path = ask_open_file)
>     Avatar.new( @source_image_path)  # exception here
>   end
>   @source_image_path = File.join('images', 'default.jpg') 
>   Avatar.new @source_image_path # works fine here
> end



>   source_image_path = File.join('images', 'default.jpg') 
>   avatar source_image_path # works fine here

The above call to avatar was not being executed - it only looked like it
was fine.

Re: [shoes] Creating own widgets

From:
Artyom Bolshakov
Date:
2011-02-01 @ 08:16
On Mon, 31 Jan 2011 20:16:26 -0700
Cecil Coupe <ccoupe@cableone.net> wrote:
And how to use Shoes stuff inside Avatar class?
E.g.
class Avatar < Shoes::Widget 
  def initialize path
    $stderr.puts path
    @path = path
    image @path 
  end
end
I get Segmentation fault at line 5: http://dumpz.org/30527/
> Artyom,
> 
>   I answered too quick.
> 
> The following version works for me. Be aware that Shoes Widgets are
> buggy, incomplete and confusing. 
> 
> > class Avatar < Shoes::Widget 
> >   def initialize path
> >     $stderr.puts path
> >     @path = path
> >   end
> > end
> > 
> > Shoes.app do
> >   button "Open file" do
> >     return unless FileTest::exists?(@source_image_path = ask_open_file)
> >     Avatar.new( @source_image_path)  # exception here
> >   end
> >   @source_image_path = File.join('images', 'default.jpg') 
> >   Avatar.new @source_image_path # works fine here
> > end
> 
> 
> 
> >   source_image_path = File.join('images', 'default.jpg') 
> >   avatar source_image_path # works fine here
> 
> The above call to avatar was not being executed - it only looked like it
> was fine.
> 
> 

Re: [shoes] Creating own widgets

From:
Steve Klabnik
Date:
2011-02-01 @ 13:45
Artoym,

I don't know if this helps you, but here are the custom widgets I use in
Hackety Hack:
https://github.com/hacketyhack/hacketyhack/blob/master/app/ui/widgets.rb

And here's an example of using one:
https://github.com/hacketyhack/hacketyhack/blob/master/app/ui/tabs/home.rb#L31

-Steve

Re: [shoes] Creating own widgets

From:
Cecil Coupe
Date:
2011-02-02 @ 02:26
Great examples Steve! If somehow, this information on how to create and
use Shoes::Widget found it's way into the wiki or even the manual(!) my
'mess' opinion would vanish.

> 
> I don't know if this helps you, but here are the custom widgets I use
> in Hackety
> Hack: https://github.com/hacketyhack/hacketyhack/blob/master/app/ui/widgets.rb
> 
> 
> And here's an example of using
> one: 
https://github.com/hacketyhack/hacketyhack/blob/master/app/ui/tabs/home.rb#L31
> 
> 
> -Steve

Re: [shoes] Creating own widgets

From:
ashbb
Date:
2011-02-01 @ 11:52
Hi Artyom,

Thank you for the post. I think this is a bug.

But sorry, I can't fix the bug for now. Need to hack Shoes C code in deep.

Try to run the following:

class Shoes::Avatar < Shoes::Widget
  def initialize path
    @path = path
    $app.image @path
  end
end

Shoes.app do
  $app = self
  button "Open file" do
    return unless FileTest::exists?(source_image_path = ask_open_file)
    Shoes::Avatar.new source_image_path
  end
  source_image_path = File.join('images', 'default.jpg')
  avatar source_image_path
end

This code replaced `avatar` with `Shoes::Avatar.new` within the button block

and added $app assigned value of self.

You will get an expected output. Not so cool, though. ;-)

ashbb

Re: [shoes] Creating own widgets

From:
Cecil Coupe
Date:
2011-02-01 @ 09:02
1. You were using Ruby 1.9.2p0 and my example used the specified 1.9.1.
It looks like you've found a bug with Shoes+1.9.2. I'm not surprised
because

2. Shoes Widgets is a mess. It was incorrect & insufficient in Shoes
Raisins/1.8.7 and has received no attention by those willing to dig into
the C underpinnings.  I've deleted my Shoes code that attempted to build
a Table Widget so I don't remember the details (other than the pain -
that I remember). In initialize, you create the Shoes gui things you
want and then somehow create/attach the procs for the events of the gui
bits which may or may not work like you think, if at all.

If you really need to write your own widget, Shoes will disappoint you
at every step. It wasn't created for that level of control --
Shoes::Widget was an after thought that has never worked (and when it
almost worked, the Shoes mess was worse than C/gtk or wxRuby)

--Cecil


On Tue, 2011-02-01 at 11:16 +0300, Artyom Bolshakov wrote:
> On Mon, 31 Jan 2011 20:16:26 -0700
> Cecil Coupe <ccoupe@cableone.net> wrote:
> And how to use Shoes stuff inside Avatar class?
> E.g.
> class Avatar < Shoes::Widget 
>   def initialize path
>     $stderr.puts path
>     @path = path
>     image @path 
>   end
> end
> I get Segmentation fault at line 5: http://dumpz.org/30527/
> > Artyom,
> > 
> >   I answered too quick.
> > 
> > The following version works for me. Be aware that Shoes Widgets are
> > buggy, incomplete and confusing. 
> > 
> > > class Avatar < Shoes::Widget 
> > >   def initialize path
> > >     $stderr.puts path
> > >     @path = path
> > >   end
> > > end
> > > 
> > > Shoes.app do
> > >   button "Open file" do
> > >     return unless FileTest::exists?(@source_image_path = ask_open_file)
> > >     Avatar.new( @source_image_path)  # exception here
> > >   end
> > >   @source_image_path = File.join('images', 'default.jpg') 
> > >   Avatar.new @source_image_path # works fine here
> > > end
> > 
> > 
> > 
> > >   source_image_path = File.join('images', 'default.jpg') 
> > >   avatar source_image_path # works fine here
> > 
> > The above call to avatar was not being executed - it only looked like it
> > was fine.
> > 
> > 

Re: [shoes] Creating own widgets

From:
ashbb
Date:
2011-02-01 @ 12:12
Hi Cecil,

I agree Shoes Widget is a mess. ;-)

But I think it's not the same as other gui toolkit widget.
Although using the same name 'widget'.

Shoes Widget class is just helpful for using Shoes' methods in the class
which is defined outside of Shoes.app block.

An old trivial note is here:

http://shoes-tutorial-note.heroku.com/html/00539_class_definition_outside_of_Shoes.app_block.html

I didn't confirm with Shoes 3, though. :-P

ashbb