Skip to content

Commit

Permalink
fix type for spinbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Vertechi committed Jun 12, 2018
1 parent 985e5a4 commit 8fc5497
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,29 @@ function colorpicker(::WidgetTheme, val=colorant"#000000"; value=val, kwargs...)
end

"""
`spinbox(label=""; value=nothing)`
`spinbox([range,] label=""; value=nothing)`
Create a widget to select numbers with placeholder `label`
Create a widget to select numbers with placeholder `label`. An optional `range` first argument
specifies maximum and minimum value accepted as well as the step.
"""
function spinbox(::WidgetTheme, label=""; value=nothing, placeholder=label, kwargs...)
function spinbox(::WidgetTheme, label=""; value=nothing, placeholder=label, isinteger=false, kwargs...)
isinteger = isa(_get(value), Integer) || isa(_get(value), Void) && isinteger
T = isinteger ? Int : Float64
if value == nothing
internalvalue = Observable("")
value = Observable{Union{Float64, Void}}(nothing)
value = Observable{Union{T, Void}}(nothing)
else
(value isa Observable) || (value = Observable{Union{Float64, Void}}(value))
(value isa Observable) || (value = Observable{Union{T, Void}}(value))
internalvalue = Observable(string(value[]))
end
on(t -> t in ["", "-"] || (value[] = parse(Float64, t)), internalvalue)
on(t -> t in ["", "-"] || (value[] = parse(T, t)), internalvalue)
ui = input(internalvalue; placeholder=placeholder, typ="number", kwargs...)
Widget{:spinbox}(ui, value)
end

spinbox(T::WidgetTheme, vals::Range, args...; value=medianelement(vals), isinteger=(eltype(vals) <: Integer), kwargs...) =
spinbox(T, args...; value=value, isinteger=isinteger, min=minimum(vals), max=maximum(vals), step=step(vals), kwargs...)

"""
`autocomplete(options, label=""; value="")`
Expand Down
3 changes: 3 additions & 0 deletions src/widget_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ slap_design!(n::Node, args...) = slap_design!(Scope()(n), args...)
slap_design!(w::Widget, args...) = (slap_design!(scope(w), args...); w)

isijulia() = isdefined(Main, :IJulia) && Main.IJulia.inited

_get(o::Observable) = o[]
_get(o) = o

0 comments on commit 8fc5497

Please sign in to comment.