Skip to content

Commit

Permalink
#93 Change Default widget for objects and Co
Browse files Browse the repository at this point in the history
It now no longer is one that represents tuples etc.
as JSON and parses that back.

Instead, it now provides a dummy widget
that will explain to the user that they must implement a
`toFormField` proc with a more elaborate tooltip.
  • Loading branch information
PhilippMDoerner committed Oct 4, 2023
1 parent 17c3aa7 commit 934c75f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions owlkettle/autoform.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[options, json, times, macros, strutils, sequtils]
import std/[options, times, macros, strformat, strutils, sequtils, typetraits]
import ./dataentries
import ./adw
import ./widgetutils
Expand Down Expand Up @@ -39,15 +39,23 @@ proc toFormField(state: auto, fieldName: static string, typ: typedesc[bool]): Wi
proc changed(newVal: bool) =
state.getField(fieldName) = newVal

proc toFormField(state: auto, fieldName: static string, typ: typedesc[object | ref object | tuple | seq]): Widget =
proc toFormField(state: auto, fieldName: static string, typ: typedesc[auto]): Widget =
return gui:
ActionRow:
title = fieldName
Entry(text = $ %*state.getField(fieldName)) {.addSuffix.}:
proc changed(text: string) =
try:
state.getField(fieldName) = text.parseJson().to(typ)
except Exception: discard
Label():
text = fmt"Override `toFormField` for '{$typ.type}'"
tooltip = fmt"""
The type '{$typ.type}' must implement a `toFormField` proc.
`toFormField(
state: auto,
fieldName: static string,
typ: typedesc[{$typ.type}]
): Widget`
This will override this dummy Widget.
See other widget example applications for examples.
"""

proc toFormField[T: enum](state: auto, fieldName: static string, typ: typedesc[T]): Widget =
let options: seq[string] = T.items.toSeq().mapIt($it)
Expand Down

0 comments on commit 934c75f

Please sign in to comment.