Keep memory of window's size and position and restore them on start
Fix also a problem with directory path on windows.
This commit is contained in:
parent
9e5bf485df
commit
2cdf674b97
|
@ -11,10 +11,27 @@ local Doc
|
||||||
|
|
||||||
local core = {}
|
local core = {}
|
||||||
|
|
||||||
local function load_projects()
|
core.project_files_empty = {}
|
||||||
local ok, t = pcall(dofile, USERDIR .. "/recent_projects.lua")
|
|
||||||
core.recent_projects = (ok and t or {})
|
local function load_session()
|
||||||
|
local ok, t = pcall(dofile, USERDIR .. "/session.lua")
|
||||||
|
if ok then
|
||||||
|
return t.recents, t.window
|
||||||
end
|
end
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function save_session()
|
||||||
|
local fp = io.open(USERDIR .. "/session.lua", "w")
|
||||||
|
if fp then
|
||||||
|
fp:write("return {recents=", common.serialize(core.recent_projects),
|
||||||
|
", window=", common.serialize(table.pack(system.get_window_size())),
|
||||||
|
"}\n")
|
||||||
|
fp:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local function add_project_to_recents(dirname)
|
local function add_project_to_recents(dirname)
|
||||||
dirname = system.absolute_path(dirname)
|
dirname = system.absolute_path(dirname)
|
||||||
|
@ -30,14 +47,6 @@ local function add_project_to_recents(dirname)
|
||||||
table.insert(recents, 1, dirname)
|
table.insert(recents, 1, dirname)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function save_projects()
|
|
||||||
local fp = io.open(USERDIR .. "/recent_projects.lua", "w")
|
|
||||||
if fp then
|
|
||||||
fp:write("return ", common.serialize(core.recent_projects), "\n")
|
|
||||||
fp:close()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function core.reschedule_project_scan()
|
function core.reschedule_project_scan()
|
||||||
if core.project_scan_thread_id then
|
if core.project_scan_thread_id then
|
||||||
|
@ -46,8 +55,10 @@ function core.reschedule_project_scan()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
core.project_files_empty = {}
|
local function normalize_path(s)
|
||||||
|
local drive, path = s:match("^([a-z]):([/\\].*)")
|
||||||
|
return drive and drive:upper() .. ":" .. path or s
|
||||||
|
end
|
||||||
|
|
||||||
function core.set_project_dir(new_dir)
|
function core.set_project_dir(new_dir)
|
||||||
core.project_dir = new_dir
|
core.project_dir = new_dir
|
||||||
|
@ -67,7 +78,6 @@ function core.open_folder_project(dirname)
|
||||||
core.on_quit_project()
|
core.on_quit_project()
|
||||||
core.root_view:close_all_docviews()
|
core.root_view:close_all_docviews()
|
||||||
add_project_to_recents(dirname)
|
add_project_to_recents(dirname)
|
||||||
save_projects()
|
|
||||||
core.set_project_dir(dirname)
|
core.set_project_dir(dirname)
|
||||||
core.on_enter_project(dirname)
|
core.on_enter_project(dirname)
|
||||||
end
|
end
|
||||||
|
@ -290,6 +300,7 @@ function core.add_project_directory(path)
|
||||||
-- top directories has a file-like "item" but the item.filename
|
-- top directories has a file-like "item" but the item.filename
|
||||||
-- will be simply the name of the directory, without its path.
|
-- will be simply the name of the directory, without its path.
|
||||||
-- The field item.topdir will identify it as a top level directory.
|
-- The field item.topdir will identify it as a top level directory.
|
||||||
|
path = normalize_path(path)
|
||||||
table.insert(core.project_directories, {
|
table.insert(core.project_directories, {
|
||||||
name = path,
|
name = path,
|
||||||
item = {filename = path:match("[^\\/]+$"), type = "dir", topdir = true},
|
item = {filename = path:match("[^\\/]+$"), type = "dir", topdir = true},
|
||||||
|
@ -317,7 +328,13 @@ function core.init()
|
||||||
CommandView = require "core.commandview"
|
CommandView = require "core.commandview"
|
||||||
Doc = require "core.doc"
|
Doc = require "core.doc"
|
||||||
|
|
||||||
load_projects()
|
do
|
||||||
|
local recent_projects, window_position = load_session()
|
||||||
|
if window_position then
|
||||||
|
system.set_window_size(table.unpack(window_position))
|
||||||
|
end
|
||||||
|
core.recent_projects = recent_projects
|
||||||
|
end
|
||||||
|
|
||||||
local project_dir = core.recent_projects[1] or "."
|
local project_dir = core.recent_projects[1] or "."
|
||||||
local files = {}
|
local files = {}
|
||||||
|
@ -328,7 +345,6 @@ function core.init()
|
||||||
elseif info.type == "dir" then
|
elseif info.type == "dir" then
|
||||||
project_dir = ARGS[i]
|
project_dir = ARGS[i]
|
||||||
add_project_to_recents(project_dir)
|
add_project_to_recents(project_dir)
|
||||||
save_projects()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -425,6 +441,7 @@ local function quit_with_function(quit_fn, force)
|
||||||
if force then
|
if force then
|
||||||
delete_temp_files()
|
delete_temp_files()
|
||||||
core.on_quit_project()
|
core.on_quit_project()
|
||||||
|
save_session()
|
||||||
quit_fn()
|
quit_fn()
|
||||||
else
|
else
|
||||||
if core.confirm_close_all() then
|
if core.confirm_close_all() then
|
||||||
|
|
|
@ -201,6 +201,29 @@ static int f_set_window_mode(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int f_get_window_size(lua_State *L) {
|
||||||
|
int x, y, w, h;
|
||||||
|
SDL_GetWindowSize(window, &w, &h);
|
||||||
|
SDL_GetWindowPosition(window, &x, &y);
|
||||||
|
lua_pushnumber(L, w);
|
||||||
|
lua_pushnumber(L, h);
|
||||||
|
lua_pushnumber(L, x);
|
||||||
|
lua_pushnumber(L, y);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int f_set_window_size(lua_State *L) {
|
||||||
|
double w = luaL_checknumber(L, 1);
|
||||||
|
double h = luaL_checknumber(L, 2);
|
||||||
|
double x = luaL_checknumber(L, 3);
|
||||||
|
double y = luaL_checknumber(L, 4);
|
||||||
|
SDL_SetWindowSize(window, w, h);
|
||||||
|
SDL_SetWindowPosition(window, x, y);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int f_window_has_focus(lua_State *L) {
|
static int f_window_has_focus(lua_State *L) {
|
||||||
unsigned flags = SDL_GetWindowFlags(window);
|
unsigned flags = SDL_GetWindowFlags(window);
|
||||||
lua_pushboolean(L, flags & SDL_WINDOW_INPUT_FOCUS);
|
lua_pushboolean(L, flags & SDL_WINDOW_INPUT_FOCUS);
|
||||||
|
@ -414,6 +437,8 @@ static const luaL_Reg lib[] = {
|
||||||
{ "set_cursor", f_set_cursor },
|
{ "set_cursor", f_set_cursor },
|
||||||
{ "set_window_title", f_set_window_title },
|
{ "set_window_title", f_set_window_title },
|
||||||
{ "set_window_mode", f_set_window_mode },
|
{ "set_window_mode", f_set_window_mode },
|
||||||
|
{ "get_window_size", f_get_window_size },
|
||||||
|
{ "set_window_size", f_set_window_size },
|
||||||
{ "window_has_focus", f_window_has_focus },
|
{ "window_has_focus", f_window_has_focus },
|
||||||
{ "show_confirm_dialog", f_show_confirm_dialog },
|
{ "show_confirm_dialog", f_show_confirm_dialog },
|
||||||
{ "chdir", f_chdir },
|
{ "chdir", f_chdir },
|
||||||
|
|
Loading…
Reference in New Issue