From b4ed485ce9f94e5f67056f8b1cbe63c27d6d4863 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Sun, 29 Aug 2021 04:46:10 +0200 Subject: [PATCH] Register `View` classes This allows matching a `View` class name to the class itself. --- data/core/init.lua | 10 ++++++++++ data/core/view.lua | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/data/core/init.lua b/data/core/init.lua index 702e31fd..758fd2c3 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -36,6 +36,16 @@ local function save_session() end +local registered_views = { } +function core.register_view(name, class) + registered_views[name] = class +end + +function core.get_registered_view(name) + return registered_views[name] +end + + local function update_recents_project(action, dir_path_abs) local dirname = common.normalize_path(dir_path_abs) if not dirname then return end diff --git a/data/core/view.lua b/data/core/view.lua index d1374ee4..b2f1b370 100644 --- a/data/core/view.lua +++ b/data/core/view.lua @@ -7,6 +7,16 @@ local Object = require "core.object" local View = Object:extend() +-- Override how objects are created. This allows for automatic Views registration +function View:extend(register_name) + local new_class = View.super.extend(self) + if register_name then + new_class.registered_name = register_name + core.register_view(register_name, new_class) + end + return new_class +end + -- context can be "application" or "session". The instance of objects -- with context "session" will be closed when a project session is -- terminated. The context "application" is for functional UI elements.