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 Jun 26, 2019
1 parent 50cbb49 commit 774fc34
Show file tree
Hide file tree
Showing 18 changed files with 422 additions and 72 deletions.
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
30 changes: 28 additions & 2 deletions Xamarin.PropertyEditing.Mac/Controls/Custom/CommandButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Windows.Input;
using AppKit;

namespace Xamarin.PropertyEditing.Mac.Controls.Custom
namespace Xamarin.PropertyEditing.Mac
{
internal class CommandButton : NSButton
{
Expand Down Expand Up @@ -34,7 +34,33 @@ public CommandButton ()
private void CanExecuteChanged (object sender, EventArgs e)
{
if (sender is ICommand cmd)
this.Enabled = cmd.CanExecute (null);
Enabled = cmd.CanExecute (null);
}
}

internal class FocusableCommandButton : CommandButton
{
public override bool CanBecomeKeyView { get { return Enabled; } }

public FocusableCommandButton ()
{
AllowsExpansionToolTips = true;
AllowsMixedState = true;
Cell.LineBreakMode = NSLineBreakMode.TruncatingTail;
Cell.UsesSingleLineMode = true;
ControlSize = NSControlSize.Small;
Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small));
Title = string.Empty;
TranslatesAutoresizingMaskIntoConstraints = false;
}

public override bool BecomeFirstResponder ()
{
var willBecomeFirstResponder = base.BecomeFirstResponder ();
if (willBecomeFirstResponder) {
ScrollRectToVisible (Bounds);
}
return willBecomeFirstResponder;
}
}
}
84 changes: 51 additions & 33 deletions Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyButton.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using AppKit;
using CoreGraphics;
using Xamarin.PropertyEditing.ViewModels;
Expand All @@ -19,11 +20,16 @@ internal PropertyViewModel ViewModel
set {
if (this.viewModel != null) {
this.viewModel.PropertyChanged -= OnPropertyChanged;
if (this.viewModel.HasVariations)
this.viewModel.CreateVariantRequested -= OnCreateVariantRequested;
}

this.viewModel = value;
if (this.viewModel != null) {
this.viewModel.PropertyChanged += OnPropertyChanged;
if (this.viewModel.HasVariations)
this.viewModel.CreateVariantRequested += OnCreateVariantRequested;

ValueSourceChanged (this.viewModel.ValueSource);
}
}
Expand Down Expand Up @@ -82,7 +88,7 @@ private void PopUpContextMenu ()
AttributedTitle = new Foundation.NSAttributedString (
Properties.Resources.CustomExpressionEllipsis,
new CoreText.CTStringAttributes {
Font = new CoreText.CTFont (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize + 1),
Font = new CoreText.CTFont (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultMenuFontSize),
})
};

Expand All @@ -99,7 +105,7 @@ private void PopUpContextMenu ()
AttributedTitle = new Foundation.NSAttributedString (
Properties.Resources.ResourceEllipsis,
new CoreText.CTStringAttributes {
Font = new CoreText.CTFont (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultFontSize + 1),
Font = new CoreText.CTFont (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultMenuFontSize),
})
};

Expand All @@ -110,11 +116,11 @@ 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),
Font = new CoreText.CTFont (PropertyEditorControl.DefaultFontName, PropertyEditorControl.DefaultMenuFontSize),
})
});
}
Expand All @@ -131,34 +137,34 @@ private void ToggleFocusImage (bool focused = false)
if (this.viewModel != null) {

switch (this.viewModel.ValueSource) {
case ValueSource.Binding:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-bound-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-bound-mac-10");
break;

case ValueSource.Default:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-default-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-default-mac-10");
break;

case ValueSource.Local:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-local-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-local-mac-10");
break;

case ValueSource.Inherited:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-10");
break;

case ValueSource.Resource:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-10");
break;

case ValueSource.Unset:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-default-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-default-mac-10");
break;

default:
// To Handle ValueSource.DefaultStyle, ValueSource.Style etc.
Image = null;
break;
case ValueSource.Binding:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-bound-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-bound-mac-10");
break;

case ValueSource.Default:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-default-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-default-mac-10");
break;

case ValueSource.Local:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-local-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-local-mac-10");
break;

case ValueSource.Inherited:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-10");
break;

case ValueSource.Resource:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-inherited-mac-10");
break;

case ValueSource.Unset:
Image = focused ? this.hostResources.GetNamedImage ("pe-property-button-default-mac-active-10") : this.hostResources.GetNamedImage ("pe-property-button-default-mac-10");
break;

default:
// To Handle ValueSource.DefaultStyle, ValueSource.Style etc.
Image = null;
break;
}
}
}
Expand Down Expand Up @@ -228,13 +234,25 @@ 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 createVariantWindow = new CreateVariantWindow (this.hostResources, this.viewModel) {
Appearance = EffectiveAppearance,
};

var result = (NSModalResponse)(int)NSApplication.SharedApplication.RunModalForWindow (createVariantWindow);
if (result == NSModalResponse.OK) {
e.Variation = Task.FromResult (createVariantWindow.ViewModel.Variation);
}
}
}
}
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
4 changes: 2 additions & 2 deletions Xamarin.PropertyEditing.Mac/Controls/EditorContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public NSView LeftEdgeView

public override void ViewWillMoveToSuperview (NSView newSuperview)
{
if (newSuperview == null && EditorView != null)
EditorView.ViewModel = null;
if (newSuperview == null)
ViewModel = null;

base.ViewWillMoveToSuperview (newSuperview);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Xamarin.PropertyEditing.Mac
internal abstract class EntryPropertyEditor<T>
: PropertyEditorControl<PropertyViewModel<T>>
{
public EntryPropertyEditor (IHostResourceProvider hostResources)
protected EntryPropertyEditor (IHostResourceProvider hostResources)
: base (hostResources)
{
Entry = new PropertyTextField {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public IHostResourceProvider HostResources

public const int DefaultControlHeight = 24;
public const int DefaultFontSize = 11;
public const int DefaultMenuFontSize = DefaultFontSize + 1;
public const int DefaultPropertyLabelFontSize = 11;
public const int DefaultDescriptionLabelFontSize = 9;
public const string DefaultFontName = ".AppleSystemUIFont";
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
80 changes: 79 additions & 1 deletion Xamarin.PropertyEditing.Mac/Controls/StringEditorControl.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using AppKit;

using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
Expand Down Expand Up @@ -79,6 +80,79 @@ protected override void OnViewModelChanged (PropertyViewModel oldModel)
this.editorInputModeConstraint.Active = ViewModel.HasInputModes;
}

if (ViewModel.HasVariations) {
if (ViewModel.Variation != null) {

var selectedVariationOptions = string.Empty;
foreach (PropertyVariationOption item in ViewModel.Variation) {
selectedVariationOptions += string.Format ("[{0}:{1}]", item.Category, item.Name);
}

if (ViewModel.GetIsLastVariant ()) {
if (this.endVariantButton == null) {
this.endVariantButton = new NSButton {
TranslatesAutoresizingMaskIntoConstraints = false,
Title = "└",
};

AddSubview (this.endVariantButton);
AddConstraints (new[] {
NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.endVariantButton, NSLayoutAttribute.Right, 1, 4),
NSLayoutConstraint.Create (this.endVariantButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
NSLayoutConstraint.Create (this.endVariantButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0),
NSLayoutConstraint.Create (this.endVariantButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, 18f),
});
}
Entry.StringValue = selectedVariationOptions;
} else {
if (this.midVariantButton == null) {
this.midVariantButton = new NSButton {
TranslatesAutoresizingMaskIntoConstraints = false,
Title = "├",
};

AddSubview (this.midVariantButton);
AddConstraints (new[] {
NSLayoutConstraint.Create (this.midVariantButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
NSLayoutConstraint.Create (this.midVariantButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0),
NSLayoutConstraint.Create (this.midVariantButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, 18f),
});
}
Entry.StringValue = selectedVariationOptions;
}

if (this.deleteVariantButton == null) {
this.deleteVariantButton = new CommandButton {
Command = ViewModel.RemoveVariationCommand,
TranslatesAutoresizingMaskIntoConstraints = false,
Title = "X",
};

AddSubview (this.deleteVariantButton);
AddConstraints (new[] {
NSLayoutConstraint.Create (this.deleteVariantButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
NSLayoutConstraint.Create (this.deleteVariantButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 20),
NSLayoutConstraint.Create (this.deleteVariantButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, 18f),
});
}
} else {
if (this.addVariantButton == null) {
this.addVariantButton = new CommandButton {
Command = ViewModel.RequestCreateVariantCommand,
TranslatesAutoresizingMaskIntoConstraints = false,
Title = "┼",
};

AddSubview (this.addVariantButton);
AddConstraints (new[] {
NSLayoutConstraint.Create (this.addVariantButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
NSLayoutConstraint.Create (this.addVariantButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0),
NSLayoutConstraint.Create (this.addVariantButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, 18f),
});
}
}
}

SetEnabled ();
}

Expand All @@ -105,6 +179,10 @@ protected override void UpdateAccessibilityValues ()
private NSLayoutConstraint editorInputModeConstraint;
private NSPopUpButton inputModePopup;
private IReadOnlyList<InputMode> viewModelInputModes;
private NSButton addVariantButton;
private NSButton endVariantButton;
private NSButton midVariantButton;
private NSButton deleteVariantButton;
}
}

Loading

0 comments on commit 774fc34

Please sign in to comment.