8
using MonoMac.Foundation;
12
public class ExpanderBackend : ViewBackend<MacExpander, IExpandEventSink>, IExpanderBackend
16
public ExpanderBackend ()
18
ViewObject = new MacExpander ();
23
return Widget.Expander.Label;
26
Widget.Expander.Label = value;
30
public bool Expanded {
32
return Widget.Box.Expanded;
35
Widget.Box.Expanded = value;
36
Widget.Expander.On = value;
40
public void SetContent (IWidgetBackend widget)
42
Widget.Box.SetContent (GetWidget (widget));
46
public class MacExpander : NSView, IViewObject
48
ExpanderWidget expander;
53
SetFrameSize (new SizeF (80, 50 + CollapsibleBox.DefaultCollapsedHeight));
54
expander = new ExpanderWidget () {
55
Frame = new RectangleF (0, 0, 80, 21),
56
AutoresizingMask = NSViewResizingMask.WidthSizable
58
box = new CollapsibleBox () {
59
Frame = new RectangleF (0, 25, 80, 100),
60
AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable
62
expander.DisclosureToggled += (sender, e) => box.Expanded = expander.On;
63
AutoresizesSubviews = true;
64
AddSubview (expander);
68
public override bool IsFlipped {
74
public ExpanderWidget Expander {
80
public CollapsibleBox Box {
86
public Widget Frontend { get; set; }
92
public void EnableEvent (Xwt.Backends.ButtonEvent ev)
96
public void DisableEvent (Xwt.Backends.ButtonEvent ev)
101
public class ExpanderWidget : NSView
103
public event EventHandler DisclosureToggled;
107
NSGradient backgroundGradient;
110
public ExpanderWidget ()
112
label = new NSTextView () {
113
AutoresizingMask = NSViewResizingMask.MaxYMargin | NSViewResizingMask.WidthSizable,
114
Alignment = NSTextAlignment.Left,
117
DrawsBackground = false,
118
Frame = new RectangleF (17, 3, 60, 13)
120
disclosure = new NSButton () {
121
BezelStyle = NSBezelStyle.Disclosure,
122
AutoresizingMask = NSViewResizingMask.MaxYMargin,
123
ImagePosition = NSCellImagePosition.ImageOnly,
124
Frame = new RectangleF (5, 4, 13, 13),
125
State = NSCellStateValue.On
127
disclosure.SetButtonType (NSButtonType.OnOff);
129
disclosure.AddObserver (this, new NSString ("cell.state"), NSKeyValueObservingOptions.New, IntPtr.Zero);
130
AutoresizesSubviews = true;
131
backgroundGradient = new NSGradient (NSColor.FromCalibratedRgba (0.93f, 0.93f, 0.97f, 1.0f),
132
NSColor.FromCalibratedRgba (0.74f, 0.76f, 0.83f, 1.0f));
133
strokeColor = NSColor.FromCalibratedRgba (0.60f, 0.60f, 0.60f, 1.0f);
136
AddSubview (disclosure);
139
public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
141
if (DisclosureToggled != null)
142
DisclosureToggled (this, EventArgs.Empty);
145
public string Label {
156
return disclosure.State == NSCellStateValue.On;
159
disclosure.State = value ? NSCellStateValue.On : NSCellStateValue.Off;
163
public override void DrawRect (RectangleF dirtyRect)
165
backgroundGradient.DrawInRect (Frame, -90);
166
if (dirtyRect == Frame) {
167
strokeColor.SetStroke ();
168
NSBezierPath.StrokeRect (dirtyRect);
173
public class CollapsibleBox : NSBox
175
internal const float DefaultCollapsedHeight = 1f;
179
public CollapsibleBox ()
182
otherHeight = DefaultCollapsedHeight;
183
TitlePosition = NSTitlePosition.NoTitle;
184
BorderType = NSBorderType.NoBorder;
185
BoxType = NSBoxType.NSBoxPrimary;
188
public void SetContent (NSView view)
193
public bool Expanded {
194
get { return expanded; }
196
SetExpanded (value, true);
200
public void SetExpanded (bool value, bool animate)
202
if (expanded != value) {
204
var frameSize = Frame.Size;
205
SizeF newFrameSize = new SizeF (frameSize.Width, otherHeight);
206
otherHeight = frameSize.Height;
207
SetFrameSize (newFrameSize, animate);
211
public override bool IsFlipped {
217
RectangleF FrameForNewSizePinnedToTopLeft (SizeF newFrameSize)
220
frame.Size = newFrameSize;
224
public void SetFrameSize (SizeF newFrameSize, bool animating)
226
RectangleF newFrame = FrameForNewSizePinnedToTopLeft (newFrameSize);
228
NSAnimation animation = new NSViewAnimation (new [] {
229
NSDictionary.FromObjectsAndKeys (
230
new object[] { this, NSValue.FromRectangleF (Frame), NSValue.FromRectangleF (newFrame) },
231
new object[] { NSViewAnimation.TargetKey, NSViewAnimation.StartFrameKey, NSViewAnimation.EndFrameKey }
234
animation.AnimationBlockingMode = NSAnimationBlockingMode.Nonblocking;
235
animation.Duration = 0.25;
236
animation.StartAnimation ();
238
Superview.SetNeedsDisplayInRect (Frame);