julian+alberto classlib merges for 3.4 (10125, 10130, 10133)
[supercollider.git] / common / build / SCClassLibrary / Common / GUI / Model.sc
blob5898abf71cec8858ca47d590f98ee30d83383cdb
1 Model {
2         var <>dependants;
4         changed { arg what ... moreArgs;
5                 dependants.do({ arg item;
6                         item.update(this, what, *moreArgs);
7                 });
8         }
9         addDependant { arg dependant;
10                 if (dependants.isNil, {
11                         dependants = IdentitySet.new(4);
12                 });
13                 dependants.add(dependant);
14         }
15         removeDependant { arg dependant;
16                 if (dependants.notNil, {
17                         dependants.remove(dependant);
18                 });
19         }
20         release {
21                 dependants = nil;
22         }
25 SimpleController {
26         var model, actions;
27         // responds to updates of a model
29         *new { arg model;
30                 ^super.newCopyArgs(model).init
31         }
32         init {
33                 model.addDependant(this);
34         }
35         put { arg what, action;
36                 if (actions.isNil, {
37                         actions = IdentityDictionary.new(4);
38                 });
39                 actions.put(what, action);
40         }
41         update { arg theChanger, what ... moreArgs;
42                 var action;
43                 action = actions.at(what);
44                 if (action.notNil, {
45                         action.valueArray(theChanger, what, moreArgs);
46                 });
47         }
48         remove {
49                 model.removeDependant(this);
50         }
53 TestDependant {
54         update { arg thing;
55                 (thing.asString ++ " was changed.\n").post;
56         }
62 NotificationCenter {
64         classvar registrations;
66         //                      who             \didSomething
67         *notify { arg object, message, args;
68                 registrations.at(object,message).copy.do({ arg function;
69                         function.valueArray(args)
70                 })
71         }
73         //                      who             \didSomething
74         *register { arg object,message,listener,action;
75                 var nr;
76                 nr = NotificationRegistration(object,message,listener);
77                 registrations.put(object,message,listener,action);
78                 ^nr;
79         }
81         *unregister { arg object,message,listener;
82                 var lastKey,lastDict;
83                 lastDict = registrations.at(object,message);
84                 if(lastDict.notNil,{
85                         lastDict.removeAt(listener);
86                         (lastDict.size == 0).if({
87                                 registrations.removeAt(object,message);
88                                 (registrations.at(object).size == 0).if({
89                                         registrations.removeAt(object);
90                                 });
91                         });
92                 });
93         }
94         *registerOneShot {  arg object,message,listener,action;
95                 registrations.put(object,message,listener,
96                         {
97                                 action.value;
98                                 this.unregister(object,message,listener)
99                         })
100         }
101         *clear {
102                 registrations = MultiLevelIdentityDictionary.new;
103         }
104         *registrationExists { |object,message,listener|
105                 ^registrations.at(object,message,listener).notNil
106         }
107         *initClass {
108                 this.clear
109         }
110         //*remove { |listener|
111         //
112         //}
115 NotificationRegistration {
117         var <>object,<>message,<>listener;
119         *new { arg o,m,l;
120                 ^super.new.object_(o).message_(m).listener_(l)
121         }
122         remove {
123                 NotificationCenter.unregister(object,message,listener)
124         }