-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move gtk.nim into bindings dir * Refactor adwaita bindings into a binding module * Re export bound enums * Add general toast overlay widget * Add first stab at adding signal listening to toast * Fix dismissal handler segfaulting * Fix memory leak for toast * Make setters and getters more semantic in their bindings This way they appear like fields on the Adwaita type. * Refine example to provide notify button * Allow more types for timeout * Disable auto-dismissing toast after timeout I currently have no solution for the use-after-free conundrum. That conundrum is caused by setting a timeout in the owlkettle layer to dismiss the toast after it's timeout expires. If you do that and the user dismisses the toast, then the toast-memory gets free'd after a while by gtk. If you try to dismiss that toast then after the timeout, you access memory that was free'd, causing a user-after-free segfault. I have a feeling gtk should be the one that dismisses these toasts automatically after the timeout expires, but I can't get that to work. * Fix example notify button triggering toasts twice * Add click handler to toast overlay * Fix toast auto dismissal issue * Refine example * Remove last remaining exception and enable urgent notification examples * Added getters for Toast properties * Remove unnecessary nil default assignment * Add docs * Make Toast Overlay example compileable without higher adw version * Update docs * Refactor AdwToast to use a wrapper type "Toast" This enables us to provide memory management hooks later. * Add native approach Note: This will segfault if you summon a toast, wait for it to be dismissed and then try to summon the next one. The reason for this is that ToastOverlay will free the toast after dismissing it. If you then swap out the ref that'll trigger the destructor hook, which will try to unref a pointer to freed memory, causing a use-after-free error. * Enable adding a list of toasts * Update docs * Comment in hooks again * Move over toast approach to custom object That object is only converted to an AdwToast at the last moment before handing it over to ToastOverlay. This way things that belong to gtk are only created when they're definitely going into to ownership of the gtk library. Thus no memory leak even conceptually can occur, as nim-types will automatically be memory managed. * Fix bugs and example * Improve docs * Move to ToastQueue This way you can just "addToast" onto a queue the same way as you do with textbuffer. As opposed to assigning the same toast over and over again. * Refinements * Remove pointless button * Make it possible to use procs that are closures for handler fields. * Better hook usage * Remove toast from event callback * Refactor * Refactor * Remove unimplemented fields from docs * Update image * Remove unnecesasry code from earlier attempt * Improve example * Improve docs * Remove owlkettle import * Fix exception message --------- Co-authored-by: Can Lehmann <can.l@posteo.de>
- Loading branch information
1 parent
2029586
commit fb0a7a0
Showing
6 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# 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 | ||
import owlkettle/[playground, adw] | ||
import std/[sequtils, sugar] | ||
|
||
viewable App: | ||
buttonLabel: string = "Button" | ||
timeout: int = 3 | ||
useMarkup: bool = true ## Enables using markup in title. Only available for Adwaita version 1.4 or higher. Compile for Adwaita version 1.4 or higher with -d:adwMinor=4. | ||
toastQueue: ToastQueue = newToastQueue() | ||
|
||
proc buildToast(state: AppState, | ||
title: string = "", | ||
priority: ToastPriority = ToastPriorityNormal): Toast = | ||
|
||
let toast = newToast( | ||
title = title, | ||
buttonLabel = state.buttonLabel, | ||
priority = priority, | ||
timeout = state.timeout | ||
) | ||
|
||
when AdwVersion >= (1, 2): | ||
toast.clickedHandler = proc() = | ||
echo "Click: ", toast.title | ||
|
||
when AdwVersion >= (1, 4): | ||
toast.useMarkup = state.useMarkup | ||
|
||
toast.dismissalHandler = proc() = | ||
echo "Dismissed: ", toast.title | ||
|
||
return toast | ||
|
||
method view(app: AppState): Widget = | ||
result = gui: | ||
Window: | ||
title = "Toast Overlay Example" | ||
defaultSize = (500, 350) | ||
|
||
HeaderBar {.addTitlebar.}: | ||
insert(app.toAutoFormMenu(ignoreFields = @["toastQueue"], sizeRequest = (400, 250))){.addRight.} | ||
|
||
Box: | ||
orient = OrientY | ||
|
||
ToastOverlay: | ||
toastQueue = app.toastQueue | ||
|
||
Box: | ||
Box {.hAlign: AlignCenter, vAlign: AlignCenter.}: | ||
orient = OrientX | ||
spacing = 12 | ||
|
||
Button: | ||
style = [ButtonPill] | ||
text = "Urgent" | ||
proc clicked() = | ||
let toast = buildToast(app, "Urgent Toast", ToastPriorityHigh) | ||
app.toastQueue.add(toast) | ||
|
||
Button: | ||
style = [ButtonPill, ButtonSuggested] | ||
text = "Notify" | ||
proc clicked() = | ||
let toast = buildToast(app, "Toast", ToastPriorityNormal) | ||
app.toastQueue.add(toast) | ||
|
||
adw.brew(gui(App())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters