dymokomi/luced-2d
Source of release 0.5.1
0.5.1 / src / settings_panel.luc
## Edit › Settings…: the dialog for what a machine keeps for luced-2d — the new
## canvas size, the shortcuts, the theme — edited as a draft and saved together.
from luce_ui.ui import Widget, Font, Text, Button, VStack, HStack, Spacer, Size, Dialog, ListView, ListItem, TableView, TableColumn, TableCell, NumberField, TextField, Command, Shortcut, ShortcutField, Application, Rule
from interop import Connection
from workspace import Workspace
from actions import Actions
from settings import Settings, Store
from color_picker import Swatch
from theme import Look
import theme
from hex_color import parse_hex
## The Settings dialog after eleusis-layout's: a section list at the left
## (General, Shortcuts, Theme), the section's rows at the
## right, and Save/Cancel below. Edits go into a draft, not the live settings:
## Cancel forgets them all, Save writes the file and applies everything at once.
## Shortcuts are the application's commands themselves: pick a row, then record
## a chord in the field beneath; another command already holding it is named,
## not silently unbound.
pub class SettingsDialog:
pub let dialog: Dialog
let store: Store
pub var settings: Settings
var draft: Settings
let workspace: Workspace
let actions: Actions
let font: Font
pub var app: Application? = none
let sections: ListView
let pages: list[VStack]
let width_field: NumberField
let height_field: NumberField
let undo_field: NumberField
let usage_field: NumberField
pub let scratch_field: TextField
let accent_field: TextField
let accent_swatch: Swatch
let radius_field: NumberField
let table: TableView
let recorder: ShortcutField
let selected_label: Text
let conflict: Text
## The commands a shortcut can be set for, in the table's order, and their default chords.
let commands: list[Command]
let defaults: map[str, str]
let connections: list[Connection]
## Called after Save applied the settings, so the window can redraw.
pub var applied: func() -> unit!? = none
pub func init(self, workspace: Workspace, actions: Actions, font: Font, store: Store) -> unit!:
self.workspace = workspace
self.actions = actions
self.font = font
self.store = store
self.settings = store.load()
self.draft = self.settings.copy()
self.connections = []
self.commands = []
var defaults: map[str, str] = {}
for command in actions.all:
if command.id() != "" and not command.is_divider():
self.commands.append(command)
defaults[command.id()] = command.shortcut().chord()
self.defaults = defaults
# General: the size a new canvas takes.
self.width_field = NumberField(1280.0, 1.0, 30000.0, 1.0, suffix = " px", font = font, cells = 8.0)
self.height_field = NumberField(800.0, 1.0, 30000.0, 1.0, suffix = " px", font = font, cells = 8.0)
self.connections.append(self.width_field.on_change(func (value: float) -> unit!:
self.draft.canvas_width = int(value)))
self.connections.append(self.height_field.on_change(func (value: float) -> unit!:
self.draft.canvas_height = int(value)))
self.undo_field = NumberField(4.0, 0.25, 256.0, 0.25, suffix = " GB", font = font, cells = 8.0)
self.undo_field.set_decimals(2)
self.connections.append(self.undo_field.on_change(func (value: float) -> unit!:
self.draft.undo_memory = value))
self.usage_field = NumberField(60.0, 25.0, 90.0, 5.0, suffix = " %", font = font, cells = 8.0)
self.connections.append(self.usage_field.on_change(func (value: float) -> unit!:
self.draft.memory_usage = value))
self.scratch_field = TextField("", font = font, cells = 24.0)
self.connections.append(self.scratch_field.on_commit(func (value: str) -> unit!:
self.draft.scratch = value))
let general = VStack([self.row("New canvas", HStack([self.width_field, Text("×", font = font, muted = true), self.height_field], spacing = 6.0)),
self.row("Undo memory", self.undo_field), self.row("Memory usage", self.usage_field), self.row("Scratch folder", self.scratch_field), Spacer()], spacing = 8.0)
# Shortcuts: every command in a table; the field beneath records for the picked row.
self.table = TableView([TableColumn(title = "Action", weight = 1.0), TableColumn(title = "Shortcut", width = 150.0)], self.cells(), font = font)
self.table.layout().set_limits(Size(200.0, 190.0), Size(16384.0, 190.0))
self.connections.append(self.table.on_activate(func (index: int) -> unit!:
self.show_selected()))
self.recorder = ShortcutField(font = font)
self.connections.append(self.recorder.on_change(self.recorded))
self.selected_label = Text("Pick a command above", font = font, muted = true)
self.conflict = Text("", font = font, muted = true)
let shortcuts = VStack([self.table, HStack([self.selected_label, Spacer()], spacing = 6.0),
HStack([self.recorder, self.button("Clear", self.clear_selected), self.button("Default", self.default_selected), Spacer(), self.button("Restore All", self.restore_shortcuts)], spacing = 8.0),
self.conflict], spacing = 8.0)
# Theme: the accent and the control radius.
self.accent_swatch = Swatch(24.0)
self.accent_field = TextField("#e7a03f", font = font, cells = 9.0)
self.connections.append(self.accent_field.on_commit(func (value: str) -> unit!:
self.set_accent(value)))
self.radius_field = NumberField(5.0, 0.0, 16.0, 1.0, suffix = " px", font = font, cells = 6.0)
self.connections.append(self.radius_field.on_change(func (value: float) -> unit!:
self.draft.control_radius = value))
let look = VStack([self.row("Accent", HStack([self.accent_swatch, self.accent_field], spacing = 8.0)), self.row("Control radius", self.radius_field), Spacer()], spacing = 8.0)
self.pages = [general, shortcuts, look]
for page in self.pages:
page.layout().set_limits(Size(400.0, 300.0), Size(16384.0, 300.0))
page.layout().set_visible(false)
self.pages[0].layout().set_visible(true)
self.sections = ListView([ListItem(text = "General"), ListItem(text = "Shortcuts"), ListItem(text = "Theme")], font = font)
self.sections.layout().set_limits(Size(170.0, 300.0), Size(170.0, 300.0))
self.connections.append(self.sections.on_activate(func (index: int) -> unit!:
self.show_page(index)))
var content: list[Widget] = []
for page in self.pages:
content.append(page)
let body = HStack([self.sections, Rule(structural = true, vertical = true), VStack(content, spacing = 0.0)], spacing = 12.0)
let footer = HStack([Spacer(), self.button("Cancel", self.cancel), self.button("Save", self.save)], spacing = 8.0)
self.dialog = Dialog([Text("Settings", font = font), body, footer], width = 720.0)
func row(self, label: str, control: Widget) -> HStack!:
let caption = Text(label, font = self.font, muted = true)
caption.layout().set_limits(Size(120.0, 0.0), Size(120.0, 16384.0))
return HStack([caption, control], spacing = 8.0)
func button(self, text: str, callback: func() -> unit!) -> Button!:
let button = Button(text, font = self.font)
self.connections.append(button.on_click(callback))
return button
pub func show_page(self, index: int) -> unit!:
var page = 0
while page < self.pages.length:
self.pages[page].layout().set_visible(page == index)
page += 1
pub func is_open(self) -> bool:
return self.dialog.is_open()
## Open on the live settings: the draft starts as their copy.
pub func open(self) -> unit!:
self.draft = self.settings.copy()
self.show_draft()
self.dialog.open()
func show_draft(self) -> unit!:
self.width_field.set_value(float(self.draft.canvas_width))
self.height_field.set_value(float(self.draft.canvas_height))
self.undo_field.set_value(self.draft.undo_memory)
self.usage_field.set_value(self.draft.memory_usage)
self.scratch_field.set_text(self.draft.scratch)
self.accent_field.set_text(self.draft.accent)
self.show_accent()
self.radius_field.set_value(self.draft.control_radius)
self.table.set_rows(self.cells())
self.conflict.set_text("")
self.show_selected()
func set_accent(self, value: str) -> unit!:
let text = value.trim()
let hex = text if text[..<1] == "#" else "#" + text
if parse_hex(hex) != none:
self.draft.accent = hex.lower()
self.accent_field.set_text(self.draft.accent)
self.show_accent()
func show_accent(self) -> unit!:
let look = Look(self.draft.accent, self.draft.control_radius)
self.accent_swatch.color = look.accent
self.accent_swatch.layout().invalidate()
## The chord the draft gives a command: its override, else its default.
func chord_for(self, id: str) -> str:
if let chord = self.draft.shortcuts[id]:
return chord
if let chord = self.defaults[id]:
return chord
return "none"
## The table's cells: a command's name and its chord as a menu shows it.
func cells(self) -> list[TableCell]!:
var cells: list[TableCell] = []
for command in self.commands:
cells.append(TableCell(text = command.text()))
let chord = self.chord_for(command.id())
let shortcut = Shortcut.parse(chord)
cells.append(TableCell(text = shortcut.text() if shortcut.is_bound() else "—", muted = not shortcut.is_bound()))
return cells
func selected_command(self) -> Command?:
let index = self.table.selected_index()
if index < 0 or index >= self.commands.length:
return none
return self.commands[index]
func show_selected(self) -> unit!:
if let command = self.selected_command():
self.selected_label.set_text(command.text())
self.recorder.set_shortcut(Shortcut.parse(self.chord_for(command.id())))
else:
self.selected_label.set_text("Pick a command above")
self.recorder.set_shortcut(Shortcut())
## A chord was recorded for the picked command; another holder is named.
func recorded(self) -> unit!:
let command = self.selected_command() else return
self.set_chord(command.id(), self.recorder.shortcut().chord())
func set_chord(self, id: str, chord: str) -> unit!:
if let fallback = self.defaults[id]:
if fallback == chord:
_ = self.draft.shortcuts.remove(id)
else:
self.draft.shortcuts[id] = chord
self.table.set_rows(self.cells())
var holder = ""
if chord != "none":
for other in self.commands:
if other.id() != id and self.chord_for(other.id()) == chord:
holder = other.text()
self.conflict.set_text(f"Also bound to {holder}" if holder != "" else "")
self.show_selected()
func clear_selected(self) -> unit!:
let command = self.selected_command() else return
self.set_chord(command.id(), "none")
func default_selected(self) -> unit!:
let command = self.selected_command() else return
self.set_chord(command.id(), self.chord_for_default(command.id()))
func chord_for_default(self, id: str) -> str:
if let chord = self.defaults[id]:
return chord
return "none"
func restore_shortcuts(self) -> unit!:
self.draft.shortcuts = {}
self.table.set_rows(self.cells())
self.conflict.set_text("")
self.show_selected()
pub func cancel(self) -> unit!:
self.dialog.hide()
## Save writes the file and applies the draft as the live settings.
pub func save(self) -> unit!:
# A text field still being typed in has not committed: take what it shows.
self.draft.scratch = self.scratch_field.text().trim()
self.set_accent(self.accent_field.text())
self.settings = self.draft.copy()
self.store.save(self.settings)
self.dialog.hide()
self.apply()
## The picker committed `hex`: it heads the history, which is saved at once.
## Keep the Swatches panel's colors in the file.
pub func remember_swatches(self, text: str) -> unit!:
self.settings.swatches = text
self.store.save(self.settings)
## The Color panel's picker mode and slider model, kept for the next launch.
pub func remember_color_mode(self, mode: str, model: str) -> unit!:
self.settings.color_mode = mode
self.settings.color_model = model
self.store.save(self.settings)
pub func remember(self, hex: str) -> unit!:
var recent: list[str] = [hex]
for other in self.workspace.colors.recent:
if other != hex and recent.length < 16:
recent.append(other)
self.workspace.colors.recent = recent
var joined = ""
for item in recent:
joined = joined + (" " if joined != "" else "") + item
self.settings.recent = joined
self.store.save(self.settings)
## The view aids changed: keep them for the next launch.
pub func remember_view(self) -> unit!:
self.settings.rulers = self.workspace.viewport.show_rulers
self.settings.grid = self.workspace.viewport.show_grid
self.settings.units = self.workspace.viewport.units
self.store.save(self.settings)
## Apply the live settings: the look, the window theme, every shortcut, the picker's history.
pub func apply(self) -> unit!:
self.workspace.viewport.show_rulers = self.settings.rulers
self.workspace.viewport.show_grid = self.settings.grid
self.workspace.viewport.units = self.settings.units
self.workspace.documents.set_history_limit(int(self.settings.undo_memory * 1073741824.0))
self.workspace.set_memory_usage(self.settings.memory_usage / 100.0)
# A folder that is not there keeps the default rather than failing the rest.
self.workspace.set_scratch_folder(self.settings.scratch) catch failure:
_ = failure
let look = Look(self.settings.accent, self.settings.control_radius)
self.workspace.look = look
var recent: list[str] = []
for item in self.settings.recent.split(" "):
if item.length == 7 and recent.length < 16:
recent.append(item)
self.workspace.colors.recent = recent
if let app = self.app:
app.set_theme(theme.editor_theme(look))
for command in self.commands:
let chord = self.live_chord(command.id())
let shortcut = Shortcut.parse(chord) catch failure:
_ = failure
continue
command.set_shortcut(shortcut)
if let done = self.applied:
done()
func live_chord(self, id: str) -> str:
if let chord = self.settings.shortcuts[id]:
return chord
return self.chord_for_default(id)
pub func close(self):
for connection in self.connections:
connection.disconnect()
self.connections.clear()