Skip to content

Commit

Permalink
Add PreferencesPage (#128)
Browse files Browse the repository at this point in the history
* Move gtk.nim into bindings dir

* Refactor adwaita bindings into a binding module

* Re export bound enums

* Add bindings and first basic broken example for preferences page

* Fix example

* Add docs

* Remove preferences page from github CI examples

* Fix example being borked

* Fix nimble file

* Fix stuff

* Build example in CI

* Compile example without adwaita 1.2

---------

Co-authored-by: Can Lehmann <can.l@posteo.de>
  • Loading branch information
PhilippMDoerner and can-lehmann committed Mar 5, 2024
1 parent bdc343f commit 968784a
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ examples/widgets/video
examples/widgets/column_view
examples/widgets/adw/window_title
examples/widgets/adw/preferences_group
examples/widgets/adw/preferences_page
examples/widgets/adw/flap
examples/widgets/adw/expander_row
examples/widgets/adw/combo_row
Expand Down
Binary file added docs/assets/examples/preferences_page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions docs/widgets_adwaita.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ PreferencesGroup:
```


## PreferencesPage

```nim
renderable PreferencesPage of BaseWidget
```

###### Fields

- All fields from [BaseWidget](#BaseWidget)
- `preferences: seq[Widget]`
- `iconName: string`
- `name: string`
- `title: string`
- `useUnderline: bool`
- `description: string`

###### Adders

- All adders from [BaseWidget](#BaseWidget)
- `add`


## PreferencesRow

```nim
Expand Down
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ The `widgets` directory contains examples for how to use different widgets.
<td><a href="https://github.com/can-lehmann/owlkettle/blob/main/examples/widgets/adw/preferences_group.nim">Preferences Group</a></td>
<td><img alt="Preferences Group" src="../docs/assets/examples/preferences_group.png" width="721px"></td>
</tr>
<tr>
<td><a href="https://github.com/can-lehmann/owlkettle/blob/main/examples/widgets/adw/preferences_page.nim">Preferences Page</a></td>
<td><img alt="Preferences Page" src="../docs/assets/examples/preferences_page.png" width="721px"></td>
</tr>
<tr>
<td><a href="https://github.com/can-lehmann/owlkettle/blob/main/examples/widgets/adw/status_page.nim">Status Page</a></td>
<td><img alt="Status Page" src="../docs/assets/examples/status_page.png" width="704px"></td>
Expand Down
77 changes: 77 additions & 0 deletions examples/widgets/adw/preferences_page.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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/[playground, adw]

viewable App:
iconName: string = "weather-clear-symbolic"
title: string = "Some Title"
name: string = "A name"
description: string = "An example of a Preferences Page"
useUnderline: bool = false

likeLevel: int
reason: string

likeOptions: seq[string] = @["A bit", "A lot", "very much", "Fantastic!", "I love it!"]

method view(app: AppState): Widget =
result = gui:
Window:
defaultSize = (800, 600)
title = "Preferences Page Example"

HeaderBar {.addTitlebar.}:
insert(app.toAutoFormMenu(sizeRequest = (400, 250))) {.addRight.}

PreferencesPage:
iconName = app.iconName
title = app.title
name = app.name
description = app.description
useUnderline = app.useUnderline

PreferencesGroup:
title = "First Preferences Group"
description = "Description"

ComboRow:
title = "How much do you like this example?"
items = app.likeOptions
selected = app.likeLevel

proc select(selectedIndex: int) =
app.likeLevel = selectedIndex

PreferencesGroup:
title = "Second Preferences Group"

ActionRow:
title = "Why do you like the example?"

Entry {.addSuffix.}:
text = app.reason

proc changed(text: string) =
app.reason = text

adw.brew(gui(App()))
48 changes: 47 additions & 1 deletion owlkettle/adw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,52 @@ renderable PreferencesGroup of BaseWidget:
subtitle = "Subtitle"
Switch() {.addSuffix.}

renderable PreferencesPage of BaseWidget:
preferences: seq[Widget]
iconName: string
name: string
title: string
useUnderline: bool
description: string

hooks:
beforeBuild:
state.internalWidget = adw_preferences_page_new()

hooks preferences:
(build, update):
state.updateChildren(
state.preferences,
widget.valPreferences,
adw_preferences_page_add,
adw_preferences_page_remove
)

hooks iconName:
property:
adw_preferences_page_set_icon_name(state.internalWidget, state.iconName.cstring)

hooks name:
property:
adw_preferences_page_set_name(state.internalWidget, state.name.cstring)

hooks title:
property:
adw_preferences_page_set_title(state.internalWidget, state.title.cstring)

hooks useUnderline:
property:
adw_preferences_page_set_use_underline(state.internalWidget, state.useUnderline.cbool)

hooks description:
property:
when AdwVersion >= (1, 4):
adw_preferences_page_set_description(state.internalWidget, state.description.cstring)

adder add:
widget.valPreferences.add(child)


renderable PreferencesRow of ListBoxRow:
title: string

Expand Down Expand Up @@ -1273,7 +1319,7 @@ when AdwVersion >= (1, 3) or defined(owlkettleDocs):
adw_banner_set_revealed(state.internalWidget, state.revealed.cbool)
export Banner

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

type AdwAppConfig = object of AppConfig
colorScheme: ColorScheme
Expand Down
15 changes: 15 additions & 0 deletions owlkettle/bindings/adw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ proc adw_preferences_group_set_title*(group: GtkWidget, title: cstring)
proc adw_preferences_row_new*(): GtkWidget
proc adw_preferences_row_set_title*(row: GtkWidget, title: cstring)

# Adw.PreferencesPage
proc adw_preferences_page_new*(): GtkWidget
proc adw_preferences_page_add*(self: GtkWidget, group: GtkWidget)
proc adw_preferences_page_remove*(self: GtkWidget, group: GtkWidget)
proc adw_preferences_page_set_icon_name*(self: GtkWidget, icon_name: cstring)
proc adw_preferences_page_set_name*(self: GtkWidget, name: cstring)
proc adw_preferences_page_set_title*(self: GtkWidget, title: cstring)
proc adw_preferences_page_set_use_underline*(self: GtkWidget, use_underline: cbool)

when AdwVersion >= (1, 3):
proc adw_preferences_page_scroll_to_top*(self: GtkWidget)

when AdwVersion >= (1, 4):
proc adw_preferences_page_set_description*(self: GtkWidget, description: cstring)

# Adw.ActionRow
proc adw_action_row_new*(): GtkWidget
proc adw_action_row_set_subtitle*(row: GtkWidget, subtitle: cstring)
Expand Down

0 comments on commit 968784a

Please sign in to comment.