Skip to content

Commit

Permalink
add tests and fix dialog example and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed Sep 22, 2024
1 parent d16172e commit 6785b58
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/src/manual/dialogs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Dialogs are transient windows that show messages or ask the user for information

## Message dialogs

Gtk4.jl supports `GtkAlertDialog` and wraps it in convenience functions `info_dialog` and `ask_dialog`.
Each takes a string for a message to show and an optional parent container.
Gtk4.jl wraps `GtkAlertDialog` in convenience functions `info_dialog` and `ask_dialog`.
Each takes a string for a message to show and an optional parent window.

For all dialog convenience functions, there are two ways of using them. For use in the REPL or an interactive script, the following forms can be used:

```julia
info_dialog("Julia rocks!")
ask_dialog("Do you like chocolate ice cream?", "Not at all", "I like it") && println("That's my favorite too.")
ask_dialog("Do you like chocolate ice cream?"; no_text = "Not at all", yes_text = "I like it") && println("That's my favorite too.")
```
Note that `ask_dialog` returns `true` if the user clicks the button corresponding to yes. These functions take an optional argument `timeout` (in seconds) that can be used to make the dialog disappear after a certain time.

Expand Down
4 changes: 2 additions & 2 deletions examples/dialogs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ question_dialog_button = GtkButton("Question dialog")
push!(box,question_dialog_button)

function open_question_dialog(b)
ask_dialog("May I ask you a question?","No","Yes",main_window) do ans
ask_dialog("May I ask you a question?",main_window) do ans
@async println("You answered $ans")
end
end
Expand All @@ -34,7 +34,7 @@ input_dialog_button = GtkButton("Input dialog")
push!(box, input_dialog_button)

function open_input_dialog(b)
input_dialog("Enter your information", "", (("Cancel", 0), ("Accept", 1)), main_window) do t
input_dialog("Enter your information", "", main_window) do t
@async println("response was ",t)
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/windows.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function input_dialog(callback::Function, message::AbstractString, entry_default
cancel = GtkButton("Cancel"; hexpand = true)
push!(boxb, cancel)
push!(boxb, accept)
isnothing(parent) && (G_.set_transient_for(dlg, parent); G_.set_modal(dlg, true))
isnothing(parent) || (G_.set_transient_for(dlg, parent); G_.set_modal(dlg, true))
dlg[] = box

signal_connect(cancel, "clicked") do b
Expand Down Expand Up @@ -456,7 +456,7 @@ Keyword arguments:
- `start_folder = ""`: if set to a path, the dialog will start out browsing a particular folder. Otherwise GTK will decide.
"""
function open_dialog(title::AbstractString, parent = nothing, filters::Union{AbstractVector, Tuple} = String[]; timeout = -1, multiple = false, select_folder = false, start_folder = "")
res = Ref{String}("")
res = Ref{Union{Vector{String},String}}("")
c = Condition()

open_dialog(title, parent, filters; timeout, multiple, select_folder, start_folder) do filename
Expand Down
7 changes: 6 additions & 1 deletion test/gui/dialogs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ main_window = GtkWindow("Dialog example")

open_dialog("Pick a file to open", main_window; timeout = 0.25)
sleep(1.0)
open_dialog("Pick a file to open", main_window, ["*.png"]; timeout = 0.25)
open_dialog("Pick files to open", main_window, ["*.png"]; timeout = 0.25, multiple = true, start_folder = ".")
sleep(1.0)

save_dialog("Pick a filename", main_window; timeout = 0.25)
sleep(1.0)

save_dialog("Pick a filename", main_window, ["*.png"]; timeout = 0.25, start_folder = ".")
sleep(1.0)

color_dialog("What is your favorite color?", main_window; timeout = 0.25)
Expand Down Expand Up @@ -77,6 +79,9 @@ csvfilter3 = GtkFileFilter("", "text/csv")
csvfilter4 = GtkFileFilter("*.csv", "text/csv")
# Pattern takes precedence over mime-type, causing mime-type to be ignored
@test csvfilter4.name == "*.csv"

imfilter = GtkFileFilter("")
@test GtkFileFilter(imfilter) == imfilter
end

@testset "New dialogs" begin
Expand Down
4 changes: 4 additions & 0 deletions test/gui/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ visible(w,false)
visible(w,true)
@test isvisible(w) == true

@test Gtk4.issuspended(w) == false
@test Gtk4.isfullscreen(w) == false
@test Gtk4.isactive(w) == true

m = Gtk4.monitor(w)
if m!==nothing
r = Gtk4.G_.get_geometry(m)
Expand Down

0 comments on commit 6785b58

Please sign in to comment.