1 /* 2 Copyright (c) 2013 Andrey Penechko 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license the "Software" to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module anchovy.utils.flexibleobject.flexibleproperty; 30 31 import std.variant : Variant; 32 33 import anchovy.utils.flexibleobject.flexibleobject : FlexibleObject; 34 import anchovy.utils.signal : Signal; 35 36 abstract class IProperty 37 { 38 // called when value changes. 39 alias PropertyChangedSignal = Signal!(FlexibleObject, Variant); 40 alias PropertyChangingSignal = Signal!(FlexibleObject, Variant*); 41 42 Variant value() @property; 43 Variant value(Variant) @property; 44 45 ref PropertyChangedSignal valueChanged() @property; 46 ref PropertyChangingSignal valueChanging() @property; 47 48 alias value this; 49 } 50 51 // Simple storage property 52 class ValueProperty : IProperty 53 { 54 this(FlexibleObject owner, Variant value = Variant(null)) 55 { 56 _value = value; 57 _owner = owner; 58 } 59 60 this(T)(FlexibleObject owner, T value) 61 { 62 _owner = owner; 63 _value = Variant(value); 64 } 65 66 override Variant value() @property 67 { 68 return _value; 69 } 70 71 override Variant value(Variant newValue) @property 72 { 73 // Strange error when comparing two variant with Widget[] inside 74 // object.Error: object.Exception@src\object_.d(107): 75 // need opCmp for class anchovy.gui.widget.Widget 76 if (newValue == _value) return _value; 77 78 if (_isChanging) 79 { 80 return _value; 81 } 82 83 _isChanging = true; 84 85 _valueChanging.emit(_owner, &newValue); 86 87 if (newValue == _value) 88 { 89 _isChanging = false; 90 return _value; 91 } 92 _value = newValue; 93 _valueChanged.emit(_owner, _value); 94 95 _isChanging = false; 96 97 return _value; 98 } 99 100 override ref PropertyChangedSignal valueChanged() @property 101 { 102 return _valueChanged; 103 } 104 105 override ref PropertyChangingSignal valueChanging() @property 106 { 107 return _valueChanging; 108 } 109 110 Variant _value; 111 PropertyChangedSignal _valueChanged; 112 PropertyChangingSignal _valueChanging; 113 FlexibleObject _owner; 114 bool _isChanging = false; 115 }