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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
// SelectorView.cs
//
//
// Author:
// Marius Ungureanu <marius.ungureanu@xamarin.com>
//
// Copyright (c) 2015 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using AppKit;
using CoreGraphics;
using Foundation;
using MonoDevelop.Components;
using MonoDevelop.Components.Mac;
using MonoDevelop.Components.MainToolbar;
using MonoDevelop.Core;
using MonoDevelop.Ide;
namespace MonoDevelop.MacIntegration.MainToolbar
{
class SizeRequestedEventArgs : EventArgs
{
public CGSize Size { get; private set; }
public SizeRequestedEventArgs (CGSize size)
{
Size = size;
}
}
class OverflowInfoEventArgs : EventArgs
{
public nfloat WindowWidth { get; set; }
public nfloat AllItemsWidth { get; set; }
public nfloat ItemsInOverflowWidth { get; set; }
}
[Register]
class SelectorView : NSButton
{
public event EventHandler<EventArgs> SizeChanged;
internal const int ConfigurationIdx = 0;
internal const int RuntimeIdx = 1;
internal PathSelectorView RealSelectorView { get; private set; }
public SelectorView ()
{
Cell = new ColoredButtonCell ();
BezelStyle = NSBezelStyle.TexturedRounded;
Title = "";
RealSelectorView = new PathSelectorView (new CGRect (6, 0, 1, 1));
RealSelectorView.UnregisterDraggedTypes ();
AddSubview (RealSelectorView);
}
public override CGSize SizeThatFits (CGSize size)
{
var fitSize = RealSelectorView.SizeThatFits (size);
return new CGSize (Math.Round (fitSize.Width) + 12.0, size.Height);
}
public override void SetFrameSize (CGSize newSize)
{
base.SetFrameSize (newSize);
RealSelectorView.SetFrameSize (newSize);
}
public override void ViewDidMoveToWindow ()
{
base.ViewDidMoveToWindow ();
UpdateLayout ();
}
void UpdateLayout ()
{
// Correct the offset position for the screen
nfloat yOffset = 1f;
if (Window?.Screen?.BackingScaleFactor == 2) {
yOffset = 0.5f;
}
RealSelectorView.Frame = new CGRect (RealSelectorView.Frame.X, yOffset, RealSelectorView.Frame.Width, RealSelectorView.Frame.Height);
}
public override void DidChangeBackingProperties ()
{
base.DidChangeBackingProperties ();
UpdateLayout ();
}
internal void OnSizeChanged ()
{
if (SizeChanged != null) {
SizeChanged (this, EventArgs.Empty);
}
}
#region PathSelectorView
[Register]
public class PathSelectorView : NSPathControl
{
[Flags]
enum CellState
{
AllHidden = 0x0,
RuntimeShown = 0x1,
ConfigurationShown = 0x2,
AllShown = 0x3,
}
static readonly string ConfigurationPlaceholder = GettextCatalog.GetString ("Default");
static readonly string RuntimePlaceholder = GettextCatalog.GetString ("Default");
CellState state = CellState.AllShown;
public override CGSize SizeThatFits (CGSize size)
{
nfloat rtWidth, cWidth;
WidthsForPathCells (out cWidth, out rtWidth);
if (10 + cWidth + rtWidth < size.Width) {
state = CellState.AllShown;
UpdatePathText (ConfigurationIdx, TextForActiveConfiguration);
UpdatePathText (RuntimeIdx, TextForRuntimeConfiguration);
return new CGSize (10 + cWidth + rtWidth, size.Height);
}
if (10 + 28 + cWidth < size.Width) {
state = CellState.ConfigurationShown;
UpdatePathText (ConfigurationIdx, TextForActiveConfiguration);
UpdatePathText (RuntimeIdx, string.Empty);
return new CGSize (10 + 28 + cWidth, size.Height);
}
state = CellState.AllHidden;
UpdatePathText (ConfigurationIdx, string.Empty);
UpdatePathText (RuntimeIdx, string.Empty);
return new CGSize (10 + 52.0, size.Height);
}
string EllipsizeString (string s)
{
if (s.Length > 50) {
var start = s.Substring (0, 20);
var end = s.Substring (s.Length - 20, 20);
return start + "…" + end;
} else {
return s;
}
}
string TextForActiveConfiguration {
get {
return EllipsizeString (ActiveConfiguration != null ? ActiveConfiguration.DisplayString : ConfigurationPlaceholder);
}
}
string TextForRuntimeConfiguration {
get {
if (ActiveRuntime != null) {
using (var mutableModel = ActiveRuntime.GetMutableModel ())
return EllipsizeString (mutableModel.FullDisplayString);
} else {
return EllipsizeString (RuntimePlaceholder);
}
}
}
void WidthsForPathCells (out nfloat configWidth, out nfloat runtimeWidth)
{
string text;
NSPathComponentCell cell;
text = TextForActiveConfiguration;
cell = PathComponentCells [ConfigurationIdx];
configWidth = new NSAttributedString (text, new NSStringAttributes { Font = cell.Font }).Size.Width + 28;
text = TextForRuntimeConfiguration;
cell = PathComponentCells [RuntimeIdx];
runtimeWidth = new NSAttributedString (text, new NSStringAttributes { Font = cell.Font }).Size.Width + 28;
}
NSMenu CreateSubMenuForRuntime (IRuntimeModel runtime)
{
if (!runtime.Children.Any ())
return null;
var menu = new NSMenu {
AutoEnablesItems = false,
ShowsStateColumn = false,
Font = NSFont.MenuFontOfSize (12),
};
foreach (var item in runtime.Children)
CreateMenuItem (menu, item);
return menu;
}
NSMenuItem CreateMenuItem (NSMenu menu, IRuntimeModel runtime)
{
NSMenuItem menuItem;
string runtimeFullDisplayString;
using (var mutableModel = runtime.GetMutableModel ()) {
runtimeFullDisplayString = mutableModel.FullDisplayString;
menuItem = new NSMenuItem {
IndentationLevel = runtime.IsIndented ? 2 : 1,
AttributedTitle = new NSAttributedString (mutableModel.DisplayString, new NSStringAttributes {
Font = runtime.Notable ? NSFontManager.SharedFontManager.ConvertFont (menu.Font, NSFontTraitMask.Bold) : menu.Font,
}),
Enabled = mutableModel.Enabled,
Hidden = !mutableModel.Visible,
};
}
var subMenu = CreateSubMenuForRuntime (runtime);
if (subMenu != null) {
menuItem.Submenu = subMenu;
menuItem.Enabled = true;
} else {
menuItem.Activated += (o2, e2) => {
string old;
using (var activeMutableModel = ActiveRuntime.GetMutableModel ())
old = activeMutableModel.FullDisplayString;
IRuntimeModel newRuntime = runtimeModel.FirstOrDefault (r => {
using (var newRuntimeMutableModel = r.GetMutableModel ())
return newRuntimeMutableModel.FullDisplayString == runtimeFullDisplayString;
});
if (newRuntime == null)
return;
ActiveRuntime = newRuntime;
var ea = new HandledEventArgs ();
if (RuntimeChanged != null)
RuntimeChanged (o2, ea);
if (ea.Handled)
ActiveRuntime = runtimeModel.First (r => {
using (var newRuntimeMutableModel = r.GetMutableModel ())
return newRuntimeMutableModel.FullDisplayString == old;
});
};
}
menu.AddItem (menuItem);
return menuItem;
}
public PathSelectorView (CGRect frameRect) : base (frameRect)
{
PathComponentCells = new [] {
new NSPathComponentCell {
Image = MultiResImage.CreateMultiResImage ("project", "disabled"),
Title = TextForActiveConfiguration,
Enabled = false,
},
new NSPathComponentCell {
Image = MultiResImage.CreateMultiResImage ("device", "disabled"),
Title = TextForRuntimeConfiguration,
Enabled = false,
}
};
UpdateStyle ();
BackgroundColor = NSColor.Clear;
FocusRingType = NSFocusRingType.None;
Ide.Gui.Styles.Changed += UpdateStyle;
}
int IndexOfCellAtX (nfloat x)
{
nfloat rWidth, cWidth;
WidthsForPathCells (out cWidth, out rWidth);
if (x >= 0 && x <= cWidth) {
return ConfigurationIdx;
} else if (x > cWidth && x <= cWidth + 10) {
// The > in the middle
return -1;
} else {
return RuntimeIdx;
}
}
public override void MouseDown (NSEvent theEvent)
{
if (!Enabled)
return;
var locationInView = ConvertPointFromView (theEvent.LocationInWindow, null);
var cellIdx = IndexOfCellAtX (locationInView.X);
if (cellIdx == -1) {
return;
}
var item = PathComponentCells [cellIdx];
if (item == null || !item.Enabled)
return;
var componentRect = ((NSPathCell)Cell).GetRect (item, Frame, this);
int idx = -1;
int i = 0;
NSMenuItem selectedItem = null;
var menu = new NSMenu {
AutoEnablesItems = false,
ShowsStateColumn = false,
Font = NSFont.MenuFontOfSize (12),
};
if (cellIdx == ConfigurationIdx) {
if (ActiveConfiguration == null)
return;
foreach (var configuration in ConfigurationModel) {
var _configuration = configuration;
var menuitem = new NSMenuItem (configuration.DisplayString, (o2, e2) => {
ActiveConfiguration = configurationModel.First (c => c.OriginalId == _configuration.OriginalId);
}) {
Enabled = true,
IndentationLevel = 1,
};
menu.AddItem (menuitem);
if (selectedItem == null && configuration.OriginalId == ActiveConfiguration.OriginalId)
selectedItem = menuitem;
}
} else if (cellIdx == RuntimeIdx) {
if (ActiveRuntime == null)
return;
using (var activeMutableModel = ActiveRuntime.GetMutableModel ()) {
foreach (var runtime in RuntimeModel) {
if (runtime.HasParent)
continue;
NSMenuItem menuitem = null;
if (runtime.IsSeparator)
menu.AddItem (NSMenuItem.SeparatorItem);
else
menuitem = CreateMenuItem (menu, runtime);
using (var mutableModel = runtime.GetMutableModel ()) {
if (selectedItem == null && menuitem != null && mutableModel.DisplayString == activeMutableModel.DisplayString)
selectedItem = menuitem;
}
++i;
}
}
} else
throw new NotSupportedException ();
if (menu.Count > 1) {
var offs = new CGPoint (componentRect.Left + 3, componentRect.Top + 3);
if (Window?.Screen?.BackingScaleFactor == 2)
offs.Y += 0.5f; // fine tune menu position on retinas
menu.PopUpMenu (selectedItem, offs, this);
}
}
public override void DidChangeBackingProperties ()
{
base.DidChangeBackingProperties ();
// Force a redraw because NSPathControl does not redraw itself when switching to a different resolution
// and the icons need redrawn
NeedsDisplay = true;
}
void UpdateStyle (object sender = null, EventArgs e = null)
{
PathComponentCells [ConfigurationIdx].TextColor = Styles.BaseForegroundColor.ToNSColor ();
PathComponentCells [RuntimeIdx].TextColor = Styles.BaseForegroundColor.ToNSColor ();
UpdateImages ();
}
protected override void Dispose (bool disposing)
{
if (disposing)
Ide.Gui.Styles.Changed -= UpdateStyle;
base.Dispose (disposing);
}
public override void ViewDidMoveToWindow ()
{
base.ViewDidMoveToWindow ();
NSNotificationCenter.DefaultCenter.AddObserver (NSWindow.DidChangeBackingPropertiesNotification,
notification => Runtime.RunInMainThread ((Action) RealignTexts));
RealignTexts ();
}
void RealignTexts ()
{
if (Window == null)
return;
// fix the icon alignment, move it slightly up
var alignFix = new CGRect (0, Window.BackingScaleFactor == 2 ? -0.5f : -1f, 16, 16);
PathComponentCells [ConfigurationIdx].Image.AlignmentRect = alignFix;
PathComponentCells [RuntimeIdx].Image.AlignmentRect = alignFix;
}
void UpdatePathText (int idx, string text)
{
PathComponentCells [idx].Title = text;
UpdateImages ();
}
void UpdateImages ()
{
string projectStyle = "";
string deviceStyle = "";
if (!PathComponentCells [ConfigurationIdx].Enabled)
projectStyle = "disabled";
if (!PathComponentCells [ConfigurationIdx].Enabled)
deviceStyle = "disabled";
// HACK
// For some reason NSPathControl does not like the images that ImageService provides. To use them it requires
// ToBitmap() to be called first. But a second problem is that ImageService only seems to provide a single resolution
// for its icons. It may be related to the images being initially loaded through the Gtk backend and then converted to NSImage
// at a later date.
// For whatever reason, we custom load the images here through NSImage, providing both 1x and 2x image reps.
PathComponentCells [ConfigurationIdx].Image = MultiResImage.CreateMultiResImage ("project", deviceStyle);
PathComponentCells [RuntimeIdx].Image = MultiResImage.CreateMultiResImage ("device", deviceStyle);
RealignTexts ();
}
void OnSizeChanged ()
{
var sview = (SelectorView)Superview;
sview.OnSizeChanged ();
}
IConfigurationModel activeConfiguration;
public IConfigurationModel ActiveConfiguration {
get { return activeConfiguration; }
set {
activeConfiguration = value;
state |= CellState.ConfigurationShown;
if (ConfigurationChanged != null)
ConfigurationChanged (this, EventArgs.Empty);
UpdatePathText (ConfigurationIdx, value.DisplayString);
OnSizeChanged ();
}
}
IRuntimeModel activeRuntime;
public IRuntimeModel ActiveRuntime {
get { return activeRuntime; }
set {
activeRuntime = value;
using (var mutableModel = value.GetMutableModel ()) {
state |= CellState.RuntimeShown;
UpdatePathText (RuntimeIdx, mutableModel.FullDisplayString);
OnSizeChanged ();
}
}
}
IEnumerable<IConfigurationModel> configurationModel;
public IEnumerable<IConfigurationModel> ConfigurationModel {
get { return configurationModel; }
set {
configurationModel = value;
int count = value.Count ();
if (count == 0) {
state |= CellState.ConfigurationShown;
UpdatePathText (ConfigurationIdx, ConfigurationPlaceholder);
activeConfiguration = null;
}
PathComponentCells [ConfigurationIdx].Enabled = count > 1;
OnSizeChanged ();
}
}
IEnumerable<IRuntimeModel> runtimeModel;
public IEnumerable<IRuntimeModel> RuntimeModel {
get { return runtimeModel; }
set {
runtimeModel = value;
int count = value.Count ();
if (count == 0) {
state |= CellState.RuntimeShown;
UpdatePathText (RuntimeIdx, RuntimePlaceholder);
activeRuntime = null;
}
PathComponentCells [RuntimeIdx].Enabled = count > 1;
OnSizeChanged ();
}
}
public event EventHandler ConfigurationChanged;
public event EventHandler<HandledEventArgs> RuntimeChanged;
public override bool Enabled {
get {
return base.Enabled;
}
set {
base.Enabled = value;
if (value) {
PathComponentCells [RuntimeIdx].Enabled = runtimeModel.Count () > 1;
PathComponentCells [ConfigurationIdx].Enabled = configurationModel.Count () > 1;
}
}
}
}
#endregion
}
}
|