root.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. local core = require "core"
  2. local DocView = require "core.docview"
  3. local command = require "core.command"
  4. local t = {
  5. ["root:close"] = function()
  6. local node = core.root_view:get_active_node()
  7. node:close_active_view(core.root_view.root_node)
  8. end,
  9. ["root:switch-to-previous-tab"] = function()
  10. local node = core.root_view:get_active_node()
  11. local idx = node:get_view_idx(core.active_view)
  12. idx = idx - 1
  13. if idx < 1 then idx = #node.views end
  14. node:set_active_view(node.views[idx])
  15. end,
  16. ["root:switch-to-next-tab"] = function()
  17. local node = core.root_view:get_active_node()
  18. local idx = node:get_view_idx(core.active_view)
  19. idx = idx + 1
  20. if idx > #node.views then idx = 1 end
  21. node:set_active_view(node.views[idx])
  22. end,
  23. ["root:move-tab-left"] = function()
  24. local node = core.root_view:get_active_node()
  25. local idx = node:get_view_idx(core.active_view)
  26. if idx > 1 then
  27. table.remove(node.views, idx)
  28. table.insert(node.views, idx - 1, core.active_view)
  29. end
  30. end,
  31. ["root:move-tab-right"] = function()
  32. local node = core.root_view:get_active_node()
  33. local idx = node:get_view_idx(core.active_view)
  34. if idx < #node.views then
  35. table.remove(node.views, idx)
  36. table.insert(node.views, idx + 1, core.active_view)
  37. end
  38. end,
  39. }
  40. for i = 1, 9 do
  41. t["root:switch-to-tab-" .. i] = function()
  42. local node = core.root_view:get_active_node()
  43. local view = node.views[i]
  44. if view then
  45. node:set_active_view(view)
  46. end
  47. end
  48. end
  49. for _, dir in ipairs { "left", "right", "up", "down" } do
  50. t["root:split-" .. dir] = function()
  51. local node = core.root_view:get_active_node()
  52. local av = node.active_view
  53. node:split(dir)
  54. if av:is(DocView) then
  55. core.root_view:open_doc(av.doc)
  56. end
  57. end
  58. t["root:switch-to-" .. dir] = function()
  59. local node = core.root_view:get_active_node()
  60. local x, y
  61. if dir == "left" or dir == "right" then
  62. y = node.position.y + node.size.y / 2
  63. x = node.position.x + (dir == "left" and -1 or node.size.x + 1)
  64. else
  65. x = node.position.x + node.size.x / 2
  66. y = node.position.y + (dir == "up" and -1 or node.size.y + 1)
  67. end
  68. local node = core.root_view.root_node:get_child_overlapping_point(x, y)
  69. if not node:get_locked_size() then
  70. core.active_view = node.active_view
  71. end
  72. end
  73. end
  74. command.add(function()
  75. local node = core.root_view:get_active_node()
  76. return not node:get_locked_size()
  77. end, t)