Skip to content

Commit

Permalink
Add MediaControls (#121)
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 mediastream controls widget

* Merge MediaControls and Video examples

* Move media controls to popover menu

* Update screenshot and docs

---------

Co-authored-by: Can Lehmann <can.l@posteo.de>
  • Loading branch information
PhilippMDoerner and can-lehmann committed Mar 5, 2024
1 parent 968784a commit 7c73fce
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ examples/widgets/progress_bar
examples/widgets/action_bar
examples/widgets/search_entry
examples/widgets/center_box
examples/widgets/video
examples/widgets/editable_label
examples/widgets/video
examples/widgets/column_view
Expand Down
Binary file modified docs/assets/examples/video.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,18 @@ renderable Video of BaseWidget
- `file: GFile`


## MediaControls

```nim
renderable MediaControls of BaseWidget
```

###### Fields

- All fields from [BaseWidget](#BaseWidget)
- `mediaStream: MediaStream`


## Expander

```nim
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,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/video.nim">Video</a></td>
<td><img alt="Video" src="../docs/assets/examples/video.png" width="985px"></td>
<td><img alt="Video" src="../docs/assets/examples/video.png" width="1115px"></td>
</tr>
</table>

Expand Down
23 changes: 18 additions & 5 deletions examples/widgets/video.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ viewable App:
sizeRequest: tuple[x, y: int] = (-1, -1)

method view(app: AppState): Widget =
const seekTime = 5 * 1000000

result = gui:
Window():
defaultSize = (1000, 600)
Expand All @@ -47,12 +49,23 @@ method view(app: AppState): Widget =
sizeRequest = (400, 400)
)) {.addRight.}

MenuButton {.addRight.}:
icon = "view-more-symbolic"
style = [ButtonFlat]
sensitive = not app.mediaStream.isNil

PopoverMenu:
MediaControls:
margin = 10
sizeRequest = (300, -1)
mediaStream = app.mediaStream

Button {.addRight.}:
icon = "media-seek-forward-symbolic"
style = [ButtonFlat]
sensitive = not app.mediaStream.isNil
proc clicked() =
app.mediaStream.seekRelative(5 * 1000000)
app.mediaStream.seekRelative(seekTime)

Button {.addRight.}:
icon = "media-playback-pause"
Expand All @@ -72,9 +85,9 @@ method view(app: AppState): Widget =
icon = "media-seek-backward-symbolic"
style = [ButtonFlat]
sensitive = not app.mediaStream.isNil
proc clicked() =
app.mediaStream.seekRelative(-5 * 1000000)

proc clicked() =
app.mediaStream.seekRelative(-seekTime)
Button {.addLeft.}:
text = "Open"
style = [ButtonSuggested]
Expand All @@ -94,7 +107,7 @@ method view(app: AppState): Widget =
style = [ButtonSuggested]

if res.kind == DialogAccept:
let path = FileChooserDialogState(state).filename
let path = FileChooserDialogState(state).filenames[0]
app.filename = path
try:
app.mediaStream = newMediaStream(path)
Expand Down
4 changes: 4 additions & 0 deletions owlkettle/bindings/gtk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ proc gtk_video_set_loop*(self: GtkWidget, loop: cbool)
proc gtk_video_set_media_stream*(self: GtkWidget, stream: GtkMediaStream)
proc gtk_video_set_resource*(self: GtkWidget, resource_path: cstring)

# Gtk.MediaControls
proc gtk_media_controls_new*(stream: GtkMediaStream): GtkWidget
proc gtk_media_controls_set_media_stream*(controls: GtkWidget, stream: GtkMediaStream)

# Gtk.Widget
proc gtk_widget_show*(widget: GtkWidget)
proc gtk_widget_hide*(widget: GtkWidget)
Expand Down
17 changes: 16 additions & 1 deletion owlkettle/widgets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3967,6 +3967,21 @@ renderable Video of BaseWidget:
property:
gtk_video_set_loop(state.internalWidget, state.loop.cbool)

renderable MediaControls of BaseWidget:
mediaStream: MediaStream

hooks:
beforeBuild:
state.internalWidget = gtk_media_controls_new(nil.GtkMediaStream)

hooks mediaStream:
property:
if isNil(state.mediaStream):
gtk_media_controls_set_media_stream(state.internalWidget, GtkMediaStream(nil))
else:
gtk_media_controls_set_media_stream(state.internalWidget, state.mediaStream.gtk)


proc `hasFileName=`*(widget: Video, has: bool) =
widget.hasMediaStream = has

Expand Down Expand Up @@ -4610,8 +4625,8 @@ export AboutDialog, AboutDialogState
export buildState, updateState, assignAppEvents
export Scale
export Expander
export Video, MediaControls
export SearchEntry
export Video
export ProgressBar
export EmojiChooser
export EditableLabel
Expand Down

0 comments on commit 7c73fce

Please sign in to comment.