khchen / wNim

Nim's Windows GUI Framework
MIT License
327 stars 17 forks source link

[feature request on autolayout] can read widget/container/variable's name and attribute #74

Open retsyo opened 3 years ago

retsyo commented 3 years ago

My thought is to create a run-able, but minimal nim code in wNim by parsing Autolayout syntax in an auto way.

Now I do so by analyzing the string created by initVflParser().parse(strVfl).toString. However, it is tedious so now my code only support simple VFL syntax without STRONG, WEAK, ~ and more features.

So can you export some functions in autolayout? For example

for i in parser.widget:
    echo i.name, i.attribute, i.weak

will output

text1, ["left", "right", "width"], false
text2, ["top", "width"], true

while

for i in parser.variable:
    echo i.name

will output

variable1
variable2

which are created as

var variable1 = newVariable()
var variable2 = newVariable()

p.s. the VFL string

H:|[btnB1(btnB2)]|
V:|-[btnB1(btnB2/2)]-[btnB2]-|

will output the following code. In the code, a widget whose name starts with btn will be created as a Button and a null callback function is supplied too.

import wNim

const
    Title = "title 1"

let app = App()
let frame = Frame(title=Title, size=(400, 300))

let panel = Panel(frame)

# create widgets
let btnB1 = Button(panel, label="btnB1")
let btnB2 = Button(panel, label="btnB2")   

proc layout() =
    panel.autolayout """
        H:|[btnB1(btnB2)]|
        V:|-[btnB1(btnB2/2)]-[btnB2]-|
    """

btnB1.wEvent_Button do (): # The button is clicked.
    echo "null Button btnB1 event"

btnB2.wEvent_Button do (): # The button is clicked.
    echo "null Button btnB2 event"

var font12 = Font(12, weight=wFontWeightBold)
btnB1.font = font12
btnB2.font = font12

panel.wEvent_Size do (): layout()

layout()
frame.center()
frame.show()
app.mainLoop()
retsyo commented 3 years ago

the long-time plan is to make a visual layout IDE for nim:

  1. we type in the VFL

  2. a frame is created on typing VFL, and then all widgets described in VFL is placed on the frame according to the constrained size and position.

  3. some rules can be used to tell the category of the widget. For example, if the widget's name in VFL starts with btn, we think it should be a Button firstly; txt for TextCtrl and so on

  4. when we click one widget, an attribute panel appears. we can adjust the attributes of the widget since these information do not exist in VFL. For example, default value, font, style for TextCtrl, and choose event type then add null (or type code for real action) callback function

  5. what is more, we can change/choose the category of the widget. Then the attribute panel changes.

  6. of cause we can save the project, which supports the VFL and attributes for all widgets

  7. we can export code for nim now.

this will work for wxPython, and In fact I have made a try, but since kiwisolver is not Autolayout. It is impossible to make all features in wxPython now.

khchen commented 3 years ago

Interesting. However, these infromation are not stored inside the object (except variables), so it is hard to output unless I rewrite the code.

Parsing the output DSL of autolayout may be a solution? It's more predictable.