kiwmi.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. local M = {}
  2. M.wscur = 1
  3. M.wsprv = 1
  4. local _lt = require('layout')
  5. -- return the position of view in a workspace
  6. -- and the workspace number
  7. function M:getViewPos(view,ws)
  8. -- first check the workspace if given
  9. if ws and WS[ws]then
  10. for i,j in ipairs(WS[ws]) do
  11. if j == view then return i,ws end
  12. end
  13. end
  14. -- then check the current workspace
  15. for i,j in ipairs(WS[WSCUR]) do
  16. if j == view then return i,WSCUR end
  17. end
  18. -- check all other visible workspaces
  19. for w,_ in ipairs(WS) do
  20. if w ~= WSCUR and w ~= ws then
  21. for i,j in ipairs(WS[w]) do
  22. if j == view then return i,w end
  23. end
  24. end
  25. end
  26. -- check the hidden workspace as well
  27. for i,j in ipairs(WS[-1]) do
  28. if j == view then return i,-1 end
  29. end
  30. end
  31. function M:focusView(view,ws)
  32. -- if workspace not given assume current workspace
  33. local w = ws or WSCUR
  34. -- if valid view and not focused already, then
  35. -- store the workspace[0] as focused view
  36. if view and view ~= kiwmi:focused_view() then
  37. WS[w][0] = view
  38. end
  39. -- focus on workspace[0] which stores focused view
  40. if WS[w][0] then
  41. if WS[w] == WS[WSCUR] then
  42. kiwmi:unfocus()
  43. WS[w][0]:show()
  44. WS[w][0]:focus()
  45. else
  46. self:switchToWorkspace(w)
  47. self:focusView()
  48. end
  49. end
  50. end
  51. -- add view to workspace given or current
  52. function M:addView(view,ws)
  53. local w = ws or WSCUR
  54. table.insert(WS[w],1,view)
  55. WS[w][0] = view -- store view at workspace[0]
  56. view:csd(false)
  57. view:tiled(true)
  58. end
  59. function M:removeView(view,ws)
  60. -- get the position and workspace of the view
  61. local i,w = self:getViewPos(view,ws)
  62. -- remove view from table
  63. table.remove(WS[w],i)
  64. -- if view was last focused view then
  65. -- update the focused view
  66. if view == WS[w][0] then
  67. local n = #WS[w]
  68. if n == 0 then WS[w][0] = false return end
  69. if i <= n then WS[w][0] = WS[w][i] end
  70. if i > n then WS[w][0] = WS[w][n] end
  71. if WS[w] == WS[WSCUR] then self:focusView() end
  72. end
  73. end
  74. -- focus next view on current workspace
  75. function M:focusViewNext()
  76. if #WS[WSCUR] < 2 then return end
  77. local i,_ = self:getViewPos(WS[WSCUR][0],WSCUR)
  78. local next = WS[WSCUR][i+1]
  79. if next then
  80. self:focusView(next)
  81. else
  82. self:focusView(WS[WSCUR][1])
  83. end
  84. end
  85. -- focus prev view on current workspace
  86. function M:focusViewPrev()
  87. if #WS[WSCUR] < 2 then return end
  88. local i,_ = self:getViewPos(WS[WSCUR][0],WSCUR)
  89. if i > 1 then
  90. self:focusView(WS[WSCUR][i-1])
  91. else
  92. self:focusView(WS[WSCUR][#WS[WSCUR]])
  93. end
  94. end
  95. -- focus on last view spawned on current workspace
  96. -- or simply the first view in the workspace table
  97. -- from any other focused view on the current workspace
  98. function M:focusViewLast()
  99. if #WS[WSCUR] < 2 or kiwmi:focused_view() == WS[WSCUR][1] then return end
  100. self:focusView(WS[WSCUR][1])
  101. end
  102. -- make the currently focused view the last view spawned
  103. -- or simply the first entry in the workspace table
  104. -- swap the last spawned view position with the current view position
  105. -- in the workspace table of views
  106. -- this makes the selected view the master view of the workspace
  107. -- and automatically calls layout arrange
  108. function M:makeViewLast()
  109. if #WS[WSCUR] < 2 then return end
  110. local i,_ = self:getViewPos(WS[WSCUR][0],WSCUR)
  111. WS[WSCUR][i],WS[WSCUR][1] = WS[WSCUR][1],WS[WSCUR][i]
  112. _lt:arrange_layout()
  113. end
  114. -- hide all views in workspace given or current
  115. function M:hideWorkspace(ws)
  116. local w = ws or WSCUR
  117. for _,v in ipairs(WS[w]) do
  118. v:hide()
  119. end
  120. end
  121. -- show all views in workspace given or current
  122. function M:showWorkspace(ws)
  123. local w = ws or WSCUR
  124. for _,v in ipairs(WS[w]) do
  125. v:show()
  126. end
  127. end
  128. -- switch to given workspace
  129. function M:switchToWorkspace(ws)
  130. if ws == WSCUR then return end
  131. self:hideWorkspace()
  132. if ws ~= -1 then
  133. self.wsprv = self.wscur
  134. self.wscur = ws
  135. end
  136. WSCUR = ws
  137. self:showWorkspace()
  138. self:focusView()
  139. end
  140. -- switch to last focused workspace(not hidden space)
  141. function M:switchToLastWorkspace()
  142. if WSCUR == -1 then
  143. self:switchToWorkspace(self.wscur)
  144. else
  145. self:switchToWorkspace(self.wsprv)
  146. end
  147. end
  148. -- switch between hidden space and
  149. -- current space
  150. function M:toggleHiddenSpace()
  151. if WSCUR == -1 then
  152. self:switchToWorkspace(self.wscur)
  153. else
  154. self:switchToWorkspace(-1)
  155. end
  156. end
  157. -- send a view to a workspace
  158. function M:sendViewToWorkspace(ws,view)
  159. local v = view or WS[WSCUR][0]
  160. if not v or not ws then return end
  161. -- first remove the vew from existing workspace
  162. -- and hide the view
  163. self:removeView(v)
  164. v:hide()
  165. -- add the view to the target workspace
  166. self:addView(v,ws)
  167. -- update current workspace
  168. self:focusView()
  169. self:showWorkspace()
  170. end
  171. -- send a view to hidden space
  172. function M:pushViewToHiddenSpace()
  173. if WSCUR == -1 then return end
  174. self:sendViewToWorkspace(-1)
  175. end
  176. -- get back the last view from hidden space
  177. -- to the current space
  178. function M:popViewFromHiddenSpace()
  179. if WSCUR == -1 then return end
  180. local v = WS[-1][0]
  181. if not v then return end
  182. self:removeView(v,-1)
  183. self:addView(v)
  184. v:hide()
  185. self:focusView()
  186. self:showWorkspace()
  187. end
  188. return M