dymokomi/luced-2d
Source of release 0.5.1
0.5.1 / src / select_mask_dialog.luc
## Select › Select and Mask…, as Photoshop's (Refine Edge): a floating dialog
## refining the selection's edge — edge detection Radius (the picture decides
## the edge within it), Smooth, Feather, Contrast and Shift Edge — with the
## selection shown as marching ants, a red overlay, or the picture on black or
## on white. A change is only recorded, once a frame: the canvas works it out
## at the view's resolution over what the view shows, on a thread of its own,
## the newest setting cancelling an older one still working, so a slider drag
## keeps up on any document. OK refines all of it at full size as one step,
## Cancel leaves the selection as it was.
from luce_ui.ui import Text, HStack, Spacer, Dialog, Menu, Command
from workspace import Workspace
from kit import Kit, SliderField
from preview_session import PreviewDialogSession
func view_names() -> list[str]:
return ["Marching Ants", "Overlay", "On Black", "On White"]
pub class SelectMaskDialog:
pub let dialog: Dialog
## The refinement's lifecycle (preview_session.luc).
pub let session: PreviewDialogSession
let view_menu: Menu
pub let radius: SliderField
pub let smooth: SliderField
pub let feather: SliderField
pub let contrast: SliderField
pub let shift: SliderField
let workspace: Workspace
pub func init(self, workspace: Workspace, kit: Kit) -> unit!:
self.workspace = workspace
let font = kit.font
var views: list[Command] = []
var index = 0
while index < view_names().length:
let chosen = index
let command = Command(view_names()[index], id = f"refine_view_{index}")
kit.bind(command, func () -> unit!:
self.choose_view(chosen))
views.append(command)
index += 1
self.view_menu = Menu("Marching Ants ▾", views, font = font)
self.radius = kit.slider_field(0.0, 250.0, 0.0, 0, " px", self.changed)
self.smooth = kit.slider_field(0.0, 100.0, 0.0, 0, "", self.changed)
self.feather = kit.slider_field(0.0, 250.0, 0.0, 0, " px", self.changed)
self.contrast = kit.slider_field(0.0, 100.0, 0.0, 0, "%", self.changed)
self.shift = kit.slider_field(-100.0, 100.0, 0.0, 0, "%", self.changed)
self.dialog = Dialog([Text("Select and Mask", font = font), kit.dialog_row("View", self.view_menu, 110.0),
Text("Edge Detection", font = font, muted = true), kit.dialog_row("Radius", self.radius.row, 110.0),
Text("Global Refinements", font = font, muted = true), kit.dialog_row("Smooth", self.smooth.row, 110.0),
kit.dialog_row("Feather", self.feather.row, 110.0), kit.dialog_row("Contrast", self.contrast.row, 110.0),
kit.dialog_row("Shift Edge", self.shift.row, 110.0),
HStack([Spacer(), kit.button("Cancel", self.cancel), kit.button("OK", self.apply)], spacing = 8.0)], width = 460.0, spacing = 10.0)
self.session = PreviewDialogSession(self.dialog, kit, "Select and Mask")
self.session.begin = workspace.selection.begin_refine
self.session.preview = self.preview
self.session.finish = self.finish
kit.dialog_keys(self.dialog, self.cancel, self.apply)
pub func open(self) -> unit!:
if not self.session.open():
return
self.choose_view(0)
self.dialog.open()
func choose_view(self, view: int) -> unit!:
self.view_menu.set_text(view_names()[view] + " ▾")
if self.session.active:
self.workspace.selection.set_refine_view(view)
self.session.refresh()
pub func changed(self, value: float) -> unit!:
_ = value
self.session.request()
# Wake the loop for the frame that shows it.
self.view_menu.layout().invalidate()
## The setting recorded; the view's frames work it out.
func preview(self) -> unit!:
self.workspace.selection.preview_refine(int(self.radius.value()), int(self.smooth.value()), int(self.feather.value()), self.contrast.value(), self.shift.value()) catch failure:
self.session.fail(failure.message)
return
self.session.refresh()
pub func cancel(self) -> unit!:
self.session.close(false)
pub func apply(self) -> unit!:
self.session.close(true)
## The session's end: OK (`keep`) refines the whole selection as one step,
## returning at once; the selection refines in the background.
func finish(self, keep: bool) -> unit!:
self.workspace.selection.finish_refine(keep, int(self.radius.value()), int(self.smooth.value()), int(self.feather.value()), self.contrast.value(), self.shift.value(), background = true)