Added core.set_active_view(); removed `focusable` boolean from View

This commit is contained in:
rxi 2020-05-19 12:58:41 +01:00
parent 8ec717f240
commit bc4bf3d384
7 changed files with 27 additions and 21 deletions

View File

@ -94,7 +94,7 @@ for _, dir in ipairs { "left", "right", "up", "down" } do
end end
local node = core.root_view.root_node:get_child_overlapping_point(x, y) local node = core.root_view.root_node:get_child_overlapping_point(x, y)
if not node:get_locked_size() then if not node:get_locked_size() then
core.active_view = node.active_view core.set_active_view(node.active_view)
end end
end end
end end

View File

@ -111,9 +111,8 @@ function CommandView:enter(text, submit, suggest, cancel)
submit = submit or noop, submit = submit or noop,
suggest = suggest or noop, suggest = suggest or noop,
cancel = cancel or noop, cancel = cancel or noop,
view = core.active_view
} }
core.active_view = self core.set_active_view(self)
self:update_suggestions() self:update_suggestions()
self.gutter_text_brightness = 100 self.gutter_text_brightness = 100
self.label = text .. ": " self.label = text .. ": "
@ -122,7 +121,7 @@ end
function CommandView:exit(submitted, inexplicit) function CommandView:exit(submitted, inexplicit)
if core.active_view == self then if core.active_view == self then
core.active_view = self.state.view core.set_active_view(core.last_active_view)
end end
local cancel = self.state.cancel local cancel = self.state.cancel
self.state = default_state self.state = default_state

View File

@ -100,7 +100,6 @@ function core.init()
core.root_view.root_node:split("down", core.command_view, true) core.root_view.root_node:split("down", core.command_view, true)
core.root_view.root_node.b:split("down", core.status_view, true) core.root_view.root_node.b:split("down", core.status_view, true)
core.active_view = core.root_view.root_node.a.active_view
core.add_thread(project_scan_thread) core.add_thread(project_scan_thread)
command.add_defaults() command.add_defaults()
@ -192,6 +191,15 @@ function core.reload_module(name)
end end
function core.set_active_view(view)
assert(view, "Tried to set active view to nil")
if view ~= core.active_view then
core.last_active_view = core.active_view
core.active_view = view
end
end
function core.add_thread(f, weak_ref) function core.add_thread(f, weak_ref)
local key = weak_ref or #core.threads + 1 local key = weak_ref or #core.threads + 1
local fn = function() return core.try(f) end local fn = function() return core.try(f) end

View File

@ -77,17 +77,18 @@ end
local type_map = { up="vsplit", down="vsplit", left="hsplit", right="hsplit" } local type_map = { up="vsplit", down="vsplit", left="hsplit", right="hsplit" }
function Node:split(dir, view, locked) function Node:split(dir, view, locked)
assert(self.type == "leaf", "tried to split non-leaf node") assert(self.type == "leaf", "Tried to split non-leaf node")
local type = assert(type_map[dir], "invalid direction") local type = assert(type_map[dir], "Invalid direction")
local last_active = core.active_view
local child = Node() local child = Node()
child:consume(self) child:consume(self)
self:consume(Node(type)) self:consume(Node(type))
self.a = child self.a = child
self.b = Node() self.b = Node()
self.b.locked = locked
if view then self.b:add_view(view) end if view then self.b:add_view(view) end
if not self.b.active_view.focusable then if locked then
self.a:set_active_view(self.a.active_view) self.b.locked = locked
core.set_active_view(last_active)
end end
if dir == "up" or dir == "left" then if dir == "up" or dir == "left" then
self.a, self.b = self.b, self.a self.a, self.b = self.b, self.a
@ -118,13 +119,15 @@ function Node:close_active_view(root)
p:set_active_view(p.active_view) p:set_active_view(p.active_view)
end end
end end
core.last_active_view = nil
end end
self.active_view:try_close(do_close) self.active_view:try_close(do_close)
end end
function Node:add_view(view) function Node:add_view(view)
assert(self.type == "leaf", "tried to add view to non-leaf node") assert(self.type == "leaf", "Tried to add view to non-leaf node")
assert(not self.locked, "Tried to add view to locked node")
if self.views[1] and self.views[1]:is(EmptyView) then if self.views[1] and self.views[1]:is(EmptyView) then
table.remove(self.views) table.remove(self.views)
end end
@ -134,9 +137,9 @@ end
function Node:set_active_view(view) function Node:set_active_view(view)
assert(self.type == "leaf", "tried to set active view on non-leaf node") assert(self.type == "leaf", "Tried to set active view on non-leaf node")
self.active_view = view self.active_view = view
core.active_view = view core.set_active_view(view)
end end
@ -377,8 +380,7 @@ end
function RootView:get_active_node() function RootView:get_active_node()
local node = self.root_node:get_node_for_view(core.active_view) return self.root_node:get_node_for_view(core.active_view)
return node or self.root_node.a
end end
@ -413,9 +415,7 @@ function RootView:on_mouse_pressed(button, x, y, clicks)
node:close_active_view(self.root_node) node:close_active_view(self.root_node)
end end
else else
if node.active_view.focusable then core.set_active_view(node.active_view)
core.active_view = node.active_view
end
node.active_view:on_mouse_pressed(button, x, y, clicks) node.active_view:on_mouse_pressed(button, x, y, clicks)
end end
end end

View File

@ -16,13 +16,13 @@ StatusView.separator2 = " | "
function StatusView:new() function StatusView:new()
StatusView.super.new(self) StatusView.super.new(self)
self.focusable = false
self.message_timeout = 0 self.message_timeout = 0
self.message = {} self.message = {}
end end
function StatusView:on_mouse_pressed() function StatusView:on_mouse_pressed()
core.set_active_view(core.last_active_view)
if system.get_time() < self.message_timeout if system.get_time() < self.message_timeout
and not core.active_view:is(LogView) then and not core.active_view:is(LogView) then
command.perform "core:open-log" command.perform "core:open-log"

View File

@ -14,7 +14,6 @@ function View:new()
self.scroll = { x = 0, y = 0, to = { x = 0, y = 0 } } self.scroll = { x = 0, y = 0, to = { x = 0, y = 0 } }
self.cursor = "arrow" self.cursor = "arrow"
self.scrollable = false self.scrollable = false
self.focusable = true
end end

View File

@ -22,7 +22,6 @@ local TreeView = View:extend()
function TreeView:new() function TreeView:new()
TreeView.super.new(self) TreeView.super.new(self)
self.scrollable = true self.scrollable = true
self.focusable = false
self.visible = true self.visible = true
self.init_size = true self.init_size = true
self.cache = {} self.cache = {}
@ -112,6 +111,7 @@ end
function TreeView:on_mouse_pressed(button, x, y) function TreeView:on_mouse_pressed(button, x, y)
core.set_active_view(core.last_active_view)
if not self.hovered_item then if not self.hovered_item then
return return
elseif self.hovered_item.type == "dir" then elseif self.hovered_item.type == "dir" then