diff options
author | Larry Ewing <lewing@xamarin.com> | 2018-04-16 23:09:22 +0300 |
---|---|---|
committer | Larry Ewing <lewing@microsoft.com> | 2018-07-16 22:05:48 +0300 |
commit | 017a4d5326935873385445943b94e5418b66349a (patch) | |
tree | 8d2e8f95a22d2425e4d86122f08ac1639b3d9c88 /Xamarin.PropertyEditing.Mac | |
parent | c4ccb2d8b638609a53d5dee2ac7385dd24d557c9 (diff) |
Start cleaning up the ViewModel bits
Diffstat (limited to 'Xamarin.PropertyEditing.Mac')
11 files changed, 162 insertions, 118 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs index 48df154..e2b0b98 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs @@ -20,7 +20,7 @@ namespace Xamarin.PropertyEditing.Mac PreferredContentSize = new CGSize (100, 100); } - protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) + public override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof (BrushPropertyViewModel.MaterialDesign): @@ -29,7 +29,7 @@ namespace Xamarin.PropertyEditing.Mac } } - protected override void OnViewModelChanged (BrushPropertyViewModel oldModel) + public override void OnViewModelChanged (BrushPropertyViewModel oldModel) { if (materialEditor != null) materialEditor.ViewModel = ViewModel; @@ -286,7 +286,7 @@ namespace Xamarin.PropertyEditing.Mac } } - class EmptyBrushEditorViewController : NSViewController + class EmptyBrushEditorViewController : PropertyViewController<BrushPropertyViewModel> { NSButton brushEditor; @@ -295,13 +295,6 @@ namespace Xamarin.PropertyEditing.Mac PreferredContentSize = new CGSize (100, 100); } - BrushPropertyViewModel viewModel; - internal BrushPropertyViewModel ViewModel - { - get => viewModel; - set => viewModel = value; - } - public override void LoadView () { View = brushEditor = new NSButton { @@ -319,7 +312,7 @@ namespace Xamarin.PropertyEditing.Mac bool inhibitSelection; - protected override void OnViewModelChanged (BrushPropertyViewModel oldModel) + public override void OnViewModelChanged (BrushPropertyViewModel oldModel) { inhibitSelection = true; base.OnViewModelChanged(oldModel); @@ -377,7 +370,7 @@ namespace Xamarin.PropertyEditing.Mac inhibitSelection = false; } - protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs args) + public override void OnPropertyChanged (object sender, PropertyChangedEventArgs args) { switch (args.PropertyName) { case nameof (BrushPropertyViewModel.SelectedBrushType): @@ -390,9 +383,13 @@ namespace Xamarin.PropertyEditing.Mac public override void WillSelect(NSTabView tabView, NSTabViewItem item) { + var brushController = item.ViewController as PropertyViewController<BrushPropertyViewModel>; + if (brushController != null) + brushController.ViewModel = ViewModel; + if (inhibitSelection) return; - + base.WillSelect(tabView, item); } diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs index 3ae55fc..98b8b96 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentEditor.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Linq; using AppKit; using CoreGraphics; @@ -91,19 +92,11 @@ namespace Xamarin.PropertyEditing.Mac Editors = CreateEditors (EditorType); this.DoConstraints (Editors.Select ( ce => new[] { - ce.Editor.ConstraintTo (this, (editor, c) => editor.Width == 90), + ce.Editor.ConstraintTo (this, (editor, c) => editor.Width == 90), ce.Editor.ConstraintTo (this, (editor, c) => editor.Height == DefaultControlHeight) }).SelectMany (e => e).ToArray ()); } - public override void UpdateFromColor (CommonColor color) - { - foreach (var e in Editors) { - var editor = e.Editor; - editor.Value = editor.ValueFromColor (color); - } - } - void UpdateComponent (object sender, EventArgs args) { if (ViewModel == null) @@ -114,7 +107,20 @@ namespace Xamarin.PropertyEditing.Mac ViewModel.Color = editor.UpdateColorFromValue (color, editor.Value); } - public override void Layout () + protected override void OnPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnPropertyChanged(sender, e); + switch (e.PropertyName) { + case nameof (SolidBrushViewModel.Color): + foreach (var c in Editors) { + var editor = c.Editor; + editor.Value = editor.ValueFromColor(ViewModel.Color); + } + break; + } + } + + public override void Layout () { base.Layout (); diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentTabVewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentTabVewController.cs index ae1bb45..efecd63 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentTabVewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentTabVewController.cs @@ -20,7 +20,7 @@ namespace Xamarin.PropertyEditing.Mac } } - protected override void OnViewModelChanged(SolidBrushViewModel oldModel) + public override void OnViewModelChanged(SolidBrushViewModel oldModel) { base.OnViewModelChanged(oldModel); var controller = TabView.Item(SelectedTabViewItemIndex).ViewController as ColorComponentViewController; diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentViewController.cs index e7b19ec..291001e 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/ColorComponentViewController.cs @@ -14,17 +14,16 @@ namespace Xamarin.PropertyEditing.Mac this.EditorType = type; } - protected override void OnViewModelChanged (SolidBrushViewModel oldModel) + public override void OnViewModelChanged (SolidBrushViewModel oldModel) { base.OnViewModelChanged (oldModel); editor.ViewModel = ViewModel; } - protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs args) + public override void OnPropertyChanged (object sender, PropertyChangedEventArgs args) { switch (args.PropertyName) { case nameof (SolidBrushViewModel.Color): - break; } } diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/NotifyingViewAdaptor.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/NotifyingViewAdaptor.cs new file mode 100644 index 0000000..92f3388 --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/NotifyingViewAdaptor.cs @@ -0,0 +1,58 @@ +using System; +using System.ComponentModel; + +namespace Xamarin.PropertyEditing.Mac +{ + + interface INotifyingListner<T> where T : NotifyingObject + { + void OnViewModelChanged (T oldModel); + void OnPropertyChanged (object sender, PropertyChangedEventArgs e); + } + + class NotifyingViewAdaptor<T> where T : NotifyingObject + { + public NotifyingViewAdaptor (INotifyingListner<T> listener) + { + this.listener = listener; + } + + INotifyingListner<T> listener; + + T viewModel; + internal T ViewModel + { + get => viewModel; + set + { + var oldModel = viewModel; + if (viewModel == value) + return; + + if (oldModel != null) + oldModel.PropertyChanged -= OnPropertyChanged; + + viewModel = value; + + OnViewModelChanged (oldModel); + viewModel.PropertyChanged += OnPropertyChanged; + } + } + + public void OnViewModelChanged (T oldModel) + { + listener.OnViewModelChanged (oldModel); + } + + public void OnPropertyChanged (object sender, PropertyChangedEventArgs e) + { + listener.OnPropertyChanged (sender, e); + } + + public void Disconnect () + { + if (viewModel != null) + viewModel.PropertyChanged -= OnPropertyChanged; + } + } +} diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyTabViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyTabViewController.cs index 4e0ca93..0884588 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyTabViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyTabViewController.cs @@ -5,38 +5,27 @@ using Xamarin.PropertyEditing.ViewModels; namespace Xamarin.PropertyEditing.Mac { - class PropertyTabViewController<T> : NSTabViewController where T : NotifyingObject + class PropertyTabViewController<T> : NSTabViewController, INotifyingListner<T> where T : NotifyingObject { public PropertyTabViewController () : base () { + adaptor = new NotifyingViewAdaptor<T> (this); } - protected virtual void OnPropertyChanged (object sender, PropertyChangedEventArgs args) + protected NotifyingViewAdaptor<T> adaptor { get; } + + public virtual void OnPropertyChanged (object sender, PropertyChangedEventArgs args) { } - protected virtual void OnViewModelChanged (T oldModel) + public virtual void OnViewModelChanged (T oldModel) { } - T viewModel; internal T ViewModel { - get => viewModel; - set - { - var oldModel = viewModel; - if (viewModel == value) - return; - - if (viewModel != null) - viewModel.PropertyChanged -= OnPropertyChanged; - - viewModel = value; - OnViewModelChanged (oldModel); - - viewModel.PropertyChanged += OnPropertyChanged; - } + get => adaptor.ViewModel; + set => adaptor.ViewModel = value; } } } diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyViewController.cs index 36a3394..ebf4d1f 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/PropertyViewController.cs @@ -10,33 +10,26 @@ using Xamarin.PropertyEditing.ViewModels; namespace Xamarin.PropertyEditing.Mac { - class PropertyViewController<T> : NSViewController where T : NotifyingObject + class PropertyViewController<T> : NSViewController, INotifyingListner<T> where T : NotifyingObject { - T viewModel; internal T ViewModel { - get => viewModel; - set - { - var oldModel = viewModel; - if (viewModel == value) - return; - - if (oldModel != null) - oldModel.PropertyChanged -= OnPropertyChanged; - - viewModel = value; + get => adaptor.ViewModel; + set => adaptor.ViewModel = value; + } - OnViewModelChanged (oldModel); - viewModel.PropertyChanged += OnPropertyChanged; - } + public PropertyViewController () + { + adaptor = new NotifyingViewAdaptor<T> (this); } - protected virtual void OnViewModelChanged (T oldModel) + protected NotifyingViewAdaptor<T> adaptor { get; } + + public virtual void OnViewModelChanged (T oldModel) { } - protected virtual void OnPropertyChanged (object sender, PropertyChangedEventArgs e) + public virtual void OnPropertyChanged (object sender, PropertyChangedEventArgs e) { } } diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/ResourceBrushViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/ResourceBrushViewController.cs index caee3dc..7a42081 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/ResourceBrushViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/ResourceBrushViewController.cs @@ -166,7 +166,7 @@ namespace Xamarin.PropertyEditing.Mac } Resource resource; - protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) + public override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof (BrushPropertyViewModel.Resource): @@ -205,8 +205,9 @@ namespace Xamarin.PropertyEditing.Mac resourceSelector.SelectRow (index, false); } - protected override void OnViewModelChanged (BrushPropertyViewModel oldModel) + public override void OnViewModelChanged (BrushPropertyViewModel oldModel) { + base.OnViewModelChanged (oldModel); if (resourceSelector != null) { viewDelegate.ViewModel = ViewModel; resourceSelector.ViewModel = ViewModel?.ResourceSelector; diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs index 370a657..f68d6fd 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditor.cs @@ -12,64 +12,49 @@ using Xamarin.PropertyEditing.ViewModels; namespace Xamarin.PropertyEditing.Mac { - abstract class ColorEditorView : NSControl + abstract class ColorEditorView : NSControl, INotifyingListner<SolidBrushViewModel> { - SolidBrushViewModel viewModel; protected const float padding = 3; + protected NotifyingViewAdaptor<SolidBrushViewModel> adaptor { get; } public ColorEditorView (IntPtr handle) : base (handle) { + adaptor = new NotifyingViewAdaptor<SolidBrushViewModel> (this); } [Export ("initWithCoder:")] public ColorEditorView (NSCoder coder) : base (coder) { + adaptor = new NotifyingViewAdaptor<SolidBrushViewModel> (this); } public ColorEditorView (CGRect frame) : base (frame) { + adaptor = new NotifyingViewAdaptor<SolidBrushViewModel> (this); } public ColorEditorView () : base () { + adaptor = new NotifyingViewAdaptor<SolidBrushViewModel> (this); } - public SolidBrushViewModel ViewModel - { - get => viewModel; - set - { - if (value == viewModel) - return; - - if (viewModel != null) - viewModel.PropertyChanged -= ModelChanged; - - var oldModel = viewModel; - viewModel = value; - ModelSet (oldModel); - viewModel.PropertyChanged += ModelChanged; - } + public SolidBrushViewModel ViewModel { + get => adaptor.ViewModel; + set => adaptor.ViewModel = value; } - protected virtual void ModelSet (SolidBrushViewModel oldModel) + protected virtual void OnViewModelChanged (SolidBrushViewModel oldModel) { - ModelChanged (ViewModel, new PropertyChangedEventArgs (nameof (SolidBrushViewModel.Color))); + OnPropertyChanged (ViewModel, new PropertyChangedEventArgs (nameof (SolidBrushViewModel.Color))); } - protected virtual void ModelChanged (object sender, PropertyChangedEventArgs args) + protected virtual void OnPropertyChanged (object sender, PropertyChangedEventArgs e) { - switch (args.PropertyName) { - case nameof (SolidBrushViewModel.Color): - UpdateFromColor (ViewModel.Color); - break; - } } public override void MouseDragged (NSEvent theEvent) { //base.MouseDragged (theEvent); - UpdateFromEvent (theEvent); } @@ -79,8 +64,6 @@ namespace Xamarin.PropertyEditing.Mac UpdateFromEvent (theEvent); } - public abstract void UpdateFromColor (CommonColor color); - public virtual void UpdateFromEvent (NSEvent theEvent) { } @@ -91,9 +74,18 @@ namespace Xamarin.PropertyEditing.Mac if (!disposing) return; - - if (ViewModel != null) - ViewModel.PropertyChanged -= ModelChanged; + + adaptor.Disconnect (); + } + + void INotifyingListner<SolidBrushViewModel>.OnViewModelChanged (SolidBrushViewModel oldModel) + { + OnViewModelChanged (oldModel); + } + + void INotifyingListner<SolidBrushViewModel>.OnPropertyChanged (object sender, PropertyChangedEventArgs e) + { + OnPropertyChanged (sender, e); } } @@ -226,7 +218,6 @@ namespace Xamarin.PropertyEditing.Mac Cmyk }; - class HistoryLayer : ColorEditorLayer { const float Margin = 3; @@ -434,7 +425,7 @@ namespace Xamarin.PropertyEditing.Mac var loc = location; var clos = Math.Min (Colors.Frame.Height, Math.Max (0, loc.Y - Colors.Frame.Y)); - Grip.Frame = new CGRect (1, loc.Y - Grip.Frame.Height / 2, Frame.Width - 2, 2 * GripRadius); + Grip.Frame = new CGRect (1, clos + Colors.Frame.Y - Grip.Frame.Height / 2, Frame.Width - 2, 2 * GripRadius); var hue = (1 - clos/ Colors.Frame.Height) * 360; if (viewModel == null) @@ -503,40 +494,54 @@ namespace Xamarin.PropertyEditing.Mac AddSubview (componentTabs.View); } + ColorEditorLayer active; + public override void UpdateFromEvent (NSEvent theEvent) { + } + + public override void MouseDown (NSEvent theEvent) + { var location = ConvertPointFromView (theEvent.LocationInWindow, null); location = ConvertPointToLayer (location); - + active = null; foreach (var layer in Layer.Sublayers) { var hit = layer.HitTest (location); for (var c = hit; c != null; c = c.SuperLayer) { - var editor = c as ColorEditorLayer; - if (editor != null) { - editor.UpdateFromLocation ( + active = c as ColorEditorLayer; + if (active != null) { + active.UpdateFromLocation ( ViewModel, - Layer.ConvertPointToLayer (location, editor)); + Layer.ConvertPointToLayer (location, active)); return; } } } } - public override void UpdateFromColor (CommonColor color) + public override void MouseDragged(NSEvent theEvent) + { + var location = ConvertPointFromView (theEvent.LocationInWindow, null); + active?.UpdateFromLocation ( + ViewModel, + Layer.ConvertPointToLayer (location, active)); + } + + protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) { - //componentTabs.UpdateFromColor (ViewModel.Color); foreach (var editor in Layer.Sublayers.OfType<ColorEditorLayer> ()) { editor.UpdateFromModel (ViewModel); - } } - protected override void ModelSet(SolidBrushViewModel oldModel) + protected override void OnViewModelChanged (SolidBrushViewModel oldModel) { - base.ModelSet(oldModel); - + base.OnViewModelChanged(oldModel); componentTabs.ViewModel = ViewModel; + foreach (var editor in Layer.Sublayers.OfType<ColorEditorLayer> ()) { + editor.UpdateFromModel (ViewModel); + } } public override void Layout () diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditorViewController.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditorViewController.cs index 394a629..bec79e5 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditorViewController.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/SolidColorBrushEditorViewController.cs @@ -8,12 +8,7 @@ namespace Xamarin.PropertyEditing.Mac { SolidColorBrushEditor brushEditor; - public SolidColorBrushEditorViewController () - { - //PreferredMinimumSize = new CGSize (200, 200); - } - - protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) + public override void OnPropertyChanged (object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof (BrushPropertyViewModel.Solid): @@ -27,7 +22,7 @@ namespace Xamarin.PropertyEditing.Mac } } - protected override void OnViewModelChanged (BrushPropertyViewModel oldModel) + public override void OnViewModelChanged (BrushPropertyViewModel oldModel) { if (brushEditor != null) brushEditor.ViewModel = ViewModel?.Solid; diff --git a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj index 7ef7e66..45427ab 100644 --- a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj +++ b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj @@ -104,6 +104,7 @@ <Compile Include="Controls\Custom\ColorComponentEditor.cs" />
<Compile Include="Controls\Custom\ColorComponentTabVewController.cs" />
<Compile Include="Controls\Custom\ColorComponentViewController.cs" />
+ <Compile Include="Controls\Custom\NotifyingViewAdaptor.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controls\" />
|