dymokomi/luced-2d
Source of release 0.5.0
## What a machine keeps for luced-2d, in `~/.luced-2d/settings.toml`: the size a
## new canvas takes, the theme's accent and control radius, the view aids, the
## picker's recent colors, and the shortcuts that differ from the defaults, by
## command id (a chord like "cmd+shift+s", "f5" or "none"). Missing keys keep
## their defaults; an unreadable file is reported and ignored, never rewritten.
from luce_config.toml import Table
from luce_std import files, paths
from luce_ui import ui
from units import is_unit
## The Swatches panel's colors before any are added or deleted: grays, then
## Photoshop's usual pure and pastel hues.
pub let default_swatches: str = "#000000 #404040 #808080 #c0c0c0 #ffffff #ff0000 #ff8000 #ffff00 #00ff00 #00ffff #0000ff #8000ff #ff00ff #800000 #804000 #808000 #008000 #008080 #000080 #400080 #ff8080 #ffc080 #ffff80 #80ff80 #80ffff #8080ff #ff80ff"
pub class Settings:
pub var canvas_width: int = 1280
pub var canvas_height: int = 800
pub var accent: str = "#e7a03f"
pub var control_radius: float = 5.0
## The view aids: rulers shown, grid shown, and the unit (px, mm or in).
pub var rulers: bool = true
pub var grid: bool = false
pub var units: str = "px"
## The colors the picker committed most recently, newest first, as sRGB hex separated by spaces.
pub var recent: str = ""
## The Swatches panel's colors, in order, as sRGB hex separated by spaces.
pub var swatches: str = default_swatches
## The Color panel's picker (triangle, square, wheel or sliders) and its sliders' model (rgb, hsb, lab or gray).
pub var color_mode: str = "triangle"
pub var color_model: str = "rgb"
## How much memory each document's undo history may take, in gigabytes.
pub var undo_memory: float = 4.0
## The share of the GPU's and the machine's memory documents' tiles may use,
## in percent (25..90), as Photoshop's Memory Usage; past it, tiles move to RAM.
pub var memory_usage: float = 60.0
## Where tiles past the RAM budget spill, as Photoshop's scratch disk; "" for the default.
pub var scratch: str = ""
pub var shortcuts: map[str, str]
pub func init(self, source: str = "") -> unit!:
# What the file says that this version does not know is passed over, so a
# file an earlier version wrote still gives the settings this one reads.
let table = Table(source)
self.canvas_width = int(table.number("canvas.width", 1280.0, 1.0, 30000.0))
self.canvas_height = int(table.number("canvas.height", 800.0, 1.0, 30000.0))
self.accent = table.string("theme.accent", "#e7a03f")
self.control_radius = table.number("theme.control_radius", 5.0, 0.0, 16.0)
self.recent = table.string("colors.recent", "")
self.swatches = table.string("colors.swatches", default_swatches)
self.color_mode = table.string("colors.mode", "triangle")
self.color_model = table.string("colors.model", "rgb")
self.undo_memory = table.number("memory.undo", 4.0, 0.25, 256.0)
self.memory_usage = table.number("memory.usage", 60.0, 25.0, 90.0)
self.scratch = table.string("memory.scratch", "")
self.rulers = table.boolean("view.rulers", true)
self.grid = table.boolean("view.grid", false)
self.units = table.string("view.units", "px")
if not is_unit(self.units):
error(ui.invalid, "view.units must be \"px\", \"mm\" or \"in\"")
var shortcuts: map[str, str] = {}
for id in table.keys("shortcuts"):
shortcuts[id] = table.string("shortcuts." + id, "")
self.shortcuts = shortcuts
## An independent copy, for a draft the dialog edits.
pub func copy(self) -> Settings!:
let other = Settings()
other.canvas_width = self.canvas_width
other.canvas_height = self.canvas_height
other.accent = self.accent
other.control_radius = self.control_radius
other.recent = self.recent
other.swatches = self.swatches
other.color_mode = self.color_mode
other.color_model = self.color_model
other.undo_memory = self.undo_memory
other.memory_usage = self.memory_usage
other.scratch = self.scratch
other.rulers = self.rulers
other.grid = self.grid
other.units = self.units
var shortcuts: map[str, str] = {}
for (id, chord) in self.shortcuts:
shortcuts[id] = chord
other.shortcuts = shortcuts
return other
## The file's text.
pub func serialize(self) -> str:
var text = "# luced-2d settings. Edit here or in Settings⦠(cmd-,); saved values apply at once.\n\n"
text = text + f"[canvas]\nwidth = {self.canvas_width}\nheight = {self.canvas_height}\n\n"
text = text + f"[theme]\naccent = \"{self.accent}\"\ncontrol_radius = {self.control_radius}\n\n"
let rulers = "true" if self.rulers else "false"
let grid = "true" if self.grid else "false"
text = text + f"[view]\nrulers = {rulers}\ngrid = {grid}\n# Rulers and sizes in \"px\", \"mm\" or \"in\".\nunits = \"{self.units}\"\n\n"
text = text + f"[memory]\n# Gigabytes each document's undo history may hold; the oldest steps go first.\nundo = {self.undo_memory}\n# Percent of the GPU's and the machine's memory tiles may use (25..90).\nusage = {self.memory_usage}\n# A folder on a disk for tiles past the RAM budget; empty for the default.\nscratch = '{self.scratch}'\n\n"
text = text + f"[colors]\n# The picker's recent colors, newest first.\nrecent = \"{self.recent}\"\n# The Swatches panel's colors, in order.\nswatches = \"{self.swatches}\"\n# The Color panel's picker (triangle, square, wheel or sliders) and its sliders' model (rgb, hsb, lab or gray).\nmode = \"{self.color_mode}\"\nmodel = \"{self.color_model}\"\n\n"
text = text + "# Shortcuts by command id; a chord like \"cmd+shift+s\", \"f5\" or \"none\". Unlisted commands keep their defaults.\n[shortcuts]\n"
for (id, chord) in self.shortcuts:
text = text + f"{id} = \"{chord}\"\n"
return text
## The settings file's home. Reading never rewrites it; saving writes it whole.
pub class Store:
pub let path: str
## Why the last load fell back to defaults, or "".
pub var warning: str = ""
pub func init(self, directory: str = "") -> unit!:
let chosen = directory if directory != "" else paths.join(files.home_directory(), ".luced-2d")
files.ensure_directory(chosen)
self.path = paths.join(chosen, "settings.toml")
pub func load(self) -> Settings!:
self.warning = ""
# No file yet is the common case: defaults, and nothing written.
let source = files.read_text(self.path, limit = 65536) catch failure:
_ = failure
return Settings()
let loaded = Settings(source) catch failure:
self.warning = f"Settings not read ({self.path}): {failure.message}"
return Settings()
return loaded
pub func save(self, settings: Settings) -> unit!:
files.write_text(self.path, settings.serialize())