NotificationCenter.registerOneShot should return a NotificationRegistration
[supercollider.git] / SCClassLibrary / Common / GUI / Model.sc
blob306631f78b7118494e517300072364f0ec08f084
1 Model {
2         var <>dependants;
4         *new {
5                 this.deprecated(thisMethod, Object.class.findMethod(\new));
6                 "NB Model's functionality is duplicated by Object".postln;
7                 ^super.new;
8         }
9         changed { arg what ... moreArgs;
10                 dependants.do({ arg item;
11                         item.update(this, what, *moreArgs);
12                 });
13         }
14         addDependant { arg dependant;
15                 if (dependants.isNil, {
16                         dependants = IdentitySet.new(4);
17                 });
18                 dependants.add(dependant);
19         }
20         removeDependant { arg dependant;
21                 if (dependants.notNil, {
22                         dependants.remove(dependant);
23                 });
24         }
25         release {
26                 dependants = nil;
27         }
30 SimpleController {
31         var model, actions;
32         // responds to updates of a model
34         *new { arg model;
35                 ^super.newCopyArgs(model).init
36         }
37         init {
38                 model.addDependant(this);
39         }
40         put { arg what, action;
41                 if (actions.isNil, {
42                         actions = IdentityDictionary.new(4);
43                 });
44                 actions.put(what, action);
45         }
46         update { arg theChanger, what ... moreArgs;
47                 var action;
48                 if(actions.notNil) {
49                         action = actions.at(what);
50                         if (action.notNil, {
51                                 action.valueArray(theChanger, what, moreArgs);
52                         });
53                 };
54         }
55         remove {
56                 model.removeDependant(this);
57         }
60 TestDependant {
61         update { arg thing;
62                 (thing.asString ++ " was changed.\n").post;
63         }
69 NotificationCenter {
71         classvar registrations;
73         //                      who             \didSomething
74         *notify { arg object, message, args;
75                 registrations.at(object,message).copy.do({ arg function;
76                         function.valueArray(args)
77                 })
78         }
80         //                      who             \didSomething
81         *register { arg object,message,listener,action;
82                 var nr;
83                 nr = NotificationRegistration(object,message,listener);
84                 registrations.put(object,message,listener,action);
85                 ^nr;
86         }
88         *unregister { arg object,message,listener;
89                 var lastKey,lastDict;
90                 lastDict = registrations.at(object,message);
91                 if(lastDict.notNil,{
92                         lastDict.removeAt(listener);
93                         (lastDict.size == 0).if({
94                                 registrations.removeAt(object,message);
95                                 (registrations.at(object).size == 0).if({
96                                         registrations.removeAt(object);
97                                 });
98                         });
99                 });
100         }
101         *registerOneShot {  arg object,message,listener,action;
102                 var nr;
103                 nr = NotificationRegistration(object,message,listener);
104                 registrations.put(object,message,listener,
105                         { |args|
106                                 action.value(args);
107                                 this.unregister(object,message,listener)
108                         });
109                 ^nr
110         }
111         *clear {
112                 registrations = MultiLevelIdentityDictionary.new;
113         }
114         *registrationExists { |object,message,listener|
115                 ^registrations.at(object,message,listener).notNil
116         }
117         *initClass {
118                 this.clear
119         }
120         //*remove { |listener|
121         //
122         //}
125 NotificationRegistration {
127         var <>object,<>message,<>listener;
129         *new { arg o,m,l;
130                 ^super.new.object_(o).message_(m).listener_(l)
131         }
132         remove {
133                 NotificationCenter.unregister(object,message,listener)
134         }