diff --git a/src/construct.jl b/src/construct.jl index a8a0a24552d0f03a1afbbf8b5c5b6fb4afb3de0b..30474f00e259224bcd420e03df4736fba531a8e6 100644 --- a/src/construct.jl +++ b/src/construct.jl @@ -1,5 +1,6 @@ export @construct + function make_widget(binding) if binding.head != :(=) error("@construct syntax error.") @@ -23,6 +24,18 @@ function symbols(bindings) map(x->x.args[1], bindings) end +""" +Modified version of the @manipulate macro from InteractNext. +Usage: + +widgets, graphic = @construct for x in 1:10, + y in slider(1:10) + plot(x,y) +end + +where widgets is a Widget_Container, the widget for x being accessible with widgets[:x], and graphic is the DOM containing Node of the plot. + +""" macro construct(expr) if expr.head != :for error("@construct syntax is @construct for ", diff --git a/src/gui.jl b/src/gui.jl index 846a22fd16b90fffc169ad73f72c3ffda0003e16..841692913d9ec107913704f4aa4910c02329909a 100644 --- a/src/gui.jl +++ b/src/gui.jl @@ -1,5 +1,11 @@ export Widget_Container, GUI, set!, add!, animate + +""" +Widget_Container contains an array of widgets and a dict, so that a widget can be accessed through getindex with its associated symbol: + +wc[:foo] +""" mutable struct Widget_Container #Widgets with symbols #widgets::Array{WebIO.Node{WebIO.DOM},1} @@ -11,20 +17,32 @@ function Widget_Container() Widget_Container([], Dict()) end +""" +returns the index of the widget associated with symbol +""" function getindex_(widget::Widget_Container, s::Symbol) - #returns the index of the widget associated with symbol widget.vals[s] end +""" +returns the widget at the given index or the one associated with given symbol +""" function Base.getindex(widget::Widget_Container, n::Integer) widget.widgets[n] end function Base.getindex(widget::Widget_Container, s::Symbol) - #returns the widget associated with symbol widget.widgets[getindex_(widget, s)] end +""" +A GUI contains widgets in a widget container as well as the DOM of the UI. + +Accessing a widget: gui.widgets[:foo], where foo is the symbol associated with the widget. +Accessing an observable value: gui[:foo]. +Accessing the DOM object: gui(), which adds non-displayed dom nodes to ensure the widgets don't time out. +""" + mutable struct GUI widgets::Widget_Container dom::WebIO.Node @@ -52,6 +70,9 @@ function (gui::GUI)() Node(:div, gui.dom, watched) end +""" +Used to set the widgets, the dom or the data array of the GUI. This replaces any existing widget or dom in the GUI. +""" function set!(gui::GUI, widgets::Widget_Container) gui.widgets = widgets gui.values = [obs(widget).val for widget in gui.widgets.widgets] @@ -65,6 +86,9 @@ function set!(gui::GUI, data::Array) gui.data = data end +""" +Pushes data to the data array(s) to the provided index/indices. +""" function Base.push!(gui::GUI, value, index::Integer) Base.push!(gui.data[index], value) end @@ -83,6 +107,9 @@ function Base.setindex!(gui::GUI, newval, s::Symbol) obs(gui.widgets[s])[] = newval end +""" +Add a Widget_Container to the GUI Widget_Container. This makes it possible to access widgets or observables using their associated symbols and ensures the widgets don't time out. +""" function add!(gui::GUI, widgets::Widget_Container) newdict = copy(widgets.vals) for key in keys(newdict) @@ -126,14 +153,17 @@ function has_changed(gui::GUI, s::Array{Symbol, 1}) !all(.!(has_changed.(gui, s))) end -# Usage: animate(gui, t) will periodically update all widgets, that is, call the -# listeners of all observables associated with widgets in the gui -# animate(gui, s::symbol(s)) will periodically update the widget(s) associated with s -# animate(gui, s::symbol(s), w::symbol(s)) will periodically update the widget(s) associated -# with s only if the widget(s) associated with w has/have changed +""" + Usage: animate(gui, t) will periodically update all widgets, that is, call the + listeners of all observables associated with widgets in the gui + animate(gui, s::symbol(s)) will periodically update the widget(s) associated with s + animate(gui, s::symbol(s), w::symbol(s)) will periodically update the widget(s) associated + with s only if the widget(s) associated with w has/have changed + + Note: Widgets will stop working after a while if their associated graphic isn't + in the responder. This seems to be due to WebIO. -# Note: Widgets will stop working after a while if their associated graphic isn't -# in the responder. This seems to be due to WebIO. + """ function animate(gui::GUI, t=0.5) ks = [k for k in keys(gui.widgets.vals)]