Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drawing.jl example halts if wrapped in a function #89

Open
dmos62 opened this issue Jun 8, 2019 · 0 comments
Open

drawing.jl example halts if wrapped in a function #89

dmos62 opened this issue Jun 8, 2019 · 0 comments

Comments

@dmos62
Copy link

dmos62 commented Jun 8, 2019

Being new to the ecosystem, I picked up the drawing.jl example and started toying with it. I refactored a lot of the inline GtkReactive behaviours out into functions, and, instead of using global Signals, I parametrized those functions with them and created a "run" function that instantiates the Gtk window, signals and behaviours.

Problem is the program halts after some drawing. It does it in different ways each time, which you notice when you log traces when signals change. Some signals stop changing, for example the drawing Bool signal, or the lines signal starts getting populated with empty lines.

Also, I can't reproduce it now, but early in the debugging I would get messages that are something like "Reactive.jl message queue full, use this ENV variable to change the queue size". What's interesting is that I would get these "queue full" errors (which would halt the program) immediately on subsequent launches of the program (I reuse REPL sessions).

I isolated the problem with signals stopping to work by wrapping the original drawing.jl example into a run function, replacing all consts with local variables. If I run this with include("wrapped_drawing.jl"); run(), and manically draw for 30-60 seconds, I can reliably cause the program to hang (without error messages). The line being drawn during the hang stays red even after releasing the left mouse button. For comparison, if I run the original example, I can't make it halt.

I only wrapped everything in one module and one function, and added a wait at the end so that the routines would wait for the window to close before continuing, but for the sake of completeness, here's the code:


module SmallRefacDrawing

using Gtk.ShortNames, GtkReactive, Graphics, Colors

function run()

  win = Window("Drawing")
  c = canvas(UserUnit)       # create a canvas with user-specified coordinates
  push!(win, c)

  lines = Signal([])   # the list of lines that we'll draw
  newline = Signal([]) # the in-progress line (will be added to list above)

  # Add mouse interactions
  drawing = Signal(false)  # this will be true if we're dragging a new line
  sigstart = map(c.mouse.buttonpress) do btn
    if btn.button == 1 && btn.modifiers == 0
      push!(drawing, true)   # start extending the line
      push!(newline, [btn.position])
    end
  end

  dummybutton = MouseButton{UserUnit}()
  sigextend = map(filterwhen(drawing, dummybutton, c.mouse.motion)) do btn
    push!(newline, push!(value(newline), btn.position))
  end

  sigend = map(c.mouse.buttonrelease) do btn
    if btn.button == 1
      push!(drawing, false)  # stop extending the line
      push!(lines, push!(value(lines), value(newline)))
      push!(newline, [])
    end
  end

  # Draw on the canvas
  redraw = draw(c, lines, newline) do cnvs, lns, newl
    fill!(cnvs, colorant"white")   # background is white
    set_coordinates(cnvs, BoundingBox(0, 1, 0, 1))  # set coords to 0..1 along each axis
    ctx = getgc(cnvs)
    for l in lns
      drawline(ctx, l, colorant"blue")
    end
    drawline(ctx, newl, colorant"red")
  end

  function drawline(ctx, l, color)
    isempty(l) && return
    p = first(l)
    move_to(ctx, p.x, p.y)
    set_source(ctx, color)
    for i = 2:length(l)
      p = l[i]
      line_to(ctx, p.x, p.y)
    end
    stroke(ctx)
  end

  Gtk.showall(win)

  window_closed = Condition()
  signal_connect(win, :destroy) do widget
    notify(window_closes)
  end
  wait(window_closed)
  @info "program stop"

end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant