Skip to content

Commit

Permalink
Slider defaults to median of the range. Closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
shashi committed Aug 22, 2014
1 parent bc0afe3 commit 8214d37
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/widgets.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@

using DataStructures
import Base.convert
import Base.convert, Base.median

export Slider, ToggleButton, Button, Options, Checkbox, Textbox,
Textarea, RadioButtons, Dropdown, Select, ToggleButtons

function median{T}(r::Range{T})
mid = (first(r) + last(r)) / 2
# snap to the nearest value in range
# This is subject to floating point calculation errors
# but should never throw an InexactError if T is integral
st = step(r)
multiple = ((mid - first(r)) / st)
offset = multiple - round(multiple)
convert(T, mid - offset * st)
end

### Input widgets

########################## Slider ############################
Expand All @@ -17,7 +28,7 @@ type Slider{T<:Number} <: InputWidget{T}
end

Slider{T}(range::Range{T};
value=first(range),
value=median(range),
input::Signal{T}=Input(value),
label="") =
Slider(input, label, value, range)
Expand Down

0 comments on commit 8214d37

Please sign in to comment.