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 Jul 4, 2019
1 parent f77ce63 commit a892d4e
Show file tree
Hide file tree
Showing 21 changed files with 496 additions and 43 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
28 changes: 27 additions & 1 deletion Xamarin.PropertyEditing.Mac/Controls/Custom/CommandButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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;
}
}
}
28 changes: 28 additions & 0 deletions Xamarin.PropertyEditing.Mac/Controls/Custom/DrawingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,33 @@ public static CommonColor UpdateCMYK (
k: k ?? color.K,
alpha: alpha ?? color.A);
}

public static CGPath ToCGPath (this NSBezierPath nsPath)
{
var cgPath = new CGPath ();
for (var i = 0; i < nsPath.ElementCount; i++) {
NSBezierPathElement type = nsPath.ElementAt (i, out CGPoint[] points);

switch (type) {
case NSBezierPathElement.ClosePath:
cgPath.CloseSubpath ();
break;

case NSBezierPathElement.CurveTo:
cgPath.AddCurveToPoint (points[0], points[1], points[2]);
break;

case NSBezierPathElement.LineTo:
cgPath.AddLineToPoint (points[0]);
break;

case NSBezierPathElement.MoveTo:
cgPath.MoveToPoint (points[0]);
break;
}
}

return cgPath;
}
}
}
86 changes: 54 additions & 32 deletions Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyButton.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using AppKit;
using CoreGraphics;
using Xamarin.PropertyEditing.ViewModels;
Expand All @@ -20,16 +21,25 @@ internal PropertyViewModel ViewModel
set {
if (this.viewModel != null) {
this.viewModel.PropertyChanged -= OnPropertyChanged;

if (this.viewModel.SupportsBindings)
this.viewModel.CreateBindingRequested -= OnBindingRequested;

if (this.viewModel.HasVariations)
this.viewModel.CreateVariantRequested -= OnCreateVariantRequested;
}

this.viewModel = value;

if (this.viewModel != null) {
this.viewModel.PropertyChanged += OnPropertyChanged;

if (this.viewModel.SupportsBindings)
this.viewModel.CreateBindingRequested += OnBindingRequested;

if (this.viewModel.HasVariations)
this.viewModel.CreateVariantRequested += OnCreateVariantRequested;

ValueSourceChanged (this.viewModel.ValueSource);
}

Expand Down Expand Up @@ -93,7 +103,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 @@ -110,7 +120,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 @@ -137,7 +147,7 @@ private void PopUpContextMenu ()
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 @@ -154,34 +164,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 @@ -251,7 +261,7 @@ 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 },
};

Expand All @@ -271,5 +281,17 @@ private void OnBindingRequested (object sender, CreateBindingRequestedEventArgs
e.BindingObject = bindingEditorWindow.ViewModel.SelectedObjects.Single ();
}
}

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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public NSColor TextColor {
internal set { this.label.TextColor = value; }
}

public bool Bordered {
get { return this.label.Bordered; }
internal set { this.label.Bordered = value; }
}

public virtual NSBackgroundStyle BackgroundStyle
{
[Export ("backgroundStyle")] get => this.label.Cell.BackgroundStyle;
Expand Down
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
Loading

0 comments on commit a892d4e

Please sign in to comment.