Skip to content

Commit

Permalink
can-lehmann#88 First draft for SpinRow
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMDoerner committed Oct 3, 2023
1 parent 7c75972 commit c463539
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
63 changes: 63 additions & 0 deletions examples/widgets/adw/spin_row.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# MIT License
#
# Copyright (c) 2022 Can Joshua Lehmann
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import owlkettle, owlkettle/[dataentries, adw, autoform]
import std/[options, sequtils]

viewable App:
climbRate: float = 0.0
digits: uint = 0
numeric: bool = false
min: float = 0.0
max: float = 100.0
snapToTicks: bool = false
updatePolicy: SpinButtonUpdatePolicy = SpinButtonUpdateAlways
value: float = 0.0
wrap: bool = false

method view(app: AppState): Widget =
result = gui:
WindowSurface:
defaultSize = (800, 600)
Box(orient = OrientX):
insert app.toAutoForm()

Separator() {.expand: false.}

Box(orient = OrientY):
HeaderBar {.expand: false.}:
WindowTitle {.addTitle.}:
title = "SpinRow Example"

SpinRow():
climbRate = app.climbRate
digits = app.digits
numeric = app.numeric
snapToTicks = app.snapToTicks
updatePolicy = app.updatePolicy
value = app.value
wrap = app.wrap

proc input(newValue: float) =
echo "New Value was input: ", newValue

adw.brew(gui(App()))
96 changes: 96 additions & 0 deletions owlkettle/adw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ type
FlapTransitionUnder
FlapTransitionSlide

SpinButtonUpdatePolicy* = enum
SpinButtonUpdateAlways
SpinButtonUpdateIfValid

{.push importc, cdecl.}
# Adw
proc adw_init()
Expand Down Expand Up @@ -149,6 +153,20 @@ proc adw_split_button_new(): GtkWidget
proc adw_split_button_set_child(button, child: GtkWidget)
proc adw_split_button_set_popover(button, child: GtkWidget)

when AdwVersion >= (1, 4):
proc adw_spin_row_new(adjustment: GtkAdjustment, climb_rate: cdouble, digits: cuint): GtkWidget
proc adw_spin_row_configure(self: GtkWidget, adjustment: GtkAdjustment, climb_rate: cdouble, digits: cuint)
proc adw_spin_row_set_adjustment(self: GtkWidget, adjustment: GtkAdjustment)
proc adw_spin_row_set_climb_rate(self: GtkWidget, climb_rate: cdouble)
proc adw_spin_row_set_digits(self: GtkWidget, digits: cuint)
proc adw_spin_row_set_numeric(self: GtkWidget, numeric: cbool)
proc adw_spin_row_set_range(self: GtkWidget, min: cdouble, max: cdouble)
proc adw_spin_row_set_snap_to_ticks(self: GtkWidget, snap_to_ticks: cbool)
proc adw_spin_row_set_update_policy(self: GtkWidget, policy: SpinButtonUpdatePolicy)
proc adw_spin_row_set_value(self: GtkWidget, value: cdouble)
proc adw_spin_row_set_wrap(self: GtkWidget, wrap: cbool)
proc adw_spin_row_update(self: GtkWidget)
proc adw_spin_row_get_value(self: GtkWidget): cdouble
when AdwVersion >= (1, 2):
# Adw.AboutWindow
proc adw_about_window_new(): GtkWidget
Expand Down Expand Up @@ -690,6 +708,84 @@ proc `valSwipe=`*(flap: Flap, swipe: bool) =
flap.valSwipeToOpen = swipe
flap.valSwipeToClose = swipe

when AdwVersion >= (1, 4):
renderable SpinRow of ActionRow:
climbRate: float = 0.0
digits: uint = 0
numeric: bool = false
min: float = 0.0
max: float = 100.0
snapToTicks: bool = false
updatePolicy: SpinButtonUpdatePolicy = SpinButtonUpdateAlways
value: float = 0.0
wrap: bool = false

proc input(newValue: float)

hooks:
beforeBuild:
let climbRate: float = if widget.hasClimbRate: widget.valClimbRate else: 0.0
let digits: uint = if widget.hasDigits: widget.valDigits else: 0.uint
state.internalWidget = adw_spin_row_new(GtkAdjustment(nil), climbRate.cdouble, digits.cuint)

connectEvents:
proc inputEventCallback(
widget: GtkWidget,
newValue: cdouble,
data: ptr EventObj[proc(newValue: float)]
): cint {.cdecl.} =
let x = data[].widget
echo data == nil
echo data[].widget == nil
echo SpinRowState(data[].widget) == nil ## Line 740, this line segfaults according to the stacktrace
SpinRowState(data[].widget).value = newValue.float
data[].callback(newValue)
data[].redraw()

result = true.cint

state.connect(state.input, "input", inputEventCallback)
# state.connect(state.output, "output", eventCallback)
# state.connect(state.wrapped, "wrapped", eventCallback)

hooks climbRate:
property:
adw_spin_row_set_climb_rate(state.internalWidget, state.climbRate.cdouble)

hooks digits:
property:
adw_spin_row_set_digits(state.internalWidget, state.digits.cuint)

hooks numeric:
property:
adw_spin_row_set_numeric(state.internalWidget, state.numeric.cbool)

hooks min:
property:
adw_spin_row_set_range(state.internalWidget, state.min.cdouble, state.max.cdouble)

hooks max:
property:
adw_spin_row_set_range(state.internalWidget, state.min.cdouble, state.max.cdouble)

hooks snapToTicks:
property:
adw_spin_row_set_snap_to_ticks(state.internalWidget, state.snapToTicks.cbool)

hooks updatePolicy:
property:
adw_spin_row_set_update_policy(state.internalWidget, state.updatePolicy)

hooks value:
property:
adw_spin_row_set_value(state.internalWidget, state.value.cdouble)

hooks wrap:
property:
adw_spin_row_set_wrap(state.internalWidget, state.wrap.cbool)

export SpinRow

renderable SplitButton of BaseWidget:
child: Widget
popover: Widget
Expand Down

0 comments on commit c463539

Please sign in to comment.