Skip to content

Commit

Permalink
can-lehmann#83 Add support for inverting widget
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMDoerner committed Sep 28, 2023
1 parent 87421cc commit 552317a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
12 changes: 9 additions & 3 deletions examples/widgets/scale.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import owlkettle
import std/options

viewable App:
discard
value: float64 = 1.0
invertScale: bool = false

method view(app: AppState): Widget =
result = gui:
Expand All @@ -33,7 +34,8 @@ method view(app: AppState): Widget =
defaultSize = (600, 400)
Box(orient = OrientX, spacing = 6, margin = 12):
Scale:
currentValue = 0.5
value = app.value
inverted = app.invertScale
marks = @[
(some "left", 0.25, ScaleMarkLeft),
(some "right", 0.5, ScaleMarkRight),
Expand All @@ -42,5 +44,9 @@ method view(app: AppState): Widget =
]
proc valueChanged(newValue: float64) =
echo "New value is ", $newValue


Button:
proc clicked() =
app.value = app.value - 0.1
app.invertScale = not app.invertScale
brew(gui(App()))
11 changes: 6 additions & 5 deletions owlkettle/gtk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,13 @@ proc gtk_scrolled_window_get_vadjustment*(window: GtkWidget): GtkAdjustment
proc gtk_scrolled_window_set_child*(window, child: GtkWidget)

# Gtk.Range
proc gtk_range_get_value*(rangeWidget: GtkWidget): cdouble
proc gtk_range_set_increments*(rangeWidget: GtkWidget; step, page: cdouble)
proc gtk_range_set_range*(rangeWidget: GtkWidget; min, max: cdouble)
proc gtk_range_set_restrict_to_fill_level*(rangeWidget: GtkWidget, restrict_to_fill_level: cbool)
proc gtk_range_set_show_fill_level*(rangeWidget: GtkWidget, show_fill_level: cbool)
proc gtk_range_get_value*(widget: GtkWidget): cdouble
proc gtk_range_set_increments*(widget: GtkWidget; step, page: cdouble)
proc gtk_range_set_range*(widget: GtkWidget; min, max: cdouble)
proc gtk_range_set_restrict_to_fill_level*(widget: GtkWidget, restrict_to_fill_level: cbool)
proc gtk_range_set_show_fill_level*(widget: GtkWidget, show_fill_level: cbool)
proc gtk_range_set_value*(widget: GtkWidget, value: cdouble)
proc gtk_range_set_inverted*(widget: GtkWidget, setting: cbool)

# Gtk.Scale
proc gtk_scale_new*(widget: GtkOrientation, adjustment: GtkAdjustment): GtkWidget
Expand Down
30 changes: 14 additions & 16 deletions owlkettle/widgets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3420,9 +3420,9 @@ renderable Scale of BaseWidget:
marks: seq[Mark]
min: float64
max: float64
precision: int = 2
inverted: bool
showCurrentValue: bool = true
currentValue: float64 = 0.0
value: float64 = 0.0
orient: Orient = OrientX

proc valueChanged(newValue: float64) # Emitted when the range value changes.
Expand All @@ -3437,19 +3437,18 @@ renderable Scale of BaseWidget:
let max: float64 = if widget.hasMax: widget.valMax else: 1
state.internalWidget.gtk_range_set_range(min.cfloat, max.cfloat)

let currentValue: float64 = widget.valCurrentValue
echo "CurrentValue: ", currentvalue
state.internalWidget.gtk_range_set_value(currentValue.cdouble)
let value: float64 = if widget.hasValue: widget.valValue else: 0
state.internalWidget.gtk_range_set_value(value.cdouble)

let inverted = if widget.hasInverted: widget.valInverted else: false
state.internalWidget.gtk_range_set_inverted(inverted.cbool)

connectEvents:
proc valueChangedEventCallback(
widget: GtkWidget,
data: ptr EventObj[proc(newValue: float)]
) {.cdecl.} =
let scaleValue: float64 = gtk_range_get_value(widget).float64
echo "Value changed callback. New value: ", scaleValue, " | old Value: ", ScaleState(data[].widget).currentValue
echo ScaleState(data[].widget).currentValue
ScaleState(data[].widget).currentValue = scaleValue
data[].callback(scaleValue)
data[].redraw()

Expand All @@ -3466,15 +3465,14 @@ renderable Scale of BaseWidget:
for mark in widget.valMarks:
let label: string = if mark.label.isSome(): mark.label.get() else: $mark.value
gtk_scale_add_mark(state.internalWidget, mark.value , mark.position.toGtk(), label.cstring)

hooks currentValue:
build:
gtk_range_set_value(state.internalWidget, widget.valCurrentValue)
echo "Build: ", widget.valCurrentValue, " - ", state.currentValue
hooks value:
update:
gtk_range_set_value(state.internalWidget, widget.valValue)

hooks inverted:
update:
gtk_range_set_value(state.internalWidget, state.currentValue) # This seemed more correct as state.currentValue keeps being the one thing that
widget.valCurrentValue = state.currentValue # Necessary as otherwise widget.valCurrentValue remains at 0.5, meaning out of sync with the value in state
echo "Update: ", widget.valCurrentValue, " - ", state.currentValue
let inverted = if widget.hasInverted: widget.valInverted else: false
state.internalWidget.gtk_range_set_inverted(inverted.cbool)


export BaseWidget, BaseWidgetState, BaseWindow, BaseWindowState
Expand Down

0 comments on commit 552317a

Please sign in to comment.