simonkrauter / NiGui

Cross-platform desktop GUI toolkit written in Nim
MIT License
718 stars 50 forks source link

Not Updating Text #19

Closed ZvontyFlugence closed 6 years ago

ZvontyFlugence commented 6 years ago

I have created a custom Label type to allow a canvas to draw and fill the label with custom colors that wouldn't normally work with a regular Label. However, after creating the MenuLabel type (see below), the label text no longer updates when its changed using label.text = "text here" where label: MenuLabel. I've already verified that the value is actually being changed and there is no issues with any values being nil, but is not drawn to the canvas with the updated text value. I believe it has something to do with the the text being drawn onto the canvas only once. If this is a basic fix, sorry, I am very new to Nim, but any ideas/clues as to how I can work around this?

menu_label.nim

import nigui

type MenuLabel* = ref object of Label

proc newMenuLabel*(text = ""): MenuLabel =
    result = new MenuLabel
    result.init()
    result.text = text

method handleDrawEvent*(control: MenuLabel, event: DrawEvent) =
    let canvas = event.control.canvas
    canvas.areaColor = rgb(23, 25, 33)
    canvas.textColor = rgb(255, 233, 83)
    canvas.lineColor = rgb(255, 233, 83)
    canvas.drawRectArea(0, 0, control.width, control.height)
    canvas.drawTextCentered(control.text)
    canvas.drawRectOutline(0, 0, control.width, control.height)

Initialization of MenuLabel object

var newFileBtn = newMenuButton("New File")
menuContainer.add(newFileBtn)
newFileBtn.onClick = proc(event: ClickEvent) =
    textArea.text = ""
    fileName.text= "New File"

Updating MenuLabel text

var pathParts = split(dialog.files[0], "\\")            
fileName.text= pathParts[len(pathParts)-1]

EDIT: added syntax highlighting for nim

simonkrauter commented 6 years ago

With my latest commit it should work as you expect. btw, I just saw that you want to create a text editor with NiGui, this is also my plan. I think would be interesting to chat with you e.g. on IRC. GitHub tip: use syntax highlighting when pasting source code.

ZvontyFlugence commented 6 years ago

Thanks for the update! As for assisting in creating a text editor with NiGui, as I said before I'm very new to Nim, and my project was mainly to learn more about Nim and GUI applications. I'm currently a high school senior heading into a Computer Science major for university, so I'm not sure how much help I'd be, but I'd love to chat and talk about it.