Moka-Layouts.st 6.2 KB

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