2022-05-31 22:34:14 +02:00
|
|
|
-- mod-version:3
|
2021-06-03 22:49:37 +02:00
|
|
|
local core = require "core"
|
|
|
|
local common = require "core.common"
|
|
|
|
local command = require "core.command"
|
|
|
|
local config = require "core.config"
|
|
|
|
local keymap = require "core.keymap"
|
|
|
|
local style = require "core.style"
|
|
|
|
local CommandView = require "core.commandview"
|
|
|
|
|
2021-12-06 00:32:20 +01:00
|
|
|
config.plugins.scale = common.merge({
|
2022-05-23 21:12:37 +02:00
|
|
|
-- The method used to apply the scaling: "code", "ui"
|
2021-07-13 16:14:00 +02:00
|
|
|
mode = "code",
|
2022-09-15 06:54:44 +02:00
|
|
|
-- Default scale applied at startup.
|
|
|
|
default_scale = "autodetect",
|
2022-05-23 21:12:37 +02:00
|
|
|
-- Allow using CTRL + MouseWheel for changing the scale.
|
2022-09-15 06:54:44 +02:00
|
|
|
use_mousewheel = true
|
2021-12-06 00:32:20 +01:00
|
|
|
}, config.plugins.scale)
|
2021-06-03 22:49:37 +02:00
|
|
|
|
|
|
|
local scale_steps = 0.05
|
|
|
|
|
|
|
|
local current_scale = SCALE
|
|
|
|
local default_scale = SCALE
|
|
|
|
|
|
|
|
local function set_scale(scale)
|
|
|
|
scale = common.clamp(scale, 0.2, 6)
|
|
|
|
|
|
|
|
-- save scroll positions
|
2023-04-29 18:50:39 +02:00
|
|
|
local v_scrolls = {}
|
|
|
|
local h_scrolls = {}
|
2021-06-03 22:49:37 +02:00
|
|
|
for _, view in ipairs(core.root_view.root_node:get_children()) do
|
|
|
|
local n = view:get_scrollable_size()
|
2023-04-29 18:50:39 +02:00
|
|
|
if n ~= math.huge and n > view.size.y then
|
|
|
|
v_scrolls[view] = view.scroll.y / (n - view.size.y)
|
|
|
|
end
|
|
|
|
local hn = view:get_h_scrollable_size()
|
|
|
|
if hn ~= math.huge and hn > view.size.x then
|
|
|
|
h_scrolls[view] = view.scroll.x / (hn - view.size.x)
|
2021-06-03 22:49:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local s = scale / current_scale
|
|
|
|
current_scale = scale
|
|
|
|
|
2021-07-13 16:14:00 +02:00
|
|
|
if config.plugins.scale.mode == "ui" then
|
2021-06-03 22:49:37 +02:00
|
|
|
SCALE = scale
|
|
|
|
|
2023-02-06 18:40:12 +01:00
|
|
|
style.padding.x = style.padding.x * s
|
|
|
|
style.padding.y = style.padding.y * s
|
|
|
|
style.divider_size = style.divider_size * s
|
|
|
|
style.scrollbar_size = style.scrollbar_size * s
|
|
|
|
style.expanded_scrollbar_size = style.expanded_scrollbar_size * s
|
|
|
|
style.caret_width = style.caret_width * s
|
|
|
|
style.tab_width = style.tab_width * s
|
2021-06-03 22:49:37 +02:00
|
|
|
|
|
|
|
for _, name in ipairs {"font", "big_font", "icon_font", "icon_big_font", "code_font"} do
|
2022-06-22 19:19:52 +02:00
|
|
|
style[name]:set_size(s * style[name]:get_size())
|
2021-06-03 22:49:37 +02:00
|
|
|
end
|
|
|
|
else
|
2022-06-22 19:19:52 +02:00
|
|
|
style.code_font:set_size(s * style.code_font:get_size())
|
2021-06-03 22:49:37 +02:00
|
|
|
end
|
|
|
|
|
2022-03-06 00:03:33 +01:00
|
|
|
for name, font in pairs(style.syntax_fonts) do
|
2022-06-22 19:19:52 +02:00
|
|
|
style.syntax_fonts[name]:set_size(s * font:get_size())
|
2021-09-19 18:51:44 +02:00
|
|
|
end
|
|
|
|
|
2021-06-03 22:49:37 +02:00
|
|
|
-- restore scroll positions
|
2023-04-29 18:50:39 +02:00
|
|
|
for view, n in pairs(v_scrolls) do
|
2021-06-03 22:49:37 +02:00
|
|
|
view.scroll.y = n * (view:get_scrollable_size() - view.size.y)
|
|
|
|
view.scroll.to.y = view.scroll.y
|
|
|
|
end
|
2023-04-29 18:50:39 +02:00
|
|
|
for view, hn in pairs(h_scrolls) do
|
|
|
|
view.scroll.x = hn * (view:get_h_scrollable_size() - view.size.x)
|
|
|
|
view.scroll.to.x = view.scroll.x
|
|
|
|
end
|
2021-06-03 22:49:37 +02:00
|
|
|
|
|
|
|
core.redraw = true
|
|
|
|
end
|
|
|
|
|
2021-06-18 02:58:22 +02:00
|
|
|
local function get_scale()
|
2021-06-15 02:33:15 +02:00
|
|
|
return current_scale
|
|
|
|
end
|
2021-06-03 22:49:37 +02:00
|
|
|
|
|
|
|
local function res_scale()
|
2021-08-28 01:12:09 +02:00
|
|
|
set_scale(default_scale)
|
2021-06-03 22:49:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function inc_scale()
|
2021-10-02 03:24:35 +02:00
|
|
|
set_scale(current_scale + scale_steps)
|
2021-06-03 22:49:37 +02:00
|
|
|
end
|
|
|
|
|
2021-10-02 03:24:35 +02:00
|
|
|
local function dec_scale()
|
|
|
|
set_scale(current_scale - scale_steps)
|
2021-06-03 22:49:37 +02:00
|
|
|
end
|
|
|
|
|
2022-09-15 06:54:44 +02:00
|
|
|
if default_scale ~= config.plugins.scale.default_scale then
|
|
|
|
if type(config.plugins.scale.default_scale) == "number" then
|
|
|
|
set_scale(config.plugins.scale.default_scale)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- The config specification used by gui generators
|
|
|
|
config.plugins.scale.config_spec = {
|
|
|
|
name = "Scale",
|
|
|
|
{
|
|
|
|
label = "Mode",
|
|
|
|
description = "The method used to apply the scaling.",
|
|
|
|
path = "mode",
|
|
|
|
type = "selection",
|
|
|
|
default = "code",
|
|
|
|
values = {
|
|
|
|
{"Everything", "ui"},
|
|
|
|
{"Code Only", "code"}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label = "Default Scale",
|
|
|
|
description = "The scaling factor applied to lite-xl.",
|
|
|
|
path = "default_scale",
|
|
|
|
type = "selection",
|
|
|
|
default = "autodetect",
|
|
|
|
values = {
|
|
|
|
{"Autodetect", "autodetect"},
|
|
|
|
{"80%", 0.80},
|
|
|
|
{"90%", 0.90},
|
|
|
|
{"100%", 1.00},
|
|
|
|
{"110%", 1.10},
|
|
|
|
{"120%", 1.20},
|
|
|
|
{"125%", 1.25},
|
|
|
|
{"130%", 1.30},
|
|
|
|
{"140%", 1.40},
|
|
|
|
{"150%", 1.50},
|
|
|
|
{"175%", 1.75},
|
|
|
|
{"200%", 2.00},
|
|
|
|
{"250%", 2.50},
|
|
|
|
{"300%", 3.00}
|
|
|
|
},
|
|
|
|
on_apply = function(value)
|
|
|
|
if type(value) == "string" then value = default_scale end
|
|
|
|
if value ~= current_scale then
|
|
|
|
set_scale(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label = "Use MouseWheel",
|
|
|
|
description = "Allow using CTRL + MouseWheel for changing the scale.",
|
|
|
|
path = "use_mousewheel",
|
|
|
|
type = "toggle",
|
|
|
|
default = true,
|
|
|
|
on_apply = function(enabled)
|
|
|
|
if enabled then
|
|
|
|
keymap.add {
|
|
|
|
["ctrl+wheelup"] = "scale:increase",
|
|
|
|
["ctrl+wheeldown"] = "scale:decrease"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
keymap.unbind("ctrl+wheelup", "scale:increase")
|
|
|
|
keymap.unbind("ctrl+wheeldown", "scale:decrease")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:49:37 +02:00
|
|
|
|
|
|
|
command.add(nil, {
|
|
|
|
["scale:reset" ] = function() res_scale() end,
|
|
|
|
["scale:decrease"] = function() dec_scale() end,
|
|
|
|
["scale:increase"] = function() inc_scale() end,
|
|
|
|
})
|
|
|
|
|
|
|
|
keymap.add {
|
|
|
|
["ctrl+0"] = "scale:reset",
|
|
|
|
["ctrl+-"] = "scale:decrease",
|
2022-05-23 21:12:37 +02:00
|
|
|
["ctrl+="] = "scale:increase"
|
2021-06-03 22:49:37 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 21:12:37 +02:00
|
|
|
if config.plugins.scale.use_mousewheel then
|
|
|
|
keymap.add {
|
|
|
|
["ctrl+wheelup"] = "scale:increase",
|
|
|
|
["ctrl+wheeldown"] = "scale:decrease"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-06-18 02:58:22 +02:00
|
|
|
return {
|
|
|
|
["set"] = set_scale,
|
|
|
|
["get"] = get_scale,
|
2021-06-15 02:33:15 +02:00
|
|
|
["increase"] = inc_scale,
|
|
|
|
["decrease"] = dec_scale,
|
|
|
|
["reset"] = res_scale
|
|
|
|
}
|