File: SCViewHolder.sc

package info (click to toggle)
supercollider 1%3A3.10.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,496 kB
  • sloc: cpp: 283,513; lisp: 74,040; ansic: 72,252; sh: 23,016; python: 7,175; makefile: 1,087; perl: 766; java: 677; yacc: 314; lex: 175; ruby: 136; objc: 65; xml: 15
file content (294 lines) | stat: -rw-r--r-- 6,855 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// SCViewHolder makes it possible to add more capabilities by holding a View, not subclassing it
SCViewHolder {

	classvar <>consumeKeyDowns = false;// should the view by default consume keydowns

	var <view;

	view_ { arg v;
		// subclasses need to ALWAYS use this method to set the view
		view = v;
		view.onClose = { this.viewDidClose; };
	}
	viewDidClose { view = nil; }
	remove {
		if(view.notNil,{
			view.remove;// will cause view.onClose
			//view = nil;
		});
	}

	action { ^view.action }
	action_ { arg f; view.action_(f) }
	doAction { view.doAction }
	keyDownAction_ { arg f;
		view.keyDownAction_(f);
	}
	keyDownResponder { ^nil }
	enableKeyDowns { this.keyDownAction = this.keyDownResponder }

	asView { ^view }
	bounds { ^view.bounds }
	bounds_ { arg b; view.bounds_(b) }
	resize_ { arg r; view.resize_(r) }
	enabled { ^view.enabled }
	enabled_ { |b| view.enabled_(b) }
	refresh { view.refresh }
	background { ^view.background }
	background_ { arg b; view.background_(b) }
	focus { arg flag=true; view.focus(flag) }
	visible_ { arg boo; view.visible = boo }

	isClosed { ^(view.isNil or: {view.isClosed}) }
	// should move lower
	font_ { arg f;
		view.font = f;
	}

	// delegate to the view
	doesNotUnderstand { |selector ... args|
		var	result;
		view.respondsTo(selector).if({
			result = view.performList(selector, args);
			^(result === view).if({ this }, { result });
		}, {
			DoesNotUnderstandError(this, selector, args).throw;
		});
	}
}


FlowViewLayout : FlowLayout {

	var rows;
	var fOnViewClose;

	*new { arg bounds, margin, gap;
		^super.new(bounds,margin,gap).prInitFlowViewLayout();
	}

	clear {
		rows = [];
		this.reset;
	}

	place { arg view;
		var row;
		if (rows.size < 1) {this.prAddRow};
		rows.last.add( view );
		// Ensure the action is only added once: it will be removed only if already added.
		view.removeAction( fOnViewClose, \onClose );
		view.addAction( fOnViewClose, \onClose );
		super.place( view );
	}

	remove { arg view, reflow = true;
		rows.copy.do { |row|
			row.remove(view);
			if ( (row.size < 1) and: (row !== rows.last) ) {
				rows.remove(row);
			};
		};
	}

	startRow {
		this.prAddRow;
		this.nextLine;
	}

	reflow {
		var newRows;
		this.reset;
		newRows = [];
		rows.do { |row,i|
			row = row.select(_.notClosed);
			if(row.isEmpty.not,{
				newRows = newRows.add(row)
			});
		};
		rows = newRows;
		rows.do { |row|
			row.do { |view| super.place(view) };
			if (row !== rows.last) { this.nextLine };
		};
	}
	wouldExceedBottom { arg aBounds;
		^(top + aBounds.height + margin.y) > bounds.bottom
	}

	rows { ^rows.copy }

	prInitFlowViewLayout {
		fOnViewClose = { |view| this.remove(view); };
	}

	prAddRow { rows = rows.add(List.new); }
}


FlowView : SCViewHolder {

	// a CompositeView with a FlowLayout as its decorator
	// has the advantage that it preserves startRow when the view is resized

	var	<parent;
	var	autoRemoves,prevMaxHeight,prevMaxRight;

	*layout { arg f,bounds;
		var v;
		v = this.new(nil,bounds);
		f.value(v);
		^v
	}
	*viewClass { ^CompositeView }
	*new { arg parent, bounds,margin,gap,windowTitle="";
		^super.new.init(parent, bounds,margin,gap,windowTitle);
	}
	init { arg argParent, bounds,margin,gap,windowTitle="";
		var w, parentView, iMadeParent = false;
		parent = argParent ?? {
			iMadeParent = true;
			Window.new(windowTitle,bounds).front
		};
		parentView = parent.asView;
		if(bounds.notNil,{
			bounds = bounds.asRect;
			if(iMadeParent) { bounds = bounds.moveTo(0, 0) };
		},{
			bounds = parentView.bounds.moveTo(0,0);
		});
		this.view = this.class.viewClass.new(parentView, bounds);

		// parent has placed me, now get my bounds
		bounds = view.bounds.moveTo(0, 0);

		// note: FlowLayout default is 4@4 4@4
		view.decorator = FlowViewLayout(bounds, margin ?? {2@0}, gap ?? {4@4}, false);
		view.decorator.owner = this;
		autoRemoves = IdentitySet.new;
	}
	startRow {
		this.decorator.startRow;
	}
	removeOnClose { arg updater;
		autoRemoves.add(updater);
	}
	hr { arg color,height=3;
		this.startRow;
		StaticText(this,Rect(0,0,this.decorator.innerBounds.width - (2 * this.decorator.gap.x), height,0))
				.string_("").background_(color ?? {Color(1,1,1,0.3)} ).resize_(2);
		this.startRow;
	}

	innerBounds { ^this.decorator.innerBounds }
	bounds_ { arg b, reflow = true;
		if(b != view.bounds,{
			view.bounds = b;
			if(this.decorator.notNil,{
				this.decorator.bounds = b.moveTo(0, 0);
				reflow.if({ this.reflowAll; });
			})
		});
	}
	indentedRemaining { ^this.decorator.indentedRemaining }
	used { ^this.decorator.used }

	reflowAll {
		this.decorator.reflow;
	}
	// returns the new bounds
	resizeToFit { arg reflow = false,tryParent = false;
		var used,new;

		if(reflow,{ this.reflowAll; });

		used = this.decorator.used;
		new = view.bounds.resizeTo(used.width,used.height);
		view.bounds = new;

		this.decorator.bounds = new.moveTo(0, 0);

		if(reflow,{ this.reflowAll; });
		// its better to call reflowDeep on the parent
		if(tryParent,{
			this.parent.tryPerform(\resizeToFit,reflow,tryParent);
		});
		^new
	}

	reflowDeep {
		this.allChildren.reverseDo({ |view|
			if(view.isKindOf(FlowView),{
				view.bounds_(view.bounds.resizeTo(2000,2000),false);
				view.reflowAll.resizeToFit;
			});
		});
	}
	front {
		// window.front
	}

	wouldExceedBottom { arg aBounds; ^this.decorator.wouldExceedBottom(aBounds) }
	anyChildExceeds {
		var r;
		r = view.bounds;
		^view.children.any({ arg c;
			r.containsRect( c.bounds ).not
		});
	}

	// if what you are adding is unsure how much space it is going to take
	// then take everything ...
	allocateRemaining {
		prevMaxHeight = this.decorator.maxHeight;
		prevMaxRight = this.decorator.maxRight;
		^this.decorator.indentedRemaining
	}
	// ... and afterwards
	// state what you actually used.
	// see FlowView-flow
	didUseAllocated { arg vbounds;
		if(prevMaxHeight.isNil,{
			Error("didUseAllocated called without prior call to allocateRemaining").throw;
		});
		this.decorator.left = vbounds.right + this.decorator.margin.x;
		// maxRight is max right of all rows ever laid
		// but maxHeight is the max of this row only
		// but they both have to be rescinded
		this.decorator.maxRight = max( prevMaxRight, vbounds.right );
		this.decorator.maxHeight = max( prevMaxHeight,vbounds.height);
	}

	remove {
		autoRemoves.do({ |updater| updater.remove });
		autoRemoves = nil;
		view.notClosed.if({
			view.remove;
		});
	}
	viewDidClose {
		autoRemoves.do({ arg u; u.remove });
		autoRemoves = nil;
		view.tryPerform(\viewDidClose);
	}

	children { ^view.children }
	decorator { ^view.decorator }
	decorator_ { |dec| view.decorator = dec }
	add { |child|
		view.add(child);
	}
	removeAll {
		view.removeAll;
		this.decorator.clear;
	}

	asFlowView {}
	asPageLayout {}

	// only SCView calls these
	prRemoveChild { |child|
		view.prRemoveChild(child);
	}
	prClose { view.prClose }
}