Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Commit

Permalink
[Mac] Initial Variations Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominique Louis authored and CartBlanche committed May 20, 2019
1 parent 807d5c7 commit c0bfc0c
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 63 deletions.
135 changes: 135 additions & 0 deletions Xamarin.PropertyEditing.Mac/Controls/CreateVariantView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System;
using System.Reflection;
using AppKit;
using CoreGraphics;
using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
internal class CreateVariantView
: BasePopOverViewModelControl
{
private const int FrameWidth = 250;
private const int ControlSpacing = 7;
private const int RightEdgeMargin = 12;

public CreateVariantView (IHostResourceProvider hostResources, PropertyViewModel propertyViewModel)
: base (hostResources, propertyViewModel, Properties.Resources.AddVariationTitle, "pe-resource-editor-32")
{
Initialize (new CreateVariantViewModel (propertyViewModel.Property));
}

private void Initialize (CreateVariantViewModel createVariantViewModel)
{
Frame = new CGRect (CGPoint.Empty, new CGSize (FrameWidth, 240));

int editorHeight = 18;

var FrameWidthThird = Frame.Width / 3;

var introduceVariation = new UnfocusableTextField {
StringValue = Properties.Resources.AddVariationHelpText,
TranslatesAutoresizingMaskIntoConstraints = false,
};

AddSubview (introduceVariation);

this.AddConstraints (new[] {
NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 37f),
NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 18f),
NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, 0f),
NSLayoutConstraint.Create (introduceVariation, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, editorHeight),
});

var controlTop = 60;

var okButton = new FocusableButton {
BezelStyle = NSBezelStyle.Rounded,
Enabled = false,
Highlighted = true,
Title = Properties.Resources.AddVariation,
TranslatesAutoresizingMaskIntoConstraints = false,
};

foreach (var viewModel in createVariantViewModel.VariationCategories) {

var name = new UnfocusableTextField {
Alignment = NSTextAlignment.Right,
StringValue = viewModel.Name + ":",
TranslatesAutoresizingMaskIntoConstraints = false,
};

AddSubview (name);

this.AddConstraints (new[] {
NSLayoutConstraint.Create (name, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, controlTop),
NSLayoutConstraint.Create (name, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, FrameWidthThird),
NSLayoutConstraint.Create (name, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, editorHeight)
});

var popUpButton = new FocusablePopUpButton {
ControlSize = NSControlSize.Small,
Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
TranslatesAutoresizingMaskIntoConstraints = false,
};

popUpButton.Activated += (o, e) => {
if (o is FocusablePopUpButton fpb) {
if (fpb.SelectedItem.RepresentedObject is NSObjectFacade menuObjectFacade) {
if (menuObjectFacade.Target is VariationFacade vf) {
vf.ViewModel.SelectedOption = vf.Option;
okButton.Enabled = createVariantViewModel.CreateVariantCommand.CanExecute (null);
}
}
}
};

var popUpButtonList = new NSMenu ();
popUpButton.Menu = popUpButtonList;

AddSubview (popUpButton);

this.AddConstraints (new[] {
NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, controlTop),
NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, FrameWidthThird + ControlSpacing),
NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, -FrameWidthThird - ControlSpacing - RightEdgeMargin),
NSLayoutConstraint.Create (popUpButton, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, editorHeight)
});

foreach (var variation in viewModel.Variations) {
popUpButtonList.AddItem (new NSMenuItem (variation.Name) {
RepresentedObject = new NSObjectFacade (
new VariationFacade {
Option = variation,
ViewModel = viewModel })
});
}

controlTop += editorHeight + 8;
}

okButton.Activated += (sender, e) => {
// TODO Add the Variation

PopOver.Close ();
};

AddSubview (okButton);

this.AddConstraints (new[] {
NSLayoutConstraint.Create (okButton, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1, -RightEdgeMargin),
NSLayoutConstraint.Create (okButton, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, -RightEdgeMargin),
NSLayoutConstraint.Create (okButton, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1, 80)
});

Frame = new CGRect (CGPoint.Empty, new CGSize (FrameWidth, controlTop + editorHeight * 2));
}
}

internal class VariationFacade
{
public PropertyVariationOption Option { get; set; }
public VariationViewModel ViewModel { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AppKit;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
Expand All @@ -9,6 +10,8 @@ internal class BasePopOverViewModelControl : BasePopOverControl
{
internal PropertyViewModel ViewModel { get; }

public AutoClosePopOver PopOver { get; internal set; }

public BasePopOverViewModelControl (IHostResourceProvider hostResources, PropertyViewModel viewModel, string title, string imageNamed)
: base (hostResources, title, imageNamed)
{
Expand Down
90 changes: 60 additions & 30 deletions Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal PropertyViewModel ViewModel
set {
if (this.viewModel != null) {
this.viewModel.PropertyChanged -= OnPropertyChanged;
this.viewModel.CreateVariantRequested -= OnCreateVariantRequested;
}

this.viewModel = value;
Expand Down Expand Up @@ -110,13 +111,27 @@ private void PopUpContextMenu ()
this.popUpContextMenu.AddItem (NSMenuItem.SeparatorItem);

// TODO If we add more menu items consider making the Label/Command a dictionary that we can iterate over to populate everything.
this.popUpContextMenu.AddItem (new CommandMenuItem (Properties.Resources.Reset, viewModel.ClearValueCommand) {
this.popUpContextMenu.AddItem (new CommandMenuItem (Properties.Resources.Reset, this.viewModel.ClearValueCommand) {
AttributedTitle = new Foundation.NSAttributedString (
Properties.Resources.Reset,
new CoreText.CTStringAttributes {
Font = new CoreText.CTFont (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize + 1),
})
});

this.popUpContextMenu.AddItem (NSMenuItem.SeparatorItem);

if (this.viewModel.HasVariations) {
this.popUpContextMenu.AddItem (new CommandMenuItem (Properties.Resources.AddVariant, this.viewModel.RequestCreateVariantCommand) {
AttributedTitle = new Foundation.NSAttributedString (
Properties.Resources.AddVariant,
new CoreText.CTStringAttributes {
Font = new CoreText.CTFont (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize + 1),
})
});

this.viewModel.CreateVariantRequested += OnCreateVariantRequested;
}
}

var menuOrigin = this.Superview.ConvertPointToView (new CGPoint (Frame.Location.X - 1, Frame.Location.Y + Frame.Size.Height + 4), null);
Expand Down Expand Up @@ -166,34 +181,34 @@ private void ToggleFocusImage (bool focused = false)
private void ValueSourceChanged (ValueSource valueSource)
{
switch (valueSource) {
case ValueSource.Binding:
ToolTip = Properties.Resources.Binding;
break;

case ValueSource.Default:
ToolTip = Properties.Resources.Default;
return;

case ValueSource.Local:
ToolTip = Properties.Resources.Local;
break;

case ValueSource.Inherited:
ToolTip = Properties.Resources.Inherited;
break;

case ValueSource.Resource:
ToolTip = (this.viewModel?.Resource?.Name != null) ? String.Format (Properties.Resources.ResourceWithName, this.viewModel.Resource.Name) : Properties.Resources.Resource;
break;

case ValueSource.Unset:
ToolTip = Properties.Resources.Unset;
break;

default:
// To Handle ValueSource.DefaultStyle, ValueSource.Style etc.
ToolTip = string.Empty;
break;
case ValueSource.Binding:
ToolTip = Properties.Resources.Binding;
break;

case ValueSource.Default:
ToolTip = Properties.Resources.Default;
return;

case ValueSource.Local:
ToolTip = Properties.Resources.Local;
break;

case ValueSource.Inherited:
ToolTip = Properties.Resources.Inherited;
break;

case ValueSource.Resource:
ToolTip = (this.viewModel?.Resource?.Name != null) ? String.Format (Properties.Resources.ResourceWithName, this.viewModel.Resource.Name) : Properties.Resources.Resource;
break;

case ValueSource.Unset:
ToolTip = Properties.Resources.Unset;
break;

default:
// To Handle ValueSource.DefaultStyle, ValueSource.Style etc.
ToolTip = string.Empty;
break;
}

ToggleFocusImage ();
Expand Down Expand Up @@ -228,13 +243,28 @@ private void OnResourceRequested (object sender, EventArgs e)
Appearance = EffectiveAppearance
};

var resourceSelectorPopOver = new AutoClosePopOver(this.hostResources) {
var resourceSelectorPopOver = new AutoClosePopOver (this.hostResources) {
ContentViewController = new NSViewController (null, null) { View = requestResourceView },
};

requestResourceView.PopOver = resourceSelectorPopOver;

resourceSelectorPopOver.Show (requestResourceView.Frame, (NSView)this, NSRectEdge.MinYEdge);
}

private void OnCreateVariantRequested (object sender, CreateVariantEventArgs e)
{
var createVariantView = new CreateVariantView (this.hostResources, this.viewModel) {
Appearance = EffectiveAppearance
};

var createVariantPopOver = new AutoClosePopOver (this.hostResources) {
ContentViewController = new NSViewController (null, null) { View = createVariantView },
};

createVariantView.PopOver = createVariantPopOver;

createVariantPopOver.Show (createVariantView.Frame, (NSView)this, NSRectEdge.MinYEdge);
}
}
}
2 changes: 0 additions & 2 deletions Xamarin.PropertyEditing.Mac/Controls/CustomExpressionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ internal class CustomExpressionView : BasePopOverViewModelControl
private const string PreviewCustomExpressionString = "PreviewCustomExpression";
private const string AutocompleteItemsString = "AutocompleteItems";

public AutoClosePopOver PopOver { get; internal set; }

public CustomExpressionView (IHostResourceProvider hostResources, PropertyViewModel viewModel)
: base (hostResources, viewModel, Properties.Resources.CustomExpression, "pe-custom-expression-32")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ internal class RequestResourceView
NSSegmentedControl segmentedControl;
NSButton showPreviewImage;
RequestResourcePanel resourceSelectorPanel;

public NSPopover PopOver { get; internal set; }

private bool showPreview;
public bool ShowPreview
{
Expand Down
55 changes: 27 additions & 28 deletions Xamarin.PropertyEditing/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Xamarin.PropertyEditing/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,8 @@
<data name="RemoveVariant" xml:space="preserve">
<value>Remove Variant</value>
</data>
<data name="AddVariation" xml:space="preserve">
<value>Add</value>
<comment>Button text telling the user clicking this will add the variation</comment>
</data>
</root>

0 comments on commit c0bfc0c

Please sign in to comment.