From 552317a091633670ce0a52df8aa7ee4f08b23164 Mon Sep 17 00:00:00 2001 From: Philipp Doerner Date: Thu, 28 Sep 2023 19:47:48 +0200 Subject: [PATCH] #83 Add support for inverting widget --- examples/widgets/scale.nim | 12 +++++++++--- owlkettle/gtk.nim | 11 ++++++----- owlkettle/widgets.nim | 30 ++++++++++++++---------------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/examples/widgets/scale.nim b/examples/widgets/scale.nim index 2c531883..5e563a25 100644 --- a/examples/widgets/scale.nim +++ b/examples/widgets/scale.nim @@ -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: @@ -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), @@ -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())) diff --git a/owlkettle/gtk.nim b/owlkettle/gtk.nim index c3fba219..702e448a 100644 --- a/owlkettle/gtk.nim +++ b/owlkettle/gtk.nim @@ -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 diff --git a/owlkettle/widgets.nim b/owlkettle/widgets.nim index 9f57cef4..eeef4c0c 100644 --- a/owlkettle/widgets.nim +++ b/owlkettle/widgets.nim @@ -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. @@ -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() @@ -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