From bc4bf3d384172aa9f1bc7ee5c5aeaa3481e5f9c6 Mon Sep 17 00:00:00 2001 From: rxi Date: Tue, 19 May 2020 12:58:41 +0100 Subject: [PATCH] Added core.set_active_view(); removed `focusable` boolean from View --- data/core/commands/root.lua | 2 +- data/core/commandview.lua | 5 ++--- data/core/init.lua | 10 +++++++++- data/core/rootview.lua | 26 +++++++++++++------------- data/core/statusview.lua | 2 +- data/core/view.lua | 1 - data/plugins/treeview.lua | 2 +- 7 files changed, 27 insertions(+), 21 deletions(-) diff --git a/data/core/commands/root.lua b/data/core/commands/root.lua index ca177d6..58c83e2 100644 --- a/data/core/commands/root.lua +++ b/data/core/commands/root.lua @@ -94,7 +94,7 @@ for _, dir in ipairs { "left", "right", "up", "down" } do end local node = core.root_view.root_node:get_child_overlapping_point(x, y) if not node:get_locked_size() then - core.active_view = node.active_view + core.set_active_view(node.active_view) end end end diff --git a/data/core/commandview.lua b/data/core/commandview.lua index e2ad9d8..3fbb1ff 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -111,9 +111,8 @@ function CommandView:enter(text, submit, suggest, cancel) submit = submit or noop, suggest = suggest or noop, cancel = cancel or noop, - view = core.active_view } - core.active_view = self + core.set_active_view(self) self:update_suggestions() self.gutter_text_brightness = 100 self.label = text .. ": " @@ -122,7 +121,7 @@ end function CommandView:exit(submitted, inexplicit) if core.active_view == self then - core.active_view = self.state.view + core.set_active_view(core.last_active_view) end local cancel = self.state.cancel self.state = default_state diff --git a/data/core/init.lua b/data/core/init.lua index c35df32..c6b5732 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -100,7 +100,6 @@ function core.init() core.root_view.root_node:split("down", core.command_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) command.add_defaults() @@ -192,6 +191,15 @@ function core.reload_module(name) 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) local key = weak_ref or #core.threads + 1 local fn = function() return core.try(f) end diff --git a/data/core/rootview.lua b/data/core/rootview.lua index 7a24b42..d6bf876 100644 --- a/data/core/rootview.lua +++ b/data/core/rootview.lua @@ -77,17 +77,18 @@ end local type_map = { up="vsplit", down="vsplit", left="hsplit", right="hsplit" } function Node:split(dir, view, locked) - assert(self.type == "leaf", "tried to split non-leaf node") - local type = assert(type_map[dir], "invalid direction") + assert(self.type == "leaf", "Tried to split non-leaf node") + local type = assert(type_map[dir], "Invalid direction") + local last_active = core.active_view local child = Node() child:consume(self) self:consume(Node(type)) self.a = child self.b = Node() - self.b.locked = locked if view then self.b:add_view(view) end - if not self.b.active_view.focusable then - self.a:set_active_view(self.a.active_view) + if locked then + self.b.locked = locked + core.set_active_view(last_active) end if dir == "up" or dir == "left" then 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) end end + core.last_active_view = nil end self.active_view:try_close(do_close) end 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 table.remove(self.views) end @@ -134,9 +137,9 @@ end 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 - core.active_view = view + core.set_active_view(view) end @@ -377,8 +380,7 @@ end function RootView:get_active_node() - local node = self.root_node:get_node_for_view(core.active_view) - return node or self.root_node.a + return self.root_node:get_node_for_view(core.active_view) end @@ -413,9 +415,7 @@ function RootView:on_mouse_pressed(button, x, y, clicks) node:close_active_view(self.root_node) end else - if node.active_view.focusable then - core.active_view = node.active_view - end + core.set_active_view(node.active_view) node.active_view:on_mouse_pressed(button, x, y, clicks) end end diff --git a/data/core/statusview.lua b/data/core/statusview.lua index 24c408f..67a4c8b 100644 --- a/data/core/statusview.lua +++ b/data/core/statusview.lua @@ -16,13 +16,13 @@ StatusView.separator2 = " | " function StatusView:new() StatusView.super.new(self) - self.focusable = false self.message_timeout = 0 self.message = {} end function StatusView:on_mouse_pressed() + core.set_active_view(core.last_active_view) if system.get_time() < self.message_timeout and not core.active_view:is(LogView) then command.perform "core:open-log" diff --git a/data/core/view.lua b/data/core/view.lua index d9230b6..ae978a9 100644 --- a/data/core/view.lua +++ b/data/core/view.lua @@ -14,7 +14,6 @@ function View:new() self.scroll = { x = 0, y = 0, to = { x = 0, y = 0 } } self.cursor = "arrow" self.scrollable = false - self.focusable = true end diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 2c2460d..d22db0f 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -22,7 +22,6 @@ local TreeView = View:extend() function TreeView:new() TreeView.super.new(self) self.scrollable = true - self.focusable = false self.visible = true self.init_size = true self.cache = {} @@ -112,6 +111,7 @@ end function TreeView:on_mouse_pressed(button, x, y) + core.set_active_view(core.last_active_view) if not self.hovered_item then return elseif self.hovered_item.type == "dir" then