[Cuis-dev] The Preference model
Hilaire Fernandes
hilaire at drgeo.eu
Sat Apr 30 13:12:48 PDT 2022
> Also sorry for calling you Hilario. It was the iPad autocompleter. It
> was a hilarious mistake ;)
No offense. When traveling in the EU Latin countries (Portgual, Spain,
Italy), people call me Hilario. So you can do the same as you wish.
I amended the class with your suggestions.
The file operations are save, load and merge.
* Save one preference, preferences from one category or all preferences.
* Load replace the system preferences with the one from the file and
* Merge with preferences in a file, with a precedence for the
preferences from the file.
I also add PreferenceNG at: #biggerCursors put: true, it is not
symmetric to the Dictionary syntax but it is handy and read nicely.
--
GNU Dr. Geo
http://drgeo.eu
http://blog.drgeo.eu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20220430/ccd40c61/attachment.htm>
-------------- next part --------------
'From Cuis 6.0 [latest update: #5113] on 30 April 2022 at 10:11:20 pm'!
!classDefinition: #PreferenceNG category: #'System-Support'!
Object subclass: #PreferenceNG
instanceVariableNames: 'name description category value type'
classVariableNames: 'ThePreferences'
poolDictionaries: ''
category: 'System-Support'!
!PreferenceNG commentStamp: 'hlsf 4/30/2022 12:47:46' prior: 0!
My instance is a Preference whose value is of a given class (type), or follow the description of the type instance
- name, category: symbol
- description: string
- type: a class (Boolean, String, Color, BlockClosure, etc.) or an instance (collection, intervale, etc.)
- value: an object whose class match type or the instance description of the type
Modus Operendi
- To create a Preference, invoke the instance creation class method (Preference name:description:category:type:value:). The new preference is automatically remembered. If a preference with same name already exist, its instance is returned.
- To access, invoke as dictionary (Preference at: #biggerCursors), a short cut exist (Preference biggerCursors)!
!PreferenceNG methodsFor: 'printing' stamp: 'hlsf 4/30/2022 14:42:44'!
printOn: aStream
aStream nextPutAll: self class name ;
nextPutAll: ' (';
nextPutAll: name capitalized ;
nextPutAll: ' = ';
nextPutAll: type printString;
nextPutAll: '::';
nextPutAll: value printString;
nextPut: $)! !
!PreferenceNG methodsFor: 'initialization' stamp: 'hlsf 4/30/2022 12:11:31'!
name: nameSymbol description: aString category: categorySymbol type: aType value: aValue
name _ nameSymbol.
description _ aString.
category _ categorySymbol.
type _ aType.
value _ aValue! !
!PreferenceNG methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 14:25:45'!
category
^ category ! !
!PreferenceNG methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 14:25:57'!
description
^ description! !
!PreferenceNG methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 14:26:02'!
name
^ name! !
!PreferenceNG methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 14:25:38'!
value
^ value! !
!PreferenceNG methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 21:29:27'!
value: aValue
type class class == Metaclass "type is a Class"
ifTrue: [
(aValue isKindOf: type) ifFalse: [self error: aValue printString, ' is not a ', type printString].
value _ aValue ]
ifFalse: [ "Should be handled somehow by the preference editor " ].
self class triggerEvent: #preferenceChanged with: self! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
!classDefinition: 'PreferenceNG class' category: #'System-Support'!
PreferenceNG class
instanceVariableNames: ''!
!PreferenceNG class methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 21:11:22'!
all
^ ThePreferences
! !
!PreferenceNG class methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 15:14:39'!
at: symbolName
^ ThePreferences at: symbolName ifAbsent: [self error: 'Unknown preference ', symbolName ]! !
!PreferenceNG class methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 21:31:23'!
at: symbolName put: aValue
| myPref |
myPref _ ThePreferences at: symbolName ifAbsent: [self error: 'Unknown preference ', symbolName ].
myPref value: aValue! !
!PreferenceNG class methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 14:28:57'!
categories
| categories |
categories _ Set new.
ThePreferences values do: [:aPreference | categories add: aPreference category].
^ categories sorted! !
!PreferenceNG class methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 12:23:31'!
cuisDefaults
^ #(
#(#drawKeyboardFocusIndicator true )
#(#balloonHelpEnabled true )
#(#biggerCursors false )
#(#browseWithPrettyPrint false )
#(#caseSensitiveFinds false )
#(#checkForSlips true )
#(#cmdDotEnabled true )
#(#diffsInChangeList true )
#(#diffsWithPrettyPrint false )
#(#menuKeyboardControl true )
#(#optionalButtons true )
#(#extraDebuggerButtons true )
#(#subPixelRenderFonts true )
#(#thoroughSenders true )
#(#cheapWindowReframe false )
#(#syntaxHighlightingAsYouType true )
#(#tapAndHoldEmulatesButton2 true )
#(#clickGrabsMorphs false )
#(#syntaxHighlightingAsYouTypeAnsiAssignment false )
#(#syntaxHighlightingAsYouTypeLeftArrowAssignment false )
#(#wantsMenuIcons true )
)! !
!PreferenceNG class methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 15:13:29'!
of: symbolName
^ (self at: symbolName) value! !
!PreferenceNG class methodsFor: 'accessing' stamp: 'hlsf 4/30/2022 14:29:56'!
select: aCategory
" I select the preferences of the given category "
^ ThePreferences values select: [:aPreference | aPreference category == aCategory ]! !
!PreferenceNG class methodsFor: 'instance creation' stamp: 'hlsf 4/30/2022 14:39:32'!
import
"Import the preferences from the old system"
Preferences preferencesDictionary valuesDo: [:oldPref |
PreferenceNG
name: oldPref name
description: (oldPref instVarNamed: #helpString)
category: (oldPref instVarNamed: #categoryList) first
type: Object
value: oldPref preferenceValue
]! !
!PreferenceNG class methodsFor: 'instance creation' stamp: 'hlsf 4/30/2022 12:17:04'!
name: nameSymbol description: aString category: categorySymbol type: aType value: aValue
(nameSymbol isSymbol or: [categorySymbol isSymbol]) ifFalse:
[self error: 'Preference Name & Category are not valid symbol.'].
^ ThePreferences
at: nameSymbol
ifAbsentPut: [
self new ::
name: nameSymbol
description: aString
category: categorySymbol
type: aType
value: aValue].
! !
!PreferenceNG class methodsFor: 'instance creation' stamp: 'hlsf 4/30/2022 12:28:57'!
reset
ThePreferences _ Dictionary new.
self cuisDefaults do: [:anArray |
self name: anArray first description: '' category: #system type: Boolean value: anArray second]
! !
!PreferenceNG class methodsFor: 'error handling' stamp: 'hlsf 4/30/2022 15:15:57'!
doesNotUnderstand: aMessage
aMessage hasArguments ifTrue: [^ super doesNotUnderstand: aMessage].
^ self of: aMessage selector! !
!PreferenceNG class methodsFor: 'class initialization' stamp: 'hlsf 4/30/2022 21:07:47'!
initialize
self reset! !
!PreferenceNG class methodsFor: 'fileIn/Out' stamp: 'hlsf 4/30/2022 22:10:43'!
loadFrom: aFileEntry
" Load all preferences from file, it replaces entirely the existing preferences "
ThePreferences _ SmartRefStream restoreFromFile: aFileEntry! !
!PreferenceNG class methodsFor: 'fileIn/Out' stamp: 'hlsf 4/30/2022 22:10:56'!
mergeWith: aFileEntry
" Load the preferences from file and merge with the existing preferences.
Duplicates from file replace the ones in system "
| newPrefs |
newPrefs _ SmartRefStream restoreFromFile: aFileEntry.
newPrefs associationsDo: [:assoc | ThePreferences at: assoc key put: assoc value]! !
!PreferenceNG class methodsFor: 'fileIn/Out' stamp: 'hlsf 4/30/2022 21:51:54'!
save: nameSymbol to: aFileEntry
" Save one preference to a file, even for a sole preference we save in a Dictionary"
| myPref |
myPref _ self at: nameSymbol.
SmartRefStream
dumpOnFile: aFileEntry
object: {myPref name -> myPref} asDictionary! !
!PreferenceNG class methodsFor: 'fileIn/Out' stamp: 'hlsf 4/30/2022 21:58:19'!
saveAllTo: aFileEntry
" Save all the preferences to a file "
SmartRefStream dumpOnFile: aFileEntry object: ThePreferences! !
!PreferenceNG class methodsFor: 'fileIn/Out' stamp: 'hlsf 4/30/2022 21:57:59'!
saveCategory: categorySymbol to: aFileEntry
| myPref |
myPref _ self select: categorySymbol.
SmartRefStream
dumpOnFile: aFileEntry
object: (myPref collect: [:aPref | aPref name -> aPref ]) asDictionary! !
PreferenceNG initialize!
More information about the Cuis-dev
mailing list