Skip to content

Commit

Permalink
clean up a few examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed Jan 12, 2025
1 parent 5c5813f commit da4ef3d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 132 deletions.
21 changes: 12 additions & 9 deletions examples/HDF5Viewer/src/HDF5Viewer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,19 @@ end
lmm(g,k) = GtkStringList([join([k,b],'/') for b in keys(g)])
lmm(d::HDF5.Dataset,k) = nothing

function create_tree_model(filename, pwin)
treemodel = h5open(filename,"r") do h
ks = keys(h)
rootmodel = GtkStringList(ks)
function create_model(pp)
k=Gtk4.string(pp)
return lmm(h[k],k)
end
GtkTreeListModel(GLib.GListModel(rootmodel),false, true, create_model)
function create_tree_model(h::HDF5.File)
ks = keys(h)
rootmodel = GtkStringList(ks)
function create_model(pp)
k=Gtk4.string(pp)
return lmm(h[k],k)
end
GtkTreeListModel(GLib.GListModel(rootmodel),false, true, create_model)
end

create_tree_model(filename::AbstractString) = h5open(create_tree_model, filename,"r")
function create_tree_model(filename, pwin)
treemodel = create_tree_model(filename)
@idle_add begin
Gtk4.destroy(pwin)
_create_gui(filename, treemodel)
Expand Down
14 changes: 5 additions & 9 deletions examples/application.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ function on_state_changed(a,v)
end

function on_new_clicked(a,v)
notification = GNotification("I wet em'")
Gtk4.GLib.G_.send_notification(app, "new-window", notification)
new_window()
end

Expand Down Expand Up @@ -41,25 +39,23 @@ function new_window()
show(window)
end

# callback to be called when application is "activated"
function activate(app)
add_action(GActionMap(app),"new_window",on_new_clicked)
add_stateful_action(GActionMap(app), "toggle", false, on_state_changed)

new_window()
end

app = GtkApplication("julia.gtk4.example",
Gtk4.GLib.ApplicationFlags_FLAGS_NONE)

if isinteractive()
Gtk4.GLib.stop_main_loop() # g_application_run will run the loop
end
# create application -- argument is the application id, which is optional
app = GtkApplication("julia.gtk4.example")

Gtk4.signal_connect(activate, app, :activate)

# When all windows are closed, loop automatically stops running
# When all windows are closed, loop automatically stops running and program exits (if Julia is not run interactively)

if isinteractive()
Gtk4.GLib.stop_main_loop() # g_application_run will run the loop
loop()=Gtk4.run(app)
t = schedule(Task(loop))
else
Expand Down
8 changes: 2 additions & 6 deletions examples/application2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,14 @@ function activate(app)
show(window)
end

app = GtkApplication("julia.gtk4.example2",
Gtk4.GLib.ApplicationFlags_FLAGS_NONE)

if isinteractive()
Gtk4.GLib.stop_main_loop() # g_application_run will run the loop
end
app = GtkApplication("julia.gtk4.example2")

Gtk4.signal_connect(activate, app, :activate)

# When all windows are closed, loop automatically stops running

if isinteractive()
Gtk4.GLib.stop_main_loop() # g_application_run will run the loop
loop()=Gtk4.run(app)
t = schedule(Task(loop))
else
Expand Down
158 changes: 55 additions & 103 deletions examples/calculator4.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,116 +24,68 @@ b0 = GtkButton("0")
b_equalto = GtkButton("=")
b_divide = GtkButton("÷")

hbox1 = GtkBox(:h)
hbox2 = GtkBox(:h)
hbox3 = GtkBox(:h)
hbox4 = GtkBox(:h)

push!(hbox1, b1, b2, b3, b_plus)
push!(hbox2, b4, b5, b6, b_minus)
push!(hbox3, b7, b8, b9, b_multiply)
push!(hbox4, b_clear, b0, b_equalto, b_divide)
hbox1 = push!(GtkBox(:h), b1, b2, b3, b_plus)
hbox2 = push!(GtkBox(:h), b4, b5, b6, b_minus)
hbox3 = push!(GtkBox(:h), b7, b8, b9, b_multiply)
hbox4 = push!(GtkBox(:h), b_clear, b0, b_equalto, b_divide)

vbox = GtkBox(:v)
label = GtkLabel("")

push!(vbox, GtkLabel(""))
push!(vbox, label)
push!(vbox, GtkLabel(""))
push!(vbox, hbox1)
push!(vbox, hbox2)
push!(vbox, hbox3)
push!(vbox, hbox4)
push!(win, vbox)

text = ""
push!(vbox, GtkLabel(""), label, GtkLabel(""), hbox1, hbox2, hbox3, hbox4)
win[] = vbox

function calculate(s)
x = "+ " * s
k = split(x)
final = 0
isempty(s) && return ""
x = "+ " * s
k = split(x)
final = 0

for i = 1:length(k)

if k[i] == "+"
final += parse(Float64, k[i + 1])
elseif k[i] == "-"
final -= parse(Float64, k[i + 1])
elseif k[i] == "x"
final *= parse(Float64, k[i + 1])
elseif k[i] == "÷"
final /= parse(Float64, k[i + 1])
end
end
return string(final)
end


function button_clicked_callback(widget)
if widget == b1
global text = text * "1"
label.label = text
elseif widget == b2
global text = text * "2"
label.label = text
elseif widget == b3
global text = text * "3"
label.label = text
elseif widget == b4
global text = text * "4"
label.label = text
elseif widget == b5
global text = text * "5"
label.label = text
elseif widget == b6
global text = text * "6"
label.label = text
elseif widget == b7
global text = text * "7"
label.label = text
elseif widget == b8
global text = text * "8"
label.label = text
elseif widget == b9
global text = text * "9"
label.label = text
elseif widget == b_plus
global text = text * " + "
label.label = text
elseif widget == b_minus
global text = text * " - "
label.label = text
elseif widget == b_multiply
global text = text * " x "
label.label = text
elseif widget == b_divide
global text = text * " ÷ "
label.label = text
elseif widget == b0
global text = text * "0"
label.label = text
elseif widget == b_clear
global text = ""
label.label = text
elseif widget == b_equalto
global text = calculate(text)
label.label = text
for i = 1:length(k)
if k[i] == "+"
final += parse(Float64, k[i + 1])
elseif k[i] == "-"
final -= parse(Float64, k[i + 1])
elseif k[i] == "x"
final *= parse(Float64, k[i + 1])
elseif k[i] == "÷"
final /= parse(Float64, k[i + 1])
end
end
return string(final)
end

id1 = signal_connect(button_clicked_callback, b1, "clicked")
id2 = signal_connect(button_clicked_callback, b2, "clicked")
id3 = signal_connect(button_clicked_callback, b3, "clicked")
id4 = signal_connect(button_clicked_callback, b4, "clicked")
id5 = signal_connect(button_clicked_callback, b5, "clicked")
id6 = signal_connect(button_clicked_callback, b6, "clicked")
id7 = signal_connect(button_clicked_callback, b7, "clicked")
id8 = signal_connect(button_clicked_callback, b8, "clicked")
id9 = signal_connect(button_clicked_callback, b9, "clicked")
id10 = signal_connect(button_clicked_callback, b0, "clicked")
id11 = signal_connect(button_clicked_callback, b_plus, "clicked")
id12 = signal_connect(button_clicked_callback, b_minus, "clicked")
id13 = signal_connect(button_clicked_callback, b_multiply, "clicked")
id14 = signal_connect(button_clicked_callback, b_divide, "clicked")
id15 = signal_connect(button_clicked_callback, b_clear, "clicked")
id16 = signal_connect(button_clicked_callback, b_equalto, "clicked")
append_str(str) = label.label *= str
append_1(widget) = append_str("1")
append_2(widget) = append_str("2")
append_3(widget) = append_str("3")
append_4(widget) = append_str("4")
append_5(widget) = append_str("5")
append_6(widget) = append_str("6")
append_7(widget) = append_str("7")
append_8(widget) = append_str("8")
append_9(widget) = append_str("9")
append_0(widget) = append_str("0")
append_plus(widget) = append_str(" + ")
append_minus(widget) = append_str(" - ")
append_times(widget) = append_str(" x ")
append_div(widget) = append_str(" ÷ ")
clear_callback(widget) = label.label = ""
calc_callback(widget) = label.label = calculate(label.label)

signal_connect(append_1, b1, "clicked")
signal_connect(append_2, b2, "clicked")
signal_connect(append_3, b3, "clicked")
signal_connect(append_4, b4, "clicked")
signal_connect(append_5, b5, "clicked")
signal_connect(append_6, b6, "clicked")
signal_connect(append_7, b7, "clicked")
signal_connect(append_8, b8, "clicked")
signal_connect(append_9, b9, "clicked")
signal_connect(append_0, b0, "clicked")
signal_connect(append_plus, b_plus, "clicked")
signal_connect(append_minus, b_minus, "clicked")
signal_connect(append_times, b_multiply, "clicked")
signal_connect(append_div, b_divide, "clicked")
signal_connect(clear_callback, b_clear, "clicked")
signal_connect(calc_callback, b_equalto, "clicked")
1 change: 1 addition & 0 deletions examples/canvas.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Gtk4

# create a canvas with minimum size 100x100 pixels
c = GtkCanvas(100,100)

# define a function that will be called every time the widget needs to be drawn
Expand Down
9 changes: 5 additions & 4 deletions examples/thread.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ ent = GtkEntry(;hexpand=true)
grid = GtkGrid()
lab = GtkLabel("")
grid[1:2,1] = lab
# @idle_add adds code to be called by the GTK main loop
@idle_add lab.label = "The GTK loop is running in thread $(Threads.threadid()) ($(Threads.threadpool()) threadpool)"
grid[1,2] = btn
grid[2,2] = sp
grid[1:2,3] = ent

signal_connect(btn, "clicked") do widget
start(sp)
start(sp) # start the spinner widget
Threads.@spawn begin
# Do work
# Do work in a separate thread to avoid bogging down the GTK main loop
stop_time = time() + 3
counter = 0
while time() < stop_time
Expand All @@ -33,8 +34,8 @@ signal_connect(btn, "clicked") do widget

# Interacting with GTK from a thread other than the main thread is
# generally not allowed, so we register an idle callback instead.
Gtk4.GLib.g_idle_add() do
stop(sp)
@idle_add begin
stop(sp) # stop the spinner widget
ent.text = "I counted to $counter in thread $tid in the $tp threadpool!"
false
end
Expand Down
2 changes: 1 addition & 1 deletion examples/thread_timeout.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Demonstrates keeping the UI responsive while doing tasks in a thread
# Demonstrates updating the GUI from a separate thread

if Threads.nthreads() == 1 && Threads.nthreads(:interactive) < 1
@warn("This example is intended to be run with multiple threads enabled.")
Expand Down

0 comments on commit da4ef3d

Please sign in to comment.