|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | +using AppKit; |
| 4 | +using CoreGraphics; |
| 5 | +using Foundation; |
| 6 | +using Xamarin.PropertyEditing.ViewModels; |
| 7 | + |
| 8 | +namespace Xamarin.PropertyEditing.Mac |
| 9 | +{ |
| 10 | + internal class CreateVariantView |
| 11 | + : BasePopOverViewModelControl |
| 12 | + { |
| 13 | + private const int FrameWidth = 250; |
| 14 | + private const int ControlSpacing = 7; |
| 15 | + private const int RightEdgeMargin = 12; |
| 16 | + |
| 17 | + public CreateVariantView (IHostResourceProvider hostResources, PropertyViewModel propertyViewModel) |
| 18 | + : base (hostResources, propertyViewModel, Properties.Resources.AddVariationTitle, "pe-resource-editor-32") |
| 19 | + { |
| 20 | + Initialize (new CreateVariantViewModel (propertyViewModel.Property)); |
| 21 | + } |
| 22 | + |
| 23 | + private void Initialize (CreateVariantViewModel createVariantViewModel) |
| 24 | + { |
| 25 | + Frame = new CGRect (CGPoint.Empty, new CGSize (FrameWidth, 240)); |
| 26 | + |
| 27 | + int editorHeight = 18; |
| 28 | + |
| 29 | + var FrameWidthThird = Frame.Width / 3; |
| 30 | + |
| 31 | + var introduceVariation = new UnfocusableTextField { |
| 32 | + StringValue = Properties.Resources.AddVariationHelpText, |
| 33 | + TranslatesAutoresizingMaskIntoConstraints = false, |
| 34 | + }; |
| 35 | + |
| 36 | + AddSubview (introduceVariation); |
| 37 | + |
| 38 | + this.AddConstraints (new[] { |
| 39 | + NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 37f), |
| 40 | + NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 18f), |
| 41 | + NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, 0f), |
| 42 | + NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, editorHeight), |
| 43 | + }); |
| 44 | + |
| 45 | + var controlTop = 60; |
| 46 | + |
| 47 | + var okButton = new FocusableButton { |
| 48 | + BezelStyle = NSBezelStyle.Rounded, |
| 49 | + Enabled = false, |
| 50 | + Highlighted = true, |
| 51 | + Title = Properties.Resources.AddVariation, |
| 52 | + TranslatesAutoresizingMaskIntoConstraints = false, |
| 53 | + }; |
| 54 | + |
| 55 | + foreach (var viewModel in createVariantViewModel.VariationCategories) { |
| 56 | + |
| 57 | + var name = new UnfocusableTextField { |
| 58 | + Alignment = NSTextAlignment.Right, |
| 59 | + StringValue = viewModel.Name + ":", |
| 60 | + TranslatesAutoresizingMaskIntoConstraints = false, |
| 61 | + }; |
| 62 | + |
| 63 | + AddSubview (name); |
| 64 | + |
| 65 | + this.AddConstraints (new[] { |
| 66 | + NSLayoutConstraint.Create (name, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, controlTop), |
| 67 | + NSLayoutConstraint.Create (name, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, FrameWidthThird), |
| 68 | + NSLayoutConstraint.Create (name, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, editorHeight) |
| 69 | + }); |
| 70 | + |
| 71 | + var popUpButton = new FocusablePopUpButton { |
| 72 | + ControlSize = NSControlSize.Small, |
| 73 | + Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)), |
| 74 | + TranslatesAutoresizingMaskIntoConstraints = false, |
| 75 | + }; |
| 76 | + |
| 77 | + popUpButton.Activated += (o, e) => { |
| 78 | + if (o is FocusablePopUpButton fpb) { |
| 79 | + if (fpb.SelectedItem.RepresentedObject is NSObjectFacade menuObjectFacade) { |
| 80 | + if (menuObjectFacade.Target is VariationFacade vf) { |
| 81 | + vf.ViewModel.SelectedOption = vf.Option; |
| 82 | + okButton.Enabled = createVariantViewModel.CreateVariantCommand.CanExecute (null); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + var popUpButtonList = new NSMenu (); |
| 89 | + popUpButton.Menu = popUpButtonList; |
| 90 | + |
| 91 | + AddSubview (popUpButton); |
| 92 | + |
| 93 | + this.AddConstraints (new[] { |
| 94 | + NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, controlTop), |
| 95 | + NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, FrameWidthThird + ControlSpacing), |
| 96 | + NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, -FrameWidthThird - ControlSpacing - RightEdgeMargin), |
| 97 | + NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, editorHeight) |
| 98 | + }); |
| 99 | + |
| 100 | + foreach (var variation in viewModel.Variations) { |
| 101 | + popUpButtonList.AddItem (new NSMenuItem (variation.Name) { |
| 102 | + RepresentedObject = new NSObjectFacade ( |
| 103 | + new VariationFacade { |
| 104 | + Option = variation, |
| 105 | + ViewModel = viewModel }) |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + controlTop += editorHeight + 8; |
| 110 | + } |
| 111 | + |
| 112 | + okButton.Activated += (sender, e) => { |
| 113 | + // TODO Add the Variation |
| 114 | + |
| 115 | + PopOver.Close (); |
| 116 | + }; |
| 117 | + |
| 118 | + AddSubview (okButton); |
| 119 | + |
| 120 | + this.AddConstraints (new[] { |
| 121 | + NSLayoutConstraint.Create (okButton, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1, -RightEdgeMargin), |
| 122 | + NSLayoutConstraint.Create (okButton, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, -RightEdgeMargin), |
| 123 | + NSLayoutConstraint.Create (okButton, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1, 80) |
| 124 | + }); |
| 125 | + |
| 126 | + Frame = new CGRect (CGPoint.Empty, new CGSize (FrameWidth, controlTop + editorHeight * 2)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + internal class VariationFacade |
| 131 | + { |
| 132 | + public PropertyVariationOption Option { get; set; } |
| 133 | + public VariationViewModel ViewModel { get; set; } |
| 134 | + } |
| 135 | +} |
0 commit comments