Moka-Layouts.st 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. Smalltalk current createPackage: 'Moka-Layouts'!
  2. Object subclass: #MKLayout
  3. instanceVariableNames: 'properties'
  4. package: 'Moka-Layouts'!
  5. !MKLayout methodsFor: 'accessing'!
  6. bottom: aNumber
  7. properties
  8. at: 'bottom'
  9. put: (self propertyLabelled: 'bottom' value: aNumber).
  10. (self hasProperty: 'top') ifTrue: [
  11. self removeProperty: 'height' ].
  12. self removeProperty: 'centerY'
  13. !
  14. centerX: aNumber
  15. properties
  16. at: 'centerX'
  17. put: (MKHorizontalCenteringLayoutProperty layout: self value: aNumber).
  18. self
  19. removeProperty: 'left';
  20. removeProperty: 'right'
  21. !
  22. centerY: aNumber
  23. properties
  24. at: 'centerY'
  25. put: (MKVerticalCenteringLayoutProperty layout: self value: aNumber).
  26. self
  27. removeProperty: 'top';
  28. removeProperty: 'bottom'
  29. !
  30. height
  31. ^ properties
  32. at: 'height'
  33. ifPresent: [ :property | property value ]
  34. ifAbsent: [ 1 ]
  35. !
  36. height: aNumber
  37. properties
  38. at: 'height'
  39. put: (self propertyLabelled: 'height' value: aNumber).
  40. (self hasProperty: 'top') ifTrue: [
  41. self removeProperty: 'bottom' ]
  42. !
  43. left: aNumber
  44. properties
  45. at: 'left'
  46. put: (self propertyLabelled: 'left' value: aNumber).
  47. (self hasProperty: 'width') ifTrue: [
  48. self removeProperty: 'right' ].
  49. self removeProperty: 'centerX'
  50. !
  51. right: aNumber
  52. properties
  53. at: 'right'
  54. put: (self propertyLabelled: 'right' value: aNumber).
  55. (self hasProperty: 'width') ifTrue: [
  56. self removeProperty: 'left' ].
  57. self removeProperty: 'centerX'
  58. !
  59. top: aNumber
  60. properties
  61. at: 'top'
  62. put: (self propertyLabelled: 'top' value: aNumber).
  63. (self hasProperty: 'height') ifTrue: [
  64. self removeProperty: 'bottom' ].
  65. self removeProperty: 'centerY'
  66. !
  67. width
  68. ^ properties
  69. at: 'width'
  70. ifPresent: [ :property | property value ]
  71. ifAbsent: [ 1 ]
  72. !
  73. width: aNumber
  74. properties
  75. at: 'width'
  76. put: (self propertyLabelled: 'width' value: aNumber).
  77. (self hasProperty: 'left') ifTrue: [
  78. self removeProperty: 'right' ]
  79. ! !
  80. !MKLayout methodsFor: 'converting'!
  81. asCssString
  82. ^ String streamContents: [ :stream |
  83. properties valuesDo: [ :each |
  84. each printCssOn: stream.
  85. stream << ';' ] ]
  86. ! !
  87. !MKLayout methodsFor: 'factory'!
  88. propertyLabelled: aString value: aValue
  89. ^ MKLabelledLayoutProperty layout: self label: aString value: aValue
  90. ! !
  91. !MKLayout methodsFor: 'initialization'!
  92. initialize
  93. super initialize.
  94. properties := Dictionary new
  95. ! !
  96. !MKLayout methodsFor: 'private'!
  97. hasProperty: aString
  98. ^ properties includesKey: aString
  99. !
  100. removeProperty: aString
  101. properties remove: aString ifAbsent: []
  102. ! !
  103. MKLayout subclass: #MKLabelLayout
  104. instanceVariableNames: ''
  105. package: 'Moka-Layouts'!
  106. !MKLabelLayout commentStamp!
  107. I am a specialized layout for label views. I can set a `textAlign` property, taking a string argument, `'left'`, `'center'` or `'right'`.!
  108. !MKLabelLayout methodsFor: 'accessing'!
  109. textAlign: aString
  110. "Map to CSS' text-align property. Possible values are `'left'`, `'center'` and `'right'`"
  111. properties
  112. at: 'text-align'
  113. put: (self propertyLabelled: 'text-align' value: aString)
  114. ! !
  115. MKLayout subclass: #MKPaneLayout
  116. instanceVariableNames: ''
  117. package: 'Moka-Layouts'!
  118. !MKPaneLayout commentStamp!
  119. I am a specialized layout for pane views. I can set border widths to my views.!
  120. !MKPaneLayout methodsFor: 'accessing'!
  121. borderBottom: aNumber
  122. properties
  123. at: 'border-bottom'
  124. put: (self propertyLabelled: 'border-bottom-width' value: aNumber asMokaPixelString)
  125. !
  126. borderLeft: aNumber
  127. properties
  128. at: 'border-left'
  129. put: (self propertyLabelled: 'border-left-width' value: aNumber asMokaPixelString)
  130. !
  131. borderRight: aNumber
  132. properties
  133. at: 'border-right'
  134. put: (self propertyLabelled: 'border-right-width' value: aNumber asMokaPixelString)
  135. !
  136. borderTop: aNumber
  137. properties
  138. at: 'border-top'
  139. put: (self propertyLabelled: 'border-top-width' value: aNumber asMokaPixelString)
  140. ! !
  141. Object subclass: #MKLayoutProperty
  142. instanceVariableNames: 'layout value'
  143. package: 'Moka-Layouts'!
  144. !MKLayoutProperty methodsFor: 'accessing'!
  145. layout
  146. ^ layout
  147. !
  148. layout: aLayout
  149. layout := aLayout
  150. !
  151. value
  152. ^ value
  153. !
  154. value: aValue
  155. value := aValue
  156. ! !
  157. !MKLayoutProperty methodsFor: 'converting'!
  158. asCssString
  159. ^ String streamContents: [ :stream |
  160. self printCssOn: stream ]
  161. ! !
  162. !MKLayoutProperty methodsFor: 'printing'!
  163. printCssOn: aStream
  164. self subclassResponsibility
  165. ! !
  166. !MKLayoutProperty class methodsFor: 'instance creation'!
  167. layout: aLayout value: aValue
  168. ^ self new
  169. layout: aLayout;
  170. value: aValue;
  171. yourself
  172. ! !
  173. MKLayoutProperty subclass: #MKHorizontalCenteringLayoutProperty
  174. instanceVariableNames: ''
  175. package: 'Moka-Layouts'!
  176. !MKHorizontalCenteringLayoutProperty methodsFor: 'accessing'!
  177. marginLeft
  178. ^ 0 - ((self layout width / 2) + self value)
  179. ! !
  180. !MKHorizontalCenteringLayoutProperty methodsFor: 'printing'!
  181. printCssOn: aStream
  182. aStream << 'left:50%;'.
  183. aStream << 'margin-left:' << self marginLeft asMokaCssString
  184. ! !
  185. MKLayoutProperty subclass: #MKLabelledLayoutProperty
  186. instanceVariableNames: 'label'
  187. package: 'Moka-Layouts'!
  188. !MKLabelledLayoutProperty methodsFor: 'accessing'!
  189. label
  190. ^ label
  191. !
  192. label: aString
  193. label := aString
  194. ! !
  195. !MKLabelledLayoutProperty methodsFor: 'printing'!
  196. printCssOn: aStream
  197. aStream << self label << ':' << self value asMokaCssString
  198. ! !
  199. !MKLabelledLayoutProperty class methodsFor: 'instance creation'!
  200. layout: aLayout label: aString value: aValue
  201. ^ self new
  202. layout: aLayout;
  203. label: aString;
  204. value: aValue;
  205. yourself
  206. ! !
  207. MKLayoutProperty subclass: #MKVerticalCenteringLayoutProperty
  208. instanceVariableNames: ''
  209. package: 'Moka-Layouts'!
  210. !MKVerticalCenteringLayoutProperty methodsFor: 'accessing'!
  211. marginTop
  212. ^ 0 - ((self layout height / 2) + self value)
  213. ! !
  214. !MKVerticalCenteringLayoutProperty methodsFor: 'printing'!
  215. printCssOn: aStream
  216. aStream << 'top:50%;'.
  217. aStream << 'margin-top:' << self marginTop asMokaCssString
  218. ! !
  219. !Number methodsFor: '*Moka-Layouts'!
  220. asMokaCssString
  221. ^ self abs > 1
  222. ifTrue: [ self asMokaPixelString ]
  223. ifFalse: [ self asMokaPercentString ]
  224. !
  225. asMokaPercentString
  226. ^ (self * 100) asString, '%'
  227. !
  228. asMokaPixelString
  229. ^ self asString, 'px'
  230. ! !
  231. !String methodsFor: '*Moka-Layouts'!
  232. asMokaCssString
  233. ^ self
  234. ! !