… | |
… | |
3 | # sends the following ext message types |
3 | # sends the following ext message types |
4 | # ws_n id # widgetset new |
4 | # ws_n id # widgetset new |
5 | # ws_d id # widgetset destroy |
5 | # ws_d id # widgetset destroy |
6 | # ws_c ws id class args # widgetset create |
6 | # ws_c ws id class args # widgetset create |
7 | # w_c id [rid] name args # widget method call |
7 | # w_c id [rid] name args # widget method call |
8 | # w_s id name value # widget member set |
8 | # w_s id @attr # widget member set |
9 | # w_g id rid name # widget member get |
9 | # w_g id rid @attr # widget member get |
10 | # |
10 | # |
11 | # and expects the following exti message types |
11 | # and expects the following exti message types |
12 | # w_r rid res # widget call return |
12 | # w_r rid res # widget call return |
13 | # w_e id name args # widget_event |
13 | # w_e id name args # widget_event |
|
|
14 | |
|
|
15 | our $DEBUG = 1; |
14 | |
16 | |
15 | cf::client->attach ( |
17 | cf::client->attach ( |
16 | on_connect => sub { |
18 | on_connect => sub { |
17 | my ($ns) = @_; |
19 | my ($ns) = @_; |
18 | |
20 | |
… | |
… | |
66 | [5, "Pow"], |
68 | [5, "Pow"], |
67 | [6, "Cha"], |
69 | [6, "Cha"], |
68 | ) { |
70 | ) { |
69 | my ($x, $label) = @$_; |
71 | my ($x, $label) = @$_; |
70 | |
72 | |
71 | $statstable->add ($x, 0, $ws->new (Label => |
73 | $statstable->add_at ($x, 0, $ws->new (Label => |
72 | can_hover => 1, can_events => 1, |
74 | can_hover => 1, can_events => 1, |
73 | align => +1, text => $label, tooltip => "#stat_$label", |
75 | align => +1, text => $label, tooltip => "#stat_$label", |
74 | )); |
76 | )); |
75 | $statstable->add ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
77 | $statstable->add_at ($x, 1, $ws->{stat}{$label} = $ws->new (Label => |
76 | can_hover => 1, can_events => 1, |
78 | can_hover => 1, can_events => 1, |
77 | align => +1, template => "88", tooltip => "#stat_$label", |
79 | align => +1, template => "88", tooltip => "#stat_$label", |
78 | )); |
80 | )); |
79 | } |
81 | } |
80 | |
82 | |
… | |
… | |
90 | return unless $cf::CFG{devel}; |
92 | return unless $cf::CFG{devel}; |
91 | |
93 | |
92 | my $ns = $pl->ns; |
94 | my $ns = $pl->ns; |
93 | |
95 | |
94 | return unless $ns->{can_widget}; |
96 | return unless $ns->{can_widget}; |
95 | |
|
|
96 | csc_start $ns; |
97 | #csc_start $ns; |
97 | }, |
98 | }, |
98 | ); |
99 | ); |
99 | |
100 | |
100 | cf::register_exticmd w_e => sub { |
101 | cf::register_exticmd w_e => sub { |
101 | my ($ns, $pkt) = @_; |
102 | my ($ns, $pkt) = @_; |
… | |
… | |
126 | my $id = ++$self->{id}; |
127 | my $id = ++$self->{id}; |
127 | |
128 | |
128 | my $ws = bless { |
129 | my $ws = bless { |
129 | id => $id, |
130 | id => $id, |
130 | ns => $self, |
131 | ns => $self, |
131 | w => {}, |
132 | _w => {}, |
132 | }, "ext::widget::set"; |
133 | }, "ext::widget::set"; |
133 | |
134 | |
134 | $ws->msg (ws_n => id => $id); |
135 | $ws->msg (ws_n => id => $id); |
135 | |
136 | |
136 | $ws |
137 | $ws |
… | |
… | |
156 | sub msg { |
157 | sub msg { |
157 | my ($self, $type, %msg) = @_; |
158 | my ($self, $type, %msg) = @_; |
158 | |
159 | |
159 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
160 | $msg{msgtype} = $type; |
161 | $msg{msgtype} = $type; |
|
|
162 | warn "msg " . $ns->{json_coder}->encode (\%msg) if $DEBUG;#d# |
161 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
163 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
162 | } |
164 | } |
163 | } |
165 | } |
164 | |
166 | |
165 | sub new { |
167 | sub new { |
166 | my ($self, $class, %args) = @_; |
168 | my ($self, $class, %args) = @_; |
167 | |
169 | |
168 | my $id = ++$self->{ns}{id}; |
170 | my $id = ++$self->{ns}{id}; |
169 | |
171 | |
170 | my $proxy = $self->{w}{$id} = bless { |
172 | my $proxy = bless { |
171 | id => $id, |
173 | id => $id, |
172 | }, "ext::widget::proxy"; |
174 | }, "ext::widget::proxy"; |
173 | |
175 | |
|
|
176 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
174 | Scalar::Util::weaken ($proxy->{ws} = $self); |
177 | Scalar::Util::weaken ($proxy->{ws} = $self); |
175 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
178 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
176 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
179 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
177 | |
180 | |
178 | for my $ev (grep /^on_/, keys %args) { |
181 | for my $ev (grep /^on_/, keys %args) { |
179 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
182 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
180 | $args{$ev} = 0; |
183 | $args{$ev} = 0; |
181 | } |
184 | } |
182 | |
185 | |
183 | $self->msg (ws_c => |
186 | $self->msg (ws_c => |
184 | ws => $self->{w}{id}, |
187 | ws => $proxy->{id}, |
185 | id => $id, |
188 | id => $id, |
186 | class => $class, |
189 | class => $class, |
187 | args => \%args, |
190 | args => \%args, |
188 | ); |
191 | ); |
189 | |
192 | |
… | |
… | |
198 | my ($self) = @_; |
201 | my ($self) = @_; |
199 | |
202 | |
200 | delete $self->{ns}{widget}{$self->{id}}; |
203 | delete $self->{ns}{widget}{$self->{id}}; |
201 | |
204 | |
202 | if (my $ws = $self->{ws}) { |
205 | if (my $ws = $self->{ws}) { |
203 | delete $ws->{w}{$self->{id}}; |
|
|
204 | $self->msg (w_c => name => "destroy"); |
206 | $self->msg (w_c => name => "destroy"); |
|
|
207 | delete $ws->{_w}{$self->{id}}; |
205 | } |
208 | } |
206 | } |
209 | } |
207 | |
210 | |
208 | sub msg { |
211 | sub msg { |
209 | my ($self, $type, %msg) = @_; |
212 | my ($self, $type, %msg) = @_; |
… | |
… | |
244 | |
247 | |
245 | () |
248 | () |
246 | } |
249 | } |
247 | |
250 | |
248 | sub set { |
251 | sub set { |
249 | my ($self, $member, $value) = @_; |
252 | my ($self, @kv) = @_; |
250 | |
253 | |
251 | $self->msg (w_s => name => $member, value => $value); |
254 | $self->msg (w_s => attr => \@kv); |
252 | } |
255 | } |
253 | |
256 | |
254 | sub get { |
257 | sub get { |
255 | my ($self, $member, $cb) = @_; |
258 | my ($self, $member, $cb) = @_; |
256 | |
259 | |
257 | $self->msg_cb ($cb, w_g => name => $member); |
260 | $self->msg_cb ($cb, w_g => attr => [$member]); |
258 | } |
261 | } |
259 | |
262 | |
260 | sub TO_JSON { |
263 | sub TO_JSON { |
261 | { __widget_ref__ => $_[0]{id} } |
264 | { __widget_ref__ => $_[0]{id} } |
262 | } |
265 | } |