--- deliantra/server/ext/widget.ext 2007/08/17 21:18:01 1.17 +++ deliantra/server/ext/widget.ext 2007/12/27 18:35:48 1.20 @@ -5,13 +5,13 @@ # ws_n ws # widgetset new # ws_d ws # widgetset destroy # ws_c ws id class @args # widgetset create +# ws_ct ws templateface \%cfg # widgetset create from template # w_c id rid name @args # widget method call # w_s id @attr # widget member set # w_g id rid @attr # widget member get # # and expects the following exti message types -# w_r rid res # widget call return -# w_e id rid @args # widget_event +# w_e id @args # widget_call our $DEBUG = 1; @@ -22,9 +22,8 @@ Scalar::Util::weaken (my $weakns = $ns); $ns->{id} = "a"; - $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { - # cannot deserialise ATM - undef + $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { + $weakns->{widget}{$_[0]} }); }, ); @@ -42,10 +41,22 @@ my $ws = $ns->{csc} = $ns->new_widgetset; - $ws->{tab} = $ws->new (Label => text => "dumb tst", c_tab => ["hull"]); + my ($tl, $entry) = $ws->template (undef, + toplevel => {}, + entry => { + text => "xyz", + on_changed => sub { + warn "changed<@_>\n";#d# + }, + }, + ); + + $tl->show; + + $ns->{xxxw} = [$tl, $entry];#d# - $ws->find ("setup_notebook")->add ($ws->{tab}); - $ws->find ("setup_dialog")->toggle_visibility; +# $ws->find ("setup_notebook")->add ($ws->{tab}); +# $ws->find ("setup_dialog")->toggle_visibility; } sub csc_start { @@ -113,22 +124,10 @@ ); cf::register_exticmd w_e => sub { - my ($ns, $id, $rid, @args) = @_; - - if (my $w = $ns->{widget}{$id}) { - if (my $cb = $w->{ev}{$rid}) { - $cb->($w, @args); - } - } - - () -}; + my ($ns, $id, @args) = @_; -cf::register_exticmd w_r => sub { - my ($ns, $rid, $res) = @_; - - if (my $cb = delete $ns->{widget_return}{$rid}) { - $cb->(@$res); + if (my $cb = $ns->{widget_cb}{$id}) { + $cb->(@args); } () @@ -150,6 +149,15 @@ $ws } +sub cf::client::alloc_wid { + pop @{ $_[0]{ids} } + or ++$_[0]{id} +} + +sub cf::client::free_wid { + push @{ $_[0]{ids} }, $_[1]; +} + ############################################################################# package ext::widget::set; @@ -179,7 +187,7 @@ sub alloc { my ($self) = @_; - my $id = ++$self->{ns}{id}; + my $id = $self->{ns}->alloc_wid; my $proxy = bless { id => $id, @@ -200,9 +208,7 @@ Scalar::Util::weaken ($proxy->{ws} = $self); for my $ev (grep /^on_/, keys %args) { - my $rid = ++$self->{ns}{id}; - $proxy->{ev}{$rid} = $args{$ev}; - $args{$ev} = $rid; + $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); } $self->msg (ws_c => @@ -215,6 +221,41 @@ $proxy } +sub template { + my ($self, $template, @args) = @_; + + my %cfg; + my @res; + + while (@args) { + my ($name, $args) = splice @args, 0, 2, (); + + my $proxy = $self->alloc; + + Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); + Scalar::Util::weaken ($proxy->{ws} = $self); + + for my $ev (grep /^on_/, keys %$args) { + $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); + } + + $cfg{$name} = { + %$args, + id => $proxy->{id}, + }; + + push @res, $proxy; + } + + $self->msg (ws_ct => + $self->{id}, + $template, + \%cfg, + ); + + @res +} + sub find { my ($self, @names) = @_; @@ -252,6 +293,35 @@ } } +sub cb { + my ($self, $cb) = @_; + + my $proxy = bless { + ns => $self->{ns}, + id => $self->{ns}->alloc_wid, + }, "ext::widget::callback"; + + Scalar::Util::weaken $proxy->{ns}; + + $self->{ns}{widget_cb}{$proxy->{id}} = $cb; + + $proxy +} + +sub oneshot_cb { + my ($self, $cb) = @_; + + if ("CODE" eq ref $cb) { + my $ocb = $cb; + $cb = $self->cb (sub { + undef $cb; + &$ocb + }); + } + + $cb +} + sub msg { my ($self, $type, @arg) = @_; @@ -266,21 +336,29 @@ my ($self, $cb, $type, @arg) = @_; if (my $ws = $self->{ws}) { - my $rid = ++$ws->{ns}{id}; - - $self->msg ($type, $rid, @arg); + my $rid = $ws->{ns}->alloc_wid; if ($cb) { - $ws->{ns}{widget_return}{$rid} = $cb; + $ws->{ns}{widget_cb}{$rid} = sub { + delete $ws->{ns}{widget_cb}{$rid}; + $ws->{ns}->free_wid ($rid); + &$cb + }; + + $self->msg ($type, $rid, @arg); } else { # synchronous case my $wait = new Coro::Signal; my @res; - $ws->{ns}{widget_return}{$rid} = sub { + $ws->{ns}{widget_cb}{$rid} = sub { + delete $ws->{ns}{widget_cb}{$rid}; + $ws->{ns}->free_wid ($rid); + @res = @_; $wait->send; }; + $self->msg ($type, $rid, @arg); $wait->wait; return @res; @@ -299,11 +377,11 @@ sub get { my ($self, $member, $cb) = @_; - $self->msg_cb ($cb, w_g => [$member]); + $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); } sub TO_JSON { - { __w_ => $_[0]{id} } + { "\fw" => $_[0]{id} } } our $AUTOLOAD; @@ -320,3 +398,18 @@ () } +package ext::widget::callback; + +sub DESTROY { + my ($self) = @_; + + if (my $ns = $self->{ns}) { + delete $ns->{widget_cb}{$self->{id}}; + $ns->free_wid ($self->{id}); + } +} + +sub TO_JSON { + { "\fc" => $_[0]{id} } +} +