Skip to content

Commit

Permalink
fix: memory leak
Browse files Browse the repository at this point in the history
optimize: use special ChangableChild object
  • Loading branch information
levovix0 committed Dec 6, 2024
1 parent f62f06a commit a50df3d
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 90 deletions.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Built-in interpolation modifiers:
```nim
type
MyComponent = ref object of Uiobj
changableChild: CustomProperty[UiRect]
changableChild: ChangableChild[UiRect]
registerComponent MyComponent
Expand All @@ -324,7 +324,7 @@ Changable childs could be used to re-build component tree on any event.
# ...in makeLaout macro...
var elements = ["first", "second"]
var elementsObj: CustomProperty[Layout]
var elementsObj: ChangableChild[Layout]
elementsObj --- Layout():
orientation = vertical
Expand Down Expand Up @@ -355,7 +355,7 @@ elementsObj --- Layout():
The `<--- ctor: prop[]; event[]; ...` syntax can be used to re-build tree based on property changes
```nim
var elements = ["first", "second"].property
var elementsObj: CustomProperty[Layout]
var elementsObj: ChangableChild[Layout]
elementsObj --- Layout():
<--- Layout(): elements[]
Expand Down
27 changes: 23 additions & 4 deletions src/sigui/events.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ type
EventBase = object
connected: seq[EventConnection[int]] # type of function argument does not matter for this

FlaggedPointer* = distinct pointer
## pointer, but first bit is used for a flag (pointers are aligned anyway)

Event*[T] = object # pointer is wrapped to an object to attach custom destructor
p: ptr EventObj[T]
uiobj*: pointer # most of events is .changed properties, after most of which redraw should happen. If not nil, emit will call redrawUiobj
uiobj*: FlaggedPointer # most of events is .changed properties, after most of which redraw should happen. If not nil, emit will call redrawUiobj

EventObj*[T] = object
## only EventHandler can be connected to event
Expand All @@ -29,7 +32,10 @@ type
connected: seq[EventConnection[T]]


var redrawUiobj*: proc(uiobj: pointer) {.cdecl.}
var redrawUiobj*: proc(uiobj: FlaggedPointer) {.cdecl.}


proc `==`*(a, b: FlaggedPointer): bool {.borrow.}


#* ------------- Event ------------- *#
Expand All @@ -38,6 +44,17 @@ proc destroyEvent(s: ptr EventBase) {.raises: [].}
proc destroyEventHandler(handler: ptr EventHandlerObj) {.raises: [].}


proc `=trace`[T](event: var Event[T], env: pointer) =
if event.p != nil:
for conn in event.p[].connected.mitems:
`=trace`(conn, env)

proc `=trace`(eh: var EventHandler, env: pointer) =
if eh.p != nil:
for event in eh.p[].connected:
`=trace`(event[], env)


proc `=destroy`[T](s: Event[T]) =
if s.p != nil:
destroyEvent(cast[ptr EventBase](s.p))
Expand All @@ -64,6 +81,7 @@ proc destroyEvent(s: ptr EventBase) =
handler[].connected.delete i
else:
inc i
`=destroy`(s[])
dealloc s


Expand All @@ -86,6 +104,7 @@ proc destroyEventHandler(handler: ptr EventHandlerObj) =
s[].connected.delete i
else:
inc i
`=destroy`(handler[])
dealloc handler


Expand Down Expand Up @@ -154,7 +173,7 @@ proc emit*[T](s: Event[T], v: T, disableFlags: set[EventConnectionFlag] = {}) =
if (disableFlags * s.p[].connected[i].flags).len == 0:
s.p[].connected[i].f(v)
inc i
if s.uiobj != nil:
if s.uiobj.pointer != nil:
redrawUiobj s.uiobj

proc emit*(s: Event[void], disableFlags: set[EventConnectionFlag] = {}) =
Expand All @@ -164,7 +183,7 @@ proc emit*(s: Event[void], disableFlags: set[EventConnectionFlag] = {}) =
if (disableFlags * s.p[].connected[i].flags).len == 0:
s.p[].connected[i].f()
inc i
if s.uiobj != nil:
if s.uiobj.pointer != nil:
redrawUiobj s.uiobj


Expand Down
4 changes: 2 additions & 2 deletions src/sigui/scrollArea.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type
{ScrollAreaSetting.low..ScrollAreaSetting.high}
).property

verticalScrollbarObj*: CustomProperty[UiObj]
verticalScrollbarObj*: ChangableChild[UiObj]
## if changed, properties below can be attached by you to new object
verticalScrollbarOpacity*: Property[float]
## has default animation
Expand All @@ -53,7 +53,7 @@ type
verticalScrollBarLastShown*: Property[Time]
verticalScrollBarHideDelay*: Property[Duration] = initDuration(milliseconds = 1000).property

horizontalScrollbarObj*: CustomProperty[UiObj]
horizontalScrollbarObj*: ChangableChild[UiObj]
## if changed, properties below can be attached by you to new object
horizontalScrollbarOpacity*: Property[float]
## has default animation
Expand Down
6 changes: 3 additions & 3 deletions src/sigui/textArea.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type


TextArea* = ref object of Uiobj
cursorObj*: CustomProperty[UiObj]
selectionObj*: CustomProperty[Uiobj]
textObj*: CustomProperty[UiText]
cursorObj*: ChangableChild[UiObj]
selectionObj*: ChangableChild[Uiobj]
textObj*: ChangableChild[UiText]

active*: Property[bool]
text*: Property[string]
Expand Down
Loading

0 comments on commit a50df3d

Please sign in to comment.