Created
October 8, 2020 19:54
-
-
Save aferust/2b469fad974bf8d80ede4db0d0627645 to your computer and use it in GitHub Desktop.
GtkD threads
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
apd.d : | |
module app; | |
import std.stdio; | |
import std.experimental.logger: trace; | |
import gio.Application : GApplication = Application; | |
import gtk.Main; | |
import gtk.Application; | |
import appwindow; | |
class GtkDApp : Application { | |
public: | |
this(){ | |
ApplicationFlags flags = ApplicationFlags.FLAGS_NONE; | |
super("org.gnome.projectname", flags); | |
this.addOnActivate(&onAppActivate); | |
this.window = null; | |
} | |
private: | |
AppWindow window; | |
void onAppActivate(GApplication app){ | |
trace("Activate App Signal"); | |
if (!app.getIsRemote()){ | |
this.window = new AppWindow(this); | |
} | |
this.window.present(); | |
} | |
} | |
void main(string[] args) { | |
Main.init(args); | |
auto app = new GtkDApp(); | |
app.run(args); | |
} | |
app.d END | |
appwindow.d | |
module appwindow; | |
import app; | |
import core.stdc.stdlib; | |
import std.functional; | |
import std.experimental.logger; | |
import gtk.Widget, gdk.Event; | |
import gtk.ApplicationWindow; | |
import gtk.Box; | |
import gtk.Label; | |
import gtk.Button; | |
import gtk.Statusbar, gtk.Separator; | |
import service; | |
class AppWindow: ApplicationWindow { | |
Box vbox1; | |
Label label1; | |
Button button1; | |
this(gtk.Application.Application application){ | |
super(application); | |
vbox1 = new Box(GtkOrientation.VERTICAL, 5); | |
label1 = new Label("my label"); | |
button1 = new Button("click me"); | |
button1.addOnReleased(toDelegate(&button1OnClicked)); | |
this.addOnDelete(delegate bool(Event e, Widget w) { | |
exit(0); | |
return true; | |
}); | |
vbox1.add(label1); | |
vbox1.add(button1); | |
add(vbox1); | |
showAll(); | |
} | |
void button1OnClicked(Button aux){ | |
auto worker = new Service(this); | |
worker.start(); | |
} | |
} | |
appwindow.d END | |
service.d | |
module service; | |
import core.thread; | |
import std.conv; | |
import glib.Idle; | |
import appwindow; | |
class Service : Thread { | |
AppWindow ctx; | |
this(AppWindow ctx){ | |
this.ctx = ctx; | |
super(&run); | |
} | |
void run(){ | |
foreach (i; 0..5){ | |
Thread.sleep(1.seconds); | |
new Idle(delegate bool(){ | |
ctx.label1.setLabel(i.to!string); | |
return false; | |
}); | |
} | |
} | |
} | |
service.d END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment