Re: [Vala] Gtk.ProgressBar



Hi,

This is a quick example I whipped up which works somewhat along the lines
as Al suggests. This is one of the times where Vala is more expressive than
Genie with Closures.

uses Gtk

init
Gtk.init (ref args)
var TestGtk = new Ventana()
TestGtk.show_all()
Gtk.main()

class Ventana : Window

dialogo : Dialogo
cb : SourceFunc
boton: Button
output : string

init
// set up the main window
title = "Genie Async"
set_default_size (250, 100)
set_border_width(8)
window_position = WindowPosition.CENTER
destroy.connect(Gtk.main_quit)
// set up the button and connect the callback
boton = new Button.with_label("Load VCard")
boton.clicked.connect(open_vcard)
add(boton)

def open_vcard()
// show the dialog with the progress bar
dialogo = new Dialogo(this)
dialogo.show_all()
// call the async method with callback
load_vcard.begin ("filename", end)

// this is the callback from the load_card method
def end (o : Object?, r : AsyncResult)
// to get the result of the async method, we call its
// end method with the AsyncResult parameter
try
boton.label = load_vcard.end(r)
except ex : ThreadError
boton.label = ex.message
// close the dialog
dialogo.destroy()

def async load_vcard(fname : string) : string raises ThreadError
// we save the callback here because Genie has limited support
// for closures
cb = load_vcard.callback

// we create a separate thread for our long running process
run : ThreadFunc of void*
run = do_load_vcard
Thread.create(run, false)
// we return control to the calling method (open_vcard)
yield
// we now return a result from our lengthy process
// for your application this could be an instance of your
// vard object created above
return output

// this is to simulate a lengthy process that we run in a separate thread
def do_load_vcard () : void*
var result = 0
for var i = 0 to 1000000000
result = i
// we save the result of our lengthy process to the output var
output = "ran %d times".printf(result)
// then we add the callback (end) to the MainLoop Idle
Idle.add((owned)cb)
return null



class Dialogo : Dialog

progress_bar: ProgressBar

construct( padre: Window )
set_transient_for(padre)
title = "Espera por favor"
progress_bar = new ProgressBar()
get_content_area().add(progress_bar)
set_modal(true)
// This causes the ProgressBar to pulse every 60 miliseconds
Timeout.add(60, pulse)

def pulse () : bool
progress_bar.pulse()
return true

Please let me know if this is clear enough (and any suggestions!)

Cheers
Chris D


2017-03-12 13:22 GMT-07:00 Al Thomas via vala-list <vala-list gnome org>:

----- Original Message -----

From: "webierta gmail com" <webierta gmail com>
Sent: Saturday, 11 March 2017, 18:55
Subject: [Vala] Gtk.ProgressBar

I explain: From a FileChooserDialog is selected and opens the vCard
file, and if it is too large it takes a while to analyze and import the
contacts. At that time I want to open a window or dialog (without
buttons) with a Gtk.ProgressBar that shows the progress of the
operation. And when the operation is finished, the window / dialog is
automatically closed.


Ideally you would use asynchronous, event based programming in Vala/Genie.
So something like:
1. Write an asynchronous process vCard function that runs in a background
   thread. I've not figured out the best way of doing this, but look at the
   async keyword in Vala and https://developer.gnome.org/
gio/stable/ch02.html
2. Write an "app_paused" object that stores two states. The first state is
   for a Timeout source (https://valadoc.org/glib-2.0/GLib.Timeout.html)
that
   will then complete and then the second state is showing a spinner
dialog.
   This needs a cancel method to cancel the timer or close the dialog.
   For a progress bar you would need some kind of signalling, so more
complex.
3. You combine these two by starting the "app_paused" object then call
   yield process_vCard(). When the processing is finished the code after
the
   yield should call the app_paused cancel method.
It would be nice to find some good examples of this.

At the moment I have not known how to solve it, and I am using
Notify.Notification to report the process, but this requires a new
dependency (libnotify) and I prefer to use few dependencies.


From https://wiki.gnome.org/HowDoI/GNotification:
"As of GLib 2.39, it is no longer necessary to link against libnotify to

create notifications in your application, GIO now provides an API for it:

GNotification."

So create a Notification (https://valadoc.org/gio-2.0/
GLib.Notification.html)
and send it through you GApplication send_notification method (
https://valadoc.org/gio-2.0/GLib.Application.send_notification.html )

Good luck with it,

Al
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list




-- 
Chris Daley
Pacific Northwest

e: chebizarro gmail com
w: http://chrisdaley.biz
m: +1-971-703-9251
s: chebizarro
tw: chebizarro
tz: PDT


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]