Skip to content

Commit

Permalink
Automatically export widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
can-lehmann committed Jun 24, 2024
1 parent 5638bea commit 6386729
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 57 deletions.
2 changes: 0 additions & 2 deletions book/guides/application_architecture.nim
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ nbCode:

proc clicked() =
view.model.increment()

export CounterView, CounterViewState

nbText: """
We can then use `CounterView` to display the counter inside the main application.
Expand Down
20 changes: 0 additions & 20 deletions owlkettle/adw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,6 @@ renderable PasswordEntryRow {.since: AdwVersion >= (1, 2).} of EntryRow:
proc changed(password: string) =
app.password = password

when AdwVersion >= (1, 2):
export EntryRow, PasswordEntryRow

type FlapChild[T] = object
widget: T
width: int
Expand Down Expand Up @@ -817,9 +814,6 @@ renderable OverlaySplitView {.since: AdwVersion >= (1, 4).} of BaseWidget:
widget.hasSidebar = true
widget.valSidebar = child

when AdwVersion >= (1, 4):
export OverlaySplitView

renderable AdwHeaderBar of BaseWidget:
## Adwaita Headerbar that combines GTK Headerbar and WindowControls.
packLeft: seq[Widget]
Expand Down Expand Up @@ -1092,9 +1086,6 @@ renderable ToolbarView {.since: AdwVersion >= (1, 4).} of BaseWidget:
widget.hasTopBars = true
widget.valTopBars.add(child)

when AdwVersion >= (1, 4):
export ToolbarView

type LicenseType* = enum
LicenseUnknown = 0
LicenseCustom = 1
Expand Down Expand Up @@ -1290,9 +1281,6 @@ renderable AboutWindow {.since: AdwVersion >= (1, 2).}:
copyright = "Erika Mustermann"
licenseType = LicenseMIT_X11

when AdwVersion >= (1, 2):
export AboutWindow

type Toast* = ref object
title*: string
customTitle*: Widget
Expand Down Expand Up @@ -1447,9 +1435,6 @@ renderable SwitchRow {.since: AdwVersion >= (1, 4).} of ActionRow:
property:
adw_switch_row_set_active(state.internalWidget, state.active.cbool)

when AdwVersion >= (1, 4):
export SwitchRow

renderable Banner {.since: AdwVersion >= (1, 3).} of BaseWidget:
## A rectangular Box taking up the entire vailable width with an optional button.
buttonLabel: string ## Label of the optional banner button. Button will only be added to the banner if this Label has a value.
Expand Down Expand Up @@ -1482,11 +1467,6 @@ renderable Banner {.since: AdwVersion >= (1, 3).} of BaseWidget:
property:
adw_banner_set_revealed(state.internalWidget, state.revealed.cbool)

when AdwVersion >= (1, 3):
export Banner

export ToastOverlay, Toast
export AdwWindow, WindowTitle, AdwHeaderBar, Avatar, ButtonContent, Clamp, PreferencesGroup, PreferencesRow, ActionRow, ExpanderRow, ComboRow, Flap, SplitButton, StatusPage, PreferencesPage

proc defaultStyleManager*(): StyleManager =
result = adw_style_manager_get_default()
Expand Down
2 changes: 2 additions & 0 deletions owlkettle/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ proc newExport*(node: NimNode, addExport: bool): NimNode =
else:
result = node

proc newExport*(name: string, addExport: bool): NimNode =
result = newExport(ident(name), addExport)

template customPragmas*() =
## Definess a custom pragma called "locker".
Expand Down
19 changes: 11 additions & 8 deletions owlkettle/widgetdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type
kind: WidgetKind
base: string
since: NimNode
private: bool
events: seq[EventDef]
fields: seq[Field]
hooks: array[HookKind, seq[NimNode]]
Expand Down Expand Up @@ -171,6 +172,8 @@ proc parsePragma(pragma: NimNode, def: var WidgetDef) =
if child.kind == nnkExprColonExpr and
child[0].eqIdent("since"):
def.since = child[1]
elif child.eqIdent("private"):
def.private = true
else:
error("Invalid widget pragma " & child.repr, pragma)

Expand Down Expand Up @@ -385,7 +388,7 @@ proc genWidget(def: WidgetDef): seq[NimNode] =

result = @[
newTree(nnkTypeDef, [
ident(def.name),
newExport(def.name, not def.private),
newEmptyNode(),
newTree(nnkRefTy, [
newTree(nnkObjectTy, [
Expand Down Expand Up @@ -427,7 +430,7 @@ proc genState(def: WidgetDef): seq[NimNode] =

result = @[
newTree(nnkTypeDef, [
ident(def.stateObjName),
newExport(def.stateObjName, not def.private),
newEmptyNode(),
newTree(nnkObjectTy, [
newEmptyNode(),
Expand All @@ -436,7 +439,7 @@ proc genState(def: WidgetDef): seq[NimNode] =
])
]),
newTree(nnkTypeDef, [
ident(def.stateName),
newExport(def.stateName, not def.private),
newEmptyNode(),
newTree(nnkRefTy, ident(def.stateObjName))
])
Expand Down Expand Up @@ -466,7 +469,7 @@ proc genDestroyState(def: WidgetDef): NimNode =

result = newProc(
procType=nnkProcDef,
name=ident("destroyState"),
name=newExport("destroyState", not def.private),
params=[newEmptyNode(),
newIdentDefs(stateObj, ident(def.stateObjName))
],
Expand Down Expand Up @@ -543,7 +546,7 @@ proc genBuildState(def: WidgetDef): NimNode =

result = newProc(
procType=nnkProcDef,
name=ident("buildState"),
name=newExport("buildState", not def.private),
params=[newEmptyNode(),
newIdentDefs(state, ident(def.stateName)),
newIdentDefs(widget, ident(def.name))
Expand Down Expand Up @@ -645,7 +648,7 @@ proc genUpdateState(def: WidgetDef): NimNode =

result = newProc(
procType=nnkProcDef,
name=ident("updateState"),
name=newExport("updateState", not def.private),
params=[newEmptyNode(),
newIdentDefs(state, ident(def.stateName)),
newIdentDefs(widget, ident(def.name))
Expand Down Expand Up @@ -692,7 +695,7 @@ proc genAssignAppEvents(def: WidgetDef): NimNode =

result = newProc(
procType=nnkProcDef,
name=ident("assignAppEvents"),
name=newExport("assignAppEvents", not def.private),
params=[newEmptyNode(),
newIdentDefs(widget, ident(def.name)),
newIdentDefs(app, bindSym("Viewable"))
Expand Down Expand Up @@ -826,7 +829,7 @@ proc genAdders(widget: WidgetDef): NimNode =
))

result.add(newProc(
name = ident(adder.name).newExport(),
name = newExport(adder.name, not widget.private),
params = params,
body = adder.body
))
Expand Down
28 changes: 1 addition & 27 deletions owlkettle/widgets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ renderable BaseWidget:
sensitive: bool = true ## If the widget is interactive
sizeRequest: tuple[x, y: int] = (-1, -1) ## Requested widget size. A value of -1 means that the natural size of the widget will be used.
tooltip: string = "" ## The widget's tooltip is shown on hover

hooks privateMargin:
(build, update):
if widget.hasPrivateMargin:
Expand Down Expand Up @@ -4748,29 +4748,3 @@ renderable ColumnView of BaseWidget:
hooks reorderable:
property:
gtk_column_view_set_reorderable(state.internalWidget, cbool(ord(state.reorderable)))

export BaseWidget, BaseWidgetState, BaseWindow, BaseWindowState
export Window, Box, Overlay, Label, Icon, Picture, Button, HeaderBar, ScrolledWindow, Entry, Spinner
export SpinButton, Paned, ColorButton, Switch, LinkButton, ToggleButton, CheckButton, RadioGroup
export DrawingArea, GlArea, MenuButton, ModelButton, Separator, Popover, PopoverMenu
export TextView, ListBox, ListBoxRow, ListBoxRowState, FlowBox, FlowBoxChild
export Frame, DropDown, Grid, Fixed, ContextMenu, LevelBar, Calendar
export Dialog, DialogState, DialogButton
export BuiltinDialog, BuiltinDialogState
export FileChooserDialog, FileChooserDialogState
export ColorChooserDialog, ColorChooserDialogState
export MessageDialog, MessageDialogState
export AboutDialog, AboutDialogState
export buildState, updateState, assignAppEvents
export Scale
export Expander
export Video, MediaControls
export SearchEntry
export ProgressBar
export EmojiChooser
export EditableLabel
export PasswordEntry
export CenterBox
export ListView
export ActionBar
export ColumnView

0 comments on commit 6386729

Please sign in to comment.