Automatically reload style when saving the project user module

This commit is contained in:
Francesco Abbate 2020-12-10 17:56:53 +01:00
parent 6409b67ea2
commit 9114148b45
6 changed files with 72 additions and 19 deletions

View File

@ -102,7 +102,15 @@ command.add(nil, {
end,
["core:open-user-module"] = function()
core.root_view:open_doc(core.open_doc(USERDIR .. "/init.lua"))
local user_module_doc = core.open_doc(USERDIR .. "/init.lua")
if not user_module_doc then return end
local doc_save = user_module_doc.save
user_module_doc.save = function(self)
doc_save(self)
core.reload_module("core.style")
core.load_user_directory()
end
core.root_view:open_doc(user_module_doc)
end,
["core:open-project-module"] = function()
@ -116,8 +124,8 @@ command.add(nil, {
end
end,
["core:open-folder"] = function()
core.command_view:enter("Open Folder", function(text)
["core:change-project-folder"] = function()
core.command_view:enter("Change Project Folder", function(text)
local path_stat = system.get_file_info(text)
if not path_stat or path_stat.type ~= 'dir' then
core.error("Cannot open folder %q", text)
@ -130,4 +138,17 @@ command.add(nil, {
return text == "" and core.recent_projects or common.dir_path_suggest(text)
end)
end,
["core:open-project-folder"] = function()
core.command_view:enter("Open Project", function(text)
local path_stat = system.get_file_info(text)
if not path_stat or path_stat.type ~= 'dir' then
core.error("Cannot open folder %q", text)
return
end
system.exec(string.format("%q %q", EXEFILE, text))
end, function(text)
return text == "" and core.recent_projects or common.dir_path_suggest(text)
end)
end,
})

View File

@ -166,33 +166,46 @@ local function write_user_init_file(init_filename)
init_file:write([[
-- put user settings here
-- this module will be loaded after everything else when the application starts
-- it will be automatically reloaded when use save it
local keymap = require "core.keymap"
local config = require "core.config"
local style = require "core.style"
-- light theme:
-- require "colors.summer"
-- style.load("colors.summer")
-- key binding:
-- keymap.add { ["ctrl+escape"] = "core:quit" }
-- customize fonts:
-- style.font = renderer.font.load(DATADIR .. "/fonts/font.ttf", 14 * SCALE)
-- style.code_font = renderer.font.load(DATADIR .. "/fonts/monospace.ttf", 13.5 * SCALE)
--
-- fonts used by the editor:
-- style.font : user interface
-- style.big_font : big text in welcome screen
-- style.icon_font : icons
-- style.code_font : code
]])
init_file:close()
end
local function load_user_directory()
local init_filename = USERDIR .. "/init.lua"
local stat_info_dir = system.get_file_info(USERDIR)
if not stat_info_dir then
create_user_directory()
end
local stat_info_file = system.get_file_info(init_filename)
if not stat_info_file then
write_user_init_file(init_filename)
end
return dofile(init_filename)
function core.load_user_directory()
return core.try(function()
local stat_dir = system.get_file_info(USERDIR)
if not stat_dir then
create_user_directory()
end
local init_filename = USERDIR .. "/init.lua"
local stat_file = system.get_file_info(init_filename)
if not stat_file then
write_user_init_file(init_filename)
end
dofile(init_filename)
end)
end
@ -242,7 +255,7 @@ function core.init()
core.project_scan_thread_id = core.add_thread(project_scan_thread)
command.add_defaults()
local got_plugin_error = not core.load_plugins()
local got_user_error = not core.try(load_user_directory)
local got_user_error = not core.load_user_directory()
do
local pdir, pname = system.absolute_path(project_dir):match("(.*)[/\\\\](.*)")

View File

@ -88,6 +88,8 @@ keymap.add {
["ctrl+p"] = "core:find-file",
["ctrl+o"] = "core:open-file",
["ctrl+n"] = "core:new-doc",
["ctrl+shift+c"] = "core:change-project-folder",
["ctrl+shift+o"] = "core:open-project-folder",
["alt+return"] = "core:toggle-fullscreen",
["alt+shift+j"] = "root:split-left",
@ -181,7 +183,6 @@ keymap.add {
["ctrl+shift+end"] = "doc:select-to-end-of-doc",
["shift+pageup"] = "doc:select-to-previous-page",
["shift+pagedown"] = "doc:select-to-next-page",
["ctrl+shift+o"] = "core:open-folder",
}
return keymap

View File

@ -19,7 +19,8 @@ local function draw_text(x, y, color)
local lines = {
{ fmt = "%s to run a command", cmd = "core:find-command" },
{ fmt = "%s to open a file from the project", cmd = "core:find-file" },
{ fmt = "%s to change project folder", cmd = "core:open-folder" },
{ fmt = "%s to change project folder", cmd = "core:change-project-folder" },
{ fmt = "%s to open a project folder", cmd = "core:open-project-folder" },
}
th = style.font:get_height()
y = y + (dh - (th + style.padding.y) * #lines) / 2

View File

@ -53,4 +53,10 @@ style.syntax["string"] = { common.color "#f7c95c" }
style.syntax["operator"] = { common.color "#93DDFA" }
style.syntax["function"] = { common.color "#93DDFA" }
style.load = function(module_name)
package.loaded[module_name] = nil
require(module_name)
end
return style

View File

@ -1,13 +1,24 @@
-- put user settings here
-- this module will be loaded after everything else when the application starts
-- it will be automatically reloaded when use save it
local keymap = require "core.keymap"
local config = require "core.config"
local style = require "core.style"
-- light theme:
-- require "colors.summer"
-- style.load("colors.summer")
-- key binding:
-- keymap.add { ["ctrl+escape"] = "core:quit" }
-- customize fonts:
-- style.font = renderer.font.load(DATADIR .. "/fonts/font.ttf", 14 * SCALE)
-- style.code_font = renderer.font.load(DATADIR .. "/fonts/monospace.ttf", 13.5 * SCALE)
--
-- fonts used by the editor:
-- style.font : user interface
-- style.big_font : big text in welcome screen
-- style.icon_font : icons
-- style.code_font : code