Skip to content

Commit

Permalink
Update ExpanderRow to adwaita 1.4 and make more properties accessible (
Browse files Browse the repository at this point in the history
…#136)

* Prepare Expander-Row for Adw 1.4

* Add additional properties to expander row

expanded
enableExpansion
showEnableSwitch
titleLines
subtitleLines

All of these properties were not yet wrapped.

* Expand upon expander example

* Update docs

* Update expander row table entry

* Add listening to expansion event

* Remove exception on lines-properties for Expanderrow

property hooks always get executed at least once, even if the
value is never set.
Therefore you shouldn't automatically throw an exception
in a hook.

* Remove unnecessary else discard statement
  • Loading branch information
PhilippMDoerner committed Mar 5, 2024
1 parent b157585 commit bdc343f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
Binary file modified docs/assets/examples/expander_row.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions docs/widgets_adwaita.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ renderable ExpanderRow of PreferencesRow
- `subtitle: string`
- `actions: seq[AlignedChild[Widget]]`
- `rows: seq[AlignedChild[Widget]]`
- `expanded: bool = false`
- `enableExpansion: bool = true`
- `showEnableSwitch: bool = false`
- `titleLines: int` Determines how many lines of text from the title are shown before it ellipsizes the text. Defaults to 0 which means it never elipsizes and instead adds new lines to show the full text. Only available for adwaita version 1.3 or higher. Does nothing if set when compiled for lower adwaita versions.
- `subtitleLines: int` Determines how many lines of text from the subtitle are shown before it ellipsizes the text. Defaults to 0 which means it never elipsizes and instead adds new lines to show the full text. Only available for adwaita version 1.3 or higher. Does nothing if set when compiled for lower adwaita versions.

###### Events

- expand: `proc (newExpandState: bool)` Triggered when row gets expanded

###### Adders

Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ The `widgets` directory contains examples for how to use different widgets.
</tr>
<tr>
<td><a href="https://github.com/can-lehmann/owlkettle/blob/main/examples/widgets/adw/expander_row.nim">Expander Row</a></td>
<td><img alt="Expander Row" src="../docs/assets/examples/expander_row.png" width="803px"></td>
<td><img alt="Expander Row" src="../docs/assets/examples/expander_row.png" width="922px"></td>
</tr>
<tr>
<td><a href="https://github.com/can-lehmann/owlkettle/blob/main/examples/widgets/adw/flap.nim">Flap</a></td>
Expand Down
31 changes: 29 additions & 2 deletions examples/widgets/adw/expander_row.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import owlkettle, owlkettle/adw
import owlkettle, owlkettle/[playground, adw]

viewable App:
discard
subtitle: string = "a very long subtitle that benefits from expander row showing multiple lines for the subtitle by quite a bit and will be elipsized once the end of that line is reached. Some more text to demonstrate it elipsizing."
enableExpansion: bool = true
expanded1: bool = false
expanded2: bool = false
showEnableSwitch: bool = false
titleLines: int = 0
subtitleLines: int = 2

method view(app: AppState): Widget =
result = gui:
Window:
title = "Expander Row Example"
HeaderBar() {.addTitlebar.}:
insert(app.toAutoFormMenu()) {.addRight.}

Clamp:
maximumSize = 500
Expand All @@ -39,6 +47,16 @@ method view(app: AppState): Widget =

ExpanderRow:
title = "Expander Row"
subtitle = app.subtitle
enableExpansion = app.enableExpansion
showEnableSwitch = app.showEnableSwitch
titleLines = app.titleLines
expanded = app.expanded1
subtitleLines = app.subtitleLines

proc expand(hasExpanded: bool) =
echo "Expanding First Expander: ", hasExpanded
app.expanded1 = hasExpanded

for it in 0..<3:
ActionRow {.addRow.}:
Expand All @@ -47,7 +65,16 @@ method view(app: AppState): Widget =
ExpanderRow:
title = "Expander Row"
subtitle = "with actions"
enableExpansion = app.enableExpansion
showEnableSwitch = app.showEnableSwitch
titleLines = app.titleLines
subtitleLines = app.subtitleLines
expanded = app.expanded2

proc expand(hasExpanded: bool) =
echo "Expanding Second Expander: ", hasExpanded
app.expanded2 = hasExpanded

Button {.addAction.}:
text = "Action"

Expand Down
54 changes: 52 additions & 2 deletions owlkettle/adw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,47 @@ renderable ExpanderRow of PreferencesRow:
subtitle: string
actions: seq[AlignedChild[Widget]]
rows: seq[AlignedChild[Widget]]
expanded: bool = false
enableExpansion: bool = true
showEnableSwitch: bool = false
titleLines: int ## Determines how many lines of text from the title are shown before it ellipsizes the text. Defaults to 0 which means it never elipsizes and instead adds new lines to show the full text. Only available for adwaita version 1.3 or higher. Does nothing if set when compiled for lower adwaita versions.
subtitleLines: int ## Determines how many lines of text from the subtitle are shown before it ellipsizes the text. Defaults to 0 which means it never elipsizes and instead adds new lines to show the full text. Only available for adwaita version 1.3 or higher. Does nothing if set when compiled for lower adwaita versions.

proc expand(newExpandState: bool) ## Triggered when row gets expanded

hooks:
beforeBuild:
state.internalWidget = adw_expander_row_new()

connectEvents:
proc expandCallback(
widget: GtkWidget,
pspec: pointer,
data: ptr EventObj[proc (isExpanded: bool)]
) {.cdecl.} =
let isExpanded = bool(adw_expander_row_get_expanded(widget))

let state = ExpanderRowState(data[].widget)
state.expanded = isExpanded
data[].callback(isExpanded)
data[].redraw()

state.connect(state.expand, "notify::expanded", expandCallback)
disconnectEvents:
state.internalWidget.disconnect(state.expand)

hooks subtitle:
property:
adw_expander_row_set_subtitle(state.internalWidget, state.subtitle.cstring)

hooks actions:
(build, update):
const rowAdder = when AdwVersion >= (1, 4):
adw_expander_row_add_suffix
else:
adw_expander_row_add_action

state.updateAlignedChildren(state.actions, widget.valActions,
adw_expander_row_add_action,
rowAdder,
adw_expander_row_remove
)

Expand All @@ -321,6 +349,28 @@ renderable ExpanderRow of PreferencesRow:
adw_expander_row_remove
)

hooks expanded:
property:
adw_expander_row_set_expanded(state.internalWidget, state.expanded.cbool)

hooks enableExpansion:
property:
adw_expander_row_set_enable_expansion(state.internalWidget, state.enableExpansion.cbool)

hooks showEnableSwitch:
property:
adw_expander_row_set_show_enable_switch(state.internalWidget, state.showEnableSwitch.cbool)

hooks titleLines:
property:
when AdwVersion >= (1, 3):
adw_expander_row_set_title_lines(state.internalWidget, state.titleLines.cint)

hooks subtitleLines:
property:
when AdwVersion >= (1, 3):
adw_expander_row_set_subtitle_lines(state.internalWidget, state.subtitleLines.cint)

adder addAction {.hAlign: AlignFill, vAlign: AlignCenter.}:
widget.hasActions = true
widget.valActions.add(AlignedChild[Widget](
Expand Down
14 changes: 13 additions & 1 deletion owlkettle/bindings/adw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,22 @@ proc adw_action_row_set_activatable_widget*(row, child: GtkWidget)
# Adw.ExpanderRow
proc adw_expander_row_new*(): GtkWidget
proc adw_expander_row_set_subtitle*(row: GtkWidget, subtitle: cstring)
proc adw_expander_row_add_action*(row, child: GtkWidget)
proc adw_expander_row_add_prefix*(row, child: GtkWidget)
proc adw_expander_row_add_row*(expanderRow, row: GtkWidget)
proc adw_expander_row_remove*(row, child: GtkWidget)
proc adw_expander_row_set_enable_expansion*(self: GtkWidget, enable_expansion: cbool)
proc adw_expander_row_set_expanded*(self: GtkWidget, expanded: cbool)
proc adw_expander_row_set_show_enable_switch*(self: GtkWidget, show_enable_switch: cbool)
proc adw_expander_row_get_expanded*(self: GtkWidget): cbool

when AdwVersion >= (1, 3):
proc adw_expander_row_set_subtitle_lines*(self: GtkWidget, subtitle_lines: cint)
proc adw_expander_row_set_title_lines*(self: GtkWidget, title_lines: cint)

when AdwVersion >= (1, 4):
proc adw_expander_row_add_suffix*(row, child: GtkWidget)
else:
proc adw_expander_row_add_action*(row, child: GtkWidget)

# Adw.ComboRow
proc adw_combo_row_new*(): GtkWidget
Expand Down

0 comments on commit bdc343f

Please sign in to comment.